Skip to content

Commit dd73ea7

Browse files
committed
more refinement
1 parent 400be94 commit dd73ea7

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
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 OtelUpDownCounterCallback {
9+
private static final AttributeKey<String> DEVICE_TYPE = AttributeKey.stringKey("device_type");
10+
private static final Attributes THERMOSTAT = Attributes.of(DEVICE_TYPE, "thermostat");
11+
private static final Attributes LOCK = Attributes.of(DEVICE_TYPE, "lock");
12+
713
public static void upDownCounterCallbackUsage(OpenTelemetry openTelemetry) {
814
Meter meter = openTelemetry.getMeter("smart.home");
9-
// The command queue maintains its own depth counter.
15+
// The device manager maintains the count of connected devices.
1016
// Use an asynchronous up-down counter to report that value when a MetricReader
1117
// collects metrics.
1218
meter
13-
.upDownCounterBuilder("command.queue.depth")
14-
.setDescription("Number of device commands waiting to be processed")
19+
.upDownCounterBuilder("devices.connected")
20+
.setDescription("Number of smart home devices currently connected")
1521
.buildWithCallback(
16-
measurement -> measurement.record(SmartHomeDevices.pendingCommandCount()));
22+
measurement -> {
23+
measurement.record(SmartHomeDevices.connectedDeviceCount("thermostat"), THERMOSTAT);
24+
measurement.record(SmartHomeDevices.connectedDeviceCount("lock"), LOCK);
25+
});
1726
}
1827
}

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

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

55
public class PrometheusUpDownCounterCallback {
66
public static void upDownCounterCallbackUsage() {
7-
// The command queue maintains its own depth counter.
7+
// The device manager maintains the count of connected devices.
88
// Use a callback gauge to report that value at scrape time.
99
GaugeWithCallback.builder()
10-
.name("command_queue_depth")
11-
.help("Number of device commands waiting to be processed")
12-
.callback(callback -> callback.call(SmartHomeDevices.pendingCommandCount()))
10+
.name("devices_connected")
11+
.help("Number of smart home devices currently connected")
12+
.labelNames("device_type")
13+
.callback(
14+
callback -> {
15+
callback.call(SmartHomeDevices.connectedDeviceCount("thermostat"), "thermostat");
16+
callback.call(SmartHomeDevices.connectedDeviceCount("lock"), "lock");
17+
})
1318
.register();
1419
}
1520
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ static double bedroomTemperatureCelsius() {
2020
return 0.0; // In production, this would query the device API.
2121
}
2222

23+
/** Returns the number of currently connected devices of the given type. */
24+
static long connectedDeviceCount(String deviceType) {
25+
return 0L; // In production, this would query the device manager.
26+
}
27+
2328
/** Returns the number of device commands currently waiting to be processed. */
2429
static long pendingCommandCount() {
2530
return 0L; // In production, this would query the command queue.

0 commit comments

Comments
 (0)