Skip to content
OpenTelemetry · How-to Intermediate

Instrument a service with OpenTelemetry

Add the OpenTelemetry SDK to a service, capture traces automatically, write one custom span, and export it all over OTLP — then point that same stream at Sluicio. Shown in Java, .NET and JavaScript.

SLSluicio team 12 min read Updated Jun 2026

Most modern services can emit production-grade telemetry with little or no code change — the OpenTelemetry SDK (or agent) wraps your HTTP server, database driver, and message client and records a span for every operation. This guide takes you from an un-instrumented service to a live trace stream in four steps, then adds a single custom span where the automatic ones aren’t enough.

Every code example below is shown in Java, .NET and JavaScript — pick your language with the tabs and the whole page follows.

Pull in the OpenTelemetry SDK plus auto-instrumentation for your framework.

The Java agent auto-instruments your app with no code changes — just download the jar.

Terminal window
curl -L -o opentelemetry-javaagent.jar \
https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar

Initialise tracing before your application logic runs.

No code — attach the agent at startup and name your service. The agent patches libraries as they load.

Terminal window
java -javaagent:./opentelemetry-javaagent.jar \
-Dotel.service.name=orders-api \
-jar app.jar

The OTLP exporter reads its target from standard environment variables — the same names in every language, so nothing here is language-specific. Set the endpoint and your workspace key:

.env
OTEL_EXPORTER_OTLP_ENDPOINT=https://your-instance-name.sluicio.com
OTEL_EXPORTER_OTLP_HEADERS=authorization=Bearer <your-ingest-key>

Auto-instrumentation covers the edges of your service. To time a specific piece of business logic — say, validating an order — wrap it in a span of your own. The setAttribute calls become searchable fields in Sluicio. Where the service keeps a copy of what it received or sent, set io.payload.url to its address and the message links to it: see Payload links.

OrderService.java
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
Tracer tracer = GlobalOpenTelemetry.getTracer("orders");
void validateOrder(Order order) {
Span span = tracer.spanBuilder("order.validate").startSpan();
try (var scope = span.makeCurrent()) {
span.setAttribute("order.id", order.getId());
span.setAttribute("order.items", order.getLines().size());
runRules(order);
} finally {
span.end(); // always close the span
}
}

When an operation fails, record the exception on its span and mark the span as an error. The SDK writes it as an exception event with exception.type, exception.message and exception.stacktrace, which Sluicio shows on the failed message, in the trace (stack trace included) and in alert notifications.

} catch (Exception e) {
span.recordException(e);
span.setStatus(StatusCode.ERROR, e.getMessage());
throw e;
}

Recording the exception and setting the status are separate on purpose: a span can record an exception it recovered from and still succeed. Sluicio counts a message as failed by its status, and names the exception when one was recorded.

Within a few seconds of restarting, your service appears in the overview and traces start flowing. Open one and you’ll see the auto-captured HTTP and database spans alongside your order.validate span, nested exactly where it ran.