Skip to content

Commit 398fda5

Browse files
author
Anuraag Agrawal
authored
Update Java docs to new format (#174)
1 parent 49c6558 commit 398fda5

File tree

4 files changed

+299
-260
lines changed

4 files changed

+299
-260
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
title: 'Getting Started with the Java SDK on Traces and Metrics Instrumentation'
33
description:
44
OpenTelemetry provides different language SDKs to instrument code for collecting telemetry data in the application.
5-
In this doc, we will introduce how to use OpenTelemetry Java SDK for traces and metrics instrumentation in the application...
65
path: '/docs/getting-started/java-sdk'
76
---
87

98
import SectionSeparator from "components/MdxSectionSeparator/sectionSeparator.jsx"
109

11-
OpenTelemetry provides different language SDKs to instrument code for collecting telemetry data in the application.
10+
# Getting Started with the Java SDK on Traces and Metrics Instrumentation
1211

13-
In this tutorial, we will introduce how to use [OpenTelemetry Java SDK](https://github.com/open-telemetry/opentelemetry-java) for manual instrumentation on traces and metrics and utilize [AWS OpenTelemetry Auto Instrumentation Agent](https://github.com/aws-observability/aws-otel-java-instrumentation) for traces auto instrumentation in the applications.
12+
The AWS Distro for OpenTelemetry (ADOT) Java refers to some components developed to complement the upstream
13+
[OpenTelemetry (OTel) Java SDK](https://github.com/open-telemetry/opentelemetry-java) and
14+
[OTel Java Auto-Instrumentation](https://github.com/open-telemetry/opentelemetry-java-instrumentation). Below are links
15+
to guides that go over how to configure the relevant components of OpenTelemetry to send trace data to the AWS X-Ray
16+
backend.
1417

1518
<SectionSeparator />
1619

1720
## Getting Started
1821

19-
* [Manual Instrumentation on Metrics with Java SDK](/docs/getting-started/java-sdk/metric-manual-instr)
20-
* [Auto Instrumentation on Traces with Auto Agent](/docs/getting-started/java-sdk/trace-auto-instr)
21-
* [Manual Instrumentation on Traces with Java SDK](/docs/getting-started/java-sdk/trace-manual-instr)
22+
* [Auto-Instrumentation for Traces with the Java agent](/docs/getting-started/java-sdk/trace-auto-instr)
23+
* [Manual Instrumentation for Traces with the Java SDK](/docs/getting-started/java-sdk/trace-manual-instr)
2224

23-
24-
## Sample Code with Java SDK
25-
* [AWS Distro for OpenTelemetry Sample Code with Java SDK](https://github.com/aws-observability/aws-otel-test-framework/tree/terraform/sample-apps/spark)
26-
* [AWS Distro for OpenTelemetry Playground in Traces Auto Instrumentation](https://github.com/aws-observability/aws-otel-playground)
25+
## Sample Code
26+
* [Sample Spring App using OpenTelemetry Java Auto Instrumentation](https://github.com/aws-samples/one-observability-demo/tree/main/PetAdoptions/petsearch-java)

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

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 47 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 'Tracing with AWS Auto Instrumentation Agent'
2+
title: 'Tracing with the AWS Distro for OpenTelemetry Java Auto-Instrumentation and X-Ray'
33
description:
44
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
@@ -10,33 +10,30 @@ path: '/docs/getting-started/java-sdk/trace-auto-instr'
1010

1111
import SectionSeparator from "components/MdxSectionSeparator/sectionSeparator.jsx"
1212

13-
The Auto-Instrumentation Java Agent for OpenTelemetry can be attached to any Java 8+ application to gather telemetry data from a diverse
14-
set of libraries and frameworks. This data can also be exported to different backends in different formats. In addittion,
15-
the agent can be configured with environment variables or CLI parameters.
13+
## Introduction
1614

17-
In this tutorial, we will introduce how to use the java agent for automatic trace instrumentation.
15+
The AWS Distro for OpenTelemetry Java Auto-Instrumentation Agent can be attached to any Java 8+ application to gather
16+
telemetry data from a diverse set of libraries and frameworks. The agent will automatically transform code at startup
17+
to collect tracing data. It is preconfigured for compatibility with X-Ray and AWS services but can also be used with any
18+
other tracing backend. Out of the box, it propagates traces using all of W3C Trace Context, B3, and X-Amzn-Trace-Id.
1819

1920
<SectionSeparator />
2021

2122
## Requirements
2223

23-
[Java 8 (or later)](https://adoptopenjdk.net/) is required to run an application using OpenTelemetry.Getting the auto-instrumentation agent
24+
[Java 8 (or later)](https://adoptopenjdk.net/) is required to run an application using OpenTelemetry.
2425

25-
Download the [latest version](https://github.com/aws-observability/aws-otel-java-instrumentation/releases/latest/download/aws-opentelemetry-agent.jar).
26+
Note: You’ll also need to have the [ADOT Collector](https://aws-otel.github.io/docs/getting-started/collector) running to export traces to X-Ray.
2627

27-
If you'd prefer to pin to a specific version, check out our [releases](https://github.com/aws-observability/aws-otel-java-instrumentation/releases).
28+
## Installation
2829

29-
This package includes the instrumentation agent and instrumentation for all supported libraries and all available data exporters,
30-
providing a complete out-of-the-box experience for tracing. The agent is preconfigured to generate trace IDs compatible
31-
with [AWS X-Ray](https://aws.amazon.com/xray/), which works with any other tracing system, and enables trace propagation
32-
using [W3C Trace Context](https://w3c.github.io/trace-context/), [B3](https://github.com/openzipkin/b3-propagation),
33-
and [X-Ray](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-tracingheader).
30+
Download the [latest version](https://github.com/aws-observability/aws-otel-java-instrumentation/releases/latest/download/aws-opentelemetry-agent.jar).
3431

35-
Note: You’ll also need to have the AWS Distro for OpenTelemetry Collector running to export traces to X-Ray.
32+
If you'd prefer to pin to a specific version, check out our [releases](https://github.com/aws-observability/aws-otel-java-instrumentation/releases).
3633

3734
<SectionSeparator />
3835

39-
## Running the agent from the command line
36+
## Running an Application with Auto-Instrumentation
4037
To run your app with the agent, specify the `-javaagent` flag when starting up your application,
4138
pointing to the downloaded agent Java Archive (JAR) artifact. In addition, while not required by the agent itself,
4239
almost all tracing systems require a service name and service namespace defined to identify your application, which you can specify
@@ -49,11 +46,20 @@ OTEL_RESOURCE_ATTRIBUTES=service.name=MyApp,service.namespace=MyTeam java -javaa
4946
The above command will start up your app with the agent activated, and instrumentation is then activated
5047
automatically. For many cases, this is all you need to use tracing.
5148

52-
<SectionSeparator />
49+
### Configuring Auto-Instrumentation
5350

54-
## Running the agent in Docker
55-
If your app is packaged in Docker, the easiest way to run with the agent is to use the
56-
`JAVA_TOOL_OPTIONS` environment variable, which automatically sets flags for java. Adding this
51+
By default OpenTelemetry Java agent uses the [OTLP exporter](https://github.com/open-telemetry/opentelemetry-java/tree/master/exporters/otlp)
52+
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`.
54+
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).
56+
For example, to set the random sampling rate for creating traces, you can set the environment variables
57+
`OTEL_TRACES_SAMPLER=parentbased_traceidratio` and `OTEL_TRACES_SAMPLER_ARG=0.3` to configure a sampling rate of 30%.
58+
59+
### Running the agent in Docker
60+
61+
If your application is packaged in Docker, the easiest way to run with the agent is to use the
62+
`JAVA_TOOL_OPTIONS` environment variable, which automatically sets flags for Java. Adding this
5763
snippet to your `Dockerfile` will typically be enough to enable tracing, although if you already set
5864
`JAVA_TOOL_OPTIONS`, don't forget to make sure to add to your existing setting rather than replacing it.
5965

@@ -64,65 +70,37 @@ ENV JAVA_TOOL_OPTIONS=-javaagent:/opt/aws-opentelemetry-agent.jar
6470

6571
<SectionSeparator />
6672

67-
## Advanced configuration of the auto-instrumentation agent
68-
By default OpenTelemetry Java agent uses the [OTLP exporter](https://github.com/open-telemetry/opentelemetry-java/tree/master/exporters/otlp)
69-
and is configured to send data to a [OpenTelemetry collector](https://github.com/open-telemetry/opentelemetry-collector/blob/master/receiver/otlpreceiver/README.md)
70-
at `http://localhost:4317`.
71-
72-
The agent can be configured using [standard OpenTelemetry options for configuration](https://github.com/open-telemetry/opentelemetry-java-instrumentation#configuration-parameters-subject-to-change).
73-
For example, to set the random sampling rate for creating traces, you can set the environment variables
74-
`OTEL_TRACES_SAMPLER=parentbased_traceidratio` and `OTEL_TRACES_SAMPLER_ARG=0.3` to configure a sampling rate of 30%.
75-
76-
<SectionSeparator />
73+
## Using Manual Instrumentation
7774

78-
## Instrumenting within your application
7975
While the Java agent provides automatic instrumentation for popular frameworks, you might find the need
8076
to perform instrumentation in your application, for example, to provide custom data or to instrument
8177
code within the application itself.
8278

83-
### Adding custom attributes
84-
You can add custom attributes to a `Span` by defining an `AttributeKey` and calling `setAttribute` on the current `Span`.
85-
```java lineNumbers=true
86-
@Controller
87-
public class AppController {
88-
89-
private static final AttributeKey<String> ORGANIZATION_ID = AttributeKeys.stringKey("organization.id");
79+
To perform manual instrumentation alongside the agent, you only need to use the `opentelemetry-api` aritfact. The
80+
version of this artifact can not be newer than the version of the agent. Notably, you do not need the `opentelemetry-sdk`
81+
artifact, any usage of it will be disabled by the agent.
9082

91-
@GetMapping("/")
92-
@ResponseBody
93-
public String handler() {
94-
String organizationId = findOrganizationIdForCurrentUser();
95-
Span.current().setAttribute(ORGANIZATION_ID, organizationId);
96-
}
83+
##### For Gradle:
84+
```java lineNumbers=true
85+
dependencies {
86+
implementation("io.opentelemetry:opentelemetry-api:1.5.0")
9787
}
9888
```
99-
In this example, the Java agent has already created a span corresponding to the Spring
100-
handler method. You use `Span.current()` to access that span and set the
101-
attribute for a key that you have defined as a constant by using `AttributeKeys`.
102-
103-
**Note:** When using AWS X-Ray as your tracing backend, you can control whether attributes are uploaded as annotations
104-
or metadata by configuring the AWS OTel Collector’s
105-
[indexed keys](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/master/exporter/awsxrayexporter#exporter-configuration).
106-
By default, all attributes will be metadata.
107-
108-
### Creating spans
109-
To add a span corresponding to a method in your application, we recommend using the `@WithSpan` annotation.
110-
```java lineNumbers=true
111-
import io.opentelemetry.extension.annotations.WithSpan;
11289

113-
public class MyClass {
114-
@WithSpan
115-
public void MyLogic() {
116-
// Span created encapsulating the logic
117-
}
118-
}
90+
##### For Maven:
91+
```xml lineNumbers=true
92+
<dependencies>
93+
<dependency>
94+
<groupId>io.opentelemetry</groupId>
95+
<artifactId>opentelemetry-api</artifactId>
96+
<version>1.5.0</version>
97+
</dependency>
98+
</dependencies>
11999
```
120-
This automatically creates a span corresponding to the method with the same name as the method. You
121-
can use `Span.current()` inside the method to customize it, for example by adding attributes.
122100

123-
You can also use the [Tracer API](https://github.com/open-telemetry/opentelemetry-java/blob/master/QUICKSTART.md#tracing)
124-
if you need more functionality or want to trace only a block, rather than a method, of code.
101+
See the [document on manual instrumentation](/docs/getting-started/java-sdk/trace-manual-instr#custom-instrumentation)
102+
for more detail on using the OpenTelemetry API.
103+
104+
<SectionSeparator />
125105

126-
### Using the AWS SDK
127-
The Java agent includes instrumentation for the AWS SDK, which is enabled by default. This means you don’t need to
128-
complete any additional steps for detailed tracing of the AWS SDK.
106+
* [Sample Spring App using OpenTelemetry Java Auto Instrumentation](https://github.com/aws-samples/one-observability-demo/tree/main/PetAdoptions/petsearch-java)

0 commit comments

Comments
 (0)