Skip to main content

OpenTelemetry

Bring OpenTelemetry spans into Risicare, and send the spans of Risicare to an OTLP backend.

Risicare works with OpenTelemetry in two directions:

  • In: the OTel bridge takes the spans of your OpenTelemetry SDK and sends them to Risicare. See OTel Bridge and Existing OTel SDK. The ingest host also accepts OTLP over HTTP from any OpenTelemetry exporter. See OTLP Ingestion.
  • Out: the OTLP exporter sends the spans that the Risicare SDK makes to an OTLP/HTTP endpoint that you name, for example your own collector. See OTLP Exporter.

Python only

The OpenTelemetry bridge is currently available in the Python SDK only.

OTel Bridge

The bridge brings OpenTelemetry spans into Risicare. It does not send Risicare spans to a collector. For that, see OTLP Exporter.

pip install risicare[otel]
import risicare
 
risicare.init(  # reads RISICARE_API_KEY from the environment
    otel_bridge=True,  # Bring OpenTelemetry spans into Risicare
)

With otel_bridge=True, the SDK adds a span processor to the global OpenTelemetry TracerProvider when your program imports opentelemetry. The spans of your OpenTelemetry SDK then also go to Risicare. Set the TracerProvider before you call init(). The bridge is off by default. To add the processor yourself, see Existing OTel SDK.

The bridge works with opentelemetry-sdk 1.20.0 and later. It is tested on opentelemetry-sdk 1.20.0 and on 1.45.2. The processor and the exporter of the bridge subclass the SpanProcessor and the SpanExporter of the OTel SDK, and no hook of the bridge raises into your code. A fault in a hook is written at DEBUG level only.

Fixed in 0.15.0: span.end() raised AttributeError

With risicare through 0.14.0 and opentelemetry-sdk 1.40.0 or later, span.end() raised AttributeError when the bridge was on (otel_bridge=True, or RisicareSpanProcessor added by hand). The OTel SDK calls the hook _on_ending from Span.end(), and RisicareSpanProcessor did not have it. Risicare 0.15.0 fixes this. To fix it, upgrade: pip install --upgrade risicare.

OTLP Exporter

To send the spans that the Risicare SDK makes to an OTLP/HTTP backend, give init() an otlp_endpoint. It does not need otel_bridge:

risicare.init(  # reads RISICARE_API_KEY from the environment
    otlp_endpoint="http://localhost:4318/v1/traces",
    otlp_headers={"Authorization": "Bearer token"},
)

The spans go to that endpoint. When init() has an api_key and you do not pass exporters=[...], they also go to Risicare. If the endpoint does not end with /v1/traces, the SDK adds it.

OTLP Ingestion

Risicare can receive spans from existing OTel instrumentation. Send OTLP over HTTP to the ingest host, with the project key:

POST https://ingest.risicare.ai/v1/traces
Content-Type: application/x-protobuf
Authorization: Bearer rsk-...

<OTLP/HTTP trace data>

/v1/traces is the standard OTLP/HTTP path. It accepts binary protobuf (application/x-protobuf or application/protobuf), which most OpenTelemetry exporters send by default, and JSON (application/json).

The older path /v1/otlp/v1/traces accepts JSON only. A protobuf request to it is refused.

This allows migrating existing OTel instrumentation to Risicare without code changes.

OTLPExporter Reference

For full control over OTLP export, create an OTLPExporter directly and pass it to init():

from risicare import OTLPExporter
 
exporter = OTLPExporter(
    endpoint="http://localhost:4318/v1/traces",
    headers={"Authorization": "Bearer token"},
    timeout_seconds=10.0,
    max_retries=3,
    compress=True,
    service_name="my-agent",
    service_version="1.0.0",
    environment="production",
)
 
