Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add troubleshooting readme and traces example #576

Merged
merged 3 commits into from
Mar 13, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/troubleshooting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Troubleshooting EDOT agent setups

This directory holds code examples for testing aspects of running applications
with the EDOT Java agent

# Compilation

Currently the examples are just standalone code. The
dependencies are the opentelemetry jars, so putting any
of the code examples into a project and adding these three
dependencies will allow for easy compilation

- io.opentelemetry:opentelemetry-api
- io.opentelemetry:opentelemetry-sdk
- io.opentelemetry:opentelemetry-exporter-otlp

(Manual compilation would require the jars and their dependencies downloaded, and then javac -cp <list of jars> path-to-java-file.)

# Traces

The TestOtelSdkTrace class is a standalone class that creates
a span (named `test span`) in a service that you name. It takes
three arguments
1. The name of the service you want to have displayed in the APM UI
2. The endpoint to send traces to, normally the Elastic APM server or the OpenTelemetry collector.
The url would typically look like `http://localhost:4318/v1/traces` or `https://somewhere:443/v1/traces`
but if `/v1/traces` is missing from the argument, it is added to the endpoint
3. The secret token or apikey in the format secret:<token> or apikey:<apikey>.
If the argument doesn't start with neither `secret:` nor `apikey:`, then the full argument is assumed to be a secret token

After running the class, you should see the trace in the APM UI, eg with service name set to `test1` and
correct endpoint and token, you should see something similar to
![this](images\test-trace.png)

# Metrics

To do

# Logs

To do
Binary file added examples/troubleshooting/images/test-trace.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package elastic.troubleshooting;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;

public class TestOtelSdkTrace {

public static void main(String[] args) throws Exception {
String servicename = args[0];
String endpoint = args[1];
String secretOrApikey = args[2];
if (!endpoint.endsWith("/v1/traces")) {
if (endpoint.endsWith("/")) {
endpoint = endpoint + "v1/traces";
} else {
endpoint = endpoint + "/v1/traces";
}
}
boolean isSecret = true;
if (secretOrApikey.startsWith("secret:")) {
secretOrApikey = secretOrApikey.substring(7);
} else if (secretOrApikey.startsWith("apikey:")) {
secretOrApikey = secretOrApikey.substring(7);
isSecret = false;
}

System.out.println("Starting test ... initializing OpenTelemetry");

Resource resource = Resource.getDefault().toBuilder().put("service.name", servicename).build();

OtlpHttpSpanExporter spanExporter = OtlpHttpSpanExporter.builder().setEndpoint(endpoint)
.addHeader("Authorization", (isSecret ? "Bearer " : "ApiKey ") + secretOrApikey).build();

SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(BatchSpanProcessor.builder(spanExporter).build()).setResource(resource)
.build();

OpenTelemetrySdk openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider)
.build();

System.out.println("Starting test ... creating a span");

Tracer tracer = openTelemetry.getTracer("TestOtelSdkTrace");
Span span = tracer.spanBuilder("test span").startSpan();
try {
System.out.println("Starting test ... ending the span");
} finally {
span.end();
}
openTelemetry.shutdown().whenComplete(() -> {
System.out.println("Ending test ... shutdown OpenTelemetry");
});

Thread.sleep(60_000L);
System.out.println("Ending test ... terminating the JVM");
}
}