@@ -397,35 +397,45 @@ class RequestHandler {
397
397
398
398
### Creating Metrics
399
399
400
- Up to v1. 16.0 of OpenTelemetry for Java , there are few libraries and frameworks that make use of the Metrics API . Similarly to
401
- Traces , you can create custom metrics in your application using the OpentTelemetry API and SDK .
400
+ Up to v1. 16.0 of OpenTelemetry for Java , there are few libraries and frameworks that make use of the Metrics API .
401
+ Similarly to Traces , you can create custom metrics in your application using the OpentTelemetry API and SDK .
402
402
403
- In the following examples we detail how to report two types of metrics: Counters and GetSamplingRules
403
+ In the following example application we demonstrate how to use the three types of metric instruments that
404
+ are available to record metrics: Counters , Gauges and Histograms .
404
405
405
- Counter :
406
+ The theoretic application being depicted is a worker that process messages from 2 different queues.
407
+
408
+ [Counters ](https: // opentelemetry.io/docs/reference/specification/metrics/api/#counter):
406
409
```java
407
410
Meter meter = opentelemetry. getMeter(" consumer-application" );
408
411
409
412
LongCounter counter = meter. counterBuilder(" messages_consumed" )
410
413
.setDescription(" Number of messages consumed" )
411
- .setUnit(" 1 " )
414
+ .setUnit(" n " )
412
415
.build();
413
416
414
417
Attributes attributes1 = Attributes . of(AttributeKey . stringKey(" processing_place" ), " Place1" );
415
418
Attributes attributes2 = Attributes . of(AttributeKey . stringKey(" processing_place" ), " Place2" );
416
419
417
- // Counters are synchronous
418
- counter. record(getProcessedMessages (), attributes1);
420
+ // Counters can be synchronous
421
+ counter. record(getProcessedMessagesQueue1 (), attributes1);
419
422
420
423
// Different attributes can be associated with the value
421
- counter. record(getProcessedMessages(), attributes2);
424
+ counter. record(getProcessedMessagesQueue2(), attributes2);
425
+
426
+ // Counters also have the asynchronous form
427
+ LongCounter messagesDroppedCounter = meter. counterBuilder(" messages_dropped" )
428
+ .setDescription(" Number of messages dropped" )
429
+ .buildWithCallback( (consumer) - > consumer. record(getTotalMessagesDropped()));
430
+
422
431
```
423
432
424
- Gauges :
433
+ [ Gauges ](https : // opentelemetry.io/docs/reference/specification/metrics/api/#asynchronous-gauge) :
425
434
```java
426
435
Meter meter = opentelemetry. getMeter(" consumer-application" );
427
436
428
437
Attributes attributes1 = Attributes . of(AttributeKey . stringKey(" queue_name" ), " Queue1" );
438
+ Attributes attributes2 = Attributes . of(AttributeKey . stringKey(" queue_name" ), " Queue2" );
429
439
430
440
Gauge gauge = meter
431
441
.gaugeBuilder(" consumer_queue_size" )
@@ -435,8 +445,26 @@ Gauge gauge = meter
435
445
// Gauges are asynchronous
436
446
.buildWithCallback(
437
447
measurement - > {
438
- measurement. record(getQueueSize(), attributes1);
448
+ measurement. record(getQueueSize1(), attributes1);
449
+ measurement. record(getQueueSize2(), attributes2);
439
450
});
440
451
```
441
452
453
+ [Histograms ](https: // opentelemetry.io/docs/reference/specification/metrics/api/#histogram):
454
+ ```java
455
+ Meter meter = opentelemetry. getMeter(" consumer-application" );
456
+
457
+ // Histograms metric data points convey a population of recorded measurements in a compressed format.
458
+ // A histogram bundles a set of events into divided populations with an overall event count and aggregate sum for all events.
459
+ // Histograms are useful to record measurements such as latency. With histograms we can extract the min, max and percentiles.
460
+ LongHistogram histogram = meter. histogramBuilder(" processing_time" )
461
+ .setUnit(" ms" )
462
+ .setDescription(" Amount of time it takes to process a message" )
463
+ .ofLongs()
464
+ .build();
465
+
466
+ histogram. record(messageProcessingTime)
467
+
468
+ ```
469
+
442
470
There are more examples in the [OpenTelemetry Java Manual ](https: // opentelemetry.io/docs/instrumentation/java/manual/#metrics).
0 commit comments