OpenTelemetry for Lua scripts (nauthilus_opentelemetry)
This module allows Lua scripts to create and control OpenTelemetry spans, set attributes/events/status, use baggage, and inject/extract trace headers. The module is request‑scoped and becomes a no‑op when tracing is disabled in configuration.
Controlled by
server.insights.tracing.enabled. When disabled, the module functions are present but do nothing.
Loading
local otel = require("nauthilus_opentelemetry")
Feature detection
if not otel.is_enabled() then
-- tracing disabled; skip expensive telemetry work
end
Creating spans
local tr = otel.tracer("nauthilus/policy")
-- Start/End manually
local sp = tr:start_span("operation", { kind = "internal", attributes = { ["peer.service"] = "example" } })
sp:add_event("started")
sp:finish()
-- With convenience: with_span
tr:with_span("policy.evaluate", function(span)
span:set_attributes({ ["key"] = "value", ["tries"] = 1, ["ok"] = true })
end, { kind = "client" })
Tracer methods
start_span(name, options?)— returns a span object. Manual lifecycle: must callfinish().with_span(name, function, options?)— runs the given function within a new span. The span is automatically finished when the function returns. The span object is passed as the first argument to the function. Available since v1.11.5.
Span methods
set_attribute(key, value)— value can be string/number/booleanset_attributes(table)— bulk set attributes from a{ key = value }tableadd_event(name, attributes?)set_status(code, description?)—code:ok|error|unsetrecord_error(err_or_msg)— marks span as error and records the errorfinish()— alias forend()end()— Warning: In Lua,endis a reserved keyword. Calling:end()directly will cause a syntax error. Use the aliasfinish()instead, or call it asspan["end"](span).
Options for start_span/with_span
kind:internal(default) |client|server|producer|consumerattributes: table of attributes{ [string] = string|number|boolean }links: array of{ trace_id = "...", span_id = "...", attributes = { ... } }