|
| 1 | +package otel; |
| 2 | + |
| 3 | +import io.opentelemetry.sdk.common.CompletableResultCode; |
| 4 | +import io.opentelemetry.sdk.common.export.MemoryMode; |
| 5 | +import io.opentelemetry.sdk.metrics.Aggregation; |
| 6 | +import io.opentelemetry.sdk.metrics.InstrumentType; |
| 7 | +import io.opentelemetry.sdk.metrics.data.AggregationTemporality; |
| 8 | +import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector; |
| 9 | +import io.opentelemetry.sdk.metrics.export.CollectionRegistration; |
| 10 | +import io.opentelemetry.sdk.metrics.export.MetricReader; |
| 11 | +import java.util.concurrent.Executors; |
| 12 | +import java.util.concurrent.ScheduledExecutorService; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.concurrent.atomic.AtomicReference; |
| 15 | +import java.util.logging.Level; |
| 16 | +import java.util.logging.Logger; |
| 17 | + |
| 18 | +public class CustomMetricReader implements MetricReader { |
| 19 | + |
| 20 | + private static final Logger logger = Logger.getLogger(CustomMetricExporter.class.getName()); |
| 21 | + |
| 22 | + private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1); |
| 23 | + private final AtomicReference<CollectionRegistration> collectionRef = |
| 24 | + new AtomicReference<>(CollectionRegistration.noop()); |
| 25 | + |
| 26 | + @Override |
| 27 | + public void register(CollectionRegistration collectionRegistration) { |
| 28 | + // Callback invoked when SdkMeterProvider is initialized, providing a handle to collect metrics. |
| 29 | + collectionRef.set(collectionRegistration); |
| 30 | + executorService.scheduleWithFixedDelay(this::collectMetrics, 0, 60, TimeUnit.SECONDS); |
| 31 | + } |
| 32 | + |
| 33 | + private void collectMetrics() { |
| 34 | + // Collect metrics. Typically, records are sent out of process via some network protocol, but we |
| 35 | + // simply log for illustrative purposes. |
| 36 | + logger.log(Level.INFO, "Collecting metrics"); |
| 37 | + collectionRef |
| 38 | + .get() |
| 39 | + .collectAllMetrics() |
| 40 | + .forEach(metric -> logger.log(Level.INFO, "Metric: " + metric)); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public CompletableResultCode forceFlush() { |
| 45 | + // Export any records which have been queued up but not yet exported. |
| 46 | + logger.log(Level.INFO, "flushing"); |
| 47 | + return CompletableResultCode.ofSuccess(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public CompletableResultCode shutdown() { |
| 52 | + // Shutdown the exporter and cleanup any resources. |
| 53 | + logger.log(Level.INFO, "shutting down"); |
| 54 | + return CompletableResultCode.ofSuccess(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public AggregationTemporality getAggregationTemporality(InstrumentType instrumentType) { |
| 59 | + // Specify the required aggregation temporality as a function of instrument type |
| 60 | + return AggregationTemporalitySelector.deltaPreferred() |
| 61 | + .getAggregationTemporality(instrumentType); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public MemoryMode getMemoryMode() { |
| 66 | + // Optionally specify the memory mode, indicating whether metric records can be reused or must |
| 67 | + // be immutable |
| 68 | + return MemoryMode.REUSABLE_DATA; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public Aggregation getDefaultAggregation(InstrumentType instrumentType) { |
| 73 | + // Optionally specify the default aggregation as a function of instrument kind |
| 74 | + return Aggregation.defaultAggregation(); |
| 75 | + } |
| 76 | +} |
0 commit comments