OpenTelemetry (Python)
Use these settings to capture spans from LLM libraries, to share a process with other OpenTelemetry code and to keep content out of your traces.
Instrumentation
Section titled “Instrumentation”init(instrument=[...]) enables OpenInference
instrumentors by name, from the fixed registry in the install table:
cetra.init(api_key="sk-cetra-test", instrument=["openai", "anthropic"])An unknown name raises ConfigError listing the known names; a name whose
package is missing raises ConfigError naming the extra to install (for
example instrument: 'openai' needs \pip install cetra-sdk[openai]`). Nothing is installed when a name fails: the error surfaces from init()` itself.
The instrumentors are created with our tracer provider, so their spans nest
under ours and carry the context() values. A library you already
instrumented yourself is left alone (one debug line). shutdown() uninstruments
everything the SDK enabled. Our provider raises the OTel attribute limit to
10 000, so flattened message attributes are not clipped.
With instrument=["openai"], cetra records the host of the OpenAI client’s base URL as server.address, so calls to OpenRouter, Novita or another provider behind the OpenAI client show that provider.
The host is the only part of the URL that is recorded.
Only the create methods of chat completions, responses and embeddings record the host; spans of parse or stream calls do not.
Existing OpenTelemetry setups
Section titled “Existing OpenTelemetry setups”By default init() builds its own TracerProvider and never registers it
globally: your OTel setup is untouched, and instrumentor spans reach cetra
because the SDK binds them to our provider. The OTel context is always the
global one, so spans nest correctly in both modes.
With use_global_provider=True:
- an app’s SDK
TracerProvideralready set as global takes our span processor (the app keeps its sampler: asample_ratebelow 1 logs one warning and is ignored); - when nothing is registered yet, our provider becomes the global one;
- any other global provider cannot take processors: the SDK logs one warning and keeps its own.
In global mode other libraries create spans on the same provider, so the
default export filter keeps only the LLM-related ones: spans whose
instrumentation scope is cetra or ai, or with an attribute key starting
with cetra., gen_ai., llm., openinference., ai., traceloop.,
langfuse., lmnr. or gcp.vertex.agent., or with an input.value /
output.value key. Pass your own callable as export_filter= to replace that
rule in either mode: it receives the finished span and returns a boolean; if
it raises, the span is exported.
cetra.span_processor() returns our span processor for apps that build their
own OTel pipeline, and cetra.tracer_provider() returns the provider in use
(a no-op provider before init(), when disabled or after shutdown()), for
libraries that accept a provider or tracer.
Masking and capture_content
Section titled “Masking and capture_content”mask is a callable applied at capture time to every value the SDK records —
inputs, outputs, metadata values, LLM messages — before serialization:
def redact(value): ... # return the value with secrets removed
cetra.init(api_key="sk-cetra-test", mask=redact)At export time a second pass rewrites spans from other instrumentation
scopes (the instrumentors’): for each attribute under a payload key —
cetra.input, cetra.output, gen_ai.input.messages,
gen_ai.output.messages, gen_ai.system_instructions, input.value,
output.value, the llm.input_messages.* / llm.output_messages.* /
gen_ai.prompt.* / gen_ai.completion. prefixes and the other keys of spec
10 — the mask receives the decoded value (JSON-parsed when the string is a
JSON object or array) and the result is serialized back. Only string values
are masked; a non-string value under a payload key is deleted. If mask
raises, the value becomes [cetra: mask failed] — the SDK fails closed, and
a batch that cannot be rewritten is dropped, never exported unmasked.
capture_content=False records no payloads at all on our spans (no
input/output, no messages, no system instructions; metrics-like attributes —
models, params, token counts, cost — are kept), deletes the payload keys from
instrumentor spans, and configures the OpenInference instrumentors we create
with their hide-inputs/hide-outputs options.
Serverless
Section titled “Serverless”atexit handlers do not run when a serverless platform freezes or terminates
an invocation. Call cetra.flush() at the end of every invocation (or
cetra.shutdown() when the instance is not reused); otherwise spans and
scores still sitting in the batch queue are lost.
Known limits
Section titled “Known limits”- Abandoned async generators are never exported. An async generator that
is neither exhausted nor
aclose()d leaves its span unended — an async generator cannot be closed from a finalizer.aclose()it when you stop early. inspect.isasyncgenfunction(andisgeneratorfunction) is false on a wrapped function.observedetects generator functions at decoration time; decorating an already-wrapped async generator function traces it as a plain call whose output is not recorded.- In global mode, the app’s own spans get
cetra.*attributes. Our processor stampscetra.session.id,cetra.user.id,cetra.tags,cetra.metadata.*,cetra.environmentandcetra.releaseon every span of the provider it is attached to, so the app’s other exporters see them. OTEL_EXPORTER_OTLP_HEADERSis sent to cetra too. The OTel HTTP exporter mergesOTEL_EXPORTER_OTLP_HEADERSandOTEL_EXPORTER_OTLP_TRACES_HEADERSfrom the environment into its request headers; headers meant for another OTLP backend reach the cetra server as well. Unset them if that matters.KeyboardInterruptandasyncio.CancelledErrorare not errors. OnlyExceptiongets the error status. ABaseExceptionthat is not anException— inside aspan()/llm_span()block or raised through@observe(functions, coroutine functions and both generator wrappers) alike — ends the span with the status unset, records no output, and is re-raised unchanged.- Zero-argument functions and
None: a call with no arguments records{}as input; aNonereturn recordsnullas output. TypeScript records no input for no arguments and no output forundefined(returnnullthere to record a JSONnull). - Integral floats: Python writes
1.0as a double; TypeScript has one number type and writes an integral value as an int. The server reads both. - Large integers: Python ints beyond ±(2^53−1) inside JSON payloads are
written as JSON numbers; TypeScript
bigintbecomes a decimal string. As metadata values both are JSON strings. - Resource attributes: Python reads
OTEL_RESOURCE_ATTRIBUTES; TypeScript reads onlyOTEL_SERVICE_NAME. - Abandoned sync generators that are garbage-collected half-consumed end with status ok (TypeScript has no finalizer and leaves them unexported).
- Usage counts:
set_usagerecords any non-negativeint, including values beyond 2^53; TypeScriptsetUsagerecords only safe integers. - Threads and serverless: see Threads and Serverless.