Skip to content
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

Merged
merged 1 commit into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,9 +35,13 @@ abstract class AbstractInstrumentBuilder<BuilderT extends AbstractInstrumentBuil
AbstractInstrumentBuilder(
Copy link
Member Author

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.

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;
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Giving these builders more descriptive names gives AbstractInstrumentBuilder#toString() a useful classname to include.

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ final class SdkDoubleGaugeBuilder extends AbstractInstrumentBuilder<SdkDoubleGau
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
String name) {
super(meterProviderSharedState, meterSharedState, name, "", DEFAULT_UNIT);
super(
meterProviderSharedState,
meterSharedState,
InstrumentType.OBSERVABLE_GAUGE,
InstrumentValueType.DOUBLE,
name,
"",
DEFAULT_UNIT);
}

@Override
Expand All @@ -35,13 +42,11 @@ public LongGaugeBuilder ofLongs() {

@Override
public ObservableDoubleGauge buildWithCallback(Consumer<ObservableDoubleMeasurement> callback) {
return new SdkObservableInstrument(
meterSharedState,
registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback));
return registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback);
}

@Override
public ObservableDoubleMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE, InstrumentValueType.DOUBLE);
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,37 @@ public void unbind() {
}
}

static final class Builder extends AbstractInstrumentBuilder<SdkDoubleHistogram.Builder>
static final class SdkDoubleHistogramBuilder
extends AbstractInstrumentBuilder<SdkDoubleHistogramBuilder>
implements DoubleHistogramBuilder {

Builder(
SdkDoubleHistogramBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
String name) {
super(meterProviderSharedState, meterSharedState, name, "", DEFAULT_UNIT);
super(
meterProviderSharedState,
meterSharedState,
InstrumentType.HISTOGRAM,
InstrumentValueType.DOUBLE,
name,
"",
DEFAULT_UNIT);
}

@Override
protected Builder getThis() {
protected SdkDoubleHistogramBuilder getThis() {
return this;
}

@Override
public SdkDoubleHistogram build() {
return buildSynchronousInstrument(
InstrumentType.HISTOGRAM, InstrumentValueType.DOUBLE, SdkDoubleHistogram::new);
return buildSynchronousInstrument(SdkDoubleHistogram::new);
}

@Override
public LongHistogramBuilder ofLongs() {
return swapBuilder(SdkLongHistogram.Builder::new);
return swapBuilder(SdkLongHistogram.SdkLongHistogramBuilder::new);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,42 +72,46 @@ public void unbind() {
}
}

static final class Builder extends AbstractInstrumentBuilder<SdkDoubleUpDownCounter.Builder>
static final class SdkDoubleUpDownCounterBuilder
extends AbstractInstrumentBuilder<SdkDoubleUpDownCounterBuilder>
implements DoubleUpDownCounterBuilder {

Builder(
SdkDoubleUpDownCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState sharedState,
String name,
String description,
String unit) {
super(meterProviderSharedState, sharedState, name, description, unit);
super(
meterProviderSharedState,
sharedState,
InstrumentType.UP_DOWN_COUNTER,
InstrumentValueType.DOUBLE,
name,
description,
unit);
}

@Override
protected Builder getThis() {
protected SdkDoubleUpDownCounterBuilder getThis() {
return this;
}

@Override
public DoubleUpDownCounter build() {
return buildSynchronousInstrument(
InstrumentType.UP_DOWN_COUNTER, InstrumentValueType.DOUBLE, SdkDoubleUpDownCounter::new);
return buildSynchronousInstrument(SdkDoubleUpDownCounter::new);
}

@Override
public ObservableDoubleUpDownCounter buildWithCallback(
Consumer<ObservableDoubleMeasurement> callback) {
return new SdkObservableInstrument(
meterSharedState,
registerDoubleAsynchronousInstrument(
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, callback));
return registerDoubleAsynchronousInstrument(
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, callback);
}

@Override
public ObservableDoubleMeasurement buildObserver() {
return buildObservableMeasurement(
InstrumentType.OBSERVABLE_UP_DOWN_COUNTER, InstrumentValueType.DOUBLE);
return buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,43 +99,46 @@ public void unbind() {
}
}

static final class Builder extends AbstractInstrumentBuilder<Builder>
static final class SdkLongCounterBuilder extends AbstractInstrumentBuilder<SdkLongCounterBuilder>
implements LongCounterBuilder {

Builder(
SdkLongCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
String name) {
super(meterProviderSharedState, meterSharedState, name, "", DEFAULT_UNIT);
super(
meterProviderSharedState,
meterSharedState,
InstrumentType.COUNTER,
InstrumentValueType.LONG,
name,
"",
DEFAULT_UNIT);
}

@Override
protected Builder getThis() {
protected SdkLongCounterBuilder getThis() {
return this;
}

@Override
public SdkLongCounter build() {
return buildSynchronousInstrument(
InstrumentType.COUNTER, InstrumentValueType.LONG, SdkLongCounter::new);
return buildSynchronousInstrument(SdkLongCounter::new);
}

@Override
public DoubleCounterBuilder ofDoubles() {
return swapBuilder(SdkDoubleCounter.Builder::new);
return swapBuilder(SdkDoubleCounter.SdkDoubleCounterBuilder::new);
}

@Override
public ObservableLongCounter buildWithCallback(Consumer<ObservableLongMeasurement> callback) {
return new SdkObservableInstrument(
meterSharedState,
registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback));
return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_COUNTER, callback);
}

@Override
public ObservableLongMeasurement buildObserver() {
return buildObservableMeasurement(
InstrumentType.OBSERVABLE_COUNTER, InstrumentValueType.LONG);
return buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder<SdkLongGaugeBu
String name,
String description,
String unit) {
super(meterProviderSharedState, sharedState, name, description, unit);
super(
meterProviderSharedState,
sharedState,
InstrumentType.OBSERVABLE_GAUGE,
InstrumentValueType.LONG,
name,
description,
unit);
}

@Override
Expand All @@ -31,13 +38,11 @@ protected SdkLongGaugeBuilder getThis() {

@Override
public ObservableLongGauge buildWithCallback(Consumer<ObservableLongMeasurement> callback) {
return new SdkObservableInstrument(
meterSharedState,
registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback));
return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback);
}

@Override
public ObservableLongMeasurement buildObserver() {
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE, InstrumentValueType.LONG);
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE);
}
}
Loading