-
Notifications
You must be signed in to change notification settings - Fork 858
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 experimental synchronous gauge #5506
Merged
+902
−360
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0bc02af
Add experimental synchronous gauge
jack-berg 2eb4859
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg 943a935
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg fb47327
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
jack-berg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...ons/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/DoubleGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.extension.incubator.metrics; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** A gauge instrument that synchronously records {@code double} values. */ | ||
@ThreadSafe | ||
public interface DoubleGauge { | ||
/** | ||
* Set the gauge value. | ||
* | ||
* @param value The current gauge value. | ||
*/ | ||
void set(double value); | ||
|
||
/** | ||
* Records a value with a set of attributes. | ||
* | ||
* @param value The current gauge value. | ||
* @param attributes A set of attributes to associate with the value. | ||
*/ | ||
void set(double value, Attributes attributes); | ||
|
||
// TODO(jack-berg): should we add overload with Context argument? | ||
} |
24 changes: 24 additions & 0 deletions
24
...rc/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedDoubleGaugeBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.extension.incubator.metrics; | ||
|
||
import io.opentelemetry.api.metrics.DoubleGaugeBuilder; | ||
import java.util.function.Consumer; | ||
|
||
/** Extended {@link DoubleGaugeBuilder} with experimental APIs. */ | ||
public interface ExtendedDoubleGaugeBuilder extends DoubleGaugeBuilder { | ||
|
||
/** | ||
* Builds and returns a DoubleGauge instrument with the configuration. | ||
* | ||
* <p>NOTE: This produces a synchronous gauge which records gauge values as they occur. Most users | ||
* will want to instead register an {@link #buildWithCallback(Consumer)} to asynchronously observe | ||
* the value of the gauge when metrics are collected. | ||
* | ||
* @return The DoubleGauge instrument. | ||
*/ | ||
DoubleGauge build(); | ||
} |
24 changes: 24 additions & 0 deletions
24
.../src/main/java/io/opentelemetry/extension/incubator/metrics/ExtendedLongGaugeBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.extension.incubator.metrics; | ||
|
||
import io.opentelemetry.api.metrics.LongGaugeBuilder; | ||
import java.util.function.Consumer; | ||
|
||
/** Extended {@link LongGaugeBuilder} with experimental APIs. */ | ||
public interface ExtendedLongGaugeBuilder extends LongGaugeBuilder { | ||
|
||
/** | ||
* Builds and returns a LongGauge instrument with the configuration. | ||
* | ||
* <p>NOTE: This produces a synchronous gauge which records gauge values as they occur. Most users | ||
* will want to instead register an {@link #buildWithCallback(Consumer)} to asynchronously observe | ||
* the value of the gauge when metrics are collected. | ||
* | ||
* @return The LongGauge instrument. | ||
*/ | ||
LongGauge build(); | ||
} |
30 changes: 30 additions & 0 deletions
30
...sions/incubator/src/main/java/io/opentelemetry/extension/incubator/metrics/LongGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.extension.incubator.metrics; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
/** A gauge instrument that synchronously records {@code long} values. */ | ||
@ThreadSafe | ||
public interface LongGauge { | ||
/** | ||
* Set the gauge value. | ||
* | ||
* @param value The current gauge value. | ||
*/ | ||
void set(long value); | ||
|
||
/** | ||
* Records a value with a set of attributes. | ||
* | ||
* @param value The current gauge value. | ||
* @param attributes A set of attributes to associate with the value. | ||
*/ | ||
void set(long value, Attributes attributes); | ||
|
||
// TODO(jack-berg): should we add overload with Context argument? | ||
} |
85 changes: 85 additions & 0 deletions
85
sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.LongGaugeBuilder; | ||
import io.opentelemetry.api.metrics.ObservableDoubleGauge; | ||
import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.extension.incubator.metrics.DoubleGauge; | ||
import io.opentelemetry.extension.incubator.metrics.ExtendedDoubleGaugeBuilder; | ||
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.function.Consumer; | ||
|
||
final class SdkDoubleGauge extends AbstractInstrument implements DoubleGauge { | ||
|
||
private final WriteableMetricStorage storage; | ||
|
||
private SdkDoubleGauge(InstrumentDescriptor descriptor, WriteableMetricStorage storage) { | ||
super(descriptor); | ||
this.storage = storage; | ||
} | ||
|
||
@Override | ||
public void set(double increment, Attributes attributes) { | ||
storage.recordDouble(increment, attributes, Context.root()); | ||
} | ||
|
||
@Override | ||
public void set(double increment) { | ||
set(increment, Attributes.empty()); | ||
} | ||
|
||
static final class SdkDoubleGaugeBuilder extends AbstractInstrumentBuilder<SdkDoubleGaugeBuilder> | ||
implements ExtendedDoubleGaugeBuilder { | ||
|
||
SdkDoubleGaugeBuilder( | ||
MeterProviderSharedState meterProviderSharedState, | ||
MeterSharedState meterSharedState, | ||
String name) { | ||
super( | ||
meterProviderSharedState, | ||
meterSharedState, | ||
// TODO: use InstrumentType.GAUGE when available | ||
InstrumentType.OBSERVABLE_GAUGE, | ||
InstrumentValueType.DOUBLE, | ||
name, | ||
"", | ||
DEFAULT_UNIT); | ||
} | ||
|
||
@Override | ||
protected SdkDoubleGaugeBuilder getThis() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public SdkDoubleGauge build() { | ||
return buildSynchronousInstrument(SdkDoubleGauge::new); | ||
} | ||
|
||
@Override | ||
public LongGaugeBuilder ofLongs() { | ||
return swapBuilder(SdkLongGauge.SdkLongGaugeBuilder::new); | ||
} | ||
|
||
@Override | ||
public ObservableDoubleGauge buildWithCallback(Consumer<ObservableDoubleMeasurement> callback) { | ||
// TODO: use InstrumentType.GAUGE when available | ||
return registerDoubleAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); | ||
} | ||
|
||
@Override | ||
public ObservableDoubleMeasurement buildObserver() { | ||
// TODO: use InstrumentType.GAUGE when available | ||
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); | ||
} | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGaugeBuilder.java
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.ObservableLongGauge; | ||
import io.opentelemetry.api.metrics.ObservableLongMeasurement; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.extension.incubator.metrics.ExtendedLongGaugeBuilder; | ||
import io.opentelemetry.extension.incubator.metrics.LongGauge; | ||
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.function.Consumer; | ||
|
||
final class SdkLongGauge extends AbstractInstrument implements LongGauge { | ||
|
||
private final WriteableMetricStorage storage; | ||
|
||
private SdkLongGauge(InstrumentDescriptor descriptor, WriteableMetricStorage storage) { | ||
super(descriptor); | ||
this.storage = storage; | ||
} | ||
|
||
@Override | ||
public void set(long increment, Attributes attributes) { | ||
storage.recordLong(increment, attributes, Context.root()); | ||
} | ||
|
||
@Override | ||
public void set(long increment) { | ||
set(increment, Attributes.empty()); | ||
} | ||
|
||
static final class SdkLongGaugeBuilder extends AbstractInstrumentBuilder<SdkLongGaugeBuilder> | ||
implements ExtendedLongGaugeBuilder { | ||
|
||
SdkLongGaugeBuilder( | ||
MeterProviderSharedState meterProviderSharedState, | ||
MeterSharedState sharedState, | ||
String name, | ||
String description, | ||
String unit, | ||
Advice.AdviceBuilder adviceBuilder) { | ||
super( | ||
meterProviderSharedState, | ||
sharedState, | ||
// TODO: use InstrumentType.GAUGE when available | ||
InstrumentType.OBSERVABLE_GAUGE, | ||
InstrumentValueType.LONG, | ||
name, | ||
description, | ||
unit, | ||
adviceBuilder); | ||
} | ||
|
||
@Override | ||
protected SdkLongGaugeBuilder getThis() { | ||
return this; | ||
} | ||
|
||
@Override | ||
public SdkLongGauge build() { | ||
return buildSynchronousInstrument(SdkLongGauge::new); | ||
} | ||
|
||
@Override | ||
public ObservableLongGauge buildWithCallback(Consumer<ObservableLongMeasurement> callback) { | ||
// TODO: use InstrumentType.GAUGE when available | ||
return registerLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); | ||
} | ||
|
||
@Override | ||
public ObservableLongMeasurement buildObserver() { | ||
// TODO: use InstrumentType.GAUGE when available | ||
return buildObservableMeasurement(InstrumentType.OBSERVABLE_GAUGE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Yes.
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.
I'm going to open an issue for this and solve in a separate PR. The synchronous use cases I'm aware of require the instrumentation to subscribe to notifications about change in some value, rather than fetching the current value which caters to async gauge. Not clear when context would be available in one of these notification event streams. I'm open to it being a use case, but its not as clear cut as what's in this PR.