risicare.init(  # reads RISICARE_API_KEY from the environment
    exporters=[exporter],
)
ParameterTypeDefaultDescription
endpointstr"http://localhost:4318/v1/traces"OTLP/HTTP traces endpoint URL
headersdictNoneAdditional HTTP headers (e.g., auth tokens)
timeout_secondsfloat10.0HTTP request timeout
max_retriesint3Maximum retry attempts with exponential backoff
compressboolTruegzip-compress payloads
service_namestrNoneService name for OTLP resource attributes
service_versionstrNoneService version for OTLP resource attributes
environmentstrNoneDeployment environment for OTLP resource attributes

The exporter maps Risicare span attributes to gen_ai.* semantic conventions automatically. Its circuit breaker opens after 5 failed export calls and stays open for 60 seconds, as the breaker of the default HTTP exporter does — see Span Delivery for the thresholds and what gets dropped. One rule differs. The OTLPExporter counts a call that ends with 429 or 503 as a failure. The default HTTP exporter does not count such a call, because the gateway is healthy and sheds load. The OTLPExporter does not use the retry horizon of the default HTTP exporter. It makes up to max_retries attempts for each call.

Multiple Exporters

Export to multiple backends:

from risicare import OTLPExporter
from risicare.exporters.http import HttpExporter
from risicare.exporters.console import ConsoleExporter
 
risicare.init(  # reads RISICARE_API_KEY from the environment
    exporters=[
        HttpExporter(),           # Risicare cloud
        OTLPExporter(             # Jaeger
            endpoint="http://jaeger:4318/v1/traces"
        ),
        ConsoleExporter(),        # Debug output
    ],
)

Semantic Conventions

Risicare follows OpenTelemetry semantic conventions for GenAI. This table lists the names that the SDK writes on its own spans:

AttributeDescription
gen_ai.systemLLM provider (openai, anthropic, etc.)
gen_ai.request.modelRequested model name
gen_ai.response.modelActual model used
gen_ai.usage.prompt_tokensInput token count
gen_ai.usage.completion_tokensOutput token count
gen_ai.request.temperatureTemperature setting
gen_ai.request.max_tokensMax tokens setting

Context Propagation

W3C Trace Context is fully supported:

from risicare import inject_trace_context, extract_trace_context
 
# Inject into outgoing request
headers = {}
inject_trace_context(headers)
# headers now contains 'traceparent' and 'tracestate'
 
# Extract from incoming request
context = extract_trace_context(request.headers)

Existing OTel SDK

If you already use the OpenTelemetry Python SDK:

import risicare
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from risicare.integrations.otel import RisicareSpanProcessor
 
# Initialize Risicare first (required for the processor to work)
risicare.init(  # reads RISICARE_API_KEY from the environment
)
 
# Add Risicare as a span processor (no constructor params needed)
provider = TracerProvider()
provider.add_span_processor(RisicareSpanProcessor())
trace.set_tracer_provider(provider)
 
# Existing OTel instrumentation now exports to Risicare

The processor and RisicareSpanExporter read the current GenAI attribute names first and the older names second. A span from a new emitter (for example the Anthropic SDK (beta), Strands, Microsoft Agent Framework or @ai-sdk/otel) reaches Risicare with its provider, token counts and stop reason. The pairs are:

Current name (read first)Older name (read second)
gen_ai.provider.namegen_ai.system
gen_ai.usage.input_tokensgen_ai.usage.prompt_tokens
gen_ai.usage.output_tokensgen_ai.usage.completion_tokens
gen_ai.response.finish_reasons (a list; the first item is the stop reason)gen_ai.response.finish_reason

A span that has both names of a pair keeps the current one. A current name with no usable value (an empty text, a count that is not a number, an empty list) does not hide the older name. A span with the older names only gives the same result as before. This holds since Python 0.11.0 (see the changelog). The bridge does not read gen_ai.operation.name, gen_ai.input.messages, gen_ai.output.messages, gen_ai.tool.* or gen_ai.response.id. They stay on the span.

Next Steps