You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
12
11
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.
14
17
15
18
<SectionSeparator />
16
19
17
20
## Getting Started
18
21
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)
22
24
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)
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
16
14
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.
18
19
19
20
<SectionSeparator />
20
21
21
22
## Requirements
22
23
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.
24
25
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.
26
27
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
28
29
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).
34
31
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).
36
33
37
34
<SectionSeparator />
38
35
39
-
## Running the agent from the command line
36
+
## Running an Application with Auto-Instrumentation
40
37
To run your app with the agent, specify the `-javaagent` flag when starting up your application,
41
38
pointing to the downloaded agent Java Archive (JAR) artifact. In addition, while not required by the agent itself,
42
39
almost all tracing systems require a service name and service namespace defined to identify your application, which you can specify
The above command will start up your app with the agent activated, and instrumentation is then activated
50
47
automatically. For many cases, this is all you need to use tracing.
51
48
52
-
<SectionSeparator />
49
+
### Configuring Auto-Instrumentation
53
50
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
57
63
snippet to your `Dockerfile` will typically be enough to enable tracing, although if you already set
58
64
`JAVA_TOOL_OPTIONS`, don't forget to make sure to add to your existing setting rather than replacing it.
## 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
77
74
78
-
## Instrumenting within your application
79
75
While the Java agent provides automatic instrumentation for popular frameworks, you might find the need
80
76
to perform instrumentation in your application, for example, to provide custom data or to instrument
81
77
code within the application itself.
82
78
83
-
### Adding custom attributes
84
-
You can add custom attributes to a `Span` by defining an `AttributeKey` and calling `setAttribute` on the current `Span`.
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.
122
100
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 />
125
105
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