Class OtlpTracer

java.lang.Object
com.codename1.backend.otel.OtlpTracer
All Implemented Interfaces:
Tracer

public final class OtlpTracer extends Object implements Tracer

OpenTelemetry tracing over OTLP/HTTP, with no OpenTelemetry library behind it.

The OpenTelemetry Java SDK cannot run here: the server is translated to C against a Java subset with no reflection and no service loading, and a binary that linked the SDK would be carrying it whether or not anyone traced. This is the part of it a server actually needs -- W3C trace context, the standard samplers, a bounded batch exporter, and the OTLP wire format in both its encodings -- and it is only in the binary when the project enables it.

Configured the way every OpenTelemetry SDK is, so operations tooling works unchanged. Each setting has a cn1.otel.* key, readable from application.properties like any other, and the standard OTEL_* environment variable is honoured for it too:

key variable default
cn1.otel.endpoint OTEL_EXPORTER_OTLP_ENDPOINT http://localhost:4318; /v1/traces is appended
cn1.otel.traces.endpoint OTEL_EXPORTER_OTLP_TRACES_ENDPOINT used as it is, and wins over the one above
cn1.otel.headers OTEL_EXPORTER_OTLP_HEADERS none; name=value,name=value
cn1.otel.protocol OTEL_EXPORTER_OTLP_PROTOCOL http/protobuf, or http/json
cn1.otel.service.name OTEL_SERVICE_NAME what the build named it, else unknown_service
cn1.otel.resource.attributes OTEL_RESOURCE_ATTRIBUTES none
cn1.otel.sampler OTEL_TRACES_SAMPLER parentbased_always_on
cn1.otel.sampler.arg OTEL_TRACES_SAMPLER_ARG the ratio, 1
cn1.otel.disabled OTEL_SDK_DISABLED false

cn1.otel.attributes.exclude names attributes never to record, as a comma separated list (db.query.text,user_agent.original), and cn1.otel.relay=true opens the endpoint the app's own spans are relayed through; see OtlpRelay.

  • Field Details

  • Constructor Details

    • OtlpTracer

      public OtlpTracer()
      A tracer whose service name comes from configuration alone.
    • OtlpTracer

      public OtlpTracer(String defaultServiceName)
      Parameters:
      defaultServiceName - used when neither cn1.otel.service.name nor OTEL_SERVICE_NAME is set; the build passes the name @OpenTelemetry gave
  • Method Details

    • open

      public static OtlpTracer open(Config config, String defaultServiceName) throws IOException

      A tracer opened against config, or null when the configuration turns tracing off. For a program that starts HttpServer or the Lambda loop itself:

      Tracing.install(OtlpTracer.open(Config.load(), "orders"));
      
      Throws:
      IOException
    • open

      public boolean open(Config config) throws IOException
      Description copied from interface: Tracer
      Reads the settings and starts whatever exports spans. Called once, by Backend.Builder.start() or by the application before Tracing.install(Tracer).
      Specified by:
      open in interface Tracer
      Returns:
      false when the configuration turned tracing off, in which case the tracer is not installed and nothing was started
      Throws:
      IOException
    • startSpan

      public Span startSpan(String name, int kind, Span parent, String traceparent, String tracestate)
      Description copied from interface: Tracer
      A new span. Never null: a span the sampler declines is a non-recording one that still carries its trace context.
      Specified by:
      startSpan in interface Tracer
      Parameters:
      parent - the local parent, or null
      traceparent - a remote parent's traceparent header, used when parent is null; null or malformed starts a new trace
      tracestate - the remote parent's tracestate, or null
    • flush

      public void flush(int timeoutMillis)
      Description copied from interface: Tracer
      Blocks until what has ended so far is exported, or the time runs out. A Lambda host freezes the process between invocations, so a background exporter there only runs when something waits for it.
      Specified by:
      flush in interface Tracer
    • shutdown

      public void shutdown(int timeoutMillis)
      Description copied from interface: Tracer
      Flushes, then stops the exporter.
      Specified by:
      shutdown in interface Tracer
    • relay

      public HttpServer.Handler relay()
      Description copied from interface: Tracer
      The handler that relays client spans to the collector, or null when this deployment does not offer one. The builder puts it ahead of the routers.
      Specified by:
      relay in interface Tracer
    • metrics

      public void metrics(Map out)
      Description copied from interface: Tracer
      Adds this tracer's counters to a metrics snapshot.
      Specified by:
      metrics in interface Tracer