|
1 | 1 | package otel; |
2 | 2 |
|
3 | 3 | import io.opentelemetry.api.OpenTelemetry; |
| 4 | +import io.opentelemetry.api.common.AttributeKey; |
| 5 | +import io.opentelemetry.api.common.Attributes; |
4 | 6 | import io.opentelemetry.api.metrics.Meter; |
5 | 7 |
|
6 | 8 | public class OtelCounterCallback { |
| 9 | + private static final AttributeKey<String> ZONE = AttributeKey.stringKey("zone"); |
| 10 | + private static final Attributes UPSTAIRS = Attributes.of(ZONE, "upstairs"); |
| 11 | + private static final Attributes DOWNSTAIRS = Attributes.of(ZONE, "downstairs"); |
| 12 | + |
7 | 13 | public static void counterCallbackUsage(OpenTelemetry openTelemetry) { |
8 | 14 | Meter meter = openTelemetry.getMeter("smart.home"); |
9 | | - // The smart energy meter maintains its own cumulative joule total in firmware. |
10 | | - // Use an asynchronous counter to report that value when a MetricReader |
11 | | - // collects metrics, without maintaining a separate counter in application code. |
| 15 | + // Each zone has its own smart energy meter tracking cumulative joule totals. |
| 16 | + // Use an asynchronous counter to report those values when a MetricReader |
| 17 | + // collects metrics, without maintaining separate counters in application code. |
12 | 18 | meter |
13 | 19 | .counterBuilder("energy.consumed") |
14 | 20 | .setDescription("Total energy consumed") |
15 | 21 | .setUnit("J") |
16 | 22 | .ofDoubles() |
17 | | - .buildWithCallback(measurement -> measurement.record(SmartHomeDevices.totalEnergyJoules())); |
| 23 | + .buildWithCallback( |
| 24 | + measurement -> { |
| 25 | + measurement.record(SmartHomeDevices.totalEnergyJoules("upstairs"), UPSTAIRS); |
| 26 | + measurement.record(SmartHomeDevices.totalEnergyJoules("downstairs"), DOWNSTAIRS); |
| 27 | + }); |
18 | 28 | } |
19 | 29 | } |
0 commit comments