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

Refactor metrics to remove MeterSharedState #6845

Merged
merged 1 commit into from
Nov 5, 2024
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 @@ -11,8 +11,6 @@
import io.opentelemetry.sdk.metrics.internal.descriptor.Advice;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.CallbackRegistration;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState;
import io.opentelemetry.sdk.metrics.internal.state.SdkObservableMeasurement;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import java.util.Collections;
Expand All @@ -23,25 +21,19 @@
final class InstrumentBuilder {

private final String name;
private final MeterProviderSharedState meterProviderSharedState;
private final MeterSharedState meterSharedState;
private final SdkMeter sdkMeter;
private final InstrumentValueType valueType;
private InstrumentType type;
private Advice.AdviceBuilder adviceBuilder = Advice.builder();
private String description = "";
private String unit = "";

InstrumentBuilder(
String name,
InstrumentType type,
InstrumentValueType valueType,
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState) {
String name, InstrumentType type, InstrumentValueType valueType, SdkMeter sdkMeter) {
this.name = name;
this.type = type;
this.valueType = valueType;
this.meterProviderSharedState = meterProviderSharedState;
this.meterSharedState = meterSharedState;
this.sdkMeter = sdkMeter;
}

InstrumentBuilder setUnit(String unit) {
Expand All @@ -60,25 +52,23 @@ InstrumentBuilder setDescription(String description) {
}

<T> T swapBuilder(SwapBuilder<T> swapper) {
return swapper.newBuilder(
meterProviderSharedState, meterSharedState, name, description, unit, adviceBuilder);
return swapper.newBuilder(sdkMeter, name, description, unit, adviceBuilder);
}

@FunctionalInterface
interface SynchronousInstrumentConstructor<I extends AbstractInstrument> {

I createInstrument(
InstrumentDescriptor instrumentDescriptor,
MeterSharedState meterSharedState,
SdkMeter sdkMeter,
WriteableMetricStorage storage);
}

<I extends AbstractInstrument> I buildSynchronousInstrument(
SynchronousInstrumentConstructor<I> instrumentFactory) {
InstrumentDescriptor descriptor = newDescriptor();
WriteableMetricStorage storage =
meterSharedState.registerSynchronousMetricStorage(descriptor, meterProviderSharedState);
return instrumentFactory.createInstrument(descriptor, meterSharedState, storage);
WriteableMetricStorage storage = sdkMeter.registerSynchronousMetricStorage(descriptor);
return instrumentFactory.createInstrument(descriptor, sdkMeter, storage);
}

SdkObservableInstrument buildDoubleAsynchronousInstrument(
Expand All @@ -87,8 +77,8 @@ SdkObservableInstrument buildDoubleAsynchronousInstrument(
Runnable runnable = () -> updater.accept(sdkObservableMeasurement);
CallbackRegistration callbackRegistration =
CallbackRegistration.create(Collections.singletonList(sdkObservableMeasurement), runnable);
meterSharedState.registerCallback(callbackRegistration);
return new SdkObservableInstrument(meterSharedState, callbackRegistration);
sdkMeter.registerCallback(callbackRegistration);
return new SdkObservableInstrument(sdkMeter, callbackRegistration);
}

SdkObservableInstrument buildLongAsynchronousInstrument(
Expand All @@ -97,14 +87,14 @@ SdkObservableInstrument buildLongAsynchronousInstrument(
Runnable runnable = () -> updater.accept(sdkObservableMeasurement);
CallbackRegistration callbackRegistration =
CallbackRegistration.create(Collections.singletonList(sdkObservableMeasurement), runnable);
meterSharedState.registerCallback(callbackRegistration);
return new SdkObservableInstrument(meterSharedState, callbackRegistration);
sdkMeter.registerCallback(callbackRegistration);
return new SdkObservableInstrument(sdkMeter, callbackRegistration);
}

SdkObservableMeasurement buildObservableMeasurement(InstrumentType type) {
this.type = type;
InstrumentDescriptor descriptor = newDescriptor();
return meterSharedState.registerObservableMeasurement(descriptor);
return sdkMeter.registerObservableMeasurement(descriptor);
}

private InstrumentDescriptor newDescriptor() {
Expand All @@ -122,10 +112,9 @@ String toStringHelper(String className) {
}

@FunctionalInterface
protected interface SwapBuilder<T> {
interface SwapBuilder<T> {
T newBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
SdkMeter sdkMeter,
String name,
String description,
String unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import io.opentelemetry.sdk.internal.ThrottlingLogger;
import io.opentelemetry.sdk.metrics.internal.descriptor.Advice;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -28,15 +26,13 @@ final class SdkDoubleCounter extends AbstractInstrument implements ExtendedDoubl
private static final Logger logger = Logger.getLogger(SdkDoubleCounter.class.getName());

private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger);
private final MeterSharedState meterSharedState;
private final SdkMeter sdkMeter;
private final WriteableMetricStorage storage;

private SdkDoubleCounter(
InstrumentDescriptor descriptor,
MeterSharedState meterSharedState,
WriteableMetricStorage storage) {
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
this.meterSharedState = meterSharedState;
this.sdkMeter = sdkMeter;
this.storage = storage;
}

Expand Down Expand Up @@ -65,27 +61,21 @@ public void add(double increment) {

@Override
public boolean isEnabled() {
return meterSharedState.isMeterEnabled() && storage.isEnabled();
return sdkMeter.isMeterEnabled() && storage.isEnabled();
}

static final class SdkDoubleCounterBuilder implements ExtendedDoubleCounterBuilder {

private final InstrumentBuilder builder;

SdkDoubleCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState sharedState,
SdkMeter sdkMeter,
String name,
String description,
String unit,
Advice.AdviceBuilder adviceBuilder) {
this.builder =
new InstrumentBuilder(
name,
InstrumentType.COUNTER,
InstrumentValueType.DOUBLE,
meterProviderSharedState,
sharedState)
new InstrumentBuilder(name, InstrumentType.COUNTER, InstrumentValueType.DOUBLE, sdkMeter)
.setUnit(unit)
.setDescription(description)
.setAdviceBuilder(adviceBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import java.util.List;
import java.util.function.Consumer;

final class SdkDoubleGauge extends AbstractInstrument implements ExtendedDoubleGauge {

private final MeterSharedState meterSharedState;
private final SdkMeter sdkMeter;
private final WriteableMetricStorage storage;

private SdkDoubleGauge(
InstrumentDescriptor descriptor,
MeterSharedState meterSharedState,
WriteableMetricStorage storage) {
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
this.meterSharedState = meterSharedState;
this.sdkMeter = sdkMeter;
this.storage = storage;
}

Expand All @@ -52,24 +48,16 @@ public void set(double increment) {

@Override
public boolean isEnabled() {
return meterSharedState.isMeterEnabled() && storage.isEnabled();
return sdkMeter.isMeterEnabled() && storage.isEnabled();
}

static final class SdkDoubleGaugeBuilder implements ExtendedDoubleGaugeBuilder {
private final InstrumentBuilder builder;

SdkDoubleGaugeBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
String name) {
SdkDoubleGaugeBuilder(SdkMeter sdkMeter, String name) {

builder =
new InstrumentBuilder(
name,
InstrumentType.GAUGE,
InstrumentValueType.DOUBLE,
meterProviderSharedState,
meterSharedState);
new InstrumentBuilder(name, InstrumentType.GAUGE, InstrumentValueType.DOUBLE, sdkMeter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import io.opentelemetry.sdk.internal.ThrottlingLogger;
import io.opentelemetry.sdk.metrics.internal.aggregator.ExplicitBucketHistogramUtils;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import java.util.List;
import java.util.Objects;
Expand All @@ -27,15 +25,13 @@ final class SdkDoubleHistogram extends AbstractInstrument implements ExtendedDou
private static final Logger logger = Logger.getLogger(SdkDoubleHistogram.class.getName());

private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger);
private final MeterSharedState meterSharedState;
private final SdkMeter sdkMeter;
private final WriteableMetricStorage storage;

private SdkDoubleHistogram(
InstrumentDescriptor descriptor,
MeterSharedState meterSharedState,
WriteableMetricStorage storage) {
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
this.meterSharedState = meterSharedState;
this.sdkMeter = sdkMeter;
this.storage = storage;
}

Expand Down Expand Up @@ -64,24 +60,17 @@ public void record(double value) {

@Override
public boolean isEnabled() {
return meterSharedState.isMeterEnabled() && storage.isEnabled();
return sdkMeter.isMeterEnabled() && storage.isEnabled();
}

static final class SdkDoubleHistogramBuilder implements ExtendedDoubleHistogramBuilder {

private final InstrumentBuilder builder;

SdkDoubleHistogramBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
String name) {
SdkDoubleHistogramBuilder(SdkMeter sdkMeter, String name) {
builder =
new InstrumentBuilder(
name,
InstrumentType.HISTOGRAM,
InstrumentValueType.DOUBLE,
meterProviderSharedState,
meterSharedState);
name, InstrumentType.HISTOGRAM, InstrumentValueType.DOUBLE, sdkMeter);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.metrics.internal.descriptor.Advice;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import java.util.List;
import java.util.function.Consumer;

final class SdkDoubleUpDownCounter extends AbstractInstrument
implements ExtendedDoubleUpDownCounter {

private final MeterSharedState meterSharedState;
private final SdkMeter sdkMeter;
private final WriteableMetricStorage storage;

private SdkDoubleUpDownCounter(
InstrumentDescriptor descriptor,
MeterSharedState meterSharedState,
WriteableMetricStorage storage) {
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
this.meterSharedState = meterSharedState;
this.sdkMeter = sdkMeter;
this.storage = storage;
}

Expand All @@ -54,27 +50,22 @@ public void add(double increment) {

@Override
public boolean isEnabled() {
return meterSharedState.isMeterEnabled() && storage.isEnabled();
return sdkMeter.isMeterEnabled() && storage.isEnabled();
}

static final class SdkDoubleUpDownCounterBuilder implements ExtendedDoubleUpDownCounterBuilder {

private final InstrumentBuilder builder;

SdkDoubleUpDownCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState sharedState,
SdkMeter sdkMeter,
String name,
String description,
String unit,
Advice.AdviceBuilder adviceBuilder) {
this.builder =
new InstrumentBuilder(
name,
InstrumentType.UP_DOWN_COUNTER,
InstrumentValueType.DOUBLE,
meterProviderSharedState,
sharedState)
name, InstrumentType.UP_DOWN_COUNTER, InstrumentValueType.DOUBLE, sdkMeter)
.setDescription(description)
.setUnit(unit)
.setAdviceBuilder(adviceBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.internal.ThrottlingLogger;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.state.MeterSharedState;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import java.util.List;
import java.util.function.Consumer;
Expand All @@ -29,15 +27,13 @@ final class SdkLongCounter extends AbstractInstrument implements ExtendedLongCou
private static final Logger logger = Logger.getLogger(SdkLongCounter.class.getName());

private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger);
private final MeterSharedState meterSharedState;
private final SdkMeter sdkMeter;
private final WriteableMetricStorage storage;

private SdkLongCounter(
InstrumentDescriptor descriptor,
MeterSharedState meterSharedState,
WriteableMetricStorage storage) {
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
this.meterSharedState = meterSharedState;
this.sdkMeter = sdkMeter;
this.storage = storage;
}

Expand Down Expand Up @@ -66,24 +62,16 @@ public void add(long increment) {

@Override
public boolean isEnabled() {
return meterSharedState.isMeterEnabled() && storage.isEnabled();
return sdkMeter.isMeterEnabled() && storage.isEnabled();
}

static final class SdkLongCounterBuilder implements ExtendedLongCounterBuilder {

private final InstrumentBuilder builder;

SdkLongCounterBuilder(
MeterProviderSharedState meterProviderSharedState,
MeterSharedState meterSharedState,
String name) {
SdkLongCounterBuilder(SdkMeter sdkMeter, String name) {
this.builder =
new InstrumentBuilder(
name,
InstrumentType.COUNTER,
InstrumentValueType.LONG,
meterProviderSharedState,
meterSharedState);
new InstrumentBuilder(name, InstrumentType.COUNTER, InstrumentValueType.LONG, sdkMeter);
}

@Override
Expand Down
Loading
Loading