File tree Expand file tree Collapse file tree 3 files changed +27
-8
lines changed
doc-snippets/prometheus-migration/src/main/java/otel Expand file tree Collapse file tree 3 files changed +27
-8
lines changed Original file line number Diff line number Diff line change 11package otel ;
22
33import io .opentelemetry .api .OpenTelemetry ;
4+ import io .opentelemetry .api .common .AttributeKey ;
5+ import io .opentelemetry .api .common .Attributes ;
46import io .opentelemetry .api .metrics .Meter ;
57
68public 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}
Original file line number Diff line number Diff line change 44
55public 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}
Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments