Testing - open-telemetry/opentelemetry.io

Testing is an essential part of any software development project, including OpenTelemetry. The OpenTelemetry project provides several testing strategies and tools to ensure the quality of the code. This document will discuss the possible testing options and provide examples for each option, using the documentation and code snippets provided.

Unit Testing

Unit testing is the practice of testing individual units of code, such as functions or methods, to ensure that they are working as expected. OpenTelemetry provides a TestExporter class that can be used for unit testing. Here’s an example of how to use it:

@Test
public void checkInstrumentation() {
SpanExporter exporter = new TestExporter();

Tracer tracer = OpenTelemetrySdk.builder()
.setTracerProvider(SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(exporter))
.build())
.build()
.getTracer("test");

// Use the tracer to create a span
// ...

// Verify the results
// ...
}

(Source: https://opentelemetry.io/docs/concepts/instrumentation/libraries/)

Telemetry Testing

OpenTelemetry also provides a MockServer class for testing OpenTelemetry instrumentation. Here’s an example of how to use it:

public class TelemetryTestingExample {
@Test
public void testTelemetry() throws Exception {
// Start the mock server
MockServer mockServer = MockServer.start();

// Configure the OpenTelemetry SDK to use the Zipkin exporter
// and send spans to the mock server
OpenTelemetrySdk sdk = OpenTelemetrySdk.builder()
.setTracerProvider(SdkTracerProvider.builder()
.addSpanProcessor(ZipkinSpanProcessor.newBuilder()
.setServiceName("test-service")
.setEndpoint(mockServer.getAddress())
.build())
.build())
.build();

// Use the OpenTelemetry API to create a span
Tracer tracer = sdk.getTracer("test");
Span span = tracer.spanBuilder("test-span").startSpan();

// End the span
span.end();

// Verify the results
// ...

// Stop the mock server
mockServer.stop();
}
}

(Source: https://github.com/open-telemetry/opentelemetry-java-examples#java-opentelemetry-examples)

Synthetic Testing

Synthetic testing involves using automated tools to simulate user interactions with a system. OpenTelemetry has a native integration with Synthetics, a tool provided by Grafana. Here’s an example of how to use it:

check(taskName: 'Check OpenTelemetry Collector',
jobName: 'OpenTelemetry Collector',
url: 'http://localhost:55680/health',
expectedStatusCode: 200)

(Source: https://opentelemetry.io/blog/2023/synthetic-testing/)

Troubleshooting

The OpenTelemetry Collector provides several metrics that can be used for troubleshooting. Here’s an example of how to use them:

otelcol --config=path/to/config.yaml metrics

This will output the metrics collected by the OpenTelemetry Collector, including otelcol_receiver_accepted_spans and otelcol_receiver_refused_spans.

(Source: https://grafana.com/docs/opentelemetry/collector/troubleshooting/)

Additional Resources

For more information on testing in OpenTelemetry, check out the following resources:

(Sources: https://opentelemetry.io/docs/concepts/instrumentation/libraries/#testing, https://grafana.com/docs/opentelemetry/collector/troubleshooting/, https://opentelemetry.io/blog/2023/synthetic-testing/)