Installation
Core SDK (langwatch)
Setup
Create a LangWatch exporter and configure it as your tracer provider:LANGWATCH_API_KEY from your environment automatically.
For custom configuration, use NewExporter with options:
Tracer
Tracer() retrieves a LangWatchTracer instance, which is a thin wrapper around an OpenTelemetry Tracer.
Example:
LangWatchTracer
TheLangWatchTracer interface provides a Start method that mirrors OpenTelemetry’s but returns a LangWatchSpan.
LangWatchSpan
TheLangWatchSpan interface embeds the standard trace.Span and adds several helper methods for LangWatch-specific data.
function
Sets the span type for categorization in LangWatch. This enables specialized UI treatment and analytics.Example:
function
Assigns a thread ID to group this trace with a conversation. Useful for multi-turn conversations.Example:
All spans within the same trace will share the same thread ID, allowing you to group related interactions together.
function
Assigns a user ID to the trace for user-centric analytics and filtering.Example:
function
Records a simple string as the span’s input. Ideal for user queries or simple text inputs.Example:
function
Records a structured object (e.g., struct, map) as the span’s input, serialized to JSON. Use for complex request objects.Example:
function
Records a simple string as the span’s output. Ideal for AI responses or simple text outputs.Example:
function
Records a structured object as the span’s output, serialized to JSON. Use for complex response objects.Example:
function
Sets the model identifier used for a request (e.g., an LLM call). This is the model you requested to use.Example:
function
Sets the model identifier reported in a response. This is the actual model that processed your request.Example:
The response model may differ from the request model, especially with OpenAI’s model updates.
function
Attaches a slice of retrieved context chunks for RAG analysis. This enables LangWatch to analyze the relevance and quality of retrieved documents.Example:
OpenAI Instrumentation
Thegithub.com/langwatch/langwatch/sdk-go/instrumentation/openai package provides middleware for the official openai-go client.
Middleware
Middleware() creates an openai.Middleware that automatically traces OpenAI API calls.
instrumentationName- Name of your application or serviceopts- Optional configuration options
...Option):
function
Records the full input payload as a span attribute. This captures the complete request sent to the LLM.Example:
function
Records the full response payload as a span attribute. For streams, this is the final accumulated response.Example:
function
Sets the
gen_ai.system attribute. Useful for identifying providers like "anthropic" or "azure". Defaults to "openai".Example:function
Specifies the
trace.TracerProvider to use. Defaults to the global provider.Example:Filtering
Control which spans are exported to reduce noise and focus on what matters.Preset Filters
Custom Filters
UseInclude() to keep matching spans or Exclude() to remove them:
Matchers
Multiple filters use AND semantics (span must pass all). Within a
Criteria, matchers use OR semantics (span matches if any matcher matches).LangWatch Span Types
SpanType is a string constant used with span.SetType() to categorize spans in LangWatch for specialized UI treatment and analytics.
Using these span types is optional but highly recommended, as it enables LangWatch to provide more tailored insights and visualizations for your traces.
Collected Attributes
The OpenAI instrumentation automatically adds these attributes to spans:Request Attributes
gen_ai.system- AI system name (e.g., “openai”)gen_ai.request.model- Model used for the requestgen_ai.request.temperature- Temperature parametergen_ai.request.top_p- Top-p parametergen_ai.request.top_k- Top-k parametergen_ai.request.frequency_penalty- Frequency penaltygen_ai.request.presence_penalty- Presence penaltygen_ai.request.max_tokens- Maximum tokenslangwatch.gen_ai.streaming- Boolean indicating streaminggen_ai.operation.name- Operation name (e.g., “completions”)langwatch.input.value- Input content (if WithCaptureInput enabled)
Response Attributes
gen_ai.response.id- Response ID from the APIgen_ai.response.model- Model that generated the responsegen_ai.response.finish_reasons- Completion finish reasonsgen_ai.usage.input_tokens- Number of input tokens usedgen_ai.usage.output_tokens- Number of output tokens generatedgen_ai.openai.response.system_fingerprint- OpenAI system fingerprintlangwatch.output.value- Output content (if WithCaptureOutput enabled)
HTTP Attributes
Standard HTTP client attributes are also included:http.request.method- HTTP methodurl.path- Request pathserver.address- Server addresshttp.response.status_code- HTTP status code
Request/Response Examples
Basic Chat Completion
RAG Pipeline
Error Handling
All SDK methods handle errors gracefully. In case of failures:- Serialization errors - Fallback to string representation
- Network errors - Logged but don’t interrupt application flow
- Invalid data - Sanitized or excluded from traces
Environment Variables
Complete Example
Here’s a comprehensive example showing a complete RAG application with proper error handling and best practices:Version Compatibility
- Go Version: 1.19 or later
- OpenTelemetry: v1.24.0 or later
- OpenAI Go SDK: Latest version