Skip to content

Commit caf7f2f

Browse files
committed
Add histogram example
1 parent a6e390c commit caf7f2f

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

src/docs/getting-started/java-sdk/trace-manual-instr.mdx

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -397,35 +397,45 @@ class RequestHandler {
397397

398398
### Creating Metrics
399399

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.
402402

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.
404405

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):
406409
```java
407410
Meter meter = opentelemetry.getMeter("consumer-application");
408411

409412
LongCounter counter = meter.counterBuilder("messages_consumed")
410413
.setDescription("Number of messages consumed")
411-
.setUnit("1")
414+
.setUnit("n")
412415
.build();
413416

414417
Attributes attributes1 = Attributes.of(AttributeKey.stringKey("processing_place"), "Place1");
415418
Attributes attributes2 = Attributes.of(AttributeKey.stringKey("processing_place"), "Place2");
416419

417-
// Counters are synchronous
418-
counter.record(getProcessedMessages(), attributes1);
420+
// Counters can be synchronous
421+
counter.record(getProcessedMessagesQueue1(), attributes1);
419422

420423
// 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+
422431
```
423432

424-
Gauges:
433+
[Gauges](https://opentelemetry.io/docs/reference/specification/metrics/api/#asynchronous-gauge):
425434
```java
426435
Meter meter = opentelemetry.getMeter("consumer-application");
427436

428437
Attributes attributes1 = Attributes.of(AttributeKey.stringKey("queue_name"), "Queue1");
438+
Attributes attributes2 = Attributes.of(AttributeKey.stringKey("queue_name"), "Queue2");
429439

430440
Gauge gauge = meter
431441
.gaugeBuilder("consumer_queue_size")
@@ -435,8 +445,26 @@ Gauge gauge = meter
435445
// Gauges are asynchronous
436446
.buildWithCallback(
437447
measurement -> {
438-
measurement.record(getQueueSize(), attributes1);
448+
measurement.record(getQueueSize1(), attributes1);
449+
measurement.record(getQueueSize2(), attributes2);
439450
});
440451
```
441452

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+
442470
There are more examples in the [OpenTelemetry Java Manual](https://opentelemetry.io/docs/instrumentation/java/manual/#metrics).

0 commit comments

Comments
 (0)