Skip to content

Commit e59e5ef

Browse files
committed
more refinement
1 parent dd73ea7 commit e59e5ef

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package otel;
2+
3+
import io.opentelemetry.api.OpenTelemetry;
4+
import io.opentelemetry.api.common.AttributeKey;
5+
import io.opentelemetry.api.common.Attributes;
6+
import io.opentelemetry.api.metrics.DoubleHistogram;
7+
import io.opentelemetry.api.metrics.Meter;
8+
import java.util.List;
9+
10+
public class OtelHistogramAsSummary {
11+
private static final AttributeKey<String> DEVICE_TYPE = AttributeKey.stringKey("device_type");
12+
private static final Attributes THERMOSTAT = Attributes.of(DEVICE_TYPE, "thermostat");
13+
private static final Attributes LOCK = Attributes.of(DEVICE_TYPE, "lock");
14+
15+
public static void summaryReplacement(OpenTelemetry openTelemetry) {
16+
Meter meter = openTelemetry.getMeter("smart.home");
17+
// No explicit bucket boundaries: captures count and sum, a good stand-in for most
18+
// Summary use cases. For quantile estimation, add boundaries that bracket your thresholds.
19+
DoubleHistogram deviceCommandDuration =
20+
meter
21+
.histogramBuilder("device.command.duration")
22+
.setDescription("Time to receive acknowledgment from a smart home device")
23+
.setUnit("s")
24+
.setExplicitBucketBoundariesAdvice(List.of())
25+
.build();
26+
27+
deviceCommandDuration.record(0.35, THERMOSTAT);
28+
deviceCommandDuration.record(0.85, LOCK);
29+
}
30+
}

doc-snippets/prometheus-migration/src/main/java/otel/OtelHistogramExponentialExporter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
44
import io.opentelemetry.sdk.metrics.Aggregation;
55
import io.opentelemetry.sdk.metrics.InstrumentType;
6+
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
67

78
public class OtelHistogramExponentialExporter {
89
static OtlpHttpMetricExporter createExporter() {
@@ -11,10 +12,8 @@ static OtlpHttpMetricExporter createExporter() {
1112
return OtlpHttpMetricExporter.builder()
1213
.setEndpoint("http://localhost:4318")
1314
.setDefaultAggregationSelector(
14-
type ->
15-
type == InstrumentType.HISTOGRAM
16-
? Aggregation.base2ExponentialBucketHistogram()
17-
: Aggregation.defaultAggregation())
15+
DefaultAggregationSelector.getDefault()
16+
.with(InstrumentType.HISTOGRAM, Aggregation.base2ExponentialBucketHistogram()))
1817
.build();
1918
}
2019
}

0 commit comments

Comments
 (0)