-
Notifications
You must be signed in to change notification settings - Fork 834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add toString to SdkMeter, SdkObservableInstrument, AbstractInstrumentBuilder #5072
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ abstract class AbstractInstrumentBuilder<BuilderT extends AbstractInstrumentBuil | |
static final String DEFAULT_UNIT = ""; | ||
|
||
private final MeterProviderSharedState meterProviderSharedState; | ||
private final InstrumentType type; | ||
private final InstrumentValueType valueType; | ||
private String description; | ||
private String unit; | ||
|
||
|
@@ -33,9 +35,13 @@ abstract class AbstractInstrumentBuilder<BuilderT extends AbstractInstrumentBuil | |
AbstractInstrumentBuilder( | ||
MeterProviderSharedState meterProviderSharedState, | ||
MeterSharedState meterSharedState, | ||
InstrumentType type, | ||
InstrumentValueType valueType, | ||
String name, | ||
String description, | ||
String unit) { | ||
this.type = type; | ||
this.valueType = valueType; | ||
this.instrumentName = name; | ||
this.description = description; | ||
this.unit = unit; | ||
|
@@ -61,50 +67,52 @@ public BuilderT setDescription(String description) { | |
return getThis(); | ||
} | ||
|
||
private InstrumentDescriptor makeDescriptor(InstrumentType type, InstrumentValueType valueType) { | ||
return InstrumentDescriptor.create(instrumentName, description, unit, type, valueType); | ||
} | ||
|
||
protected <T> T swapBuilder(SwapBuilder<T> swapper) { | ||
return swapper.newBuilder( | ||
meterProviderSharedState, meterSharedState, instrumentName, description, unit); | ||
} | ||
|
||
final <I extends AbstractInstrument> I buildSynchronousInstrument( | ||
InstrumentType type, | ||
InstrumentValueType valueType, | ||
BiFunction<InstrumentDescriptor, WriteableMetricStorage, I> instrumentFactory) { | ||
InstrumentDescriptor descriptor = makeDescriptor(type, valueType); | ||
InstrumentDescriptor descriptor = | ||
InstrumentDescriptor.create(instrumentName, description, unit, type, valueType); | ||
WriteableMetricStorage storage = | ||
meterSharedState.registerSynchronousMetricStorage(descriptor, meterProviderSharedState); | ||
return instrumentFactory.apply(descriptor, storage); | ||
} | ||
|
||
final CallbackRegistration registerDoubleAsynchronousInstrument( | ||
final SdkObservableInstrument registerDoubleAsynchronousInstrument( | ||
InstrumentType type, Consumer<ObservableDoubleMeasurement> updater) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't the type available now at the class level, and hence not needed to be passed in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I think I see... for types that can be both observable and not, we need to be able to pass this in, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes exactly. The type is assumed to be the synchronous version (i.e. COUNTER, UP_DOWN_COUNTER, etc) until a callback is regsitered. |
||
SdkObservableMeasurement sdkObservableMeasurement = | ||
buildObservableMeasurement(type, InstrumentValueType.DOUBLE); | ||
SdkObservableMeasurement sdkObservableMeasurement = buildObservableMeasurement(type); | ||
Runnable runnable = () -> updater.accept(sdkObservableMeasurement); | ||
CallbackRegistration callbackRegistration = | ||
CallbackRegistration.create(Collections.singletonList(sdkObservableMeasurement), runnable); | ||
meterSharedState.registerCallback(callbackRegistration); | ||
return callbackRegistration; | ||
return new SdkObservableInstrument(meterSharedState, callbackRegistration); | ||
} | ||
|
||
final CallbackRegistration registerLongAsynchronousInstrument( | ||
final SdkObservableInstrument registerLongAsynchronousInstrument( | ||
InstrumentType type, Consumer<ObservableLongMeasurement> updater) { | ||
SdkObservableMeasurement sdkObservableMeasurement = | ||
buildObservableMeasurement(type, InstrumentValueType.LONG); | ||
SdkObservableMeasurement sdkObservableMeasurement = buildObservableMeasurement(type); | ||
Runnable runnable = () -> updater.accept(sdkObservableMeasurement); | ||
CallbackRegistration callbackRegistration = | ||
CallbackRegistration.create(Collections.singletonList(sdkObservableMeasurement), runnable); | ||
meterSharedState.registerCallback(callbackRegistration); | ||
return callbackRegistration; | ||
return new SdkObservableInstrument(meterSharedState, callbackRegistration); | ||
} | ||
|
||
final SdkObservableMeasurement buildObservableMeasurement(InstrumentType type) { | ||
InstrumentDescriptor descriptor = | ||
InstrumentDescriptor.create(instrumentName, description, unit, type, valueType); | ||
return meterSharedState.registerObservableMeasurement(descriptor); | ||
} | ||
|
||
final SdkObservableMeasurement buildObservableMeasurement( | ||
InstrumentType type, InstrumentValueType valueType) { | ||
return meterSharedState.registerObservableMeasurement(makeDescriptor(type, valueType)); | ||
@Override | ||
public String toString() { | ||
return this.getClass().getSimpleName() | ||
+ "{descriptor=" | ||
+ InstrumentDescriptor.create(instrumentName, description, unit, type, valueType) | ||
+ "}"; | ||
} | ||
|
||
@FunctionalInterface | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,41 +97,44 @@ public void unbind() { | |
} | ||
} | ||
|
||
static final class Builder extends AbstractInstrumentBuilder<SdkDoubleCounter.Builder> | ||
implements DoubleCounterBuilder { | ||
static final class SdkDoubleCounterBuilder | ||
extends AbstractInstrumentBuilder<SdkDoubleCounterBuilder> implements DoubleCounterBuilder { | ||
|
||
Builder( | ||
SdkDoubleCounterBuilder( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Giving these builders more descriptive names gives |
||
MeterProviderSharedState meterProviderSharedState, | ||
MeterSharedState sharedState, | ||
String name, | ||
String description, | ||
String unit) { | ||
super(meterProviderSharedState, sharedState, name, description, unit); | ||
super( | ||
meterProviderSharedState, | ||
sharedState, | ||
InstrumentType.COUNTER, | ||
InstrumentValueType.DOUBLE, | ||
name, | ||
description, | ||
unit); | ||
} | ||
|
||
@Override | ||
protected Builder getThis() { | ||
protected SdkDoubleCounterBuilder getThis() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public SdkDoubleCounter build() { | ||
return buildSynchronousInstrument( | ||
InstrumentType.COUNTER, InstrumentValueType.DOUBLE, SdkDoubleCounter::new); | ||
return buildSynchronousInstrument(SdkDoubleCounter::new); | ||
} | ||
|
||
@Override | ||
public ObservableDoubleCounter buildWithCallback( | ||
Consumer<ObservableDoubleMeasurement> callback) { | ||
return new SdkObservableInstrument( | ||
meterSharedState, | ||
registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback)); | ||
return registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback); | ||
} | ||
|
||
@Override | ||
public ObservableDoubleMeasurement buildObserver() { | ||
return buildObservableMeasurement( | ||
InstrumentType.OBSERVABLE_COUNTER, InstrumentValueType.DOUBLE); | ||
return buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The refactoring in this class ensures all descriptive fields required for a
toString()
implementation are available when constructed.