Java Tutorial 0/145 lessons ~6 min read Lesson 73

    OpenTelemetry

    opentelemetry opentelemetry is the vendor-neutral standard for traces, metrics and logs. spring boot 3.x auto-instruments http, jdbc and kafka — export to

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    OpenTelemetry is the vendor-neutral standard for traces, metrics and logs. Spring Boot 3.x auto-instruments HTTP, JDBC and Kafka — export to Jaeger, Grafana Tempo or Datadog with zero code changes.

    Informative example

    Manual span + Spring Boot config:

    ts
    @Service
    public class OrderService {
    private final Tracer tracer;
    public Order create(OrderRequest req) {
    Span span = tracer.spanBuilder("createOrder").startSpan();
    try (var scope = span.makeCurrent()) {
    span.setAttribute("order.sku", req.sku());
    return repo.save(new Order(req));
    } finally { span.end(); }
    }
    }
    # application.yml
    management.tracing.sampling.probability=1.0
    management.otlp.tracing.endpoint=http://localhost:4318/v1/traces

    Best practices

    • Propagate trace context across HTTP and Kafka headers.
    • Sample in production (1-10%) — 100% sampling is expensive.
    • Correlate traceId with log MDC for unified debugging.

    Purpose of this lesson

    Master OpenTelemetry so you can apply it confidently in production Java code, technical interviews, and code reviews.

    Step-by-step explanation

    1. Understand the core idea behind OpenTelemetry.
    2. Walk through the runnable example and tweak it in the playground.
    3. Apply the pattern in a small Spring Boot or CLI exercise of your own.
    4. Re-read the common mistakes and interview Q&A to lock the concept in.

    Interactive workflow diagram

    1OpenTelemetry — typical flow
    1 / 4

    Identify use case

    Recognize when opentelemetry is the right tool for the problem.

    Debugging tips

    • Read the full stack trace — Java's exception messages name the offending class and line.
    • Reproduce in the smallest possible main() method before fixing in the real app.
    • Use IntelliJ's debugger breakpoints and 'Evaluate Expression' rather than scattering System.out.

    Optimization strategies

    • Profile before optimizing — JFR (Java Flight Recorder) and async-profiler reveal real hotspots.
    • Prefer immutable data and stream pipelines over hand-rolled loops when readability matters.
    • Reach for the right JDK collection (ArrayList vs LinkedList vs ArrayDeque) before writing custom data structures.

    Enterprise example

    Grafana's LGTM stack (Loki, Grafana, Tempo, Mimir) ingests OpenTelemetry natively — one SDK, full observability.

    Interview questions & answers

    Q1Explain OpenTelemetry in one minute.
    Describe what problem it solves, the JDK APIs involved, and one production trade-off.
    Q2When would you avoid OpenTelemetry?
    Mention performance, complexity, or readability cases where a simpler approach wins.

    Summary

    In this lesson you learned OpenTelemetry — the concept, syntax, a runnable example, and the production pitfalls to avoid. Apply it in the playground before moving on.

    Ready to mark this lesson complete?Track your journey across the entire course.