Skip to content
Open
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 @@ -5,22 +5,44 @@

package io.opentelemetry.sdk.metrics;

import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage;
import javax.annotation.Nullable;

abstract class AbstractInstrument {

private final InstrumentDescriptor descriptor;

/**
* True when the record-path {@link Context} has no observable effect for this instrument, so
* parameterless record overloads may substitute {@link Context#root()} for {@link
* Context#current()} and skip a thread-local lookup. Both potential consumers must be inactive:
* the exemplar filter (disabled meter-wide) and every backing storage's {@link
* io.opentelemetry.sdk.metrics.internal.view.AttributesProcessor} (e.g. baggage append).
*/
private final boolean canUseRootContext;

// All arguments cannot be null because they are checked in the abstract builder classes.
AbstractInstrument(InstrumentDescriptor descriptor) {
AbstractInstrument(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
this.descriptor = descriptor;
this.canUseRootContext = sdkMeter.isExemplarsAlwaysOff() && !storage.usesContext();
}

final InstrumentDescriptor getDescriptor() {
return descriptor;
}

/**
* Returns {@link Context#current()}, or {@link Context#root()} when the current context can have
* no observable effect on this instrument's outputs (see {@link #canUseRootContext}). Used by
* parameterless record overloads on synchronous instruments to skip a thread-local lookup.
*/
final Context currentOrRootContext() {
return canUseRootContext ? Context.root() : Context.current();
}
Comment thread
jack-berg marked this conversation as resolved.

@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BoundDoubleCounter bind(Attributes attributes) {

@Override
public void add(double value) {
add(value, Context.current());
add(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BoundDoubleGauge bind(Attributes attributes) {

@Override
public void set(double value) {
set(value, Context.current());
set(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BoundDoubleHistogram bind(Attributes attributes) {

@Override
public void record(double value) {
record(value, Context.current());
record(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BoundDoubleUpDownCounter bind(Attributes attributes) {

@Override
public void add(double value) {
add(value, Context.current());
add(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public BoundLongCounter bind(Attributes attributes) {

@Override
public void add(long value) {
add(value, Context.current());
add(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public BoundLongGauge bind(Attributes attributes) {

@Override
public void set(long value) {
set(value, Context.current());
set(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BoundLongHistogram bind(Attributes attributes) {

@Override
public void record(long value) {
record(value, Context.current());
record(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public BoundLongUpDownCounter bind(Attributes attributes) {

@Override
public void add(long value) {
add(value, Context.current());
add(value, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SdkDoubleCounter extends AbstractInstrument implements DoubleCounter {

SdkDoubleCounter(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -48,7 +48,7 @@ public void add(double increment, Attributes attributes, Context context) {

@Override
public void add(double increment, Attributes attributes) {
add(increment, attributes, Context.current());
add(increment, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SdkDoubleGauge extends AbstractInstrument implements DoubleGauge {

SdkDoubleGauge(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -35,7 +35,7 @@ public boolean isEnabled() {

@Override
public void set(double value, Attributes attributes) {
storage.recordDouble(value, attributes, Context.current());
storage.recordDouble(value, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SdkDoubleHistogram extends AbstractInstrument implements DoubleHistogram {

SdkDoubleHistogram(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -48,7 +48,7 @@ public void record(double value, Attributes attributes, Context context) {

@Override
public void record(double value, Attributes attributes) {
record(value, attributes, Context.current());
record(value, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SdkDoubleUpDownCounter extends AbstractInstrument implements DoubleUpDownC

SdkDoubleUpDownCounter(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -40,7 +40,7 @@ public void add(double increment, Attributes attributes, Context context) {

@Override
public void add(double increment, Attributes attributes) {
add(increment, attributes, Context.current());
add(increment, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SdkLongCounter extends AbstractInstrument implements LongCounter {

SdkLongCounter(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -49,7 +49,7 @@ public void add(long increment, Attributes attributes, Context context) {

@Override
public void add(long increment, Attributes attributes) {
add(increment, attributes, Context.current());
add(increment, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SdkLongGauge extends AbstractInstrument implements LongGauge {
final WriteableMetricStorage storage;

SdkLongGauge(InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -34,7 +34,7 @@ public boolean isEnabled() {

@Override
public void set(long value, Attributes attributes) {
storage.recordLong(value, attributes, Context.current());
storage.recordLong(value, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SdkLongHistogram extends AbstractInstrument implements LongHistogram {

SdkLongHistogram(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -49,7 +49,7 @@ public void record(long value, Attributes attributes, Context context) {

@Override
public void record(long value, Attributes attributes) {
record(value, attributes, Context.current());
record(value, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SdkLongUpDownCounter extends AbstractInstrument implements LongUpDownCount

SdkLongUpDownCounter(
InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) {
super(descriptor);
super(descriptor, sdkMeter, storage);
this.sdkMeter = sdkMeter;
this.storage = storage;
}
Expand All @@ -40,7 +40,7 @@ public void add(long increment, Attributes attributes, Context context) {

@Override
public void add(long increment, Attributes attributes) {
add(increment, attributes, Context.current());
add(increment, attributes, currentOrRootContext());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.internal.MeterConfig;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.exemplar.AlwaysOffExemplarFilter;
import io.opentelemetry.sdk.metrics.internal.export.RegisteredReader;
import io.opentelemetry.sdk.metrics.internal.state.AsynchronousMetricStorage;
import io.opentelemetry.sdk.metrics.internal.state.BoundStorageHandle;
Expand Down Expand Up @@ -88,6 +89,16 @@ final class SdkMeter implements Meter {

private final MeterProviderSharedState meterProviderSharedState;
private final InstrumentationScopeInfo instrumentationScopeInfo;

/**
* Returns true if the meter provider's exemplar filter samples nothing. Callers can use this to
* skip {@link io.opentelemetry.context.Context#current()} lookups on record paths that only need
* the current context to derive an exemplar span context.
*/
boolean isExemplarsAlwaysOff() {
return meterProviderSharedState.getExemplarFilter() instanceof AlwaysOffExemplarFilter;
}

private final Map<RegisteredReader, MetricStorageRegistry> readerStorageRegistries;

private volatile boolean meterEnabled;
Expand Down Expand Up @@ -392,6 +403,16 @@ public boolean isEnabled() {
}
return false;
}

@Override
public boolean usesContext() {
for (WriteableMetricStorage storage : storages) {
if (storage.usesContext()) {
return true;
}
}
return false;
}
}

private static class MultiBoundStorageHandle implements BoundStorageHandle {
Expand Down
Loading
Loading