Before You Begin
Before you can set up automatic instrumentation for your Java application, you will need to do a few things.Prepare Your Development Environment
To complete the required steps, you will need:- A working Java environment
- An application written in Java
Get Your Honeycomb API Key
To send data to Honeycomb, you’ll need to sign up for a free Honeycomb account and create a Honeycomb Ingest API Key. To get started, you can create a key that you expect to swap out when you deploy to production. Name it something helpful, perhaps noting that it’s a getting started key. Make note of your API key; for security reasons, you will not be able to see the key again, and you will need it later! If you want to use an API key you previously stored in a secure location, you can also look up details for Honeycomb API Keys any time in your Environment Settings, and use them to retrieve keys from your storage location.Add Automatic Instrumentation
Automatic instrumentation is handled with a Java Agent that runs alongside your application. Adding manual instrumentation uses the OpenTelemetry API, which is available when using our SDK as a dependency.Acquire Dependencies
The OpenTelemetry Java Agent supports many Java libraries and frameworks. The automatic instrumentation agent for OpenTelemetry Java will automatically generate trace data from your application. The agent is packaged as a JAR file and is run alongside your app. In order to use the automatic instrumentation agent, you must first download it:Configure
As per the OpenTelemetry specification, you must set aservice.name resource in your SDK configuration.
The service name is used as the name of a dataset to store trace data in Honeycomb.
When using OpenTelemetry for Java, all of the following configuration properties are required:
Fields marked with an asterisk (*) are required for exporting metrics to Honeycomb.
To learn more about configuration options, visit the OpenTelemetry SDK Autoconfigure GitHub repository.
If you use Honeycomb Classic, you must also specify the Dataset using the
x-honeycomb-dataset header.Run
Run your application with the automatic instrumentation agent as a sidecar:Add Custom Instrumentation
Automatic instrumentation is the easiest way to get started with instrumenting your code. To get additional insight into your system, you should also add custom, or manual, instrumentation where appropriate. You can use manual instrumentation whether you are using the Agent or the Builder. Follow the instructions below to add custom instrumentation to your code. To learn more about custom, or manual, instrumentation, visit the comprehensive set of topics covered by Manual Instrumentation for Java, including the Annotations API, in OpenTelemetry’s documentation.Acquire Dependencies
To add custom instrumentation, some OpenTelemetry libraries can be added as dependencies for your application. The OpenTelemetry API provides methods that let you access the currently executing span and add attributes to it, and/or to create new spans. The Annotations library provides decorators the OpenTelemetry JavaAgent will use to create spans for decorated methods.Use the
opentelemetry-instrumentation-bom to align the versions of these dependencies with the version of OpenTelemetry JavaAgent in use.- Gradle
- Maven
Add Attributes to Spans
Adding attributes to a currently executing span in a trace can be useful. For example, you may have an application or service that handles users and you want to associate the user with the span when querying your service in Honeycomb. To do this, get the current span from the context and set an attribute with the user ID. In your code, importio.opentelemetry.api.trace.Span to get access to the span:
user.id field to the current span so that you can use the field in WHERE, GROUP BY or ORDER clauses in the Honeycomb query builder.
Acquire a Tracer
To create spans, you need to get aTracer.
Tracer, OpenTelemetry requires you to give it a name as a string.
This string is the only required parameter.
When traces are sent to Honeycomb, the name of the Tracer is turned into the library.name field, which can be used to show all spans created from a particular tracer.
In general, pick a name that matches the appropriate scope for your traces.
If you have one tracer for each service, then use the service name.
If you have multiple tracers that live in different “layers” of your application, then use the name that corresponds to that “layer”.
The library.name field is also used with traces created from instrumentation libraries.
Create New Spans
Automatic instrumentation can show the shape of requests to your system, but only you know the really important parts. To get the full picture of what’s happening, you will have to add custom, or manual, instrumentation and create some custom spans. To do this, create or re-useTracer registered by the Agent and start a span.
In your code, import io.opentelemetry.api.GlobalOpenTelemetry, io.opentelemetry.api.trace.Span, and io.opentelemetry.api.trace.Tracer:
Creating Spans Around Methods
You can also use the annotation@WithSpan to wrap the execution of a method with a span.
The span will be automatically closed once the method has completed.
Unless explicitly specified, the span will be named className.methodName.
To override the name of the span, add a name in parentheses as an argument.
In your code, import io.opentelemetry.instrumentation.annotations.WithSpan to allow usage of this annotation:
Add Multi-Span Attributes
Sometimes you want to add the same attribute to many spans within the same trace. This attribute may include variables calculated during your program, or other useful values for correlation or debugging purposes. To add this attribute, leverage the OpenTelemetry concept of baggage. Baggage allows you to add akey with a value as an attribute to every subsequent child span of the current application context.
In your code, import io.opentelemetry.api.baggage.Baggage to allow use of the Baggage class:
Any Baggage attributes that you set in your application will be attached to outgoing network requests as a header. If your service communicates to a third party API, do NOT put sensitive information in the Baggage attributes.
Sampling
You can configure the OpenTelemetry SDK to sample the data it generates. Honeycomb weights sampled data based on sample rate, so you must set a resource attribute containing the sample rate. Use aTraceIdRatioBased sampler, with a ratio expressed as 1/N.
Then, also create a resource attribute called SampleRate with the value of N.
This allows Honeycomb to reweigh scalar values, like counts, so that they are accurate even with sampled data.
In the example below, our goal is to keep approximately half (1/2) of the data volume.
The resource attribute contains the denominator (2), while the OpenTelemetry sampler argument contains the decimal value (0.5).
| System Property / Environment Variable | Value |
|---|---|
otel.traces.sampler OTEL_TRACES_SAMPLER | traceidratio |
otel.traces.sampler.arg OTEL_TRACES_SAMPLER_ARG | 0.5 |
otel.resource.attributes OTEL_RESOURCE_ATTRIBUTES | SampleRate=2 |
SampleRate must be a positive integer.
Using HTTP Instead of gRPC
By default, OpenTelemetry for Java uses gRPC protocol. To use HTTP instead of gRPC, update the protocol using one of the configuration methods:- System property:
-Dotel.exporter.otlp.protocol=http/protobuf - Environment variable:
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_TRACES_PROTOCOL and OTEL_EXPORTER_OTLP_METRICS_PROTOCOL.
If you are sending data to Honeycomb directly, you can find Trace and Metric Endpoint configuration options, as well as OpenTelemetry Headers, in the OpenTelemetry for Java chart.If you are using an OpenTelemetry Collector, specify the endpoint of the collector, and add the headers to the collector configuration file.
Endpoint URLs for OTLP/HTTP
When using theOTEL_EXPORTER_OTLP_ENDPOINT environment variable with an SDK and an HTTP exporter, the final path of the endpoint is modified by the SDK to represent the specific signal being sent.
For example, when exporting trace data, the endpoint is updated to append v1/traces.
When exporting metrics data, the endpoint is updated to append v1/metrics.
So, if you were to set the OTEL_EXPORTER_OTLP_ENDPOINT to https://api.honeycomb.io, traces would be sent to https://api.honeycomb.io/v1/traces and metrics would be sent to https://api.honeycomb.io/v1/metrics.
The same modification is not necessary for gRPC.
OTEL_EXPORTER_OTLP_<SIGNAL>_ENDPOINT instead of the more generic OTEL_EXPORTER_OTLP_ENDPOINT.
When using a signal-specific environment variable, these paths must be appended manually.
Set OTEL_EXPORTER_OTLP_TRACES_ENDPOINT for traces, appending the endpoint with v1/traces, and OTEL_EXPORTER_OTLP_METRICS_ENDPOINT for metrics, appending the endpoint with v1/metrics.
Send both traces and metrics to Honeycomb using this method by setting the following variables: