-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow multiple async callbacks, allow callbacks to be removed (#4143)
* Allow multiple async callbacks, allow callbacks to be removed * Use AutoCloseable to remove callbacks, don't unregister from MetricStorageRegistry * Use CopyOnWriteArrayList * PR feedback * PR feedback
- Loading branch information
Showing
32 changed files
with
731 additions
and
289 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
54 changes: 54 additions & 0 deletions
54
sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkObservableInstrument.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,54 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics; | ||
|
||
import io.opentelemetry.api.metrics.ObservableDoubleCounter; | ||
import io.opentelemetry.api.metrics.ObservableDoubleGauge; | ||
import io.opentelemetry.api.metrics.ObservableDoubleUpDownCounter; | ||
import io.opentelemetry.api.metrics.ObservableLongCounter; | ||
import io.opentelemetry.api.metrics.ObservableLongGauge; | ||
import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; | ||
import io.opentelemetry.sdk.internal.ThrottlingLogger; | ||
import io.opentelemetry.sdk.metrics.internal.state.AsynchronousMetricStorage; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.function.Consumer; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
class SdkObservableInstrument<O> | ||
implements ObservableDoubleCounter, | ||
ObservableLongCounter, | ||
ObservableDoubleGauge, | ||
ObservableLongGauge, | ||
ObservableDoubleUpDownCounter, | ||
ObservableLongUpDownCounter { | ||
|
||
private static final Logger logger = Logger.getLogger(SdkObservableInstrument.class.getName()); | ||
|
||
private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); | ||
private final String instrumentName; | ||
private final List<AsynchronousMetricStorage<?, O>> storages; | ||
private final Consumer<O> callback; | ||
private final AtomicBoolean removed = new AtomicBoolean(false); | ||
|
||
SdkObservableInstrument( | ||
String instrumentName, List<AsynchronousMetricStorage<?, O>> storages, Consumer<O> callback) { | ||
this.instrumentName = instrumentName; | ||
this.storages = storages; | ||
this.callback = callback; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (!removed.compareAndSet(false, true)) { | ||
throttlingLogger.log( | ||
Level.WARNING, "Instrument " + instrumentName + " has called close() multiple times."); | ||
return; | ||
} | ||
storages.forEach(storage -> storage.removeCallback(callback)); | ||
} | ||
} |
Oops, something went wrong.