Tracing API Referenece

Before you start

In Gateway version 3.0.0, the tracing API became part of the Kong core application. The API is in the kong.tracing namespace.

The tracing API follows the OpenTelemetry API specification. 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 NoopTracer. The tracer is first initialized when opentelemetry_tracing configuration is enabled.

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

  1. local tracer
  2. -- Create a new tracer
  3. tracer = kong.tracing.new("custom-tracer")
  4. -- Use the global tracer
  5. tracer = kong.tracing

Sampling traces

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")

The span properties can be set by passing a table to the start_span method.

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

Make sure to ends the span when you are done:

  1. span:finish() -- ends the span

Note: The span table will be cleared and put into the table pool after the span is finished, do not use it after the span is finished.

Get or set the active span

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

Scope

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 OpenTelemetry specification defines the general semantic attributes, it can be used to describe the span. It could also be meaningful to visualize the span in a UI.

  1. span:set_attribute("key", "value")

The following semantic conventions for spans are defined:

  • General: General semantic attributes that may be used in describing different kinds of operations.
  • HTTP: For HTTP client and server spans.
  • Database: For SQL and NoSQL client call spans.
  • RPC/RMI: For remote procedure call (e.g., gRPC) spans.
  • Messaging: For messaging systems (queues, publish/subscribe, etc.) spans.
  • FaaS: For Function as a Service (e.g., AWS Lambda) spans.
  • Exceptions: 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 add_event method.

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

Record error message

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. })

Set the span status

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 release method.

  1. local span = kong.tracing:start_span("my-span")
  2. span:release()

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.

References