Skip to Content
Confident AI is free to try . No credit card required.

OpenTelemetry

OpenTelemetry is an open-source observability framework that allows teams to collect, analyze, and visualize telemetry data.

Confident AI provides a ConfidentSpanExporter that allows teams to export traces to the Observatory.

Quickstart

Login using your API key on Confident AI in the CLI:

deepeval login --confident-api-key YOUR_API_KEY

Add ConfidentSpanExporter to your OpenTelemetry tracer:

example.py
import time import json from opentelemetry import trace from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from deepeval.tracing.otel.exporter import ConfidentSpanExporter # Set up tracer provider tracer_provider = trace.get_tracer_provider() if not isinstance(tracer_provider, TracerProvider): trace.set_tracer_provider(TracerProvider()) # Add confident span exporter wrapped in batch span processor tracer_provider.add_span_processor(BatchSpanProcessor(ConfidentSpanExporter())) # Get tracer tracer = trace.get_tracer("deepeval_tracer") def llm(input: str) -> str: with tracer.start_as_current_span("llm_span") as span: span.set_attribute("confident.span.type", "llm") span.set_attribute("confident.llm.model", "gpt-3.5-turbo") span.set_attribute("confident.llm.attributes.input", [ json.dumps({"role": "system", "content": "You are a helpful assistant."}), json.dumps({"role": "user", "content": input}) ]) time.sleep(0.5) span.set_attribute("confident.llm.attributes.output", "Hello world") return "Hello world" def my_app(input: str): with tracer.start_as_current_span("my_app") as span: span.set_attribute("confident.span.input", input) res = llm(input) span.set_attribute("confident.span.output", res) my_app("Hi")

Run the script:

python example.py

This will create a trace in the Observatory, with the span name my_app.

Attributes mapping

Confident AI uses confident.* namespace to map OpenTelemetry span attributes with llm tracing data model.

Trace attributes

Trace attributes are set in the span attributes.

  • confident.trace.name (of type str) used for updating trace name.
  • confident.trace.tags (of type list[str]) used for updating trace tags.
  • confident.trace.thread_id (of type str) used for updating trace thread id.
  • confident.trace.user_id (of type str) used for updating trace user id.
with tracer.start_as_current_span("custom_span") as span: # your code span.set_attribute("confident.trace.name", "test_trace") span.set_attribute("confident.trace.tags", ["tag1", "tag2"]) span.set_attribute("confident.trace.thread_id", "123") span.set_attribute("confident.trace.user_id", "456")

Trace input and output

You can set trace input and output at runtime using the following attributes:

  • confident.trace.input (of type Any) used for updating trace input.
  • confident.trace.output (of type Any) used for updating trace output.
with tracer.start_as_current_span("custom_span") as span: # your code span.set_attribute("confident.trace.input", input) span.set_attribute("confident.trace.output", output)
💡

It is recommended to set trace attributes once in any span in a trace lifecycle. The value of the specific attribute will be updated to the latest value if set in multiple spans.

Spans

Span attributes start with confident.{span_type}.* namespace. Span type can be one of the following:

  • span: Default (custom) span type.
  • llm: LLM span.
  • agent: Agent span.
  • retriever: Retriever span.
  • tool: Tool span.

Given below is the sample codes for setting attributes for different span types.

def meta_agent(input: str): with tracer.start_as_current_span("meta_agent") as span: span.set_attribute("confident.span.input", input) span.set_attribute("confident.span.output", input) span.set_attribute("confident.span.error", "Error") span.set_attribute("confident.span.feedback", json.dumps({ "rating": 1, "expected_output": "Expected output", "explanation": "Explanation" })) span.set_attribute("confident.span.metric_collection", "My metrics") span.set_attribute("confident.trace.attributes", json.dumps({ "name": "test_trace", "tags": ["tag1", "tag2"], "thread_id": "123", "user_id": "456" }))

LLM test case

LLM Test Case attributes can be used to unit test interactions within your LLM application. It can be set in any span type.

Given below is the example of running online evaluation for a span.

with tracer.start_as_current_span("confident_evaluation") as span: input = "What is the capital of France?" output = my_llm_app(input) # your LLM application span.set_attribute('confident.span.metric_collection', "<your_metric_collection>") span.set_attribute('confident.span.llm_test_case.input', input) span.set_attribute('confident.span.llm_test_case.actual_output', output)
💡

Make sure you have metric collection created on the platform for running online evaluation on this test case.

LLM test case attributes mapping:

AttributeTypeRequiredDescription
confident.span.llm_test_case.inputstrYesTest case input
confident.span.llm_test_case.actual_outputstrYesTest case actual output
confident.span.llm_test_case.expected_outputstrNoTest case expected output
confident.span.llm_test_case.contextlist[str]NoTest case context
confident.span.llm_test_case.retrieval_contextlist[str]NoTest case retrieval context
confident.span.llm_test_case.tools_calledstrNoShould be stringified json of type Tools Called
confident.span.llm_test_case.expected_toolsstrNoShould be stringified json of type Expected Tools

Default span

AttributeTypeRequiredDescription
confident.span.inputAnyNoSpan input
confident.span.outputAnyNoSpan output
confident.span.errorstrNoSpan error
confident.span.metric_collectionstrNoSpan metric collection

LLM span

To create a LLM span, set the confident.span.type to llm.

LLM span attributes: reference

AttributeTypeRequiredDescription
confident.llm.modelstrNoLLM model
confident.llm.cost_per_input_tokenfloatNoCost per input token
confident.llm.cost_per_output_tokenfloatNoCost per output token

LLM attributes: reference

AttributeTypeRequiredDescription
confident.llm.attributes.inputstr, list[str]YesLLM Span input (parsed to list[dict] if list[str] is provided)
confident.llm.attributes.outputAnyYesLLM Span output
confident.llm.attributes.input_token_countintNoLLM Span input token count
confident.llm.attributes.input_token_countintNoLLM Span input token count

Agent span

To create an agent span, set the confident.span.type to agent.

Agent span attributes: reference

AttributeTypeRequiredDescription
confident.agent.namestrYesAgent span name
confident.agent.available_toolslist[str]NoAgent span available tools
confident.agent.agent_handoffslist[str]NoAgent span agent handoffs

Agent attributes: reference

AttributeTypeRequiredDescription
confident.agent.attributes.inputAnyYesAgent span input
confident.agent.attributes.outputAnyYesAgent span output

Tool span

To create a tool span, set the confident.span.type to tool.

Tool span attributes: reference

AttributeTypeRequiredDescription
confident.tool.namestrYesTool span name
confident.tool.descriptionstrNoTool span description

Tool attributes: reference

AttributeTypeRequiredDescription
confident.tool.attributes.input_parametersstrNoTool span input parameters (parsed to dict if str is provided)
confident.tool.attributes.outputAnyNoTool span output

Retriever span

To create a retrieval span, set the confident.span.type to retriever.

Retriever span attributes: reference

AttributeTypeRequiredDescription
confident.retriever.embedderstrYesRetrieval span embedder model

Retriever attributes: reference

AttributeTypeRequiredDescription
confident.retriever.attributes.embedding_inputstrYesRetrieval span embedding input
confident.retriever.attributes.retrieval_contextlist[str]YesRetrieval span retrieval context
confident.retriever.attributes.top_kintNoRetrieval span top k
confident.retriever.attributes.chunk_sizeintNoRetrieval span chunk size
Last updated on