Skip to content

Commit cdfdb21

Browse files
authored
Merge pull request #371 from aws-otel/raphasil-update-java-metrics
* Fix minor inconsistencies in the documentation. * Add examples for Manually adding metrics to an application. * Reference existing SparkJava sample app that makes use of Traces and metrics.
2 parents a424e76 + cb3963d commit cdfdb21

File tree

3 files changed

+128
-20
lines changed

3 files changed

+128
-20
lines changed

src/docs/getting-started/java-sdk.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ backend.
1717

1818
## Getting Started
1919

20-
* [Auto-Instrumentation for Traces with the Java agent](/docs/getting-started/java-sdk/trace-auto-instr)
21-
* [Manual Instrumentation for Traces with the Java SDK](/docs/getting-started/java-sdk/trace-manual-instr)
20+
* [Auto-Instrumentation for Traces and Metrics with the Java agent](/docs/getting-started/java-sdk/trace-auto-instr)
21+
* [Manual Instrumentation for Traces and Metrics with the Java SDK](/docs/getting-started/java-sdk/trace-manual-instr)
2222

2323
## Sample Code
2424
* [Sample Spring App using OpenTelemetry Java Auto-Instrumentation](https://catalog.us-east-1.prod.workshops.aws/v2/workshops/31676d37-bbe9-4992-9cd1-ceae13c5116c/en-US/adot/javawalkthrough)

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
2-
title: 'Tracing with the AWS Distro for OpenTelemetry Java Auto-Instrumentation and X-Ray'
2+
title: 'Auto-Instrumentation for Traces and Metrics with the Java agent'
33
description:
4-
Learn how to get started with Java Auto-Instrumentation Agent... This package includes the instrumentation agent,
4+
Learn how to get started with Java Auto-Instrumentation Agent. This package includes the instrumentation agent,
55
instrumentations for all supported libraries and all available data exporters, providing a complete out of the box
6-
experience for tracing on AWS. The agent is preconfigured to generate trace IDs compatible with AWS X-Ray, which
6+
experience for tracing and metrics on AWS. The agent is preconfigured to generate trace IDs compatible with AWS X-Ray, which
77
will also work with any other tracing system, and enables trace propagation using W3C Trace Context, B3, and X-Ray.
88
path: '/docs/getting-started/java-sdk/trace-auto-instr'
99
---
@@ -50,12 +50,15 @@ automatically. For many cases, this is all you need to use tracing.
5050

5151
By default OpenTelemetry Java agent uses the [OTLP exporter](https://github.com/open-telemetry/opentelemetry-java/tree/master/exporters/otlp)
5252
and is configured to send data to a [OpenTelemetry collector](https://github.com/open-telemetry/opentelemetry-collector/blob/master/receiver/otlpreceiver/README.md)
53-
at `http://localhost:4317`.
53+
at `http://localhost:4317` for both metrics and traces.
5454

55-
The agent can be configured using [standard OpenTelemetry options for configuration](https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/docs/agent-config.md).
55+
The agent can be configured using [standard OpenTelemetry options for configuration](https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk-extensions/autoconfigure/README.md) using either environment variables or system properties.
5656
For example, to set the random sampling rate for creating traces, you can set the environment variables
5757
`OTEL_TRACES_SAMPLER=parentbased_traceidratio` and `OTEL_TRACES_SAMPLER_ARG=0.3` to configure a sampling rate of 30%.
5858

59+
Another useful configuration that can be used during development is to log traces and metrics. This can be achieved by
60+
setting `OTEL_TRACES_EXPORTER=logging` and `OTEL_METRICS_EXPORTER=logging`.
61+
5962
### Using X-Ray Remote Sampling
6063

6164
The ADOT Java Auto-Instrumentation Agent can be configured to use [X-Ray remote sampling](https://docs.aws.amazon.com/xray/latest/devguide/xray-console-sampling.html)
@@ -90,7 +93,7 @@ artifact, any usage of it will be disabled by the agent.
9093
##### For Gradle:
9194
```kotlin lineNumbers=true
9295
dependencies {
93-
implementation("io.opentelemetry:opentelemetry-api:1.6.0")
96+
implementation("io.opentelemetry:opentelemetry-api:1.16.0")
9497
}
9598
```
9699

@@ -100,7 +103,7 @@ dependencies {
100103
<dependency>
101104
<groupId>io.opentelemetry</groupId>
102105
<artifactId>opentelemetry-api</artifactId>
103-
<version>1.6.0</version>
106+
<version>1.16.0</version>
104107
</dependency>
105108
</dependencies>
106109
```
@@ -110,6 +113,7 @@ for more detail on using the OpenTelemetry API.
110113

111114
<SectionSeparator />
112115

113-
## Sample Application
116+
## Sample Applications
114117

115-
Visit the [Sample Spring App using OpenTelemetry Java Auto-Instrumentation](https://catalog.us-east-1.prod.workshops.aws/v2/workshops/31676d37-bbe9-4992-9cd1-ceae13c5116c/en-US/adot/javawalkthrough).
118+
* Visit the [Sample Spring App using OpenTelemetry Java Auto-Instrumentation](https://catalog.us-east-1.prod.workshops.aws/v2/workshops/31676d37-bbe9-4992-9cd1-ceae13c5116c/en-US/adot/javawalkthrough).
119+
* [SparkJava Sample Application Using Traces and Metrics](https://github.com/aws-observability/aws-otel-java-instrumentation/tree/main/sample-apps/spark).

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

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 'Tracing with the AWS Distro for OpenTelemetry Java SDK and X-Ray'
2+
title: 'Manual Instrumentation for Traces and Metrics with the Java SDK'
33
description:
44
Learn how to get started with Java SDK for adding tracing to applications and libraries.
55
path: '/docs/getting-started/java-sdk/trace-manual-instr'
@@ -37,7 +37,7 @@ to align dependency versions for non-contrib components.
3737
##### For Gradle:
3838
```kotlin lineNumbers=true
3939
dependencies {
40-
api(platform("io.opentelemetry:opentelemetry-bom:1.6.0"))
40+
api(platform("io.opentelemetry:opentelemetry-bom:1.16.0"))
4141

4242
implementation("io.opentelemetry:opentelemetry-api")
4343
implementation("io.opentelemetry:opentelemetry-exporter-otlp")
@@ -46,7 +46,7 @@ dependencies {
4646

4747
implementation("io.opentelemetry:opentelemetry-extension-aws")
4848
implementation("io.opentelemetry:opentelemetry-sdk-extension-aws")
49-
implementation("io.opentelemetry.contrib:opentelemetry-aws-xray:1.6.0")
49+
implementation("io.opentelemetry.contrib:opentelemetry-aws-xray:1.16.0")
5050
}
5151
```
5252

@@ -57,7 +57,7 @@ dependencies {
5757
<dependency>
5858
<groupId>io.opentelemetry</groupId>
5959
<artifactId>opentelemetry-bom</artifactId>
60-
<version>1.6.0</version>
60+
<version>1.16.0</version>
6161
<type>pom</type>
6262
<scope>import</scope>
6363
<dependency>
@@ -87,7 +87,7 @@ dependencies {
8787
<dependency>
8888
<groupId>io.opentelemetry.contrib</groupId>
8989
<artifactId>opentelemetry-aws-xray</artifactId>
90-
<version>1.6.0</version>
90+
<version>1.16.0</version>
9191
</dependency>
9292
</dependencies>
9393
```
@@ -159,6 +159,36 @@ OpenTelemetrySdk.builder()
159159
.buildAndRegisterGlobal();
160160
```
161161

162+
### Adding support for Metrics
163+
164+
The API and SDK for Metrics became stable in v1.15.0 of OpenTelemetry for Java.
165+
The following piece of code initialize the OpenTelemetry SDK to use Metrics and Traces.
166+
167+
```java
168+
MetricReader metricReader = PeriodicMetricReader.builder(
169+
OtlpGrpcMetricExporter.getDefault())
170+
.build();
171+
OpenTelemetry opentelemetry = OpenTelemetrySdk.builder()
172+
// Traces configuration
173+
.setPropagators(
174+
ContextPropagators.create(
175+
TextMapPropagator.composite(
176+
W3CTraceContextPropagator.getInstance(), AwsXrayPropagator.getInstance())))
177+
178+
.setTracerProvider(
179+
SdkTracerProvider.builder()
180+
.addSpanProcessor(
181+
BatchSpanProcessor.builder(OtlpGrpcSpanExporter.getDefault()).build())
182+
.setIdGenerator(AwsXrayIdGenerator.getInstance())
183+
.build()
184+
// Metrics Configuration
185+
.setMeterProvider(
186+
SdkMeterProvider.builder()
187+
.registerMetricReader(metricReader)
188+
.build())
189+
.buildAndRegisterGlobal();
190+
```
191+
162192
### Debug Logging
163193

164194
The SDK uses `java.util.logging` to log messages at `FINE` level - logging frameworks like Logback or Log4J map this to
@@ -181,7 +211,7 @@ library instrumentation. When using this, do not include `opentelemetry-bom`.
181211
##### For Gradle:
182212
```kotlin lineNumbers=true
183213
dependencies {
184-
api(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:1.6.0-alpha"))
214+
api(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:1.16.0-alpha"))
185215

186216
implementation("io.opentelemetry:opentelemetry-api")
187217
implementation("io.opentelemetry:opentelemetry-exporter-otlp")
@@ -200,7 +230,7 @@ dependencies {
200230
<dependency>
201231
<groupId>io.opentelemetry.instrumentation</groupId>
202232
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
203-
<version>1.6.0</version>
233+
<version>1.16.0-alpha</version>
204234
<type>pom</type>
205235
<scope>import</scope>
206236
<dependency>
@@ -234,7 +264,7 @@ The `opentelemetry-instrumentation-aws-sdk-2.2` artifact provides instrumentatio
234264
##### For Gradle:
235265
```java lineNumbers=true
236266
dependencies {
237-
api(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:1.5.0-alpha"))\
267+
api(platform("io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha:1.16.0-alpha"))\
238268

239269
implementation("io.opentelemetry.instrumentation:opentelemetry-aws-sdk-2.2")
240270

@@ -249,7 +279,7 @@ dependencies {
249279
<dependency>
250280
<groupId>io.opentelemetry.instrumentation</groupId>
251281
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
252-
<version>1.5.0</version>
282+
<version>1.16.0-alpha</version>
253283
<type>pom</type>
254284
<scope>import</scope>
255285
<dependency>
@@ -364,3 +394,77 @@ class RequestHandler {
364394
}
365395
}
366396
```
397+
398+
### Creating Metrics
399+
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+
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.
405+
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):
409+
```java
410+
Meter meter = opentelemetry.getMeter("consumer-application");
411+
412+
LongCounter counter = meter.counterBuilder("messages_consumed")
413+
.setDescription("Number of messages consumed")
414+
.setUnit("n")
415+
.build();
416+
417+
Attributes attributes1 = Attributes.of(AttributeKey.stringKey("processing_place"), "Place1");
418+
Attributes attributes2 = Attributes.of(AttributeKey.stringKey("processing_place"), "Place2");
419+
420+
// Counters can be synchronous
421+
counter.record(getProcessedMessagesQueue1(), attributes1);
422+
423+
// Different attributes can be associated with the value
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+
431+
```
432+
433+
[Gauges](https://opentelemetry.io/docs/reference/specification/metrics/api/#asynchronous-gauge):
434+
```java
435+
Meter meter = opentelemetry.getMeter("consumer-application");
436+
437+
Attributes attributes1 = Attributes.of(AttributeKey.stringKey("queue_name"), "Queue1");
438+
Attributes attributes2 = Attributes.of(AttributeKey.stringKey("queue_name"), "Queue2");
439+
440+
Gauge gauge = meter
441+
.gaugeBuilder("consumer_queue_size")
442+
.setDescription("The size of the queue that is being consumed")
443+
.setUnit("1")
444+
.ofLongs()
445+
// Gauges are asynchronous
446+
.buildWithCallback(
447+
measurement -> {
448+
measurement.record(getQueueSize1(), attributes1);
449+
measurement.record(getQueueSize2(), attributes2);
450+
});
451+
```
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+
470+
There are more examples in the [OpenTelemetry Java Manual](https://opentelemetry.io/docs/instrumentation/java/manual/#metrics).

0 commit comments

Comments
 (0)