Skip to content

Commit 57ea826

Browse files
committed
Add labels to counter callback examples
1 parent 69b8ecb commit 57ea826

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package otel;
22

33
import io.opentelemetry.api.OpenTelemetry;
4+
import io.opentelemetry.api.common.AttributeKey;
5+
import io.opentelemetry.api.common.Attributes;
46
import io.opentelemetry.api.metrics.Meter;
57

68
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+
713
public static void counterCallbackUsage(OpenTelemetry openTelemetry) {
814
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.
1218
meter
1319
.counterBuilder("energy.consumed")
1420
.setDescription("Total energy consumed")
1521
.setUnit("J")
1622
.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+
});
1828
}
1929
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
public class PrometheusCounterCallback {
66
public static void counterCallbackUsage() {
7-
// The smart energy meter maintains its own cumulative joule total in firmware.
8-
// Use a callback counter to report that value at scrape time without
9-
// maintaining a separate counter in application code.
7+
// Each zone has its own smart energy meter tracking cumulative joule totals.
8+
// Use a callback counter to report those values at scrape time without
9+
// maintaining separate counters in application code.
1010
CounterWithCallback.builder()
1111
.name("energy_consumed_joules_total")
1212
.help("Total energy consumed in joules")
13-
.callback(callback -> callback.call(SmartHomeDevices.totalEnergyJoules()))
13+
.labelNames("zone")
14+
.callback(
15+
callback -> {
16+
callback.call(SmartHomeDevices.totalEnergyJoules("upstairs"), "upstairs");
17+
callback.call(SmartHomeDevices.totalEnergyJoules("downstairs"), "downstairs");
18+
})
1419
.register();
1520
}
1621
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/** Stubs representing smart home device APIs. */
44
class SmartHomeDevices {
55
/**
6-
* Returns cumulative energy consumed in joules since installation, as reported by the smart
7-
* energy meter device.
6+
* Returns cumulative energy consumed in joules since installation in the given zone, as reported
7+
* by the smart energy meter device.
88
*/
9-
static double totalEnergyJoules() {
9+
static double totalEnergyJoules(String zone) {
1010
return 0.0; // In production, this would query the device API.
1111
}
1212

0 commit comments

Comments
 (0)