Tracing API Referenece

    The tracing API follows the . This specification defines how to use the API as an instrument to your module. If you are familiar with the OpenTelemetry API, the tracing API will be familiar.

    With the tracing API, you can set the instrumentation of your module with the following operations:

    Create a tracer

    Kong uses a global tracer internally to instrument the core modules and plugins.

    By default, the tracer is a . The tracer is first initialized when opentelemetry_tracing configuration is enabled.

    You can create a new tracer manually, or use the global tracer instance:

    The sampling rate of a tracer can be configured:

    1. local tracer = kong.tracing.new("custom-tracer", {
    2. -- Set the sampling rate to 0.1
    3. sampling_rate = 0.1,
    4. })

    The default sampling rate is 1.0, which samples all traces.

    Create a span

    A span represents a single operation within a trace. Spans can be nested to form trace trees. Each trace contains a root span, which typically describes the entire operation and, optionally, one or more sub-spans for its sub-operations.

    1. local tracer = kong.tracing
    2. local span = tracer:start_span("my-span")
    1. local span = tracer:start_span("my-span", {
    2. start_time_ns = ngx.now() * 1e9, -- override the start time
    3. -- UNSPECIFIED: 0
    4. -- INTERNAL: 1
    5. -- SERVER: 2
    6. -- CLIENT: 3
    7. -- PRODUCER: 4
    8. should_sample = true, -- by setting it to `true` to ignore the sampling decision
    9. })

    Make sure to ends the span when you are done:

    The active span is the span that is currently being executed.

    To avoid overheads, the active span is manually set by calling the set_active_span method. When you finish a span, the active span becomes the parent of the finished span.

    To set or get the active span, you can use the following example code:

    1. local tracer = kong.tracing
    2. local span = tracer:start_span("my-span")
    3. tracer.set_active_span(span)
    4. local active_span = tracer.active_span() -- returns the active span

    The tracers are scoped to a specific context by a namespace key.

    To get the active span for a specific namespace, you can use the following:

    1. -- get global tracer's active span, and set it as the parent of new created span
    2. local global_tracer = kong.tracing
    3. local tracer = kong.tracing.new("custom-tracer")
    4. local root_span = global_tracer.active_span()
    5. local span = tracer.start_span("my-span", {
    6. parent = root_span
    7. })

    Set the span attributes

    The attributes of a span are a map of key-value pairs and can be set by passing a table to the set_attributes method.

    1. local span = tracer:start_span("my-span")

    The following semantic conventions for spans are defined:

    • : General semantic attributes that may be used in describing different kinds of operations.
    • HTTP: For HTTP client and server spans.
    • : For remote procedure call (e.g., gRPC) spans.
    • Messaging: For messaging systems (queues, publish/subscribe, etc.) spans.
    • : For Function as a Service (e.g., AWS Lambda) spans.
    • : For recording exceptions associated with a span.
    • Compatibility: For spans generated by compatibility components, e.g. OpenTracing Shim layer.

    Set the span events

    The events of a span are time-series events that can be set by passing a table to the method.

    1. local span = kong.tracing:start_span("my-span")
    2. span:add_event("my-event", {
    3. -- attributes
    4. ["key"] = "value",
    5. })

    The event could also be used to record error messages.

    1. local span = kong.tracing:start_span("my-span")
    2. span:record_error("my-error-message")
    3. -- or (same as above)
    4. span:add_event("exception", {
    5. ["exception.message"] = "my-error-message",
    6. })

    The status of a span is a status code and can be set by passing a table to the set_status method.

    1. local span = kong.tracing:start_span("my-span")
    2. -- Status codes:
    3. -- - `0` unset
    4. -- - `1` ok
    5. -- - `2` error

    Release the span (optional)

    The spans are stored in a pool, and can be released by calling the method.

    By default, the span will be released after the Nginx request ends.

    Visualize the trace

    Because of the compatibility with OpenTelemetry, the traces can be natively visualized through any OpenTelemetry UI.

    Please refer to the OpenTelemetry plugin to see how to visualize the traces.