Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

feat: add Go SDK with OpenAI and Anthropic support (#1021)

Aman Agarwalb17ca7d82307
diff --git a/.github/workflows/go-release.yml b/.github/workflows/go-release.yml
new file mode 100644
index 00000000..3c2602ac
--- /dev/null
+++ b/.github/workflows/go-release.yml
@@ -0,0 +1,57 @@
+name: Go SDK Release
+
+on:
+ push:
+ tags:
+ - 'go-*.*.*'
+
+permissions:
+ contents: write
+
+jobs:
+ release:
+ name: Create Release
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v4
+ with:
+ fetch-depth: 0
+
+ - name: Set up Go
+ uses: actions/setup-go@v5
+ with:
+ go-version: '1.23'
+
+ - name: Get version
+ id: get_version
+ run: echo "VERSION=${GITHUB_REF#refs/tags/go-}" >> $GITHUB_OUTPUT
+
+ - name: Run tests
+ working-directory: ./sdk/go
+ run: go test -v ./...
+
+ - name: Build
+ working-directory: ./sdk/go
+ run: go build -v ./...
+
+ - name: Generate changelog
+ id: changelog
+ run: |
+ # Extract changelog for this version
+ if [ -f "sdk/go/CHANGELOG.md" ]; then
+ awk '/^## \[${{ steps.get_version.outputs.VERSION }}\]/{flag=1;next}/^## \[/{flag=0}flag' sdk/go/CHANGELOG.md > /tmp/changelog.md
+ else
+ echo "Release ${{ steps.get_version.outputs.VERSION }}" > /tmp/changelog.md
+ fi
+
+ - name: Create GitHub Release
+ uses: softprops/action-gh-release@v2
+ with:
+ name: Go SDK v${{ steps.get_version.outputs.VERSION }}
+ body_path: /tmp/changelog.md
+ draft: false
+ prerelease: false
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
diff --git a/.github/workflows/go-tests.yml b/.github/workflows/go-tests.yml
new file mode 100644
index 00000000..b2c1ee50
--- /dev/null
+++ b/.github/workflows/go-tests.yml
@@ -0,0 +1,36 @@
+name: Go SDK Tests
+
+on:
+ push:
+ branches: ["main"]
+ paths:
+ - 'sdk/go/**'
+ tags:
+ - 'go-*.*.*'
+ pull_request:
+ branches: ["main"]
+ paths:
+ - 'sdk/go/**'
+
+permissions:
+ contents: read
+
+jobs:
+ test-go-sdk:
+ runs-on: ubuntu-latest
+ defaults:
+ run:
+ working-directory: sdk/go
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v5
+ - name: Set up Go
+ uses: actions/setup-go@v5
+ with:
+ go-version-file: sdk/go/go.mod
+ - name: Install dependencies
+ run: go mod download
+ - name: Build
+ run: go build ./...
+ - name: Run tests
+ run: go test ./...
diff --git a/docs/docs.json b/docs/docs.json
index dedadc5b..f2b603cb 100644
--- a/docs/docs.json
+++ b/docs/docs.json
@@ -178,7 +178,8 @@
]
},
"latest/sdk/configuration",
- "latest/sdk/instrumentation-methods"
+ "latest/sdk/instrumentation-methods",
+ "latest/sdk/go-overview"
]
},
{
@@ -201,8 +202,10 @@
"icon": "robot",
"pages": [
"latest/sdk/integrations/openai",
+ "latest/sdk/integrations/go-openai",
"latest/sdk/integrations/ollama",
"latest/sdk/integrations/anthropic",
+ "latest/sdk/integrations/go-anthropic",
"latest/sdk/integrations/deepseek",
"latest/sdk/integrations/gpt4all",
"latest/sdk/integrations/cohere",
diff --git a/docs/latest/sdk/go-overview.mdx b/docs/latest/sdk/go-overview.mdx
new file mode 100644
index 00000000..bf5b66a9
--- /dev/null
+++ b/docs/latest/sdk/go-overview.mdx
@@ -0,0 +1,143 @@
+---
+title: 'Go SDK Overview'
+sidebarTitle: 'Go SDK Overview'
+icon: 'golang'
+description: 'OpenTelemetry-native observability for Go AI applications. Monitor OpenAI and Anthropic with automatic token tracking, cost calculation, and distributed tracing.'
+---
+
+The OpenLIT Go SDK provides **OpenTelemetry-native observability** for Go applications using LLM APIs. It wraps your provider clients with lightweight instrumentation that automatically collects traces, metrics, and cost data without requiring any changes to your application logic.
+
+## Supported providers
+
+<CardGroup cols={2}>
+ <Card title="OpenAI" icon="robot">
+ Chat completions, streaming, embeddings, and image generation — with full token usage and cost tracking.
+ </Card>
+ <Card title="Anthropic" icon="robot">
+ Claude messages and streaming — with cache token tracking and full OpenTelemetry semantic conventions.
+ </Card>
+</CardGroup>
+
+## What gets collected
+
+Every instrumented call automatically records:
+
+- **Distributed traces** — spans with request/response details, model name, token counts, and cost
+- **OTel metrics** — `gen_ai.client.token.usage`, `gen_ai.client.operation.duration`, `gen_ai.server.time_to_first_token`, `gen_ai.server.time_per_output_token`, `gen_ai.server.request.duration`
+- **Streaming metrics** — time-to-first-chunk and per-chunk latency observations
+- **Cost tracking** — automatic cost calculation using the built-in pricing data
+
+## Installation
+
+<Tabs>
+ <Tab title="Latest Version">
+ ```shell
+ go get github.com/openlit/openlit/sdk/go
+ ```
+ </Tab>
+ <Tab title="Specific Version">
+ ```shell
+ go get github.com/openlit/openlit/sdk/[email protected]
+ ```
+ Replace `v1.2.3` with the version you want to install.
+ </Tab>
+</Tabs>
+
+## Quick start
+
+```go
+package main
+
+import (
+ "context"
+ "fmt"
+ "log"
+
+ openlit "github.com/openlit/openlit/sdk/go"
+ "github.com/openlit/openlit/sdk/go/instrumentation/openai"
+)
+
+func main() {
+ // 1. Initialize OpenLIT (once, at startup)
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://127.0.0.1:4318",
+ ApplicationName: "my-ai-app",
+ Environment: "production",
+ }); err != nil {
+ log.Fatal(err)
+ }
+ defer openlit.Shutdown(context.Background())
+
+ // 2. Create an instrumented client
+ client := openai.NewClient("your-openai-api-key")
+
+ // 3. Use it exactly like a normal client
+ resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []openai.Message{
+ {Role: "user", Content: "Hello, world!"},
+ },
+ MaxTokens: 100,
+ })
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ fmt.Println(resp.Choices[0].Message.Content)
+}
+```
+
+Replace `YOUR_OTEL_ENDPOINT` with the URL of your OpenTelemetry backend, such as `http://127.0.0.1:4318` for a local OpenLIT deployment.
+
+## Configuration
+
+Pass a `Config` struct to `openlit.Init()`:
+
+| Field | Environment Variable | Description | Default |
+|-------|---------------------|-------------|---------|
+| `OtlpEndpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | OTLP backend URL | `http://127.0.0.1:4318` |
+| `OtlpHeaders` | — | Additional HTTP headers for OTLP requests | `{}` |
+| `ApplicationName` | — | Name of your application | `default` |
+| `Environment` | — | Deployment environment label | `default` |
+| `ServiceVersion` | — | Service version string | `""` |
+| `DisableTracing` | — | Disable trace collection | `false` |
+| `DisableMetrics` | — | Disable metrics collection | `false` |
+| `DisableBatch` | — | Disable batch export (useful for testing) | `false` |
+| `DisableCaptureMessageContent` | — | Omit prompt/completion text from spans | `false` |
+| `DetailedTracing` | — | Enable component-level tracing detail | `false` |
+| `DisablePricingFetch` | — | Skip fetching remote pricing data | `false` |
+| `PricingEndpoint` | — | URL for custom pricing JSON | built-in |
+| `PricingInfo` | — | In-process pricing overrides | `{}` |
+| `TraceExporterTimeout` | — | Timeout for trace exports | `10s` |
+| `MetricExporterTimeout` | — | Timeout for metric exports | `10s` |
+| `MetricExportInterval` | — | Interval for metric exports | `30s` |
+
+### Via environment variable
+
+```go
+// If OTEL_EXPORTER_OTLP_ENDPOINT is set, no endpoint config is needed
+openlit.Init(openlit.Config{
+ ApplicationName: "my-ai-app",
+})
+```
+
+```shell
+export OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318
+```
+
+## Getting started
+
+<CardGroup cols={2}>
+ <Card title="OpenAI Integration" href="/latest/sdk/integrations/go-openai" icon="robot">
+ Monitor chat completions, streaming, embeddings, and image generation
+ </Card>
+ <Card title="Anthropic Integration" href="/latest/sdk/integrations/go-anthropic" icon="robot">
+ Monitor Claude messages and streaming with cache token tracking
+ </Card>
+ <Card title="Destinations" href="/latest/sdk/destinations/overview" icon="link">
+ Send telemetry to Datadog, Grafana, New Relic, and other observability stacks
+ </Card>
+ <Card title="Configuration" href="/latest/sdk/configuration" icon="bolt">
+ Configure the OpenLIT SDK according to your requirements
+ </Card>
+</CardGroup>
diff --git a/docs/latest/sdk/integrations/go-anthropic.mdx b/docs/latest/sdk/integrations/go-anthropic.mdx
new file mode 100644
index 00000000..77464e37
--- /dev/null
+++ b/docs/latest/sdk/integrations/go-anthropic.mdx
@@ -0,0 +1,222 @@
+---
+title: 'Monitor Claude using OpenTelemetry (Go)'
+sidebarTitle: 'Anthropic (Go)'
+---
+
+OpenLIT uses OpenTelemetry instrumentation to help you monitor Go applications built with Anthropic models. This includes tracking performance, token usage (including prompt cache tokens), costs, and how users interact with the application.
+
+The Go SDK wraps your Anthropic client with an `InstrumentedClient` that automatically emits traces and metrics for every API call — with zero changes to your application logic.
+
+The integration supports:
+- Messages (standard and streaming)
+- Prompt caching token tracking (`cache_creation_input_tokens`, `cache_read_input_tokens`)
+- Tool use
+
+## Get started
+
+<Steps>
+ <Step title="Install the Go SDK">
+ Open your terminal and run:
+
+ <Tabs>
+ <Tab title="Latest Version">
+ ```shell
+ go get github.com/openlit/openlit/sdk/go
+ ```
+ </Tab>
+ <Tab title="Specific Version">
+ ```shell
+ go get github.com/openlit/openlit/sdk/[email protected]
+ ```
+ Replace `v1.2.3` with the version you want to install.
+ </Tab>
+ </Tabs>
+ </Step>
+
+ <Step title="Initialize OpenLIT">
+ Add this once at the start of your application (e.g. in `main()`):
+
+ <Tabs>
+ <Tab title="Via Config Struct">
+ ```go
+ import (
+ "context"
+ openlit "github.com/openlit/openlit/sdk/go"
+ )
+
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "YOUR_OTEL_ENDPOINT",
+ ApplicationName: "my-ai-app",
+ Environment: "production",
+ }); err != nil {
+ log.Fatal(err)
+ }
+ defer openlit.Shutdown(context.Background())
+ ```
+ </Tab>
+ <Tab title="Via Environment Variable">
+ ```go
+ import (
+ "context"
+ openlit "github.com/openlit/openlit/sdk/go"
+ )
+
+ if err := openlit.Init(openlit.Config{
+ ApplicationName: "my-ai-app",
+ }); err != nil {
+ log.Fatal(err)
+ }
+ defer openlit.Shutdown(context.Background())
+ ```
+
+ Then set your OTLP endpoint via environment variable:
+ ```shell
+ export OTEL_EXPORTER_OTLP_ENDPOINT=YOUR_OTEL_ENDPOINT
+ ```
+ </Tab>
+ </Tabs>
+
+ **Replace** `YOUR_OTEL_ENDPOINT` with the URL of your OpenTelemetry backend, such as `http://127.0.0.1:4318` for a local OpenLIT deployment.
+ </Step>
+
+ <Step title="Create an instrumented client">
+ Replace your existing Anthropic client creation with the OpenLIT instrumented client:
+
+ ```go
+ import "github.com/openlit/openlit/sdk/go/instrumentation/anthropic"
+
+ client := anthropic.NewClient("your-anthropic-api-key")
+ ```
+
+ Optional configuration:
+ ```go
+ // Custom API version or base URL
+ client := anthropic.NewClient("your-api-key",
+ anthropic.WithAPIVersion("2023-06-01"),
+ anthropic.WithBaseURL("https://api.anthropic.com/v1"),
+ )
+ ```
+ </Step>
+
+ <Step title="Use the client">
+ Use the instrumented client exactly as you would a normal Anthropic client:
+
+ **Message:**
+ ```go
+ resp, err := client.CreateMessage(ctx, anthropic.MessageRequest{
+ Model: "claude-opus-4-5",
+ MaxTokens: 256,
+ System: "You are a helpful assistant.",
+ Messages: []anthropic.Message{
+ {Role: "user", Content: "What is OpenTelemetry?"},
+ },
+ })
+ if err != nil {
+ return err
+ }
+
+ for _, block := range resp.Content {
+ if block.Type == "text" {
+ fmt.Println(block.Text)
+ }
+ }
+ ```
+
+ **Streaming:**
+ ```go
+ stream, err := client.CreateMessageStream(ctx, anthropic.MessageRequest{
+ Model: "claude-opus-4-5",
+ MaxTokens: 1024,
+ Messages: []anthropic.Message{
+ {Role: "user", Content: "Tell me a story."},
+ },
+ })
+ if err != nil {
+ return err
+ }
+ defer stream.Close()
+
+ for {
+ event, err := stream.Recv()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return err
+ }
+ if event.Type == "content_block_delta" && event.Delta != nil {
+ fmt.Print(event.Delta.Text)
+ }
+ }
+ ```
+
+ **Tool use:**
+ ```go
+ resp, err := client.CreateMessage(ctx, anthropic.MessageRequest{
+ Model: "claude-opus-4-5",
+ MaxTokens: 512,
+ Messages: []anthropic.Message{
+ {Role: "user", Content: "What is the weather in San Francisco?"},
+ },
+ Tools: []anthropic.Tool{
+ {
+ Name: "get_weather",
+ Description: "Get the current weather in a location",
+ InputSchema: map[string]interface{}{
+ "type": "object",
+ "properties": map[string]interface{}{
+ "location": map[string]interface{}{
+ "type": "string",
+ "description": "The city and state",
+ },
+ },
+ "required": []string{"location"},
+ },
+ },
+ },
+ })
+ ```
+ </Step>
+</Steps>
+
+## What gets collected
+
+Every call to the instrumented client automatically records:
+
+| Data | Attribute |
+|------|-----------|
+| Operation name | `gen_ai.operation.name` |
+| Model requested | `gen_ai.request.model` |
+| Model used | `gen_ai.response.model` |
+| Response ID | `gen_ai.response.id` |
+| Input tokens | `gen_ai.usage.input_tokens` |
+| Output tokens | `gen_ai.usage.output_tokens` |
+| Cache creation tokens | `gen_ai.usage.prompt_tokens_details.cache_write` |
+| Cache read tokens | `gen_ai.usage.prompt_tokens_details.cache_read` |
+| Estimated cost | `gen_ai.usage.cost` |
+| Finish reason | `gen_ai.response.finish_reasons` |
+| Tool calls | `gen_ai.tool.name`, `gen_ai.tool.call.id` |
+| Time to first token | `gen_ai.server.time_to_first_token` (streaming) |
+| Time per output token | `gen_ai.server.time_per_output_token` (streaming) |
+
+Metrics emitted:
+- `gen_ai.client.token.usage` — token usage histogram (input/output)
+- `gen_ai.client.operation.duration` — total operation duration
+- `gen_ai.server.time_to_first_token` — TTFT for streaming
+- `gen_ai.client.operation.time_to_first_chunk` — client-side TTFT
+- `gen_ai.client.operation.time_per_output_chunk` — per-chunk latency
+- `gen_ai.server.request.duration` — estimated server processing time
+
+---
+
+<CardGroup cols={3}>
+ <Card title="Go SDK Overview" href="/latest/sdk/go-overview" icon="golang">
+ Full configuration reference and setup guide
+ </Card>
+ <Card title="Configuration" href="/latest/sdk/configuration" icon="bolt">
+ Configure the OpenLIT SDK according to your requirements
+ </Card>
+ <Card title="Destinations" href="/latest/sdk/destinations/overview" icon="link">
+ Send telemetry to Datadog, Grafana, New Relic, and other observability stacks
+ </Card>
+</CardGroup>
diff --git a/docs/latest/sdk/integrations/go-openai.mdx b/docs/latest/sdk/integrations/go-openai.mdx
new file mode 100644
index 00000000..f4a638c9
--- /dev/null
+++ b/docs/latest/sdk/integrations/go-openai.mdx
@@ -0,0 +1,204 @@
+---
+title: 'Monitor OpenAI using OpenTelemetry (Go)'
+sidebarTitle: 'OpenAI (Go)'
+---
+
+OpenLIT uses OpenTelemetry instrumentation to help you monitor Go applications built with OpenAI models. This includes tracking performance, token usage, costs, and how users interact with the application.
+
+The Go SDK wraps your OpenAI client with an `InstrumentedClient` that automatically emits traces and metrics for every API call — with zero changes to your application logic.
+
+The integration supports:
+- Chat completions (standard and streaming)
+- Embeddings
+- Image generation (`dall-e-2`, `dall-e-3`)
+
+## Get started
+
+<Steps>
+ <Step title="Install the Go SDK">
+ Open your terminal and run:
+
+ <Tabs>
+ <Tab title="Latest Version">
+ ```shell
+ go get github.com/openlit/openlit/sdk/go
+ ```
+ </Tab>
+ <Tab title="Specific Version">
+ ```shell
+ go get github.com/openlit/openlit/sdk/[email protected]
+ ```
+ Replace `v1.2.3` with the version you want to install.
+ </Tab>
+ </Tabs>
+ </Step>
+
+ <Step title="Initialize OpenLIT">
+ Add this once at the start of your application (e.g. in `main()`):
+
+ <Tabs>
+ <Tab title="Via Config Struct">
+ ```go
+ import (
+ "context"
+ openlit "github.com/openlit/openlit/sdk/go"
+ )
+
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "YOUR_OTEL_ENDPOINT",
+ ApplicationName: "my-ai-app",
+ Environment: "production",
+ }); err != nil {
+ log.Fatal(err)
+ }
+ defer openlit.Shutdown(context.Background())
+ ```
+ </Tab>
+ <Tab title="Via Environment Variable">
+ ```go
+ import (
+ "context"
+ openlit "github.com/openlit/openlit/sdk/go"
+ )
+
+ if err := openlit.Init(openlit.Config{
+ ApplicationName: "my-ai-app",
+ }); err != nil {
+ log.Fatal(err)
+ }
+ defer openlit.Shutdown(context.Background())
+ ```
+
+ Then set your OTLP endpoint via environment variable:
+ ```shell
+ export OTEL_EXPORTER_OTLP_ENDPOINT=YOUR_OTEL_ENDPOINT
+ ```
+ </Tab>
+ </Tabs>
+
+ **Replace** `YOUR_OTEL_ENDPOINT` with the URL of your OpenTelemetry backend, such as `http://127.0.0.1:4318` for a local OpenLIT deployment.
+ </Step>
+
+ <Step title="Create an instrumented client">
+ Replace your existing OpenAI client creation with the OpenLIT instrumented client:
+
+ ```go
+ import "github.com/openlit/openlit/sdk/go/instrumentation/openai"
+
+ client := openai.NewClient("your-openai-api-key")
+ ```
+
+ Optional configuration:
+ ```go
+ // Custom base URL (e.g. for Azure OpenAI or local models)
+ client := openai.NewClient("your-api-key",
+ openai.WithBaseURL("https://your-custom-endpoint/v1"),
+ )
+ ```
+ </Step>
+
+ <Step title="Use the client">
+ Use the instrumented client exactly as you would a normal OpenAI client:
+
+ **Chat completion:**
+ ```go
+ resp, err := client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []openai.Message{
+ {Role: "system", Content: "You are a helpful assistant."},
+ {Role: "user", Content: "What is OpenTelemetry?"},
+ },
+ MaxTokens: 256,
+ Temperature: 0.7,
+ })
+ if err != nil {
+ return err
+ }
+ fmt.Println(resp.Choices[0].Message.Content)
+ ```
+
+ **Streaming:**
+ ```go
+ stream, err := client.CreateChatCompletionStream(ctx, openai.ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []openai.Message{
+ {Role: "user", Content: "Tell me a story."},
+ },
+ })
+ if err != nil {
+ return err
+ }
+ defer stream.Close()
+
+ for {
+ chunk, err := stream.Recv()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return err
+ }
+ if len(chunk.Choices) > 0 {
+ fmt.Print(chunk.Choices[0].Delta.Content)
+ }
+ }
+ ```
+
+ **Embeddings:**
+ ```go
+ resp, err := client.CreateEmbedding(ctx, openai.EmbeddingRequest{
+ Model: "text-embedding-3-small",
+ Input: "The quick brown fox",
+ })
+ ```
+
+ **Image generation:**
+ ```go
+ resp, err := client.CreateImage(ctx, openai.ImageRequest{
+ Model: "dall-e-3",
+ Prompt: "A futuristic city skyline at sunset",
+ N: 1,
+ Size: "1024x1024",
+ })
+ ```
+ </Step>
+</Steps>
+
+## What gets collected
+
+Every call to the instrumented client automatically records:
+
+| Data | Attribute |
+|------|-----------|
+| Operation name | `gen_ai.operation.name` |
+| Model requested | `gen_ai.request.model` |
+| Model used | `gen_ai.response.model` |
+| Input tokens | `gen_ai.usage.input_tokens` |
+| Output tokens | `gen_ai.usage.output_tokens` |
+| Estimated cost | `gen_ai.usage.cost` |
+| Finish reason | `gen_ai.response.finish_reasons` |
+| Tool calls | `gen_ai.tool.name`, `gen_ai.tool.call.id` |
+| Time to first token | `gen_ai.server.time_to_first_token` (streaming) |
+| Time per output token | `gen_ai.server.time_per_output_token` (streaming) |
+
+Metrics emitted:
+- `gen_ai.client.token.usage` — token usage histogram (input/output)
+- `gen_ai.client.operation.duration` — total operation duration
+- `gen_ai.server.time_to_first_token` — TTFT for streaming
+- `gen_ai.client.operation.time_to_first_chunk` — client-side TTFT
+- `gen_ai.client.operation.time_per_output_chunk` — per-chunk latency
+- `gen_ai.server.request.duration` — estimated server processing time
+
+---
+
+<CardGroup cols={3}>
+ <Card title="Go SDK Overview" href="/latest/sdk/go-overview" icon="golang">
+ Full configuration reference and setup guide
+ </Card>
+ <Card title="Configuration" href="/latest/sdk/configuration" icon="bolt">
+ Configure the OpenLIT SDK according to your requirements
+ </Card>
+ <Card title="Destinations" href="/latest/sdk/destinations/overview" icon="link">
+ Send telemetry to Datadog, Grafana, New Relic, and other observability stacks
+ </Card>
+</CardGroup>
diff --git a/docs/latest/sdk/overview.mdx b/docs/latest/sdk/overview.mdx
index 35d94262..3b01777c 100644
--- a/docs/latest/sdk/overview.mdx
+++ b/docs/latest/sdk/overview.mdx
@@ -46,13 +46,16 @@ The OpenLIT SDK automatically instruments:
## Supported languages
-<CardGroup cols={2}>
- <Card title="Python" icon="python">
+<CardGroup cols={3}>
+ <Card title="Python" icon="python">
Complete AI observability with automatic dependency detection. Zero-code instrumentation for production Python LLM applications with real-time performance monitoring.
</Card>
- <Card title="TypeScript/JavaScript" icon="node-js">
+ <Card title="TypeScript/JavaScript" icon="node-js">
Full LLM monitoring support for TypeScript / JavaScript applications with distributed tracing, metrics and cost optimization.
</Card>
+ <Card title="Go" icon="golang" href="/latest/sdk/go-overview">
+ OpenTelemetry-native instrumentation for Go applications. Wrap your OpenAI and Anthropic clients for automatic tracing, token tracking, and cost monitoring.
+ </Card>
</CardGroup>
## How it works
diff --git a/operator/api/v1alpha1/autoinstrumentation_types.go b/operator/api/v1alpha1/autoinstrumentation_types.go
index a3d20d85..bb1b9bb1 100644
--- a/operator/api/v1alpha1/autoinstrumentation_types.go
+++ b/operator/api/v1alpha1/autoinstrumentation_types.go
@@ -37,6 +37,10 @@ type AutoInstrumentationSpec struct {
// +optional
Python *PythonInstrumentation `json:"python,omitempty"`
+ // Go defines Go-specific instrumentation configuration
+ // +optional
+ Go *GoInstrumentation `json:"go,omitempty"`
+
// OTLP defines OpenTelemetry Protocol configuration
// +kubebuilder:validation:Required
OTLP OTLPConfig `json:"otlp"`
@@ -64,6 +68,17 @@ type PythonInstrumentation struct {
Instrumentation *InstrumentationSettings `json:"instrumentation,omitempty"`
}
+// GoInstrumentation defines Go-specific instrumentation settings
+type GoInstrumentation struct {
+ // Image is the container image for Go SDK injection
+ // +optional
+ Image string `json:"image,omitempty"`
+
+ // Env defines environment variables to inject
+ // +optional
+ Env []EnvVar `json:"env,omitempty"`
+}
+
// EnvVar represents an environment variable to be injected
type EnvVar struct {
// Name is the environment variable name
diff --git a/sdk/go/CHANGELOG.md b/sdk/go/CHANGELOG.md
new file mode 100644
index 00000000..12175498
--- /dev/null
+++ b/sdk/go/CHANGELOG.md
@@ -0,0 +1,23 @@
+# Changelog
+
+All notable changes to the OpenLIT Go SDK will be documented in this file.
+
+The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
+and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+
+## [0.1.0] - 2026-02-26
+
+### Added
+- Initial release of the OpenLIT Go SDK
+- OpenTelemetry-native instrumentation for OpenAI (chat completions, streaming, embeddings, image generation)
+- OpenTelemetry-native instrumentation for Anthropic (messages, streaming, tool use)
+- Automatic token usage tracking with `gen_ai.client.token.usage` histogram
+- Operation duration metric `gen_ai.client.operation.duration`
+- Streaming metrics: `gen_ai.server.time_to_first_token`, `gen_ai.server.time_per_output_token`, `gen_ai.client.operation.time_to_first_chunk`, `gen_ai.client.operation.time_per_output_chunk`, `gen_ai.server.request.duration`
+- Error-path metrics with `error.type` dimension on all non-streaming operations
+- Automatic cost calculation using built-in pricing data
+- Prompt cache token tracking for Anthropic (`cache_creation_input_tokens`, `cache_read_input_tokens`)
+- `DisableCaptureMessageContent` flag for privacy-sensitive workloads
+- Configurable OTLP endpoint, headers, export interval, and timeouts
+- `openlit.sdk.version` resource attribute on all telemetry
+- OTel semantic conventions alignment with Python and TypeScript SDKs
diff --git a/sdk/go/README.md b/sdk/go/README.md
new file mode 100644
index 00000000..f195d8bb
--- /dev/null
+++ b/sdk/go/README.md
@@ -0,0 +1,290 @@
+# OpenLIT Go SDK
+
+OpenTelemetry-native observability SDK for LLM applications in Go. Monitor your AI applications with automatic instrumentation for popular LLM providers.
+
+[![Go Reference](https://pkg.go.dev/badge/github.com/openlit/openlit/sdk/go.svg)](https://pkg.go.dev/github.com/openlit/openlit/sdk/go)
+[![Go Report Card](https://goreportcard.com/badge/github.com/openlit/openlit/sdk/go)](https://goreportcard.com/report/github.com/openlit/openlit/sdk/go)
+
+## Features
+
+- 🚀 **One-line initialization** - Get started with minimal code
+- 📊 **OpenTelemetry-native** - Built on OpenTelemetry standards
+- 💰 **Cost tracking** - Automatic cost calculation for LLM requests
+- 📈 **Metrics collection** - Token usage, latency, and performance metrics
+- 🔄 **Streaming support** - Full support for streaming responses
+- 🔌 **Flexible integration** - Works with multiple Go client libraries
+
+## Supported Integrations
+
+- ✅ **OpenAI** - Chat completions, embeddings, images, audio
+- ✅ **Anthropic** - Messages API with tool calling support
+
+## Installation
+
+```bash
+go get github.com/openlit/openlit/sdk/go
+```
+
+## Quick Start
+
+### 1. Initialize OpenLIT
+
+```go
+package main
+
+import (
+ "context"
+ "log"
+
+ "github.com/openlit/openlit/sdk/go"
+)
+
+func main() {
+ // Initialize OpenLIT
+ err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://127.0.0.1:4318",
+ Environment: "production",
+ ApplicationName: "my-go-app",
+ })
+ if err != nil {
+ log.Fatalf("Failed to initialize OpenLIT: %v", err)
+ }
+ defer openlit.Shutdown(context.Background())
+
+ // Your application code here
+}
+```
+
+### 2. Instrument OpenAI
+
+```go
+import (
+ "github.com/openlit/openlit/sdk/go/instrumentation/openai"
+ openai_sdk "github.com/sashabaranov/go-openai"
+)
+
+// Create and instrument OpenAI client
+client := openai_sdk.NewClient("your-api-key")
+instrumentedClient := openai.Instrument(client)
+
+// Use as normal - automatically traced!
+resp, err := instrumentedClient.CreateChatCompletion(ctx, openai_sdk.ChatCompletionRequest{
+ Model: openai_sdk.GPT4,
+ Messages: []openai_sdk.ChatCompletionMessage{
+ {
+ Role: openai_sdk.ChatMessageRoleUser,
+ Content: "Hello!",
+ },
+ },
+})
+```
+
+### 3. Instrument Anthropic
+
+```go
+import (
+ "github.com/openlit/openlit/sdk/go/instrumentation/anthropic"
+)
+
+// Create and instrument Anthropic client
+client := anthropic.NewClient("your-api-key")
+instrumentedClient := anthropic.Instrument(client)
+
+// Use as normal - automatically traced!
+resp, err := instrumentedClient.CreateMessage(ctx, anthropic.MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ Messages: []anthropic.Message{
+ {
+ Role: "user",
+ Content: "Hello!",
+ },
+ },
+ MaxTokens: 1024,
+})
+```
+
+## Configuration Options
+
+```go
+config := openlit.Config{
+ // Required
+ OtlpEndpoint: "http://127.0.0.1:4318", // OTLP endpoint
+
+ // Optional
+ Environment: "production", // Deployment environment
+ ApplicationName: "my-go-app", // Application name
+ ServiceVersion: "1.0.0", // Service version
+
+ // OTLP Configuration
+ OtlpHeaders: map[string]string{}, // Custom OTLP headers
+
+ // Feature Flags
+ DisableTracing: false, // Disable tracing
+ DisableMetrics: false, // Disable metrics
+ DisableBatch: false, // Disable batch processing
+
+ // Timeouts
+ TraceExporterTimeout: 10 * time.Second, // Trace export timeout
+ MetricExporterTimeout: 10 * time.Second, // Metric export timeout
+ MetricExportInterval: 30 * time.Second, // Metric export interval
+
+ // Pricing
+ PricingEndpoint: "", // Custom pricing endpoint
+ DisablePricingFetch: false, // Disable pricing fetch
+ PricingInfo: map[string]ModelPricing{}, // Custom pricing
+}
+```
+
+## Streaming Support
+
+Both OpenAI and Anthropic integrations support streaming responses:
+
+### OpenAI Streaming
+
+```go
+stream, err := instrumentedClient.CreateChatCompletionStream(ctx, request)
+if err != nil {
+ log.Fatal(err)
+}
+defer stream.Close()
+
+for {
+ response, err := stream.Recv()
+ if errors.Is(err, io.EOF) {
+ break
+ }
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ fmt.Printf(response.Choices[0].Delta.Content)
+}
+```
+
+### Anthropic Streaming
+
+```go
+stream, err := instrumentedClient.CreateMessageStream(ctx, request)
+if err != nil {
+ log.Fatal(err)
+}
+defer stream.Close()
+
+for {
+ event, err := stream.Recv()
+ if errors.Is(err, io.EOF) {
+ break
+ }
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Handle event
+}
+```
+
+## Collected Telemetry
+
+### Traces
+
+- Operation name and type (chat, embedding, etc.)
+- Request and response models
+- Input/output messages
+- Token usage (input, output, total)
+- Cost calculations
+- Response times
+- Error details
+
+### Metrics
+
+- `gen_ai.client.token.usage` - Token usage counter
+- `gen_ai.client.operation.duration` - Operation duration histogram
+- `gen_ai.server.time_to_first_token` - TTFT for streaming
+- `gen_ai.server.time_per_output_token` - TBT for streaming
+
+### Attributes
+
+All traces include standard OpenTelemetry semantic conventions:
+- `gen_ai.operation.name`
+- `gen_ai.system`
+- `gen_ai.request.model`
+- `gen_ai.response.model`
+- `gen_ai.usage.input_tokens`
+- `gen_ai.usage.output_tokens`
+- `server.address`
+- `server.port`
+
+## Advanced Usage
+
+### Custom Pricing
+
+Provide custom pricing information for models:
+
+```go
+config := openlit.Config{
+ PricingInfo: map[string]openlit.ModelPricing{
+ "gpt-4-custom": {
+ InputCostPerToken: 0.00003,
+ OutputCostPerToken: 0.00006,
+ },
+ },
+}
+```
+
+### Custom Headers
+
+Add custom headers to OTLP exports:
+
+```go
+config := openlit.Config{
+ OtlpHeaders: map[string]string{
+ "Authorization": "Bearer token",
+ "X-Custom-Header": "value",
+ },
+}
+```
+
+## Integration with OpenLIT Dashboard
+
+The Go SDK works seamlessly with the [OpenLIT Dashboard](https://github.com/openlit/openlit):
+
+1. Start OpenLIT stack:
+```bash
+docker compose up -d
+```
+
+2. Configure SDK to send data:
+```go
+openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://localhost:4318",
+})
+```
+
+3. View your traces at http://localhost:3000
+
+## Examples
+
+See the [examples/](examples/) directory for complete working examples:
+- [OpenAI Chat Completion](examples/openai/chat/)
+- [OpenAI Streaming](examples/openai/streaming/)
+- [Anthropic Messages](examples/anthropic/messages/)
+- [Anthropic Streaming](examples/anthropic/streaming/)
+
+## Requirements
+
+- Go 1.21 or higher
+- OpenTelemetry Collector or compatible backend
+
+## Contributing
+
+We welcome contributions! See [CONTRIBUTING.md](../../CONTRIBUTING.md) for details.
+
+## License
+
+Apache License 2.0 - see [LICENSE](../../LICENSE)
+
+## Support
+
+- 📚 [Documentation](https://docs.openlit.io/)
+- 💬 [Slack Community](https://join.slack.com/t/openlit/shared_invite/zt-2etnfttwg-TjP_7BZXfYg84oAukY8QRQ)
+- 🐛 [Issue Tracker](https://github.com/openlit/openlit/issues)
diff --git a/sdk/go/config.go b/sdk/go/config.go
new file mode 100644
index 00000000..ff143da8
--- /dev/null
+++ b/sdk/go/config.go
@@ -0,0 +1,122 @@
+package openlit
+
+import (
+ "time"
+)
+
+// Config holds the configuration for OpenLIT SDK initialization
+type Config struct {
+ // OtlpEndpoint is the OTLP endpoint for sending traces and metrics
+ // Default: "http://127.0.0.1:4318"
+ OtlpEndpoint string
+
+ // OtlpHeaders are additional headers to send with OTLP requests
+ OtlpHeaders map[string]string
+
+ // Environment identifies the deployment environment
+ // Default: "default"
+ Environment string
+
+ // ApplicationName is the name of your application
+ // Default: "default"
+ ApplicationName string
+
+ // TracerName is the name for the tracer provider
+ // Default: "openlit"
+ TracerName string
+
+ // ServiceVersion is the version of your service
+ ServiceVersion string
+
+ // DisableTracing disables trace collection
+ // Default: false
+ DisableTracing bool
+
+ // DisableMetrics disables metrics collection
+ // Default: false
+ DisableMetrics bool
+
+ // DisableBatch disables batch processing for exports
+ // Default: false
+ DisableBatch bool
+
+ // TraceExporterTimeout is the timeout for trace exports
+ // Default: 10 seconds
+ TraceExporterTimeout time.Duration
+
+ // MetricExporterTimeout is the timeout for metric exports
+ // Default: 10 seconds
+ MetricExporterTimeout time.Duration
+
+ // MetricExportInterval is the interval for metric exports
+ // Default: 30 seconds
+ MetricExportInterval time.Duration
+
+ // PricingInfo contains custom pricing information for models
+ PricingInfo map[string]ModelPricing
+
+ // PricingEndpoint is the endpoint to fetch pricing information
+ // Default: "https://github.com/openlit/openlit/raw/main/assets/pricing.json"
+ PricingEndpoint string
+
+ // DisablePricingFetch disables automatic pricing information fetching
+ DisablePricingFetch bool
+
+ // DisableCaptureMessageContent prevents prompt/completion text from being
+ // recorded as span attributes. Set to true for privacy-sensitive workloads.
+ // Default: false (content IS captured, matching Python SDK default of capture_message_content=True)
+ DisableCaptureMessageContent bool
+
+ // DetailedTracing enables detailed component-level tracing.
+ // Default: false
+ DetailedTracing bool
+}
+
+// ModelPricing represents pricing information for a model
+type ModelPricing struct {
+ InputCostPerToken float64
+ OutputCostPerToken float64
+}
+
+// setDefaults sets default values for the configuration
+func (c *Config) setDefaults() {
+ if c.OtlpEndpoint == "" {
+ c.OtlpEndpoint = "http://127.0.0.1:4318"
+ }
+
+ if c.Environment == "" {
+ c.Environment = "default"
+ }
+
+ if c.ApplicationName == "" {
+ c.ApplicationName = "default"
+ }
+
+ if c.TracerName == "" {
+ c.TracerName = "openlit"
+ }
+
+ if c.TraceExporterTimeout == 0 {
+ c.TraceExporterTimeout = 10 * time.Second
+ }
+
+ if c.MetricExporterTimeout == 0 {
+ c.MetricExporterTimeout = 10 * time.Second
+ }
+
+ if c.MetricExportInterval == 0 {
+ c.MetricExportInterval = 30 * time.Second
+ }
+
+ if c.PricingEndpoint == "" {
+ c.PricingEndpoint = "https://github.com/openlit/openlit/raw/main/assets/pricing.json"
+ }
+
+ if c.PricingInfo == nil {
+ c.PricingInfo = make(map[string]ModelPricing)
+ }
+
+ if c.OtlpHeaders == nil {
+ c.OtlpHeaders = make(map[string]string)
+ }
+}
diff --git a/sdk/go/go.mod b/sdk/go/go.mod
new file mode 100644
index 00000000..f1fef5c2
--- /dev/null
+++ b/sdk/go/go.mod
@@ -0,0 +1,31 @@
+module github.com/openlit/openlit/sdk/go
+
+go 1.22.7
+
+require (
+ go.opentelemetry.io/otel v1.33.0
+ go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.33.0
+ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.33.0
+ go.opentelemetry.io/otel/metric v1.33.0
+ go.opentelemetry.io/otel/sdk v1.33.0
+ go.opentelemetry.io/otel/sdk/metric v1.33.0
+ go.opentelemetry.io/otel/trace v1.33.0
+)
+
+require (
+ github.com/cenkalti/backoff/v4 v4.3.0 // indirect
+ github.com/go-logr/logr v1.4.2 // indirect
+ github.com/go-logr/stdr v1.2.2 // indirect
+ github.com/google/uuid v1.6.0 // indirect
+ github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 // indirect
+ go.opentelemetry.io/auto/sdk v1.1.0 // indirect
+ go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 // indirect
+ go.opentelemetry.io/proto/otlp v1.4.0 // indirect
+ golang.org/x/net v0.33.0 // indirect
+ golang.org/x/sys v0.28.0 // indirect
+ golang.org/x/text v0.21.0 // indirect
+ google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def // indirect
+ google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def // indirect
+ google.golang.org/grpc v1.69.2 // indirect
+ google.golang.org/protobuf v1.36.1 // indirect
+)
diff --git a/sdk/go/go.sum b/sdk/go/go.sum
new file mode 100644
index 00000000..7b712591
--- /dev/null
+++ b/sdk/go/go.sum
@@ -0,0 +1,57 @@
+github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
+github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
+github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
+github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
+github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
+github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
+github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
+github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
+github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
+github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
+github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0 h1:TmHmbvxPmaegwhDubVz0lICL0J5Ka2vwTzhoePEXsGE=
+github.com/grpc-ecosystem/grpc-gateway/v2 v2.24.0/go.mod h1:qztMSjm835F2bXf+5HKAPIS5qsmQDqZna/PgVt4rWtI=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
+github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
+go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
+go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
+go.opentelemetry.io/otel v1.33.0 h1:/FerN9bax5LoK51X/sI0SVYrjSE0/yUL7DpxW4K3FWw=
+go.opentelemetry.io/otel v1.33.0/go.mod h1:SUUkR6csvUQl+yjReHu5uM3EtVV7MBm5FHKRlNx4I8I=
+go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.33.0 h1:bSjzTvsXZbLSWU8hnZXcKmEVaJjjnandxD0PxThhVU8=
+go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.33.0/go.mod h1:aj2rilHL8WjXY1I5V+ra+z8FELtk681deydgYT8ikxU=
+go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0 h1:Vh5HayB/0HHfOQA7Ctx69E/Y/DcQSMPpKANYVMQ7fBA=
+go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.33.0/go.mod h1:cpgtDBaqD/6ok/UG0jT15/uKjAY8mRA53diogHBg3UI=
+go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.33.0 h1:wpMfgF8E1rkrT1Z6meFh1NDtownE9Ii3n3X2GJYjsaU=
+go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.33.0/go.mod h1:wAy0T/dUbs468uOlkT31xjvqQgEVXv58BRFWEgn5v/0=
+go.opentelemetry.io/otel/metric v1.33.0 h1:r+JOocAyeRVXD8lZpjdQjzMadVZp2M4WmQ+5WtEnklQ=
+go.opentelemetry.io/otel/metric v1.33.0/go.mod h1:L9+Fyctbp6HFTddIxClbQkjtubW6O9QS3Ann/M82u6M=
+go.opentelemetry.io/otel/sdk v1.33.0 h1:iax7M131HuAm9QkZotNHEfstof92xM+N8sr3uHXc2IM=
+go.opentelemetry.io/otel/sdk v1.33.0/go.mod h1:A1Q5oi7/9XaMlIWzPSxLRWOI8nG3FnzHJNbiENQuihM=
+go.opentelemetry.io/otel/sdk/metric v1.33.0 h1:Gs5VK9/WUJhNXZgn8MR6ITatvAmKeIuCtNbsP3JkNqU=
+go.opentelemetry.io/otel/sdk/metric v1.33.0/go.mod h1:dL5ykHZmm1B1nVRk9dDjChwDmt81MjVp3gLkQRwKf/Q=
+go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
+go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
+go.opentelemetry.io/proto/otlp v1.4.0 h1:TA9WRvW6zMwP+Ssb6fLoUIuirti1gGbP28GcKG1jgeg=
+go.opentelemetry.io/proto/otlp v1.4.0/go.mod h1:PPBWZIP98o2ElSqI35IHfu7hIhSwvc5N38Jw8pXuGFY=
+golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
+golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
+golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
+golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
+golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
+google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def h1:0Km0hi+g2KXbXL0+riZzSCKz23f4MmwicuEb00JeonI=
+google.golang.org/genproto/googleapis/api v0.0.0-20241230172942-26aa7a208def/go.mod h1:u2DoMSpCXjrzqLdobRccQMc9wrnMAJ1DLng0a2yqM2Q=
+google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def h1:4P81qv5JXI/sDNae2ClVx88cgDDA6DPilADkG9tYKz8=
+google.golang.org/genproto/googleapis/rpc v0.0.0-20241230172942-26aa7a208def/go.mod h1:bdAgzvd4kFrpykc5/AC2eLUiegK9T/qxZHD4hXYf/ho=
+google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
+google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
+google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
+google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/sdk/go/helpers/config.go b/sdk/go/helpers/config.go
new file mode 100644
index 00000000..afc90e31
--- /dev/null
+++ b/sdk/go/helpers/config.go
@@ -0,0 +1,23 @@
+package helpers
+
+import "sync"
+
+var (
+ captureMessageContent = true // default: capture content (matches Python SDK)
+ captureMessageContentMu sync.RWMutex
+)
+
+// SetCaptureMessageContent configures whether prompt/completion text is recorded
+// in span attributes. Called by openlit.Init based on DisableCaptureMessageContent.
+func SetCaptureMessageContent(capture bool) {
+ captureMessageContentMu.Lock()
+ captureMessageContent = capture
+ captureMessageContentMu.Unlock()
+}
+
+// GetCaptureMessageContent returns whether message content should be captured.
+func GetCaptureMessageContent() bool {
+ captureMessageContentMu.RLock()
+ defer captureMessageContentMu.RUnlock()
+ return captureMessageContent
+}
diff --git a/sdk/go/helpers/http.go b/sdk/go/helpers/http.go
new file mode 100644
index 00000000..dd601158
--- /dev/null
+++ b/sdk/go/helpers/http.go
@@ -0,0 +1,120 @@
+package helpers
+
+import (
+ "net/http"
+ "strconv"
+ "strings"
+ "time"
+
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/codes"
+ "go.opentelemetry.io/otel/trace"
+)
+
+// InstrumentedRoundTripper wraps an http.RoundTripper with OpenTelemetry tracing
+type InstrumentedRoundTripper struct {
+ base http.RoundTripper
+ tracer trace.Tracer
+ spanName string
+ attributes []trace.SpanStartOption
+}
+
+// NewInstrumentedRoundTripper creates a new instrumented round tripper
+func NewInstrumentedRoundTripper(base http.RoundTripper, spanName string, opts ...trace.SpanStartOption) *InstrumentedRoundTripper {
+ if base == nil {
+ base = http.DefaultTransport
+ }
+
+ return &InstrumentedRoundTripper{
+ base: base,
+ tracer: otel.Tracer("openlit"),
+ spanName: spanName,
+ attributes: opts,
+ }
+}
+
+// RoundTrip executes a single HTTP transaction with tracing
+func (irt *InstrumentedRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
+ ctx := req.Context()
+
+ // Only create a span if we're not already in one with the same name.
+ // This prevents double-instrumentation.
+ if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
+ return irt.base.RoundTrip(req)
+ }
+
+ ctx, span := irt.tracer.Start(ctx, irt.spanName, irt.attributes...)
+ defer span.End()
+
+ req = req.WithContext(ctx)
+
+ startTime := time.Now()
+ resp, err := irt.base.RoundTrip(req)
+ _ = time.Since(startTime)
+
+ if err != nil {
+ span.RecordError(err)
+ span.SetStatus(codes.Error, err.Error())
+ return nil, err
+ }
+
+ if resp.StatusCode >= 400 {
+ span.SetStatus(codes.Error, resp.Status)
+ }
+
+ return resp, nil
+}
+
+// SetServerAddressAndPort extracts server address and port from a base URL.
+func SetServerAddressAndPort(baseURL string, defaultAddress string, defaultPort int) (string, int) {
+ if baseURL == "" {
+ return defaultAddress, defaultPort
+ }
+
+ address := defaultAddress
+ port := defaultPort
+
+ if idx := strings.LastIndex(baseURL, "://"); idx != -1 {
+ remaining := baseURL[idx+3:]
+ // Strip any path component
+ if slashIdx := strings.Index(remaining, "/"); slashIdx != -1 {
+ remaining = remaining[:slashIdx]
+ }
+ if portIdx := strings.LastIndex(remaining, ":"); portIdx != -1 {
+ address = remaining[:portIdx]
+ portStr := remaining[portIdx+1:]
+ if p, err := strconv.Atoi(portStr); err == nil {
+ port = p
+ }
+ } else {
+ address = remaining
+ if strings.HasPrefix(baseURL, "https") {
+ port = 443
+ } else if strings.HasPrefix(baseURL, "http") {
+ port = 80
+ }
+ }
+ }
+
+ return address, port
+}
+
+// RecordError records an error on a span
+func RecordError(span trace.Span, err error) {
+ if err != nil && span != nil {
+ span.RecordError(err)
+ span.SetStatus(codes.Error, err.Error())
+ }
+}
+
+// RecordException records an exception with an explicit message on a span
+func RecordException(span trace.Span, err error, message string) {
+ if err != nil && span != nil {
+ span.RecordError(err)
+ if message != "" {
+ span.SetStatus(codes.Error, message)
+ } else {
+ span.SetStatus(codes.Error, err.Error())
+ }
+ }
+}
diff --git a/sdk/go/helpers/pricing.go b/sdk/go/helpers/pricing.go
new file mode 100644
index 00000000..90797b47
--- /dev/null
+++ b/sdk/go/helpers/pricing.go
@@ -0,0 +1,193 @@
+package helpers
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "strings"
+ "sync"
+ "time"
+)
+
+// PricingInfo holds pricing information for a model
+type PricingInfo struct {
+ InputCostPerToken float64 `json:"input_cost_per_token"`
+ OutputCostPerToken float64 `json:"output_cost_per_token"`
+}
+
+// PricingCache manages pricing information with automatic fetching
+type PricingCache struct {
+ mu sync.RWMutex
+ prices map[string]PricingInfo
+ endpoint string
+ disableFetch bool
+ lastFetch time.Time
+ fetchInterval time.Duration
+ client *http.Client
+}
+
+// pricingResponse represents the structure from the pricing JSON
+type pricingResponse struct {
+ Data map[string]struct {
+ Input float64 `json:"input"`
+ Output float64 `json:"output"`
+ } `json:"data"`
+}
+
+var (
+ globalPricingCache *PricingCache
+ pricingCacheMutex sync.Mutex
+)
+
+// NewPricingCache creates a new pricing cache
+func NewPricingCache(endpoint string, disableFetch bool, customPricing map[string]PricingInfo) *PricingCache {
+ if endpoint == "" {
+ endpoint = "https://github.com/openlit/openlit/raw/main/assets/pricing.json"
+ }
+
+ pc := &PricingCache{
+ prices: make(map[string]PricingInfo),
+ endpoint: endpoint,
+ disableFetch: disableFetch,
+ fetchInterval: 24 * time.Hour, // Refresh pricing daily
+ client: &http.Client{
+ Timeout: 10 * time.Second,
+ },
+ }
+
+ // Add custom pricing
+ for model, pricing := range customPricing {
+ pc.prices[normalizeModelName(model)] = pricing
+ }
+
+ // Fetch pricing if not disabled
+ if !disableFetch {
+ go pc.fetchPricing(context.Background())
+ }
+
+ return pc
+}
+
+// InitGlobalPricingCache initializes the global pricing cache
+func InitGlobalPricingCache(endpoint string, disableFetch bool, customPricing map[string]PricingInfo) {
+ pricingCacheMutex.Lock()
+ defer pricingCacheMutex.Unlock()
+
+ globalPricingCache = NewPricingCache(endpoint, disableFetch, customPricing)
+}
+
+// GetGlobalPricingCache returns the global pricing cache
+func GetGlobalPricingCache() *PricingCache {
+ pricingCacheMutex.Lock()
+ defer pricingCacheMutex.Unlock()
+
+ if globalPricingCache == nil {
+ globalPricingCache = NewPricingCache("", false, nil)
+ }
+
+ return globalPricingCache
+}
+
+// GetPricing returns pricing information for a model
+func (pc *PricingCache) GetPricing(model string) (PricingInfo, bool) {
+ pc.mu.RLock()
+ defer pc.mu.RUnlock()
+
+ normalizedModel := normalizeModelName(model)
+ pricing, ok := pc.prices[normalizedModel]
+ return pricing, ok
+}
+
+// SetPricing sets pricing information for a model
+func (pc *PricingCache) SetPricing(model string, pricing PricingInfo) {
+ pc.mu.Lock()
+ defer pc.mu.Unlock()
+
+ pc.prices[normalizeModelName(model)] = pricing
+}
+
+// CalculateCost calculates the cost for input and output tokens
+func (pc *PricingCache) CalculateCost(model string, inputTokens, outputTokens int) float64 {
+ pricing, ok := pc.GetPricing(model)
+ if !ok {
+ return 0.0
+ }
+
+ inputCost := float64(inputTokens) * pricing.InputCostPerToken
+ outputCost := float64(outputTokens) * pricing.OutputCostPerToken
+
+ return inputCost + outputCost
+}
+
+// fetchPricing fetches pricing information from the endpoint
+func (pc *PricingCache) fetchPricing(ctx context.Context) {
+ pc.mu.Lock()
+ // Check if we need to fetch
+ if time.Since(pc.lastFetch) < pc.fetchInterval {
+ pc.mu.Unlock()
+ return
+ }
+ pc.mu.Unlock()
+
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, pc.endpoint, nil)
+ if err != nil {
+ return
+ }
+
+ resp, err := pc.client.Do(req)
+ if err != nil {
+ return
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode != http.StatusOK {
+ return
+ }
+
+ var pricingData pricingResponse
+ if err := json.NewDecoder(resp.Body).Decode(&pricingData); err != nil {
+ return
+ }
+
+ pc.mu.Lock()
+ defer pc.mu.Unlock()
+
+ // Update pricing information
+ for model, prices := range pricingData.Data {
+ normalizedModel := normalizeModelName(model)
+ // Don't override custom pricing
+ if _, exists := pc.prices[normalizedModel]; !exists {
+ pc.prices[normalizedModel] = PricingInfo{
+ InputCostPerToken: prices.Input,
+ OutputCostPerToken: prices.Output,
+ }
+ }
+ }
+
+ pc.lastFetch = time.Now()
+}
+
+// RefreshPricing forces a refresh of pricing information
+func (pc *PricingCache) RefreshPricing(ctx context.Context) error {
+ if pc.disableFetch {
+ return fmt.Errorf("pricing fetch is disabled")
+ }
+
+ pc.fetchPricing(ctx)
+ return nil
+}
+
+// normalizeModelName normalizes model names for lookup
+func normalizeModelName(model string) string {
+ // Convert to lowercase and remove common prefixes
+ model = strings.ToLower(model)
+ model = strings.TrimSpace(model)
+ return model
+}
+
+// CalculateGlobalCost is a convenience function using the global cache
+func CalculateGlobalCost(model string, inputTokens, outputTokens int) float64 {
+ cache := GetGlobalPricingCache()
+ return cache.CalculateCost(model, inputTokens, outputTokens)
+}
diff --git a/sdk/go/helpers/tokens.go b/sdk/go/helpers/tokens.go
new file mode 100644
index 00000000..d4755ede
--- /dev/null
+++ b/sdk/go/helpers/tokens.go
@@ -0,0 +1,58 @@
+package helpers
+
+import (
+ "strings"
+)
+
+// EstimateTokenCount provides a rough estimation of token count
+// This is a simple approximation - for accurate counts, use tiktoken or similar
+func EstimateTokenCount(text string) int {
+ if text == "" {
+ return 0
+ }
+
+ // Simple approximation: ~4 characters per token
+ // This matches the rough heuristic used in the industry
+ charCount := len(text)
+ tokenCount := charCount / 4
+
+ // Account for word boundaries - each word is typically at least 1 token
+ words := strings.Fields(text)
+ if len(words) > tokenCount {
+ tokenCount = len(words)
+ }
+
+ return tokenCount
+}
+
+// EstimateTokensFromMessages estimates tokens from a list of messages
+func EstimateTokensFromMessages(messages []string) int {
+ total := 0
+ for _, msg := range messages {
+ total += EstimateTokenCount(msg)
+ }
+ return total
+}
+
+// TokenStats holds token usage statistics
+type TokenStats struct {
+ InputTokens int
+ OutputTokens int
+ TotalTokens int
+}
+
+// NewTokenStats creates a new TokenStats instance
+func NewTokenStats(input, output int) TokenStats {
+ return TokenStats{
+ InputTokens: input,
+ OutputTokens: output,
+ TotalTokens: input + output,
+ }
+}
+
+// Add adds another TokenStats to this one
+func (ts *TokenStats) Add(other TokenStats) {
+ ts.InputTokens += other.InputTokens
+ ts.OutputTokens += other.OutputTokens
+ ts.TotalTokens += other.TotalTokens
+}
diff --git a/sdk/go/instrumentation/anthropic/anthropic_test.go b/sdk/go/instrumentation/anthropic/anthropic_test.go
new file mode 100644
index 00000000..1b6823b8
--- /dev/null
+++ b/sdk/go/instrumentation/anthropic/anthropic_test.go
@@ -0,0 +1,502 @@
+package anthropic
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ openlit "github.com/openlit/openlit/sdk/go"
+)
+
+// initSDK initialises OpenLIT with all exporters disabled and registers cleanup.
+func initSDK(t *testing.T) {
+ t.Helper()
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://localhost:4318",
+ Environment: "test",
+ ApplicationName: "go-sdk-unit-tests",
+ DisableTracing: true,
+ DisableMetrics: true,
+ DisableBatch: true,
+ DisablePricingFetch: true,
+ }); err != nil {
+ t.Fatalf("openlit.Init: %v", err)
+ }
+ t.Cleanup(func() { openlit.Shutdown(context.Background()) }) //nolint:errcheck
+}
+
+// ---------------------------------------------------------------------------
+// Messages API — success paths
+// ---------------------------------------------------------------------------
+
+func TestCreateMessage(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/messages" {
+ t.Errorf("unexpected path: %s", r.URL.Path)
+ }
+ if r.Method != http.MethodPost {
+ t.Errorf("unexpected method: %s", r.Method)
+ }
+ if r.Header.Get("x-api-key") == "" {
+ t.Error("missing x-api-key header")
+ }
+ if r.Header.Get("anthropic-version") == "" {
+ t.Error("missing anthropic-version header")
+ }
+
+ resp := MessageResponse{
+ ID: "msg-001",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{
+ {Type: "text", Text: "Hello! I'm Claude."},
+ },
+ Model: "claude-3-5-sonnet-20241022",
+ StopReason: "end_turn",
+ Usage: &Usage{InputTokens: 12, OutputTokens: 8},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 256,
+ Messages: []Message{{Role: "user", Content: "Hello!"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage: %v", err)
+ }
+ if resp.ID != "msg-001" {
+ t.Errorf("want ID msg-001, got %s", resp.ID)
+ }
+ if resp.Role != "assistant" {
+ t.Errorf("want role assistant, got %s", resp.Role)
+ }
+ if len(resp.Content) != 1 {
+ t.Fatalf("want 1 content block, got %d", len(resp.Content))
+ }
+ if resp.Content[0].Text != "Hello! I'm Claude." {
+ t.Errorf("unexpected content: %s", resp.Content[0].Text)
+ }
+ if resp.StopReason != "end_turn" {
+ t.Errorf("want stop_reason end_turn, got %s", resp.StopReason)
+ }
+ if resp.Usage.InputTokens != 12 {
+ t.Errorf("want 12 input tokens, got %d", resp.Usage.InputTokens)
+ }
+ if resp.Usage.OutputTokens != 8 {
+ t.Errorf("want 8 output tokens, got %d", resp.Usage.OutputTokens)
+ }
+}
+
+func TestCreateMessage_WithSystem(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req MessageRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ t.Errorf("decode body: %v", err)
+ }
+ if req.System != "You are a concise assistant." {
+ t.Errorf("unexpected system: %s", req.System)
+ }
+ if req.MaxTokens != 100 {
+ t.Errorf("want max_tokens=100, got %d", req.MaxTokens)
+ }
+
+ resp := MessageResponse{
+ ID: "msg-002",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{{Type: "text", Text: "Sure."}},
+ Model: "claude-3-haiku-20240307",
+ StopReason: "end_turn",
+ Usage: &Usage{InputTokens: 20, OutputTokens: 2},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-haiku-20240307",
+ System: "You are a concise assistant.",
+ MaxTokens: 100,
+ Messages: []Message{{Role: "user", Content: "Be brief."}},
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage: %v", err)
+ }
+ if resp.Content[0].Text != "Sure." {
+ t.Errorf("unexpected text: %s", resp.Content[0].Text)
+ }
+}
+
+func TestCreateMessage_MultiTurn(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req MessageRequest
+ json.NewDecoder(r.Body).Decode(&req) //nolint:errcheck
+ if len(req.Messages) != 3 {
+ t.Errorf("want 3 messages, got %d", len(req.Messages))
+ }
+ if req.Messages[1].Role != "assistant" {
+ t.Errorf("want messages[1].role=assistant, got %s", req.Messages[1].Role)
+ }
+
+ resp := MessageResponse{
+ ID: "msg-003",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{{Type: "text", Text: "Continuation."}},
+ Model: "claude-3-5-sonnet-20241022",
+ StopReason: "end_turn",
+ Usage: &Usage{InputTokens: 30, OutputTokens: 5},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 256,
+ Messages: []Message{
+ {Role: "user", Content: "Question 1"},
+ {Role: "assistant", Content: "Answer 1"},
+ {Role: "user", Content: "Question 2"},
+ },
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage: %v", err)
+ }
+ if resp.Content[0].Text != "Continuation." {
+ t.Errorf("unexpected response: %s", resp.Content[0].Text)
+ }
+}
+
+func TestCreateMessage_WithTools(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req MessageRequest
+ json.NewDecoder(r.Body).Decode(&req) //nolint:errcheck
+ if len(req.Tools) != 1 {
+ t.Errorf("want 1 tool, got %d", len(req.Tools))
+ }
+ if req.Tools[0].Name != "get_weather" {
+ t.Errorf("unexpected tool name: %s", req.Tools[0].Name)
+ }
+
+ resp := MessageResponse{
+ ID: "msg-004",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{{Type: "text", Text: "Let me check the weather."}},
+ Model: "claude-3-5-sonnet-20241022",
+ StopReason: "end_turn",
+ Usage: &Usage{InputTokens: 50, OutputTokens: 10},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 256,
+ Messages: []Message{{Role: "user", Content: "What's the weather in Paris?"}},
+ Tools: []Tool{
+ {
+ Name: "get_weather",
+ Description: "Get current weather for a location",
+ InputSchema: map[string]interface{}{
+ "type": "object",
+ "properties": map[string]interface{}{
+ "location": map[string]interface{}{"type": "string"},
+ },
+ "required": []string{"location"},
+ },
+ },
+ },
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage with tools: %v", err)
+ }
+ if resp.ID != "msg-004" {
+ t.Errorf("want ID msg-004, got %s", resp.ID)
+ }
+}
+
+func TestCreateMessage_MaxTokensStop(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ resp := MessageResponse{
+ ID: "msg-005",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{{Type: "text", Text: "This is a truncated"}},
+ Model: "claude-3-5-sonnet-20241022",
+ StopReason: "max_tokens",
+ Usage: &Usage{InputTokens: 10, OutputTokens: 5},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 5,
+ Messages: []Message{{Role: "user", Content: "Tell me a very long story"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage: %v", err)
+ }
+ if resp.StopReason != "max_tokens" {
+ t.Errorf("want stop_reason max_tokens, got %s", resp.StopReason)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Messages API — error paths
+// ---------------------------------------------------------------------------
+
+func TestCreateMessage_Error_Unauthorized(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusUnauthorized)
+ fmt.Fprint(w, `{"type":"error","error":{"type":"authentication_error","message":"Invalid API key"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("invalid-key", WithBaseURL(srv.URL))
+
+ _, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 100,
+ Messages: []Message{{Role: "user", Content: "Hello"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for invalid API key, got nil")
+ }
+ if !strings.Contains(err.Error(), "401") {
+ t.Errorf("expected 401 in error, got: %v", err)
+ }
+}
+
+func TestCreateMessage_Error_ServerError(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprint(w, `{"type":"error","error":{"type":"api_error","message":"Internal server error"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ _, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 100,
+ Messages: []Message{{Role: "user", Content: "Hello"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for server error, got nil")
+ }
+ if !strings.Contains(err.Error(), "500") {
+ t.Errorf("expected 500 in error, got: %v", err)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Content capture control
+// ---------------------------------------------------------------------------
+
+func TestCreateMessage_ContentCapture_Disabled(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ resp := MessageResponse{
+ ID: "priv-msg",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{{Type: "text", Text: "classified response"}},
+ Model: "claude-3-5-sonnet-20241022",
+ StopReason: "end_turn",
+ Usage: &Usage{InputTokens: 5, OutputTokens: 3},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://localhost:4318",
+ DisableTracing: true,
+ DisableMetrics: true,
+ DisableBatch: true,
+ DisablePricingFetch: true,
+ DisableCaptureMessageContent: true,
+ }); err != nil {
+ t.Fatalf("openlit.Init: %v", err)
+ }
+ t.Cleanup(func() { openlit.Shutdown(context.Background()) }) //nolint:errcheck
+
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+ // Response is still returned correctly; spans just won't have content attributes.
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 50,
+ Messages: []Message{{Role: "user", Content: "classified prompt"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage: %v", err)
+ }
+ if resp.ID != "priv-msg" {
+ t.Errorf("want ID priv-msg, got %s", resp.ID)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Custom API version
+// ---------------------------------------------------------------------------
+
+func TestCreateMessage_CustomAPIVersion(t *testing.T) {
+ const customVersion = "2024-01-01"
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Header.Get("anthropic-version") != customVersion {
+ t.Errorf("want anthropic-version=%s, got %s", customVersion, r.Header.Get("anthropic-version"))
+ }
+
+ resp := MessageResponse{
+ ID: "msg-ver",
+ Type: "message",
+ Role: "assistant",
+ Content: []ContentBlock{{Type: "text", Text: "ok"}},
+ Model: "claude-3-5-sonnet-20241022",
+ Usage: &Usage{InputTokens: 5, OutputTokens: 1},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient(
+ "sk-ant-test",
+ WithBaseURL(srv.URL),
+ WithAPIVersion(customVersion),
+ )
+
+ resp, err := client.CreateMessage(context.Background(), MessageRequest{
+ Model: "claude-3-5-sonnet-20241022",
+ MaxTokens: 10,
+ Messages: []Message{{Role: "user", Content: "hi"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateMessage: %v", err)
+ }
+ if resp.ID != "msg-ver" {
+ t.Errorf("want ID msg-ver, got %s", resp.ID)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Streaming
+// ---------------------------------------------------------------------------
+
+func TestCreateMessage_Streaming(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.WriteHeader(http.StatusOK)
+
+ events := []struct{ event, data string }{
+ {"message_start", `{"type":"message_start","message":{"id":"msg-stream","type":"message","role":"assistant","content":[],"model":"claude-3-haiku-20240307","usage":{"input_tokens":10,"output_tokens":0}}}`},
+ {"content_block_start", `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`},
+ {"content_block_delta", `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}`},
+ {"content_block_delta", `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" there"}}`},
+ {"content_block_stop", `{"type":"content_block_stop","index":0}`},
+ {"message_delta", `{"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":2}}`},
+ {"message_stop", `{"type":"message_stop"}`},
+ }
+ for _, ev := range events {
+ fmt.Fprintf(w, "event: %s\ndata: %s\n\n", ev.event, ev.data)
+ w.(http.Flusher).Flush()
+ }
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-ant-test", WithBaseURL(srv.URL))
+
+ stream, err := client.CreateMessageStream(context.Background(), MessageRequest{
+ Model: "claude-3-haiku-20240307",
+ MaxTokens: 40,
+ Messages: []Message{{Role: "user", Content: "Hi"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateMessageStream: %v", err)
+ }
+ defer stream.Close() //nolint:errcheck
+
+ accumulated := ""
+ eventCount := 0
+ for {
+ event, err := stream.Recv()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ t.Fatalf("stream.Recv: %v", err)
+ }
+ if event.Delta != nil && event.Delta.Text != "" {
+ accumulated += event.Delta.Text
+ eventCount++
+ }
+ }
+
+ if eventCount == 0 {
+ t.Error("expected at least one content delta event")
+ }
+ if accumulated != "Hello there" {
+ t.Errorf("want accumulated content %q, got %q", "Hello there", accumulated)
+ }
+}
+
+func TestCreateMessage_Streaming_Error(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusUnauthorized)
+ fmt.Fprint(w, `{"type":"error","error":{"type":"authentication_error","message":"Invalid API key"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("bad-key", WithBaseURL(srv.URL))
+
+ _, err := client.CreateMessageStream(context.Background(), MessageRequest{
+ Model: "claude-3-haiku-20240307",
+ MaxTokens: 40,
+ Messages: []Message{{Role: "user", Content: "Hi"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for streaming with invalid key, got nil")
+ }
+ if !strings.Contains(err.Error(), "401") {
+ t.Errorf("expected 401 in streaming error, got: %v", err)
+ }
+}
diff --git a/sdk/go/instrumentation/anthropic/client.go b/sdk/go/instrumentation/anthropic/client.go
new file mode 100644
index 00000000..0a4a89a0
--- /dev/null
+++ b/sdk/go/instrumentation/anthropic/client.go
@@ -0,0 +1,86 @@
+package anthropic
+
+import (
+ "context"
+ "net/http"
+
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/trace"
+)
+
+// InstrumentedClient wraps an Anthropic client with instrumentation
+type InstrumentedClient struct {
+ httpClient *http.Client
+ tracer trace.Tracer
+ baseURL string
+ apiKey string
+ apiVersion string
+}
+
+// NewClient creates a new instrumented Anthropic client
+func NewClient(apiKey string, opts ...ClientOption) *InstrumentedClient {
+ client := &InstrumentedClient{
+ httpClient: &http.Client{},
+ tracer: otel.Tracer("openlit.anthropic"),
+ baseURL: "https://api.anthropic.com/v1",
+ apiKey: apiKey,
+ apiVersion: "2023-06-01",
+ }
+
+ for _, opt := range opts {
+ opt(client)
+ }
+
+ // Wrap the HTTP client with instrumentation
+ client.httpClient.Transport = NewInstrumentedTransport(
+ client.httpClient.Transport,
+ client.tracer,
+ )
+
+ return client
+}
+
+// ClientOption is a functional option for configuring the client
+type ClientOption func(*InstrumentedClient)
+
+// WithBaseURL sets a custom base URL
+func WithBaseURL(baseURL string) ClientOption {
+ return func(c *InstrumentedClient) {
+ c.baseURL = baseURL
+ }
+}
+
+// WithHTTPClient sets a custom HTTP client
+func WithHTTPClient(httpClient *http.Client) ClientOption {
+ return func(c *InstrumentedClient) {
+ c.httpClient = httpClient
+ }
+}
+
+// WithAPIVersion sets a custom API version
+func WithAPIVersion(apiVersion string) ClientOption {
+ return func(c *InstrumentedClient) {
+ c.apiVersion = apiVersion
+ }
+}
+
+// Instrument wraps an existing Anthropic client library
+func Instrument(client interface{}) interface{} {
+ // Placeholder for wrapping existing client libraries
+ return client
+}
+
+// GetHTTPClient returns the instrumented HTTP client
+func (c *InstrumentedClient) GetHTTPClient() *http.Client {
+ return c.httpClient
+}
+
+// CreateMessage sends a message request to Anthropic
+func (c *InstrumentedClient) CreateMessage(ctx context.Context, req MessageRequest) (*MessageResponse, error) {
+ return c.createMessage(ctx, req, false)
+}
+
+// CreateMessageStream sends a streaming message request to Anthropic
+func (c *InstrumentedClient) CreateMessageStream(ctx context.Context, req MessageRequest) (*MessageStream, error) {
+ return c.createMessageStream(ctx, req)
+}
diff --git a/sdk/go/instrumentation/anthropic/instrumentor.go b/sdk/go/instrumentation/anthropic/instrumentor.go
new file mode 100644
index 00000000..adb92e57
--- /dev/null
+++ b/sdk/go/instrumentation/anthropic/instrumentor.go
@@ -0,0 +1,264 @@
+package anthropic
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/openlit/openlit/sdk/go/helpers"
+ "github.com/openlit/openlit/sdk/go/semconv"
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/codes"
+ "go.opentelemetry.io/otel/metric"
+ "go.opentelemetry.io/otel/trace"
+)
+
+const (
+ defaultServerAddress = "api.anthropic.com"
+ defaultServerPort = 443
+)
+
+// createMessage handles non-streaming message requests
+func (c *InstrumentedClient) createMessage(ctx context.Context, req MessageRequest, stream bool) (result *MessageResponse, retErr error) {
+ tracer := otel.Tracer("openlit.anthropic")
+ meter := otel.Meter("openlit.anthropic")
+ tokenUsageHistogram, _ := meter.Int64Histogram(semconv.GenAIClientTokenUsage,
+ metric.WithDescription("Number of tokens used in GenAI operations"),
+ metric.WithUnit("{token}"))
+ operationDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationDuration,
+ metric.WithDescription("Duration of GenAI operations"),
+ metric.WithUnit("s"))
+
+ spanName := fmt.Sprintf("%s %s", semconv.GenAIOperationTypeChat, req.Model)
+ ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindClient))
+ defer span.End()
+
+ startTime := time.Now()
+ var errType string
+ defer func() {
+ if retErr != nil {
+ if errType == "" {
+ errType = fmt.Sprintf("%T", retErr)
+ }
+ operationDurationHistogram.Record(ctx, time.Since(startTime).Seconds(), metric.WithAttributes(
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.ErrorType, errType),
+ ))
+ }
+ }()
+
+ setRequestAttributes(span, req)
+
+ reqBody, err := json.Marshal(req)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/messages", bytes.NewReader(reqBody))
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("x-api-key", c.apiKey)
+ httpReq.Header.Set("anthropic-version", c.apiVersion)
+
+ httpResp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to execute request: %w", err)
+ }
+ defer httpResp.Body.Close()
+
+ if httpResp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(httpResp.Body)
+ apiErr := fmt.Errorf("API error: %s - %s", httpResp.Status, string(body))
+ helpers.RecordError(span, apiErr)
+ errType = fmt.Sprintf("%d", httpResp.StatusCode)
+ return nil, apiErr
+ }
+
+ var resp MessageResponse
+ if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ duration := time.Since(startTime)
+
+ setResponseAttributes(span, &resp, req.Model, duration)
+
+ if resp.Usage != nil {
+ cost := helpers.CalculateGlobalCost(resp.Model, resp.Usage.InputTokens, resp.Usage.OutputTokens)
+ semconv.SetFloat64Attribute(span, semconv.GenAIUsageCost, cost)
+
+ // Record OTel metrics
+ attrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.GenAIResponseModel, resp.Model),
+ }
+ tokenUsageHistogram.Record(ctx, int64(resp.Usage.InputTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "input"))...))
+ tokenUsageHistogram.Record(ctx, int64(resp.Usage.OutputTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "output"))...))
+ operationDurationHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
+ }
+
+ span.SetStatus(codes.Ok, "")
+ return &resp, nil
+}
+
+// setRequestAttributes sets common request attributes on a span
+func setRequestAttributes(span trace.Span, req MessageRequest) {
+ semconv.SetStringAttribute(span, semconv.GenAIOperationName, semconv.GenAIOperationTypeChat)
+ semconv.SetStringAttribute(span, semconv.GenAISystem, semconv.GenAISystemAnthropic)
+ semconv.SetStringAttribute(span, semconv.GenAIRequestModel, req.Model)
+ semconv.SetStringAttribute(span, semconv.ServerAddress, defaultServerAddress)
+ semconv.SetIntAttribute(span, semconv.ServerPort, defaultServerPort)
+ semconv.SetBoolAttribute(span, semconv.GenAIRequestIsStream, req.Stream)
+
+ if req.MaxTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIRequestMaxTokens, req.MaxTokens)
+ }
+ if req.Temperature > 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIRequestTemperature, req.Temperature)
+ }
+ if req.TopP > 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIRequestTopP, req.TopP)
+ }
+ if req.TopK > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIRequestTopK, req.TopK)
+ }
+ if len(req.StopSequences) > 0 {
+ semconv.SetStringSliceAttribute(span, semconv.GenAIRequestStopSequences, req.StopSequences)
+ }
+
+ if helpers.GetCaptureMessageContent() {
+ // Set system instructions if provided
+ if req.System != "" {
+ semconv.SetStringAttribute(span, semconv.GenAISystemInstructions, req.System)
+ }
+
+ // Convert messages to structured format
+ messages := make([]semconv.Message, 0, len(req.Messages))
+ for _, msg := range req.Messages {
+ m := semconv.Message{Role: msg.Role}
+ switch v := msg.Content.(type) {
+ case string:
+ m.Content = v
+ case []ContentBlock:
+ for _, block := range v {
+ if block.Type == "text" && block.Text != "" {
+ m.Content = block.Text
+ break
+ }
+ }
+ }
+ messages = append(messages, m)
+ }
+ if len(messages) > 0 {
+ semconv.SetMessagesAttribute(span, semconv.GenAIInputMessages, messages) //nolint:errcheck
+ }
+ }
+
+ // Set tool definitions if provided
+ if len(req.Tools) > 0 {
+ tools := make([]semconv.ToolDefinition, len(req.Tools))
+ for i, tool := range req.Tools {
+ tools[i] = semconv.ToolDefinition{Type: "function"}
+ tools[i].Function.Name = tool.Name
+ tools[i].Function.Description = tool.Description
+ tools[i].Function.Parameters = tool.InputSchema
+ }
+ semconv.SetToolDefinitionsAttribute(span, semconv.GenAIToolDefinitions, tools) //nolint:errcheck
+ }
+}
+
+// setResponseAttributes sets common response attributes on a span
+func setResponseAttributes(span trace.Span, resp *MessageResponse, requestModel string, duration time.Duration) {
+ semconv.SetStringAttribute(span, semconv.GenAIResponseID, resp.ID)
+ semconv.SetStringAttribute(span, semconv.GenAIResponseModel, resp.Model)
+
+ if resp.StopReason != "" {
+ semconv.SetStringSliceAttribute(span, semconv.GenAIResponseFinishReasons, []string{resp.StopReason})
+ }
+
+ if resp.Usage != nil {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageInputTokens, resp.Usage.InputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageOutputTokens, resp.Usage.OutputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, resp.Usage.InputTokens+resp.Usage.OutputTokens)
+ if resp.Usage.CacheCreationInputTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheWrite, resp.Usage.CacheCreationInputTokens)
+ }
+ if resp.Usage.CacheReadInputTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheRead, resp.Usage.CacheReadInputTokens)
+ }
+ }
+
+ // Extract tool_use blocks from the model's response
+ toolUseBlocks := make([]ContentBlock, 0)
+ for _, block := range resp.Content {
+ if block.Type == "tool_use" {
+ toolUseBlocks = append(toolUseBlocks, block)
+ }
+ }
+ if len(toolUseBlocks) > 0 {
+ names := make([]string, 0, len(toolUseBlocks))
+ ids := make([]string, 0, len(toolUseBlocks))
+ args := make([]string, 0, len(toolUseBlocks))
+ for _, block := range toolUseBlocks {
+ if block.Name != "" {
+ names = append(names, block.Name)
+ }
+ if block.ID != "" {
+ ids = append(ids, block.ID)
+ }
+ if block.Input != nil {
+ if inputJSON, err := json.Marshal(block.Input); err == nil {
+ args = append(args, string(inputJSON))
+ }
+ }
+ }
+ semconv.SetStringAttribute(span, semconv.GenAIToolName, strings.Join(names, ", "))
+ semconv.SetStringAttribute(span, semconv.GenAIToolCallID, strings.Join(ids, ", "))
+ semconv.SetStringSliceAttribute(span, semconv.GenAIToolCallArguments, args)
+ }
+
+ if helpers.GetCaptureMessageContent() && len(resp.Content) > 0 {
+ completionText := extractContentText(resp.Content)
+ if completionText != "" {
+ semconv.SetStringAttribute(span, semconv.GenAICompletion, completionText)
+ }
+
+ outputMessages := []semconv.Message{
+ {Role: resp.Role, Content: completionText},
+ }
+ semconv.SetMessagesAttribute(span, semconv.GenAIOutputMessages, outputMessages) //nolint:errcheck
+ }
+}
+
+// extractContentText extracts text from content blocks
+func extractContentText(content []ContentBlock) string {
+ var text string
+ for _, block := range content {
+ if block.Type == "text" && block.Text != "" {
+ if text != "" {
+ text += "\n"
+ }
+ text += block.Text
+ }
+ }
+ return text
+}
diff --git a/sdk/go/instrumentation/anthropic/streaming.go b/sdk/go/instrumentation/anthropic/streaming.go
new file mode 100644
index 00000000..23edb20a
--- /dev/null
+++ b/sdk/go/instrumentation/anthropic/streaming.go
@@ -0,0 +1,302 @@
+package anthropic
+
+import (
+ "bufio"
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/openlit/openlit/sdk/go/helpers"
+ "github.com/openlit/openlit/sdk/go/semconv"
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/codes"
+ "go.opentelemetry.io/otel/metric"
+ "go.opentelemetry.io/otel/trace"
+)
+
+// createMessageStream handles streaming message requests
+func (c *InstrumentedClient) createMessageStream(ctx context.Context, req MessageRequest) (*MessageStream, error) {
+ tracer := otel.Tracer("openlit.anthropic")
+
+ spanName := fmt.Sprintf("%s %s", semconv.GenAIOperationTypeChat, req.Model)
+ ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindClient))
+
+ req.Stream = true
+
+ setRequestAttributes(span, req)
+
+ reqBody, err := json.Marshal(req)
+ if err != nil {
+ span.End()
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/messages", bytes.NewReader(reqBody))
+ if err != nil {
+ span.End()
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("x-api-key", c.apiKey)
+ httpReq.Header.Set("anthropic-version", c.apiVersion)
+ httpReq.Header.Set("Accept", "text/event-stream")
+
+ httpResp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ span.End()
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to execute request: %w", err)
+ }
+
+ if httpResp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(httpResp.Body)
+ httpResp.Body.Close()
+ span.End()
+ err := fmt.Errorf("API error: %s - %s", httpResp.Status, string(body))
+ helpers.RecordError(span, err)
+ return nil, err
+ }
+
+ stream := &MessageStream{
+ reader: make(chan MessageStreamEvent, 10),
+ body: httpResp.Body,
+ done: false,
+ }
+
+ go c.readStream(ctx, span, httpResp.Body, stream, req)
+
+ return stream, nil
+}
+
+// readStream reads the SSE stream and sends events to the channel
+func (c *InstrumentedClient) readStream(ctx context.Context, span trace.Span, body io.ReadCloser, stream *MessageStream, req MessageRequest) {
+ defer body.Close()
+ defer close(stream.reader)
+ defer span.End()
+
+ meter := otel.Meter("openlit.anthropic")
+ tokenUsageHistogram, _ := meter.Int64Histogram(semconv.GenAIClientTokenUsage,
+ metric.WithDescription("Number of tokens used in GenAI operations"),
+ metric.WithUnit("{token}"))
+ operationDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationDuration,
+ metric.WithDescription("Duration of GenAI operations"),
+ metric.WithUnit("s"))
+ timeToFirstTokenHistogram, _ := meter.Float64Histogram(semconv.GenAIServerTimeToFirstToken,
+ metric.WithDescription("Time to first token in streaming responses"),
+ metric.WithUnit("s"))
+ timePerOutputTokenHistogram, _ := meter.Float64Histogram(semconv.GenAIServerTimePerOutputToken,
+ metric.WithDescription("Average time between output tokens in streaming responses"),
+ metric.WithUnit("s"))
+ clientTimeToFirstChunkHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationTimeToFirstChunk,
+ metric.WithDescription("Client-side time to first chunk in streaming responses"),
+ metric.WithUnit("s"))
+ clientTimePerOutputChunkHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationTimePerOutputChunk,
+ metric.WithDescription("Per-chunk output token latency observations in streaming responses"),
+ metric.WithUnit("s"))
+ serverRequestDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIServerRequestDuration,
+ metric.WithDescription("Estimated server-side request processing duration"),
+ metric.WithUnit("s"))
+
+ scanner := bufio.NewScanner(body)
+ startTime := time.Now()
+ firstTokenTime := time.Duration(0)
+ var tokenTimestamps []time.Time
+ accumulatedContent := ""
+ var tbtSeconds float64
+
+ var messageID string
+ var messageModel string
+ var stopReason string
+ var inputTokens, outputTokens int
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ if line == "" || strings.HasPrefix(line, ":") {
+ continue
+ }
+
+ if !strings.HasPrefix(line, "event: ") && !strings.HasPrefix(line, "data: ") {
+ continue
+ }
+
+ var eventData string
+
+ if strings.HasPrefix(line, "event: ") {
+ // Read next line for data
+ if scanner.Scan() {
+ dataLine := scanner.Text()
+ if strings.HasPrefix(dataLine, "data: ") {
+ eventData = strings.TrimPrefix(dataLine, "data: ")
+ }
+ }
+ } else if strings.HasPrefix(line, "data: ") {
+ eventData = strings.TrimPrefix(line, "data: ")
+ }
+
+ if eventData == "" {
+ continue
+ }
+
+ var event MessageStreamEvent
+ if err := json.Unmarshal([]byte(eventData), &event); err != nil {
+ stream.err = fmt.Errorf("failed to parse event: %w", err)
+ helpers.RecordError(span, err)
+ return
+ }
+
+ switch event.Type {
+ case "message_start":
+ if event.Message != nil {
+ messageID = event.Message.ID
+ messageModel = event.Message.Model
+ if event.Message.Usage != nil {
+ inputTokens = event.Message.Usage.InputTokens
+ }
+ }
+
+ case "content_block_delta":
+ if event.Delta != nil && event.Delta.Text != "" {
+ now := time.Now()
+ if firstTokenTime == 0 {
+ firstTokenTime = now.Sub(startTime)
+ }
+ tokenTimestamps = append(tokenTimestamps, now)
+ accumulatedContent += event.Delta.Text
+ }
+
+ case "message_delta":
+ if event.Delta != nil && event.Delta.StopReason != "" {
+ stopReason = event.Delta.StopReason
+ }
+ if event.Usage != nil {
+ outputTokens = event.Usage.OutputTokens
+ }
+ }
+
+ select {
+ case stream.reader <- event:
+ case <-ctx.Done():
+ stream.err = ctx.Err()
+ helpers.RecordError(span, ctx.Err())
+ return
+ }
+ }
+
+ if err := scanner.Err(); err != nil {
+ stream.err = fmt.Errorf("scanner error: %w", err)
+ helpers.RecordError(span, err)
+ return
+ }
+
+ duration := time.Since(startTime)
+
+ // Use request model as fallback if stream didn't send model info
+ if messageModel == "" {
+ messageModel = req.Model
+ }
+
+ semconv.SetStringAttribute(span, semconv.GenAIResponseID, messageID)
+ semconv.SetStringAttribute(span, semconv.GenAIResponseModel, messageModel)
+
+ if stopReason != "" {
+ semconv.SetStringSliceAttribute(span, semconv.GenAIResponseFinishReasons, []string{stopReason})
+ }
+
+ if inputTokens > 0 || outputTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageInputTokens, inputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageOutputTokens, outputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, inputTokens+outputTokens)
+
+ cost := helpers.CalculateGlobalCost(messageModel, inputTokens, outputTokens)
+ semconv.SetFloat64Attribute(span, semconv.GenAIUsageCost, cost)
+
+ // Record OTel metrics
+ attrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.GenAIResponseModel, messageModel),
+ }
+ tokenUsageHistogram.Record(ctx, int64(inputTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "input"))...))
+ tokenUsageHistogram.Record(ctx, int64(outputTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "output"))...))
+ operationDurationHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
+ }
+
+ // TTFT — set as span attribute and record histogram metrics
+ if firstTokenTime > 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIServerTimeToFirstToken, firstTokenTime.Seconds())
+ ttftAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ timeToFirstTokenHistogram.Record(ctx, firstTokenTime.Seconds(), metric.WithAttributes(ttftAttrs...))
+ clientTtfcAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ clientTimeToFirstChunkHistogram.Record(ctx, firstTokenTime.Seconds(), metric.WithAttributes(clientTtfcAttrs...))
+ }
+
+ // TBT (time between tokens) — average inter-token latency + per-chunk observations
+ if len(tokenTimestamps) > 1 {
+ total := time.Duration(0)
+ chunkAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ for i := 1; i < len(tokenTimestamps); i++ {
+ dur := tokenTimestamps[i].Sub(tokenTimestamps[i-1])
+ total += dur
+ clientTimePerOutputChunkHistogram.Record(ctx, dur.Seconds(), metric.WithAttributes(chunkAttrs...))
+ }
+ tbt := total / time.Duration(len(tokenTimestamps)-1)
+ tbtSeconds = tbt.Seconds()
+ semconv.SetFloat64Attribute(span, semconv.GenAIServerTimePerOutputToken, tbtSeconds)
+ tbtAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ timePerOutputTokenHistogram.Record(ctx, tbtSeconds, metric.WithAttributes(tbtAttrs...))
+ }
+
+ // Server request duration (estimated: TTFT + TBT_avg × (output_tokens - 1))
+ if firstTokenTime > 0 {
+ outputTokenCount := 0
+ if outputTokens > 0 {
+ outputTokenCount = outputTokens
+ } else {
+ outputTokenCount = len(tokenTimestamps)
+ }
+ serverDur := firstTokenTime.Seconds()
+ if tbtSeconds > 0 && outputTokenCount > 1 {
+ serverDur += tbtSeconds * float64(outputTokenCount-1)
+ }
+ serverAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemAnthropic),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ serverRequestDurationHistogram.Record(ctx, serverDur, metric.WithAttributes(serverAttrs...))
+ }
+
+ if helpers.GetCaptureMessageContent() && accumulatedContent != "" {
+ semconv.SetStringAttribute(span, semconv.GenAICompletion, accumulatedContent)
+ }
+
+ span.SetStatus(codes.Ok, "")
+ stream.done = true
+}
diff --git a/sdk/go/instrumentation/anthropic/transport.go b/sdk/go/instrumentation/anthropic/transport.go
new file mode 100644
index 00000000..05f50f8d
--- /dev/null
+++ b/sdk/go/instrumentation/anthropic/transport.go
@@ -0,0 +1,32 @@
+package anthropic
+
+import (
+ "net/http"
+
+ "go.opentelemetry.io/otel/trace"
+)
+
+// InstrumentedTransport wraps an HTTP transport with OpenTelemetry instrumentation
+type InstrumentedTransport struct {
+ base http.RoundTripper
+ tracer trace.Tracer
+}
+
+// NewInstrumentedTransport creates a new instrumented transport
+func NewInstrumentedTransport(base http.RoundTripper, tracer trace.Tracer) *InstrumentedTransport {
+ if base == nil {
+ base = http.DefaultTransport
+ }
+
+ return &InstrumentedTransport{
+ base: base,
+ tracer: tracer,
+ }
+}
+
+// RoundTrip executes a single HTTP transaction
+func (t *InstrumentedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ // The actual tracing is done at the operation level (messages, etc.)
+ // This transport just passes through the request
+ return t.base.RoundTrip(req)
+}
diff --git a/sdk/go/instrumentation/anthropic/types.go b/sdk/go/instrumentation/anthropic/types.go
new file mode 100644
index 00000000..69126bfb
--- /dev/null
+++ b/sdk/go/instrumentation/anthropic/types.go
@@ -0,0 +1,120 @@
+package anthropic
+
+import "io"
+
+// MessageRequest represents a message request to Anthropic
+type MessageRequest struct {
+ Model string `json:"model"`
+ Messages []Message `json:"messages"`
+ MaxTokens int `json:"max_tokens"`
+ System string `json:"system,omitempty"`
+ Temperature float64 `json:"temperature,omitempty"`
+ TopP float64 `json:"top_p,omitempty"`
+ TopK int `json:"top_k,omitempty"`
+ StopSequences []string `json:"stop_sequences,omitempty"`
+ Stream bool `json:"stream,omitempty"`
+ Tools []Tool `json:"tools,omitempty"`
+ ToolChoice interface{} `json:"tool_choice,omitempty"`
+ Metadata *MessageMetadata `json:"metadata,omitempty"`
+}
+
+// Message represents a message in a conversation
+type Message struct {
+ Role string `json:"role"`
+ Content interface{} `json:"content"` // string or []ContentBlock
+}
+
+// ContentBlock represents a block of content
+type ContentBlock struct {
+ Type string `json:"type"`
+ Text string `json:"text,omitempty"`
+ Source *ImageSource `json:"source,omitempty"`
+ ID string `json:"id,omitempty"`
+ Name string `json:"name,omitempty"`
+ Input interface{} `json:"input,omitempty"`
+}
+
+// ImageSource represents an image source
+type ImageSource struct {
+ Type string `json:"type"`
+ MediaType string `json:"media_type"`
+ Data string `json:"data"`
+}
+
+// Tool represents a tool definition
+type Tool struct {
+ Name string `json:"name"`
+ Description string `json:"description,omitempty"`
+ InputSchema map[string]interface{} `json:"input_schema"`
+}
+
+// MessageMetadata contains metadata about the message
+type MessageMetadata struct {
+ UserID string `json:"user_id,omitempty"`
+}
+
+// MessageResponse represents a response from Anthropic
+type MessageResponse struct {
+ ID string `json:"id"`
+ Type string `json:"type"`
+ Role string `json:"role"`
+ Content []ContentBlock `json:"content"`
+ Model string `json:"model"`
+ StopReason string `json:"stop_reason,omitempty"`
+ StopSequence string `json:"stop_sequence,omitempty"`
+ Usage *Usage `json:"usage"`
+}
+
+// Usage represents token usage information
+type Usage struct {
+ InputTokens int `json:"input_tokens"`
+ OutputTokens int `json:"output_tokens"`
+ CacheCreationInputTokens int `json:"cache_creation_input_tokens,omitempty"`
+ CacheReadInputTokens int `json:"cache_read_input_tokens,omitempty"`
+}
+
+// MessageStreamEvent represents a streaming event
+type MessageStreamEvent struct {
+ Type string `json:"type"`
+ Message *MessageResponse `json:"message,omitempty"`
+ Index int `json:"index,omitempty"`
+ Delta *MessageDelta `json:"delta,omitempty"`
+ Usage *Usage `json:"usage,omitempty"`
+}
+
+// MessageDelta represents incremental changes in a stream
+type MessageDelta struct {
+ Type string `json:"type,omitempty"`
+ Text string `json:"text,omitempty"`
+ StopReason string `json:"stop_reason,omitempty"`
+ StopSequence string `json:"stop_sequence,omitempty"`
+}
+
+// MessageStream represents a streaming response
+type MessageStream struct {
+ reader chan MessageStreamEvent
+ body io.ReadCloser // underlying HTTP response body; Close() drains it
+ err error
+ done bool
+}
+
+// Recv receives the next event from the stream.
+// Returns io.EOF when the stream ends cleanly; returns the underlying error
+// if the stream was aborted.
+func (s *MessageStream) Recv() (MessageStreamEvent, error) {
+ event, ok := <-s.reader
+ if !ok {
+ if s.err != nil {
+ return MessageStreamEvent{}, s.err
+ }
+ return MessageStreamEvent{}, io.EOF
+ }
+ return event, nil
+}
+
+// Close closes the underlying HTTP response body.
+// The goroutine that reads the body will detect the close and exit,
+// after which the reader channel is closed by defer in the goroutine.
+func (s *MessageStream) Close() error {
+ return s.body.Close()
+}
diff --git a/sdk/go/instrumentation/openai/client.go b/sdk/go/instrumentation/openai/client.go
new file mode 100644
index 00000000..34840bf8
--- /dev/null
+++ b/sdk/go/instrumentation/openai/client.go
@@ -0,0 +1,90 @@
+package openai
+
+import (
+ "context"
+ "net/http"
+
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/trace"
+)
+
+// InstrumentedClient wraps any OpenAI client with instrumentation
+type InstrumentedClient struct {
+ httpClient *http.Client
+ tracer trace.Tracer
+ baseURL string
+ apiKey string
+}
+
+// NewClient creates a new instrumented OpenAI client
+func NewClient(apiKey string, opts ...ClientOption) *InstrumentedClient {
+ client := &InstrumentedClient{
+ httpClient: &http.Client{},
+ tracer: otel.Tracer("openlit.openai"),
+ baseURL: "https://api.openai.com/v1",
+ apiKey: apiKey,
+ }
+
+ for _, opt := range opts {
+ opt(client)
+ }
+
+ // Wrap the HTTP client with instrumentation
+ client.httpClient.Transport = NewInstrumentedTransport(
+ client.httpClient.Transport,
+ client.tracer,
+ )
+
+ return client
+}
+
+// ClientOption is a functional option for configuring the client
+type ClientOption func(*InstrumentedClient)
+
+// WithBaseURL sets a custom base URL
+func WithBaseURL(baseURL string) ClientOption {
+ return func(c *InstrumentedClient) {
+ c.baseURL = baseURL
+ }
+}
+
+// WithHTTPClient sets a custom HTTP client
+func WithHTTPClient(httpClient *http.Client) ClientOption {
+ return func(c *InstrumentedClient) {
+ c.httpClient = httpClient
+ }
+}
+
+// Instrument wraps an existing OpenAI client library
+// This works with popular Go OpenAI libraries by wrapping their HTTP transport
+func Instrument(client interface{}) interface{} {
+ // This is a placeholder for the actual instrumentation logic
+ // The implementation will depend on the specific client library being used
+ // For now, we'll return the client as-is
+ return client
+}
+
+// GetHTTPClient returns the instrumented HTTP client for use with other libraries
+func (c *InstrumentedClient) GetHTTPClient() *http.Client {
+ return c.httpClient
+}
+
+// CreateChatCompletion sends a chat completion request
+func (c *InstrumentedClient) CreateChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error) {
+ return c.createChatCompletion(ctx, req, false)
+}
+
+// CreateChatCompletionStream sends a streaming chat completion request
+func (c *InstrumentedClient) CreateChatCompletionStream(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionStream, error) {
+ return c.createChatCompletionStream(ctx, req)
+}
+
+// CreateEmbedding sends an embedding request
+func (c *InstrumentedClient) CreateEmbedding(ctx context.Context, req EmbeddingRequest) (*EmbeddingResponse, error) {
+ return c.createEmbedding(ctx, req)
+}
+
+// CreateImage sends an image generation request
+func (c *InstrumentedClient) CreateImage(ctx context.Context, req ImageRequest) (*ImageResponse, error) {
+ return c.createImage(ctx, req)
+}
diff --git a/sdk/go/instrumentation/openai/instrumentor.go b/sdk/go/instrumentation/openai/instrumentor.go
new file mode 100644
index 00000000..30fadcee
--- /dev/null
+++ b/sdk/go/instrumentation/openai/instrumentor.go
@@ -0,0 +1,444 @@
+package openai
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/openlit/openlit/sdk/go/helpers"
+ "github.com/openlit/openlit/sdk/go/semconv"
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/codes"
+ "go.opentelemetry.io/otel/metric"
+ "go.opentelemetry.io/otel/trace"
+)
+
+const (
+ defaultServerAddress = "api.openai.com"
+ defaultServerPort = 443
+)
+
+// createChatCompletion handles non-streaming chat completions
+func (c *InstrumentedClient) createChatCompletion(ctx context.Context, req ChatCompletionRequest, stream bool) (result *ChatCompletionResponse, retErr error) {
+ tracer := otel.Tracer("openlit.openai")
+ meter := otel.Meter("openlit.openai")
+ tokenUsageHistogram, _ := meter.Int64Histogram(semconv.GenAIClientTokenUsage,
+ metric.WithDescription("Number of tokens used in GenAI operations"),
+ metric.WithUnit("{token}"))
+ operationDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationDuration,
+ metric.WithDescription("Duration of GenAI operations"),
+ metric.WithUnit("s"))
+
+ spanName := fmt.Sprintf("%s %s", semconv.GenAIOperationTypeChat, req.Model)
+ ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindClient))
+ defer span.End()
+
+ startTime := time.Now()
+ var errType string
+ defer func() {
+ if retErr != nil {
+ if errType == "" {
+ errType = fmt.Sprintf("%T", retErr)
+ }
+ operationDurationHistogram.Record(ctx, time.Since(startTime).Seconds(), metric.WithAttributes(
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.ErrorType, errType),
+ ))
+ }
+ }()
+
+ setRequestAttributes(span, req)
+
+ reqBody, err := json.Marshal(req)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/chat/completions", bytes.NewReader(reqBody))
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
+
+ httpResp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to execute request: %w", err)
+ }
+ defer httpResp.Body.Close()
+
+ if httpResp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(httpResp.Body)
+ apiErr := fmt.Errorf("API error: %s - %s", httpResp.Status, string(body))
+ helpers.RecordError(span, apiErr)
+ errType = fmt.Sprintf("%d", httpResp.StatusCode)
+ return nil, apiErr
+ }
+
+ var resp ChatCompletionResponse
+ if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ duration := time.Since(startTime)
+
+ setResponseAttributes(span, &resp, req.Model, duration)
+
+ if resp.Usage != nil {
+ cost := helpers.CalculateGlobalCost(resp.Model, resp.Usage.PromptTokens, resp.Usage.CompletionTokens)
+ semconv.SetFloat64Attribute(span, semconv.GenAIUsageCost, cost)
+
+ // Record OTel metrics
+ attrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.GenAIResponseModel, resp.Model),
+ }
+ tokenUsageHistogram.Record(ctx, int64(resp.Usage.PromptTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "input"))...))
+ tokenUsageHistogram.Record(ctx, int64(resp.Usage.CompletionTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "output"))...))
+ operationDurationHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
+ }
+
+ span.SetStatus(codes.Ok, "")
+ return &resp, nil
+}
+
+// createEmbedding handles embedding requests
+func (c *InstrumentedClient) createEmbedding(ctx context.Context, req EmbeddingRequest) (result *EmbeddingResponse, retErr error) {
+ tracer := otel.Tracer("openlit.openai")
+ meter := otel.Meter("openlit.openai")
+ tokenUsageHistogram, _ := meter.Int64Histogram(semconv.GenAIClientTokenUsage,
+ metric.WithDescription("Number of tokens used in GenAI operations"),
+ metric.WithUnit("{token}"))
+ operationDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationDuration,
+ metric.WithDescription("Duration of GenAI operations"),
+ metric.WithUnit("s"))
+
+ spanName := fmt.Sprintf("%s %s", semconv.GenAIOperationTypeEmbedding, req.Model)
+ ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindClient))
+ defer span.End()
+
+ startTime := time.Now()
+ var errType string
+ defer func() {
+ if retErr != nil {
+ if errType == "" {
+ errType = fmt.Sprintf("%T", retErr)
+ }
+ operationDurationHistogram.Record(ctx, time.Since(startTime).Seconds(), metric.WithAttributes(
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeEmbedding),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.ErrorType, errType),
+ ))
+ }
+ }()
+
+ semconv.SetStringAttribute(span, semconv.GenAIOperationName, semconv.GenAIOperationTypeEmbedding)
+ semconv.SetStringAttribute(span, semconv.GenAISystem, semconv.GenAISystemOpenAI)
+ semconv.SetStringAttribute(span, semconv.GenAIRequestModel, req.Model)
+ semconv.SetStringAttribute(span, semconv.ServerAddress, defaultServerAddress)
+ semconv.SetIntAttribute(span, semconv.ServerPort, defaultServerPort)
+
+ reqBody, err := json.Marshal(req)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/embeddings", bytes.NewReader(reqBody))
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
+
+ httpResp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to execute request: %w", err)
+ }
+ defer httpResp.Body.Close()
+
+ if httpResp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(httpResp.Body)
+ apiErr := fmt.Errorf("API error: %s - %s", httpResp.Status, string(body))
+ helpers.RecordError(span, apiErr)
+ errType = fmt.Sprintf("%d", httpResp.StatusCode)
+ return nil, apiErr
+ }
+
+ var resp EmbeddingResponse
+ if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ duration := time.Since(startTime)
+
+ semconv.SetStringAttribute(span, semconv.GenAIResponseModel, resp.Model)
+ if resp.Usage != nil {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageInputTokens, resp.Usage.PromptTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, resp.Usage.TotalTokens)
+
+ cost := helpers.CalculateGlobalCost(resp.Model, resp.Usage.PromptTokens, 0)
+ semconv.SetFloat64Attribute(span, semconv.GenAIUsageCost, cost)
+
+ attrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeEmbedding),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.GenAIResponseModel, resp.Model),
+ }
+ tokenUsageHistogram.Record(ctx, int64(resp.Usage.PromptTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "input"))...))
+ operationDurationHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
+ }
+
+ span.SetStatus(codes.Ok, "")
+ return &resp, nil
+}
+
+// createImage handles image generation requests
+func (c *InstrumentedClient) createImage(ctx context.Context, req ImageRequest) (result *ImageResponse, retErr error) {
+ tracer := otel.Tracer("openlit.openai")
+ meter := otel.Meter("openlit.openai")
+ operationDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationDuration,
+ metric.WithDescription("Duration of GenAI operations"),
+ metric.WithUnit("s"))
+
+ spanName := fmt.Sprintf("%s %s", semconv.GenAIOperationTypeImage, req.Model)
+ ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindClient))
+ defer span.End()
+
+ startTime := time.Now()
+ var errType string
+ defer func() {
+ if retErr != nil {
+ if errType == "" {
+ errType = fmt.Sprintf("%T", retErr)
+ }
+ operationDurationHistogram.Record(ctx, time.Since(startTime).Seconds(), metric.WithAttributes(
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeImage),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.ErrorType, errType),
+ ))
+ }
+ }()
+
+ semconv.SetStringAttribute(span, semconv.GenAIOperationName, semconv.GenAIOperationTypeImage)
+ semconv.SetStringAttribute(span, semconv.GenAISystem, semconv.GenAISystemOpenAI)
+ semconv.SetStringAttribute(span, semconv.GenAIRequestModel, req.Model)
+ semconv.SetStringAttribute(span, semconv.ServerAddress, defaultServerAddress)
+ semconv.SetIntAttribute(span, semconv.ServerPort, defaultServerPort)
+
+ if helpers.GetCaptureMessageContent() && req.Prompt != "" {
+ semconv.SetStringAttribute(span, semconv.GenAIPrompt, req.Prompt)
+ }
+
+ reqBody, err := json.Marshal(req)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/images/generations", bytes.NewReader(reqBody))
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
+
+ httpResp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to execute request: %w", err)
+ }
+ defer httpResp.Body.Close()
+
+ if httpResp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(httpResp.Body)
+ apiErr := fmt.Errorf("API error: %s - %s", httpResp.Status, string(body))
+ helpers.RecordError(span, apiErr)
+ errType = fmt.Sprintf("%d", httpResp.StatusCode)
+ return nil, apiErr
+ }
+
+ var resp ImageResponse
+ if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to decode response: %w", err)
+ }
+
+ duration := time.Since(startTime)
+ attrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeImage),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ operationDurationHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
+
+ span.SetStatus(codes.Ok, "")
+ return &resp, nil
+}
+
+// setRequestAttributes sets common request attributes on a span
+func setRequestAttributes(span trace.Span, req ChatCompletionRequest) {
+ semconv.SetStringAttribute(span, semconv.GenAIOperationName, semconv.GenAIOperationTypeChat)
+ semconv.SetStringAttribute(span, semconv.GenAISystem, semconv.GenAISystemOpenAI)
+ semconv.SetStringAttribute(span, semconv.GenAIRequestModel, req.Model)
+ semconv.SetStringAttribute(span, semconv.ServerAddress, defaultServerAddress)
+ semconv.SetIntAttribute(span, semconv.ServerPort, defaultServerPort)
+ semconv.SetBoolAttribute(span, semconv.GenAIRequestIsStream, req.Stream)
+
+ if req.User != "" {
+ semconv.SetStringAttribute(span, semconv.GenAIRequestUser, req.User)
+ }
+ if req.Temperature > 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIRequestTemperature, req.Temperature)
+ }
+ if req.TopP > 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIRequestTopP, req.TopP)
+ }
+ if req.MaxTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIRequestMaxTokens, req.MaxTokens)
+ }
+ if req.FrequencyPenalty != 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIRequestFrequencyPenalty, req.FrequencyPenalty)
+ }
+ if req.PresencePenalty != 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIRequestPresencePenalty, req.PresencePenalty)
+ }
+ if len(req.Stop) > 0 {
+ semconv.SetStringSliceAttribute(span, semconv.GenAIRequestStopSequences, req.Stop)
+ }
+ if req.Seed > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIRequestSeed, req.Seed)
+ }
+ if req.N > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIRequestChoiceCount, req.N)
+ }
+ if req.ResponseFormat != nil {
+ semconv.SetStringAttribute(span, semconv.GenAIOutputType, req.ResponseFormat.Type)
+ }
+
+ // Capture message content if enabled
+ if helpers.GetCaptureMessageContent() {
+ messages := make([]semconv.Message, 0, len(req.Messages))
+ for _, msg := range req.Messages {
+ messages = append(messages, semconv.Message{
+ Role: msg.Role,
+ Content: msg.Content,
+ })
+ }
+ if len(messages) > 0 {
+ semconv.SetMessagesAttribute(span, semconv.GenAIInputMessages, messages) //nolint:errcheck
+ }
+ }
+}
+
+// setResponseAttributes sets common response attributes on a span
+func setResponseAttributes(span trace.Span, resp *ChatCompletionResponse, requestModel string, duration time.Duration) {
+ semconv.SetStringAttribute(span, semconv.GenAIResponseID, resp.ID)
+ semconv.SetStringAttribute(span, semconv.GenAIResponseModel, resp.Model)
+
+ if resp.SystemFingerprint != "" {
+ semconv.SetStringAttribute(span, semconv.GenAIOpenAIResponseSystemFingerprint, resp.SystemFingerprint)
+ }
+ if resp.ServiceTier != "" {
+ semconv.SetStringAttribute(span, semconv.GenAIOpenAIResponseServiceTier, resp.ServiceTier)
+ }
+
+ if len(resp.Choices) > 0 {
+ finishReasons := make([]string, 0, len(resp.Choices))
+ for _, choice := range resp.Choices {
+ if choice.FinishReason != "" {
+ finishReasons = append(finishReasons, choice.FinishReason)
+ }
+ }
+ if len(finishReasons) > 0 {
+ semconv.SetStringSliceAttribute(span, semconv.GenAIResponseFinishReasons, finishReasons)
+ }
+
+ // Extract tool calls from the model's response
+ if len(resp.Choices[0].Message.ToolCalls) > 0 {
+ toolCalls := resp.Choices[0].Message.ToolCalls
+ names := make([]string, 0, len(toolCalls))
+ ids := make([]string, 0, len(toolCalls))
+ args := make([]string, 0, len(toolCalls))
+ types := make([]string, 0, len(toolCalls))
+ for _, tc := range toolCalls {
+ if tc.Function.Name != "" {
+ names = append(names, tc.Function.Name)
+ }
+ if tc.ID != "" {
+ ids = append(ids, tc.ID)
+ }
+ if tc.Function.Arguments != "" {
+ args = append(args, tc.Function.Arguments)
+ }
+ if tc.Type != "" {
+ types = append(types, tc.Type)
+ }
+ }
+ semconv.SetStringAttribute(span, semconv.GenAIToolName, strings.Join(names, ", "))
+ semconv.SetStringAttribute(span, semconv.GenAIToolCallID, strings.Join(ids, ", "))
+ semconv.SetStringSliceAttribute(span, semconv.GenAIToolCallArguments, args)
+ semconv.SetStringAttribute(span, semconv.GenAIToolType, strings.Join(types, ", "))
+ }
+
+ // Capture completion content if enabled
+ if helpers.GetCaptureMessageContent() {
+ outputMessages := make([]semconv.Message, 0, len(resp.Choices))
+ for _, choice := range resp.Choices {
+ outputMessages = append(outputMessages, semconv.Message{
+ Role: choice.Message.Role,
+ Content: choice.Message.Content,
+ })
+ }
+ semconv.SetMessagesAttribute(span, semconv.GenAIOutputMessages, outputMessages) //nolint:errcheck
+ }
+ }
+
+ if resp.Usage != nil {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageInputTokens, resp.Usage.PromptTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageOutputTokens, resp.Usage.CompletionTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, resp.Usage.TotalTokens)
+
+ if resp.Usage.CompletionTokensDetails != nil {
+ if resp.Usage.CompletionTokensDetails.ReasoningTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageCompletionTokensDetailsReasoning, resp.Usage.CompletionTokensDetails.ReasoningTokens)
+ }
+ if resp.Usage.CompletionTokensDetails.AudioTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageCompletionTokensDetailsAudio, resp.Usage.CompletionTokensDetails.AudioTokens)
+ }
+ }
+
+ if resp.Usage.PromptTokensDetails != nil {
+ if resp.Usage.PromptTokensDetails.CachedTokens > 0 {
+ semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheRead, resp.Usage.PromptTokensDetails.CachedTokens)
+ }
+ }
+ }
+}
diff --git a/sdk/go/instrumentation/openai/openai_test.go b/sdk/go/instrumentation/openai/openai_test.go
new file mode 100644
index 00000000..e54a070e
--- /dev/null
+++ b/sdk/go/instrumentation/openai/openai_test.go
@@ -0,0 +1,664 @@
+package openai
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ openlit "github.com/openlit/openlit/sdk/go"
+)
+
+// initSDK initialises OpenLIT with all exporters disabled and registers cleanup.
+func initSDK(t *testing.T) {
+ t.Helper()
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://localhost:4318",
+ Environment: "test",
+ ApplicationName: "go-sdk-unit-tests",
+ DisableTracing: true,
+ DisableMetrics: true,
+ DisableBatch: true,
+ DisablePricingFetch: true,
+ }); err != nil {
+ t.Fatalf("openlit.Init: %v", err)
+ }
+ t.Cleanup(func() { openlit.Shutdown(context.Background()) }) //nolint:errcheck
+}
+
+// ---------------------------------------------------------------------------
+// Chat completion — success paths
+// ---------------------------------------------------------------------------
+
+func TestChatCompletion(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/chat/completions" {
+ t.Errorf("unexpected path: %s", r.URL.Path)
+ }
+ if r.Method != http.MethodPost {
+ t.Errorf("unexpected method: %s", r.Method)
+ }
+ if r.Header.Get("Authorization") == "" {
+ t.Error("missing Authorization header")
+ }
+
+ resp := ChatCompletionResponse{
+ ID: "chatcmpl-001",
+ Object: "chat.completion",
+ Model: "gpt-4o",
+ Choices: []ChatCompletionChoice{
+ {
+ Index: 0,
+ Message: ChatMessage{Role: "assistant", Content: "Hello! How can I help?"},
+ FinishReason: "stop",
+ },
+ },
+ Usage: &Usage{PromptTokens: 8, CompletionTokens: 6, TotalTokens: 14},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{{Role: "user", Content: "Hello"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletion: %v", err)
+ }
+ if resp.ID != "chatcmpl-001" {
+ t.Errorf("want ID chatcmpl-001, got %s", resp.ID)
+ }
+ if len(resp.Choices) != 1 {
+ t.Fatalf("want 1 choice, got %d", len(resp.Choices))
+ }
+ if resp.Choices[0].Message.Content != "Hello! How can I help?" {
+ t.Errorf("unexpected content: %s", resp.Choices[0].Message.Content)
+ }
+ if resp.Choices[0].FinishReason != "stop" {
+ t.Errorf("want finish_reason=stop, got %s", resp.Choices[0].FinishReason)
+ }
+ if resp.Usage.PromptTokens != 8 {
+ t.Errorf("want 8 prompt tokens, got %d", resp.Usage.PromptTokens)
+ }
+ if resp.Usage.CompletionTokens != 6 {
+ t.Errorf("want 6 completion tokens, got %d", resp.Usage.CompletionTokens)
+ }
+ if resp.Usage.TotalTokens != 14 {
+ t.Errorf("want 14 total tokens, got %d", resp.Usage.TotalTokens)
+ }
+}
+
+func TestChatCompletion_WithParams(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req ChatCompletionRequest
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ t.Errorf("decode body: %v", err)
+ }
+ if req.Temperature != 0.7 {
+ t.Errorf("want temperature=0.7, got %v", req.Temperature)
+ }
+ if req.MaxTokens != 100 {
+ t.Errorf("want max_tokens=100, got %d", req.MaxTokens)
+ }
+ if req.TopP != 0.9 {
+ t.Errorf("want top_p=0.9, got %v", req.TopP)
+ }
+
+ resp := ChatCompletionResponse{
+ ID: "chatcmpl-002",
+ Model: "gpt-4o-mini",
+ Choices: []ChatCompletionChoice{
+ {Message: ChatMessage{Role: "assistant", Content: "OK!"}, FinishReason: "stop"},
+ },
+ Usage: &Usage{PromptTokens: 10, CompletionTokens: 2, TotalTokens: 12},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o-mini",
+ Messages: []ChatMessage{{Role: "user", Content: "Help"}},
+ Temperature: 0.7,
+ MaxTokens: 100,
+ TopP: 0.9,
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletion: %v", err)
+ }
+ if resp.ID != "chatcmpl-002" {
+ t.Errorf("want ID chatcmpl-002, got %s", resp.ID)
+ }
+}
+
+func TestChatCompletion_WithSystemMessage(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req ChatCompletionRequest
+ json.NewDecoder(r.Body).Decode(&req) //nolint:errcheck
+ if len(req.Messages) != 2 {
+ t.Errorf("want 2 messages (system+user), got %d", len(req.Messages))
+ }
+ if req.Messages[0].Role != "system" {
+ t.Errorf("want first message role=system, got %s", req.Messages[0].Role)
+ }
+
+ resp := ChatCompletionResponse{
+ ID: "chatcmpl-sys",
+ Model: "gpt-4o-mini",
+ Choices: []ChatCompletionChoice{
+ {Message: ChatMessage{Role: "assistant", Content: "15"}, FinishReason: "stop"},
+ },
+ Usage: &Usage{PromptTokens: 15, CompletionTokens: 1, TotalTokens: 16},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o-mini",
+ Messages: []ChatMessage{
+ {Role: "system", Content: "You are a calculator. Reply only with the numeric result."},
+ {Role: "user", Content: "What is 7 + 8?"},
+ },
+ Temperature: 0,
+ MaxTokens: 10,
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletion: %v", err)
+ }
+ if resp.Choices[0].Message.Content != "15" {
+ t.Errorf("want answer=15, got %s", resp.Choices[0].Message.Content)
+ }
+}
+
+func TestChatCompletion_MultiTurn(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req ChatCompletionRequest
+ json.NewDecoder(r.Body).Decode(&req) //nolint:errcheck
+ if len(req.Messages) != 3 {
+ t.Errorf("want 3 messages, got %d", len(req.Messages))
+ }
+ if req.Messages[1].Role != "assistant" {
+ t.Errorf("want messages[1].role=assistant, got %s", req.Messages[1].Role)
+ }
+
+ resp := ChatCompletionResponse{
+ ID: "chatcmpl-003",
+ Model: "gpt-4o",
+ Choices: []ChatCompletionChoice{
+ {Message: ChatMessage{Role: "assistant", Content: "Follow-up answer"}, FinishReason: "stop"},
+ },
+ Usage: &Usage{PromptTokens: 20, CompletionTokens: 5, TotalTokens: 25},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{
+ {Role: "user", Content: "First question"},
+ {Role: "assistant", Content: "First answer"},
+ {Role: "user", Content: "Follow-up question"},
+ },
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletion: %v", err)
+ }
+ if resp.Choices[0].Message.Content != "Follow-up answer" {
+ t.Errorf("unexpected content: %s", resp.Choices[0].Message.Content)
+ }
+}
+
+func TestChatCompletion_WithTools(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ var req ChatCompletionRequest
+ json.NewDecoder(r.Body).Decode(&req) //nolint:errcheck
+ if len(req.Tools) != 1 {
+ t.Errorf("want 1 tool, got %d", len(req.Tools))
+ }
+ if req.Tools[0].Function.Name != "get_weather" {
+ t.Errorf("unexpected tool name: %s", req.Tools[0].Function.Name)
+ }
+
+ resp := ChatCompletionResponse{
+ ID: "chatcmpl-004",
+ Model: "gpt-4o",
+ Choices: []ChatCompletionChoice{
+ {
+ Message: ChatMessage{
+ Role: "assistant",
+ Content: "",
+ ToolCalls: []ToolCall{
+ {
+ ID: "call_abc",
+ Type: "function",
+ Function: struct {
+ Name string `json:"name"`
+ Arguments string `json:"arguments"`
+ }{Name: "get_weather", Arguments: `{"location":"London"}`},
+ },
+ },
+ },
+ FinishReason: "tool_calls",
+ },
+ },
+ Usage: &Usage{PromptTokens: 50, CompletionTokens: 20, TotalTokens: 70},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{{Role: "user", Content: "What's the weather in London?"}},
+ Tools: []Tool{
+ {
+ Type: "function",
+ Function: Function{
+ Name: "get_weather",
+ Description: "Get the current weather for a location",
+ Parameters: map[string]interface{}{
+ "type": "object",
+ "properties": map[string]interface{}{
+ "location": map[string]interface{}{"type": "string"},
+ },
+ "required": []string{"location"},
+ },
+ },
+ },
+ },
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletion with tools: %v", err)
+ }
+ if resp.Choices[0].FinishReason != "tool_calls" {
+ t.Errorf("want finish_reason=tool_calls, got %s", resp.Choices[0].FinishReason)
+ }
+ if len(resp.Choices[0].Message.ToolCalls) != 1 {
+ t.Fatalf("want 1 tool call, got %d", len(resp.Choices[0].Message.ToolCalls))
+ }
+ if resp.Choices[0].Message.ToolCalls[0].Function.Name != "get_weather" {
+ t.Errorf("unexpected tool call name: %s", resp.Choices[0].Message.ToolCalls[0].Function.Name)
+ }
+ if resp.Choices[0].Message.ToolCalls[0].Function.Arguments != `{"location":"London"}` {
+ t.Errorf("unexpected arguments: %s", resp.Choices[0].Message.ToolCalls[0].Function.Arguments)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Chat completion — error paths
+// ---------------------------------------------------------------------------
+
+func TestChatCompletion_Error_Unauthorized(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusUnauthorized)
+ fmt.Fprint(w, `{"error":{"message":"Invalid API key","type":"invalid_request_error"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("invalid-key", WithBaseURL(srv.URL))
+
+ _, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{{Role: "user", Content: "Hello"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for invalid API key, got nil")
+ }
+ if !strings.Contains(err.Error(), "401") {
+ t.Errorf("expected 401 in error, got: %v", err)
+ }
+}
+
+func TestChatCompletion_Error_RateLimit(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusTooManyRequests)
+ fmt.Fprint(w, `{"error":{"message":"Rate limit exceeded","type":"requests"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ _, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{{Role: "user", Content: "Hello"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for rate limit, got nil")
+ }
+ if !strings.Contains(err.Error(), "429") {
+ t.Errorf("expected 429 in error, got: %v", err)
+ }
+}
+
+func TestChatCompletion_Error_ServerError(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusInternalServerError)
+ fmt.Fprint(w, `{"error":{"message":"Internal server error","type":"server_error"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ _, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{{Role: "user", Content: "Hello"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for server error, got nil")
+ }
+ if !strings.Contains(err.Error(), "500") {
+ t.Errorf("expected 500 in error, got: %v", err)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Chat completion — content capture
+// ---------------------------------------------------------------------------
+
+func TestChatCompletion_ContentCapture_Disabled(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ resp := ChatCompletionResponse{
+ ID: "priv-001",
+ Model: "gpt-4o",
+ Choices: []ChatCompletionChoice{
+ {Message: ChatMessage{Role: "assistant", Content: "classified"}, FinishReason: "stop"},
+ },
+ Usage: &Usage{PromptTokens: 5, CompletionTokens: 2, TotalTokens: 7},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ if err := openlit.Init(openlit.Config{
+ OtlpEndpoint: "http://localhost:4318",
+ DisableTracing: true,
+ DisableMetrics: true,
+ DisableBatch: true,
+ DisablePricingFetch: true,
+ DisableCaptureMessageContent: true,
+ }); err != nil {
+ t.Fatalf("openlit.Init: %v", err)
+ }
+ t.Cleanup(func() { openlit.Shutdown(context.Background()) }) //nolint:errcheck
+
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+ // Response is still returned correctly; spans just won't have content attributes.
+ resp, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o",
+ Messages: []ChatMessage{{Role: "user", Content: "classified prompt"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletion: %v", err)
+ }
+ if resp.ID != "priv-001" {
+ t.Errorf("want ID priv-001, got %s", resp.ID)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Streaming
+// ---------------------------------------------------------------------------
+
+func TestChatCompletion_Streaming(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.WriteHeader(http.StatusOK)
+
+ chunks := []string{
+ `{"id":"cmpl-s1","object":"chat.completion.chunk","model":"gpt-4o-mini","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]}`,
+ `{"id":"cmpl-s1","object":"chat.completion.chunk","model":"gpt-4o-mini","choices":[{"index":0,"delta":{"content":" world"},"finish_reason":null}]}`,
+ `{"id":"cmpl-s1","object":"chat.completion.chunk","model":"gpt-4o-mini","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2,"total_tokens":7}}`,
+ }
+ for _, c := range chunks {
+ fmt.Fprintf(w, "data: %s\n\n", c)
+ w.(http.Flusher).Flush()
+ }
+ fmt.Fprint(w, "data: [DONE]\n\n")
+ w.(http.Flusher).Flush()
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ stream, err := client.CreateChatCompletionStream(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o-mini",
+ Messages: []ChatMessage{{Role: "user", Content: "Hi"}},
+ })
+ if err != nil {
+ t.Fatalf("CreateChatCompletionStream: %v", err)
+ }
+ defer stream.Close() //nolint:errcheck
+
+ accumulated := ""
+ chunkCount := 0
+ for {
+ chunk, err := stream.Recv()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ t.Fatalf("stream.Recv: %v", err)
+ }
+ if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
+ accumulated += chunk.Choices[0].Delta.Content
+ chunkCount++
+ }
+ }
+
+ if chunkCount == 0 {
+ t.Error("expected at least one content chunk")
+ }
+ if accumulated != "Hello world" {
+ t.Errorf("want accumulated content %q, got %q", "Hello world", accumulated)
+ }
+}
+
+func TestChatCompletion_Streaming_Error(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusUnauthorized)
+ fmt.Fprint(w, `{"error":{"message":"Invalid API key"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("bad-key", WithBaseURL(srv.URL))
+
+ _, err := client.CreateChatCompletionStream(context.Background(), ChatCompletionRequest{
+ Model: "gpt-4o-mini",
+ Messages: []ChatMessage{{Role: "user", Content: "Hi"}},
+ })
+ if err == nil {
+ t.Fatal("expected error for streaming with invalid key, got nil")
+ }
+ if !strings.Contains(err.Error(), "401") {
+ t.Errorf("expected 401 in streaming error, got: %v", err)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Embeddings
+// ---------------------------------------------------------------------------
+
+func TestEmbedding(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/embeddings" {
+ t.Errorf("unexpected path: %s", r.URL.Path)
+ }
+
+ resp := EmbeddingResponse{
+ Object: "list",
+ Model: "text-embedding-3-small",
+ Data: []EmbeddingData{
+ {Object: "embedding", Embedding: []float64{0.1, 0.2, 0.3, 0.4, 0.5}, Index: 0},
+ },
+ Usage: &EmbeddingUsage{PromptTokens: 3, TotalTokens: 3},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateEmbedding(context.Background(), EmbeddingRequest{
+ Model: "text-embedding-3-small",
+ Input: "Hello, world!",
+ })
+ if err != nil {
+ t.Fatalf("CreateEmbedding: %v", err)
+ }
+ if resp.Model != "text-embedding-3-small" {
+ t.Errorf("want model text-embedding-3-small, got %s", resp.Model)
+ }
+ if len(resp.Data) != 1 {
+ t.Fatalf("want 1 embedding, got %d", len(resp.Data))
+ }
+ if len(resp.Data[0].Embedding) != 5 {
+ t.Errorf("want dim=5, got %d", len(resp.Data[0].Embedding))
+ }
+ if resp.Usage.TotalTokens != 3 {
+ t.Errorf("want 3 total tokens, got %d", resp.Usage.TotalTokens)
+ }
+}
+
+func TestEmbedding_BatchInput(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ resp := EmbeddingResponse{
+ Object: "list",
+ Model: "text-embedding-3-large",
+ Data: []EmbeddingData{
+ {Embedding: []float64{0.1, 0.2}, Index: 0},
+ {Embedding: []float64{0.3, 0.4}, Index: 1},
+ },
+ Usage: &EmbeddingUsage{PromptTokens: 6, TotalTokens: 6},
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateEmbedding(context.Background(), EmbeddingRequest{
+ Model: "text-embedding-3-large",
+ Input: []string{"First sentence", "Second sentence"},
+ })
+ if err != nil {
+ t.Fatalf("CreateEmbedding batch: %v", err)
+ }
+ if len(resp.Data) != 2 {
+ t.Errorf("want 2 embeddings, got %d", len(resp.Data))
+ }
+ if resp.Data[0].Index != 0 || resp.Data[1].Index != 1 {
+ t.Error("unexpected embedding indices")
+ }
+}
+
+func TestEmbedding_Error(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusBadRequest)
+ fmt.Fprint(w, `{"error":{"message":"Invalid model"}}`)
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ _, err := client.CreateEmbedding(context.Background(), EmbeddingRequest{
+ Model: "invalid-model",
+ Input: "test",
+ })
+ if err == nil {
+ t.Fatal("expected error for invalid model, got nil")
+ }
+ if !strings.Contains(err.Error(), "400") {
+ t.Errorf("expected 400 in error, got: %v", err)
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Image generation
+// ---------------------------------------------------------------------------
+
+func TestImageGeneration(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/images/generations" {
+ t.Errorf("unexpected path: %s", r.URL.Path)
+ }
+
+ var req ImageRequest
+ json.NewDecoder(r.Body).Decode(&req) //nolint:errcheck
+ if req.Model != "dall-e-3" {
+ t.Errorf("want model dall-e-3, got %s", req.Model)
+ }
+ if req.Size != "1024x1024" {
+ t.Errorf("want size=1024x1024, got %s", req.Size)
+ }
+
+ resp := ImageResponse{
+ Created: 1700000000,
+ Data: []ImageData{
+ {URL: "https://example.com/img.png", RevisedPrompt: "A serene lake at dawn"},
+ },
+ }
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(resp) //nolint:errcheck
+ }))
+ defer srv.Close()
+
+ initSDK(t)
+ client := NewClient("sk-test", WithBaseURL(srv.URL))
+
+ resp, err := client.CreateImage(context.Background(), ImageRequest{
+ Model: "dall-e-3",
+ Prompt: "A serene lake at dawn",
+ Size: "1024x1024",
+ })
+ if err != nil {
+ t.Fatalf("CreateImage: %v", err)
+ }
+ if len(resp.Data) != 1 {
+ t.Fatalf("want 1 image, got %d", len(resp.Data))
+ }
+ if resp.Data[0].URL != "https://example.com/img.png" {
+ t.Errorf("unexpected URL: %s", resp.Data[0].URL)
+ }
+ if resp.Data[0].RevisedPrompt != "A serene lake at dawn" {
+ t.Errorf("unexpected revised prompt: %s", resp.Data[0].RevisedPrompt)
+ }
+}
diff --git a/sdk/go/instrumentation/openai/streaming.go b/sdk/go/instrumentation/openai/streaming.go
new file mode 100644
index 00000000..1d250e7f
--- /dev/null
+++ b/sdk/go/instrumentation/openai/streaming.go
@@ -0,0 +1,283 @@
+package openai
+
+import (
+ "bufio"
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+ "time"
+
+ "github.com/openlit/openlit/sdk/go/helpers"
+ "github.com/openlit/openlit/sdk/go/semconv"
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/codes"
+ "go.opentelemetry.io/otel/metric"
+ "go.opentelemetry.io/otel/trace"
+)
+
+// createChatCompletionStream handles streaming chat completions
+func (c *InstrumentedClient) createChatCompletionStream(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionStream, error) {
+ tracer := otel.Tracer("openlit.openai")
+
+ spanName := fmt.Sprintf("%s %s", semconv.GenAIOperationTypeChat, req.Model)
+ ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindClient))
+
+ req.Stream = true
+ if req.StreamOptions == nil {
+ req.StreamOptions = &StreamOptions{IncludeUsage: true}
+ }
+
+ setRequestAttributes(span, req)
+
+ reqBody, err := json.Marshal(req)
+ if err != nil {
+ span.End()
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", c.baseURL+"/chat/completions", bytes.NewReader(reqBody))
+ if err != nil {
+ span.End()
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
+ httpReq.Header.Set("Accept", "text/event-stream")
+
+ httpResp, err := c.httpClient.Do(httpReq)
+ if err != nil {
+ span.End()
+ helpers.RecordError(span, err)
+ return nil, fmt.Errorf("failed to execute request: %w", err)
+ }
+
+ if httpResp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(httpResp.Body)
+ httpResp.Body.Close()
+ span.End()
+ err := fmt.Errorf("API error: %s - %s", httpResp.Status, string(body))
+ helpers.RecordError(span, err)
+ return nil, err
+ }
+
+ stream := &ChatCompletionStream{
+ reader: make(chan ChatCompletionChunk, 10),
+ body: httpResp.Body,
+ done: false,
+ }
+
+ go c.readStream(ctx, span, httpResp.Body, stream, req)
+
+ return stream, nil
+}
+
+// readStream reads the SSE stream and sends chunks to the channel
+func (c *InstrumentedClient) readStream(ctx context.Context, span trace.Span, body io.ReadCloser, stream *ChatCompletionStream, req ChatCompletionRequest) {
+ defer body.Close()
+ defer close(stream.reader)
+ defer span.End()
+
+ meter := otel.Meter("openlit.openai")
+ tokenUsageHistogram, _ := meter.Int64Histogram(semconv.GenAIClientTokenUsage,
+ metric.WithDescription("Number of tokens used in GenAI operations"),
+ metric.WithUnit("{token}"))
+ operationDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationDuration,
+ metric.WithDescription("Duration of GenAI operations"),
+ metric.WithUnit("s"))
+ timeToFirstTokenHistogram, _ := meter.Float64Histogram(semconv.GenAIServerTimeToFirstToken,
+ metric.WithDescription("Time to first token in streaming responses"),
+ metric.WithUnit("s"))
+ timePerOutputTokenHistogram, _ := meter.Float64Histogram(semconv.GenAIServerTimePerOutputToken,
+ metric.WithDescription("Average time between output tokens in streaming responses"),
+ metric.WithUnit("s"))
+ clientTimeToFirstChunkHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationTimeToFirstChunk,
+ metric.WithDescription("Client-side time to first chunk in streaming responses"),
+ metric.WithUnit("s"))
+ clientTimePerOutputChunkHistogram, _ := meter.Float64Histogram(semconv.GenAIClientOperationTimePerOutputChunk,
+ metric.WithDescription("Per-chunk output token latency observations in streaming responses"),
+ metric.WithUnit("s"))
+ serverRequestDurationHistogram, _ := meter.Float64Histogram(semconv.GenAIServerRequestDuration,
+ metric.WithDescription("Estimated server-side request processing duration"),
+ metric.WithUnit("s"))
+
+ scanner := bufio.NewScanner(body)
+ startTime := time.Now()
+ firstTokenTime := time.Duration(0)
+ var tokenTimestamps []time.Time
+ var lastChunk *ChatCompletionChunk
+ accumulatedContent := ""
+ var tbtSeconds float64
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ if line == "" || strings.HasPrefix(line, ":") {
+ continue
+ }
+
+ if !strings.HasPrefix(line, "data: ") {
+ continue
+ }
+
+ data := strings.TrimPrefix(line, "data: ")
+
+ if data == "[DONE]" {
+ break
+ }
+
+ var chunk ChatCompletionChunk
+ if err := json.Unmarshal([]byte(data), &chunk); err != nil {
+ stream.err = fmt.Errorf("failed to parse chunk: %w", err)
+ helpers.RecordError(span, err)
+ return
+ }
+
+ if len(chunk.Choices) > 0 && chunk.Choices[0].Delta.Content != "" {
+ now := time.Now()
+ if firstTokenTime == 0 {
+ firstTokenTime = now.Sub(startTime)
+ }
+ tokenTimestamps = append(tokenTimestamps, now)
+ accumulatedContent += chunk.Choices[0].Delta.Content
+ }
+
+ lastChunk = &chunk
+
+ select {
+ case stream.reader <- chunk:
+ case <-ctx.Done():
+ stream.err = ctx.Err()
+ helpers.RecordError(span, ctx.Err())
+ return
+ }
+ }
+
+ if err := scanner.Err(); err != nil {
+ stream.err = fmt.Errorf("scanner error: %w", err)
+ helpers.RecordError(span, err)
+ return
+ }
+
+ duration := time.Since(startTime)
+
+ if lastChunk != nil {
+ semconv.SetStringAttribute(span, semconv.GenAIResponseID, lastChunk.ID)
+ semconv.SetStringAttribute(span, semconv.GenAIResponseModel, lastChunk.Model)
+
+ if lastChunk.SystemFingerprint != "" {
+ semconv.SetStringAttribute(span, semconv.GenAIOpenAIResponseSystemFingerprint, lastChunk.SystemFingerprint)
+ }
+ if lastChunk.ServiceTier != "" {
+ semconv.SetStringAttribute(span, semconv.GenAIOpenAIResponseServiceTier, lastChunk.ServiceTier)
+ }
+
+ if lastChunk.Usage != nil {
+ semconv.SetIntAttribute(span, semconv.GenAIUsageInputTokens, lastChunk.Usage.PromptTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageOutputTokens, lastChunk.Usage.CompletionTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, lastChunk.Usage.TotalTokens)
+
+ cost := helpers.CalculateGlobalCost(lastChunk.Model, lastChunk.Usage.PromptTokens, lastChunk.Usage.CompletionTokens)
+ semconv.SetFloat64Attribute(span, semconv.GenAIUsageCost, cost)
+
+ // Record OTel metrics
+ attrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ attribute.String(semconv.GenAIResponseModel, lastChunk.Model),
+ }
+ tokenUsageHistogram.Record(ctx, int64(lastChunk.Usage.PromptTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "input"))...))
+ tokenUsageHistogram.Record(ctx, int64(lastChunk.Usage.CompletionTokens),
+ metric.WithAttributes(append(attrs, attribute.String(semconv.GenAITokenType, "output"))...))
+ operationDurationHistogram.Record(ctx, duration.Seconds(), metric.WithAttributes(attrs...))
+ }
+
+ if len(lastChunk.Choices) > 0 {
+ finishReasons := make([]string, 0, len(lastChunk.Choices))
+ for _, choice := range lastChunk.Choices {
+ if choice.FinishReason != "" {
+ finishReasons = append(finishReasons, choice.FinishReason)
+ }
+ }
+ if len(finishReasons) > 0 {
+ semconv.SetStringSliceAttribute(span, semconv.GenAIResponseFinishReasons, finishReasons)
+ }
+ }
+ }
+
+ // TTFT — set as span attribute and record histogram metrics
+ if firstTokenTime > 0 {
+ semconv.SetFloat64Attribute(span, semconv.GenAIServerTimeToFirstToken, firstTokenTime.Seconds())
+ ttftAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ timeToFirstTokenHistogram.Record(ctx, firstTokenTime.Seconds(), metric.WithAttributes(ttftAttrs...))
+ clientTtfcAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ clientTimeToFirstChunkHistogram.Record(ctx, firstTokenTime.Seconds(), metric.WithAttributes(clientTtfcAttrs...))
+ }
+
+ // TBT (time between tokens) — average inter-token latency + per-chunk observations
+ if len(tokenTimestamps) > 1 {
+ total := time.Duration(0)
+ chunkAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ for i := 1; i < len(tokenTimestamps); i++ {
+ dur := tokenTimestamps[i].Sub(tokenTimestamps[i-1])
+ total += dur
+ clientTimePerOutputChunkHistogram.Record(ctx, dur.Seconds(), metric.WithAttributes(chunkAttrs...))
+ }
+ tbt := total / time.Duration(len(tokenTimestamps)-1)
+ tbtSeconds = tbt.Seconds()
+ semconv.SetFloat64Attribute(span, semconv.GenAIServerTimePerOutputToken, tbtSeconds)
+ tbtAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ timePerOutputTokenHistogram.Record(ctx, tbtSeconds, metric.WithAttributes(tbtAttrs...))
+ }
+
+ // Server request duration (estimated: TTFT + TBT_avg × (output_tokens - 1))
+ if firstTokenTime > 0 {
+ outputTokenCount := 0
+ if lastChunk != nil && lastChunk.Usage != nil {
+ outputTokenCount = lastChunk.Usage.CompletionTokens
+ }
+ if outputTokenCount == 0 {
+ outputTokenCount = len(tokenTimestamps)
+ }
+ serverDur := firstTokenTime.Seconds()
+ if tbtSeconds > 0 && outputTokenCount > 1 {
+ serverDur += tbtSeconds * float64(outputTokenCount-1)
+ }
+ serverAttrs := []attribute.KeyValue{
+ attribute.String(semconv.GenAISystem, semconv.GenAISystemOpenAI),
+ attribute.String(semconv.GenAIOperationName, semconv.GenAIOperationTypeChat),
+ attribute.String(semconv.GenAIRequestModel, req.Model),
+ }
+ serverRequestDurationHistogram.Record(ctx, serverDur, metric.WithAttributes(serverAttrs...))
+ }
+
+ if helpers.GetCaptureMessageContent() && accumulatedContent != "" {
+ semconv.SetStringAttribute(span, semconv.GenAICompletion, accumulatedContent)
+ }
+
+ span.SetStatus(codes.Ok, "")
+ stream.done = true
+}
diff --git a/sdk/go/instrumentation/openai/transport.go b/sdk/go/instrumentation/openai/transport.go
new file mode 100644
index 00000000..059c5d5d
--- /dev/null
+++ b/sdk/go/instrumentation/openai/transport.go
@@ -0,0 +1,32 @@
+package openai
+
+import (
+ "net/http"
+
+ "go.opentelemetry.io/otel/trace"
+)
+
+// InstrumentedTransport wraps an HTTP transport with OpenTelemetry instrumentation
+type InstrumentedTransport struct {
+ base http.RoundTripper
+ tracer trace.Tracer
+}
+
+// NewInstrumentedTransport creates a new instrumented transport
+func NewInstrumentedTransport(base http.RoundTripper, tracer trace.Tracer) *InstrumentedTransport {
+ if base == nil {
+ base = http.DefaultTransport
+ }
+
+ return &InstrumentedTransport{
+ base: base,
+ tracer: tracer,
+ }
+}
+
+// RoundTrip executes a single HTTP transaction
+func (t *InstrumentedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ // The actual tracing is done at the operation level (chat, embedding, etc.)
+ // This transport just passes through the request
+ return t.base.RoundTrip(req)
+}
diff --git a/sdk/go/instrumentation/openai/types.go b/sdk/go/instrumentation/openai/types.go
new file mode 100644
index 00000000..0b0e70d3
--- /dev/null
+++ b/sdk/go/instrumentation/openai/types.go
@@ -0,0 +1,216 @@
+package openai
+
+import "io"
+
+// ChatCompletionRequest represents a chat completion request
+type ChatCompletionRequest struct {
+ Model string `json:"model"`
+ Messages []ChatMessage `json:"messages"`
+ Temperature float64 `json:"temperature,omitempty"`
+ TopP float64 `json:"top_p,omitempty"`
+ N int `json:"n,omitempty"`
+ Stream bool `json:"stream,omitempty"`
+ Stop []string `json:"stop,omitempty"`
+ MaxTokens int `json:"max_tokens,omitempty"`
+ PresencePenalty float64 `json:"presence_penalty,omitempty"`
+ FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
+ User string `json:"user,omitempty"`
+ Seed int `json:"seed,omitempty"`
+ Tools []Tool `json:"tools,omitempty"`
+ ToolChoice interface{} `json:"tool_choice,omitempty"`
+ ResponseFormat *ResponseFormat `json:"response_format,omitempty"`
+ StreamOptions *StreamOptions `json:"stream_options,omitempty"`
+}
+
+// ChatMessage represents a message in a conversation
+type ChatMessage struct {
+ Role string `json:"role"`
+ Content string `json:"content,omitempty"`
+ Name string `json:"name,omitempty"`
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
+ ToolCallID string `json:"tool_call_id,omitempty"`
+}
+
+// ToolCall represents a tool/function call
+type ToolCall struct {
+ ID string `json:"id"`
+ Type string `json:"type"`
+ Function struct {
+ Name string `json:"name"`
+ Arguments string `json:"arguments"`
+ } `json:"function"`
+}
+
+// Tool represents a tool/function definition
+type Tool struct {
+ Type string `json:"type"`
+ Function Function `json:"function"`
+}
+
+// Function represents a function definition
+type Function struct {
+ Name string `json:"name"`
+ Description string `json:"description,omitempty"`
+ Parameters map[string]interface{} `json:"parameters,omitempty"`
+}
+
+// ResponseFormat specifies the format of the response
+type ResponseFormat struct {
+ Type string `json:"type"` // "text" or "json_object"
+}
+
+// StreamOptions configures streaming behavior
+type StreamOptions struct {
+ IncludeUsage bool `json:"include_usage,omitempty"`
+}
+
+// ChatCompletionResponse represents a chat completion response
+type ChatCompletionResponse struct {
+ ID string `json:"id"`
+ Object string `json:"object"`
+ Created int64 `json:"created"`
+ Model string `json:"model"`
+ Choices []ChatCompletionChoice `json:"choices"`
+ Usage *Usage `json:"usage,omitempty"`
+ SystemFingerprint string `json:"system_fingerprint,omitempty"`
+ ServiceTier string `json:"service_tier,omitempty"`
+}
+
+// ChatCompletionChoice represents a completion choice
+type ChatCompletionChoice struct {
+ Index int `json:"index"`
+ Message ChatMessage `json:"message"`
+ FinishReason string `json:"finish_reason"`
+ Logprobs interface{} `json:"logprobs,omitempty"`
+}
+
+// ChatCompletionChunk represents a streaming chunk
+type ChatCompletionChunk struct {
+ ID string `json:"id"`
+ Object string `json:"object"`
+ Created int64 `json:"created"`
+ Model string `json:"model"`
+ Choices []ChatCompletionChunkChoice `json:"choices"`
+ Usage *Usage `json:"usage,omitempty"`
+ SystemFingerprint string `json:"system_fingerprint,omitempty"`
+ ServiceTier string `json:"service_tier,omitempty"`
+}
+
+// ChatCompletionChunkChoice represents a streaming choice
+type ChatCompletionChunkChoice struct {
+ Index int `json:"index"`
+ Delta ChatMessageDelta `json:"delta"`
+ FinishReason string `json:"finish_reason,omitempty"`
+ Logprobs interface{} `json:"logprobs,omitempty"`
+}
+
+// ChatMessageDelta represents incremental message content
+type ChatMessageDelta struct {
+ Role string `json:"role,omitempty"`
+ Content string `json:"content,omitempty"`
+ ToolCalls []ToolCall `json:"tool_calls,omitempty"`
+}
+
+// Usage represents token usage information
+type Usage struct {
+ PromptTokens int `json:"prompt_tokens"`
+ CompletionTokens int `json:"completion_tokens"`
+ TotalTokens int `json:"total_tokens"`
+ CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details,omitempty"`
+ PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details,omitempty"`
+}
+
+// CompletionTokensDetails provides detailed completion token breakdown
+type CompletionTokensDetails struct {
+ ReasoningTokens int `json:"reasoning_tokens,omitempty"`
+ AudioTokens int `json:"audio_tokens,omitempty"`
+}
+
+// PromptTokensDetails provides detailed prompt token breakdown
+type PromptTokensDetails struct {
+ CachedTokens int `json:"cached_tokens,omitempty"`
+}
+
+// EmbeddingRequest represents an embedding request
+type EmbeddingRequest struct {
+ Model string `json:"model"`
+ Input interface{} `json:"input"` // string or []string
+ EncodingFormat string `json:"encoding_format,omitempty"`
+ Dimensions int `json:"dimensions,omitempty"`
+ User string `json:"user,omitempty"`
+}
+
+// EmbeddingResponse represents an embedding response
+type EmbeddingResponse struct {
+ Object string `json:"object"`
+ Data []EmbeddingData `json:"data"`
+ Model string `json:"model"`
+ Usage *EmbeddingUsage `json:"usage"`
+}
+
+// EmbeddingData represents a single embedding
+type EmbeddingData struct {
+ Object string `json:"object"`
+ Embedding []float64 `json:"embedding"`
+ Index int `json:"index"`
+}
+
+// EmbeddingUsage represents embedding token usage
+type EmbeddingUsage struct {
+ PromptTokens int `json:"prompt_tokens"`
+ TotalTokens int `json:"total_tokens"`
+}
+
+// ImageRequest represents an image generation request
+type ImageRequest struct {
+ Model string `json:"model,omitempty"`
+ Prompt string `json:"prompt"`
+ N int `json:"n,omitempty"`
+ Size string `json:"size,omitempty"`
+ Quality string `json:"quality,omitempty"`
+ ResponseFormat string `json:"response_format,omitempty"`
+ Style string `json:"style,omitempty"`
+ User string `json:"user,omitempty"`
+}
+
+// ImageResponse represents an image generation response
+type ImageResponse struct {
+ Created int64 `json:"created"`
+ Data []ImageData `json:"data"`
+}
+
+// ImageData represents a single generated image
+type ImageData struct {
+ URL string `json:"url,omitempty"`
+ B64JSON string `json:"b64_json,omitempty"`
+ RevisedPrompt string `json:"revised_prompt,omitempty"`
+}
+
+// ChatCompletionStream represents a streaming response
+type ChatCompletionStream struct {
+ reader chan ChatCompletionChunk
+ body io.ReadCloser // underlying HTTP response body; Close() drains it
+ err error
+ done bool
+}
+
+// Recv receives the next chunk from the stream.
+// Returns io.EOF when the stream ends cleanly; returns the underlying error
+// if the stream was aborted.
+func (s *ChatCompletionStream) Recv() (ChatCompletionChunk, error) {
+ chunk, ok := <-s.reader
+ if !ok {
+ if s.err != nil {
+ return ChatCompletionChunk{}, s.err
+ }
+ return ChatCompletionChunk{}, io.EOF
+ }
+ return chunk, nil
+}
+
+// Close closes the underlying HTTP response body.
+// The goroutine that reads the body will detect the close and exit,
+// after which the reader channel is closed by defer in the goroutine.
+func (s *ChatCompletionStream) Close() error {
+ return s.body.Close()
+}
diff --git a/sdk/go/integration_test.go b/sdk/go/integration_test.go
new file mode 100644
index 00000000..acb205f7
--- /dev/null
+++ b/sdk/go/integration_test.go
@@ -0,0 +1,109 @@
+package openlit
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/openlit/openlit/sdk/go/instrumentation/anthropic"
+ "github.com/openlit/openlit/sdk/go/instrumentation/openai"
+)
+
+func TestSDKInitialization(t *testing.T) {
+ err := Init(Config{
+ OtlpEndpoint: "http://localhost:4318",
+ Environment: "test",
+ ApplicationName: "test-app",
+ DisableTracing: true,
+ DisableMetrics: true,
+ })
+ if err != nil {
+ t.Fatalf("Failed to initialize: %v", err)
+ }
+
+ if !IsInitialized() {
+ t.Error("Expected SDK to be initialized")
+ }
+
+ cfg := GetConfig()
+ if cfg == nil {
+ t.Fatal("Expected config to be available")
+ }
+
+ if cfg.Environment != "test" {
+ t.Errorf("Expected environment 'test', got '%s'", cfg.Environment)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+
+ err = Shutdown(ctx)
+ if err != nil {
+ t.Errorf("Failed to shutdown: %v", err)
+ }
+
+ if IsInitialized() {
+ t.Error("Expected SDK to be shut down")
+ }
+}
+
+func TestSDKDefaultCaptureMessageContent(t *testing.T) {
+ err := Init(Config{
+ OtlpEndpoint: "http://localhost:4318",
+ DisableTracing: true,
+ DisableMetrics: true,
+ })
+ if err != nil {
+ t.Fatalf("Failed to initialize: %v", err)
+ }
+ defer Shutdown(context.Background()) //nolint:errcheck
+
+ cfg := GetConfig()
+ // DisableCaptureMessageContent defaults to false, meaning content IS captured
+ if cfg.DisableCaptureMessageContent {
+ t.Error("Expected DisableCaptureMessageContent to default to false (content capture enabled)")
+ }
+}
+
+func TestMultipleInitialization(t *testing.T) {
+ err := Init(Config{
+ OtlpEndpoint: "http://localhost:4318",
+ DisableTracing: true,
+ DisableMetrics: true,
+ })
+ if err != nil {
+ t.Fatalf("First init failed: %v", err)
+ }
+ defer Shutdown(context.Background()) //nolint:errcheck
+
+ // Second init should fail
+ err = Init(Config{
+ OtlpEndpoint: "http://localhost:4318",
+ })
+ if err == nil {
+ t.Error("Expected error on second initialization")
+ }
+}
+
+func TestInstrumentationIntegration(t *testing.T) {
+ err := Init(Config{
+ OtlpEndpoint: "http://localhost:4318",
+ DisableTracing: true,
+ DisableMetrics: true,
+ })
+ if err != nil {
+ t.Fatalf("Failed to initialize: %v", err)
+ }
+ defer Shutdown(context.Background()) //nolint:errcheck
+
+ // Create clients (no actual API calls made)
+ openaiClient := openai.NewClient("test-key")
+ if openaiClient == nil {
+ t.Error("Failed to create OpenAI client")
+ }
+
+ anthropicClient := anthropic.NewClient("test-key")
+ if anthropicClient == nil {
+ t.Error("Failed to create Anthropic client")
+ }
+}
diff --git a/sdk/go/openlit.go b/sdk/go/openlit.go
new file mode 100644
index 00000000..5e473f86
--- /dev/null
+++ b/sdk/go/openlit.go
@@ -0,0 +1,254 @@
+// Package openlit provides OpenTelemetry-native observability for LLM applications in Go.
+// It offers automatic instrumentation for popular LLM providers like OpenAI and Anthropic.
+package openlit
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "sync"
+
+ "github.com/openlit/openlit/sdk/go/helpers"
+ "go.opentelemetry.io/otel"
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
+ "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
+ "go.opentelemetry.io/otel/propagation"
+ "go.opentelemetry.io/otel/sdk/metric"
+ "go.opentelemetry.io/otel/sdk/resource"
+ "go.opentelemetry.io/otel/sdk/trace"
+ semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
+)
+
+var (
+ globalConfig *Config
+ globalShutdown func(context.Context) error
+ initMutex sync.Mutex
+ isInitialized bool
+ tracerProvider *trace.TracerProvider
+ meterProvider *metric.MeterProvider
+)
+
+// Init initializes the OpenLIT SDK with the provided configuration.
+// This should be called once at application startup.
+//
+// Example:
+//
+// err := openlit.Init(openlit.Config{
+// OtlpEndpoint: "http://127.0.0.1:4318",
+// Environment: "production",
+// ApplicationName: "my-go-app",
+// })
+// if err != nil {
+// log.Fatalf("Failed to initialize OpenLIT: %v", err)
+// }
+// defer openlit.Shutdown(context.Background())
+func Init(cfg Config) error {
+ initMutex.Lock()
+ defer initMutex.Unlock()
+
+ if isInitialized {
+ return fmt.Errorf("OpenLIT is already initialized")
+ }
+
+ // Set default values
+ cfg.setDefaults()
+ globalConfig = &cfg
+
+ // Initialize pricing cache with user config
+ customPricing := make(map[string]helpers.PricingInfo)
+ for model, p := range cfg.PricingInfo {
+ customPricing[model] = helpers.PricingInfo{
+ InputCostPerToken: p.InputCostPerToken,
+ OutputCostPerToken: p.OutputCostPerToken,
+ }
+ }
+ helpers.InitGlobalPricingCache(cfg.PricingEndpoint, cfg.DisablePricingFetch, customPricing)
+
+ // Propagate capture_message_content setting to helpers (inverted: disable=false means capture=true)
+ helpers.SetCaptureMessageContent(!cfg.DisableCaptureMessageContent)
+
+ // Create resource
+ res, err := newResource(cfg)
+ if err != nil {
+ return fmt.Errorf("failed to create resource: %w", err)
+ }
+
+ // Initialize tracer provider
+ if !cfg.DisableTracing {
+ tp, err := newTracerProvider(res, cfg)
+ if err != nil {
+ return fmt.Errorf("failed to create tracer provider: %w", err)
+ }
+ tracerProvider = tp
+ otel.SetTracerProvider(tp)
+ }
+
+ // Initialize meter provider
+ if !cfg.DisableMetrics {
+ mp, err := newMeterProvider(res, cfg)
+ if err != nil {
+ return fmt.Errorf("failed to create meter provider: %w", err)
+ }
+ meterProvider = mp
+ otel.SetMeterProvider(mp)
+ }
+
+ // Set global propagator
+ otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
+ propagation.TraceContext{},
+ propagation.Baggage{},
+ ))
+
+ // Set up shutdown function
+ globalShutdown = func(ctx context.Context) error {
+ var err error
+ if tracerProvider != nil {
+ if shutdownErr := tracerProvider.Shutdown(ctx); shutdownErr != nil {
+ err = shutdownErr
+ }
+ }
+ if meterProvider != nil {
+ if shutdownErr := meterProvider.Shutdown(ctx); shutdownErr != nil {
+ if err != nil {
+ err = fmt.Errorf("%v; %w", err, shutdownErr)
+ } else {
+ err = shutdownErr
+ }
+ }
+ }
+ return err
+ }
+
+ isInitialized = true
+ log.Printf("OpenLIT initialized successfully (Environment: %s, Application: %s)", cfg.Environment, cfg.ApplicationName)
+
+ return nil
+}
+
+// Shutdown gracefully shuts down the OpenLIT SDK.
+// It should be called before application exit to ensure all telemetry data is flushed.
+func Shutdown(ctx context.Context) error {
+ initMutex.Lock()
+ defer initMutex.Unlock()
+
+ if !isInitialized {
+ return nil
+ }
+
+ if globalShutdown != nil {
+ err := globalShutdown(ctx)
+ if err != nil {
+ return fmt.Errorf("failed to shutdown OpenLIT: %w", err)
+ }
+ }
+
+ isInitialized = false
+ globalConfig = nil
+ tracerProvider = nil
+ meterProvider = nil
+ globalShutdown = nil
+
+ log.Println("OpenLIT shut down successfully")
+ return nil
+}
+
+// GetConfig returns the current global configuration.
+// Returns nil if OpenLIT is not initialized.
+func GetConfig() *Config {
+ initMutex.Lock()
+ defer initMutex.Unlock()
+ return globalConfig
+}
+
+// IsInitialized returns whether OpenLIT has been initialized.
+func IsInitialized() bool {
+ initMutex.Lock()
+ defer initMutex.Unlock()
+ return isInitialized
+}
+
+// newResource creates a new resource with the application metadata
+func newResource(cfg Config) (*resource.Resource, error) {
+ attrs := []resource.Option{
+ resource.WithAttributes(
+ semconv.ServiceNameKey.String(cfg.ApplicationName),
+ semconv.DeploymentEnvironmentKey.String(cfg.Environment),
+ attribute.String("openlit.sdk.version", Version),
+ ),
+ }
+
+ if cfg.ServiceVersion != "" {
+ attrs = append(attrs, resource.WithAttributes(
+ semconv.ServiceVersionKey.String(cfg.ServiceVersion),
+ ))
+ }
+
+ return resource.New(
+ context.Background(),
+ append(attrs, resource.WithTelemetrySDK())...,
+ )
+}
+
+// newTracerProvider creates a new tracer provider with OTLP exporter
+func newTracerProvider(res *resource.Resource, cfg Config) (*trace.TracerProvider, error) {
+ // WithEndpointURL accepts the full URL including scheme, avoiding the
+ // host-only limitation of WithEndpoint + WithInsecure.
+ opts := []otlptracehttp.Option{
+ otlptracehttp.WithEndpointURL(cfg.OtlpEndpoint),
+ otlptracehttp.WithTimeout(cfg.TraceExporterTimeout),
+ }
+
+ if len(cfg.OtlpHeaders) > 0 {
+ opts = append(opts, otlptracehttp.WithHeaders(cfg.OtlpHeaders))
+ }
+
+ exporter, err := otlptracehttp.New(context.Background(), opts...)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create trace exporter: %w", err)
+ }
+
+ var spanProcessor trace.SpanProcessor
+ if cfg.DisableBatch {
+ spanProcessor = trace.NewSimpleSpanProcessor(exporter)
+ } else {
+ spanProcessor = trace.NewBatchSpanProcessor(exporter)
+ }
+
+ tp := trace.NewTracerProvider(
+ trace.WithResource(res),
+ trace.WithSpanProcessor(spanProcessor),
+ trace.WithSampler(trace.AlwaysSample()),
+ )
+
+ return tp, nil
+}
+
+// newMeterProvider creates a new meter provider with OTLP exporter
+func newMeterProvider(res *resource.Resource, cfg Config) (*metric.MeterProvider, error) {
+ opts := []otlpmetrichttp.Option{
+ otlpmetrichttp.WithEndpointURL(cfg.OtlpEndpoint),
+ otlpmetrichttp.WithTimeout(cfg.MetricExporterTimeout),
+ }
+
+ if len(cfg.OtlpHeaders) > 0 {
+ opts = append(opts, otlpmetrichttp.WithHeaders(cfg.OtlpHeaders))
+ }
+
+ exporter, err := otlpmetrichttp.New(context.Background(), opts...)
+ if err != nil {
+ return nil, fmt.Errorf("failed to create metric exporter: %w", err)
+ }
+
+ reader := metric.NewPeriodicReader(
+ exporter,
+ metric.WithInterval(cfg.MetricExportInterval),
+ )
+
+ mp := metric.NewMeterProvider(
+ metric.WithResource(res),
+ metric.WithReader(reader),
+ )
+
+ return mp, nil
+}
diff --git a/sdk/go/semconv/attributes.go b/sdk/go/semconv/attributes.go
new file mode 100644
index 00000000..a669c446
--- /dev/null
+++ b/sdk/go/semconv/attributes.go
@@ -0,0 +1,177 @@
+package semconv
+
+import (
+ "encoding/json"
+
+ "go.opentelemetry.io/otel/attribute"
+ "go.opentelemetry.io/otel/trace"
+)
+
+// SetStringAttribute sets a string attribute on a span if value is not empty
+func SetStringAttribute(span trace.Span, key, value string) {
+ if value != "" {
+ span.SetAttributes(attribute.String(key, value))
+ }
+}
+
+// SetIntAttribute sets an int attribute on a span
+func SetIntAttribute(span trace.Span, key string, value int) {
+ span.SetAttributes(attribute.Int(key, value))
+}
+
+// SetInt64Attribute sets an int64 attribute on a span
+func SetInt64Attribute(span trace.Span, key string, value int64) {
+ span.SetAttributes(attribute.Int64(key, value))
+}
+
+// SetFloat64Attribute sets a float64 attribute on a span
+func SetFloat64Attribute(span trace.Span, key string, value float64) {
+ span.SetAttributes(attribute.Float64(key, value))
+}
+
+// SetBoolAttribute sets a bool attribute on a span
+func SetBoolAttribute(span trace.Span, key string, value bool) {
+ span.SetAttributes(attribute.Bool(key, value))
+}
+
+// SetStringSliceAttribute sets a string slice attribute on a span if not empty
+func SetStringSliceAttribute(span trace.Span, key string, values []string) {
+ if len(values) > 0 {
+ span.SetAttributes(attribute.StringSlice(key, values))
+ }
+}
+
+// SetJSONAttribute sets a JSON-encoded attribute on a span
+func SetJSONAttribute(span trace.Span, key string, value interface{}) error {
+ if value == nil {
+ return nil
+ }
+
+ jsonBytes, err := json.Marshal(value)
+ if err != nil {
+ return err
+ }
+
+ span.SetAttributes(attribute.String(key, string(jsonBytes)))
+ return nil
+}
+
+// Message represents a chat message structure
+type Message struct {
+ Role string `json:"role"`
+ Content string `json:"content,omitempty"`
+ Parts []MessagePart `json:"parts,omitempty"`
+}
+
+// MessagePart represents a part of a message (text, image, etc.)
+type MessagePart struct {
+ Type string `json:"type"`
+ Content string `json:"content,omitempty"`
+ Text string `json:"text,omitempty"`
+}
+
+// ToolCall represents a tool/function call
+type ToolCall struct {
+ ID string `json:"id,omitempty"`
+ Type string `json:"type"`
+ Function struct {
+ Name string `json:"name"`
+ Arguments string `json:"arguments"`
+ } `json:"function,omitempty"`
+}
+
+// ToolDefinition represents a tool/function definition
+type ToolDefinition struct {
+ Type string `json:"type"`
+ Function struct {
+ Name string `json:"name"`
+ Description string `json:"description,omitempty"`
+ Parameters map[string]interface{} `json:"parameters,omitempty"`
+ } `json:"function,omitempty"`
+}
+
+// SetMessagesAttribute sets structured messages as JSON attribute
+func SetMessagesAttribute(span trace.Span, key string, messages []Message) error {
+ if len(messages) == 0 {
+ return nil
+ }
+ return SetJSONAttribute(span, key, messages)
+}
+
+// SetToolCallsAttribute sets tool calls as JSON attribute
+func SetToolCallsAttribute(span trace.Span, key string, toolCalls []ToolCall) error {
+ if len(toolCalls) == 0 {
+ return nil
+ }
+ return SetJSONAttribute(span, key, toolCalls)
+}
+
+// SetToolDefinitionsAttribute sets tool definitions as JSON attribute
+func SetToolDefinitionsAttribute(span trace.Span, key string, tools []ToolDefinition) error {
+ if len(tools) == 0 {
+ return nil
+ }
+ return SetJSONAttribute(span, key, tools)
+}
+
+// AttributeBuilder provides a fluent interface for building attributes
+type AttributeBuilder struct {
+ attributes []attribute.KeyValue
+}
+
+// NewAttributeBuilder creates a new attribute builder
+func NewAttributeBuilder() *AttributeBuilder {
+ return &AttributeBuilder{
+ attributes: make([]attribute.KeyValue, 0),
+ }
+}
+
+// String adds a string attribute
+func (ab *AttributeBuilder) String(key, value string) *AttributeBuilder {
+ if value != "" {
+ ab.attributes = append(ab.attributes, attribute.String(key, value))
+ }
+ return ab
+}
+
+// Int adds an int attribute
+func (ab *AttributeBuilder) Int(key string, value int) *AttributeBuilder {
+ ab.attributes = append(ab.attributes, attribute.Int(key, value))
+ return ab
+}
+
+// Int64 adds an int64 attribute
+func (ab *AttributeBuilder) Int64(key string, value int64) *AttributeBuilder {
+ ab.attributes = append(ab.attributes, attribute.Int64(key, value))
+ return ab
+}
+
+// Float64 adds a float64 attribute
+func (ab *AttributeBuilder) Float64(key string, value float64) *AttributeBuilder {
+ ab.attributes = append(ab.attributes, attribute.Float64(key, value))
+ return ab
+}
+
+// Bool adds a bool attribute
+func (ab *AttributeBuilder) Bool(key string, value bool) *AttributeBuilder {
+ ab.attributes = append(ab.attributes, attribute.Bool(key, value))
+ return ab
+}
+
+// StringSlice adds a string slice attribute
+func (ab *AttributeBuilder) StringSlice(key string, values []string) *AttributeBuilder {
+ if len(values) > 0 {
+ ab.attributes = append(ab.attributes, attribute.StringSlice(key, values))
+ }
+ return ab
+}
+
+// Build returns the built attributes
+func (ab *AttributeBuilder) Build() []attribute.KeyValue {
+ return ab.attributes
+}
+
+// SetAttributes sets all attributes on a span
+func (ab *AttributeBuilder) SetAttributes(span trace.Span) {
+ span.SetAttributes(ab.attributes...)
+}
diff --git a/sdk/go/semconv/genai.go b/sdk/go/semconv/genai.go
new file mode 100644
index 00000000..1f30a1a4
--- /dev/null
+++ b/sdk/go/semconv/genai.go
@@ -0,0 +1,217 @@
+// Package semconv provides semantic conventions for Gen AI observability.
+// These constants align with OpenTelemetry semantic conventions for generative AI.
+package semconv
+
+// Operation Types
+const (
+ // GenAIOperationName is the name of the operation being performed
+ GenAIOperationName = "gen_ai.operation.name"
+
+ // GenAIOperationTypeChat represents a chat completion operation
+ GenAIOperationTypeChat = "chat"
+ // GenAIOperationTypeCompletion represents a text completion operation
+ GenAIOperationTypeCompletion = "completion"
+ // GenAIOperationTypeEmbedding represents an embedding operation
+ GenAIOperationTypeEmbedding = "embedding"
+ // GenAIOperationTypeImage represents an image generation operation
+ GenAIOperationTypeImage = "image"
+ // GenAIOperationTypeAudio represents an audio generation operation
+ GenAIOperationTypeAudio = "audio"
+)
+
+// System Identifiers
+const (
+ // GenAISystem identifies the AI system being used
+ GenAISystem = "gen_ai.system"
+
+ // GenAISystemOpenAI represents OpenAI system
+ GenAISystemOpenAI = "openai"
+ // GenAISystemAnthropic represents Anthropic system
+ GenAISystemAnthropic = "anthropic"
+)
+
+// Request Attributes
+const (
+ // GenAIRequestModel is the model name being requested
+ GenAIRequestModel = "gen_ai.request.model"
+ // GenAIRequestTemperature is the temperature parameter
+ GenAIRequestTemperature = "gen_ai.request.temperature"
+ // GenAIRequestTopP is the top_p parameter
+ GenAIRequestTopP = "gen_ai.request.top_p"
+ // GenAIRequestTopK is the top_k parameter
+ GenAIRequestTopK = "gen_ai.request.top_k"
+ // GenAIRequestMaxTokens is the maximum tokens requested
+ GenAIRequestMaxTokens = "gen_ai.request.max_tokens"
+ // GenAIRequestFrequencyPenalty is the frequency penalty parameter
+ GenAIRequestFrequencyPenalty = "gen_ai.request.frequency_penalty"
+ // GenAIRequestPresencePenalty is the presence penalty parameter
+ GenAIRequestPresencePenalty = "gen_ai.request.presence_penalty"
+ // GenAIRequestStopSequences are the stop sequences
+ GenAIRequestStopSequences = "gen_ai.request.stop_sequences"
+ // GenAIRequestSeed is the seed for deterministic generation
+ GenAIRequestSeed = "gen_ai.request.seed"
+ // GenAIRequestChoiceCount is the number of choices requested
+ GenAIRequestChoiceCount = "gen_ai.request.choice_count"
+ // GenAIRequestIsStream indicates whether the request uses streaming
+ GenAIRequestIsStream = "gen_ai.request.is_stream"
+ // GenAIRequestUser is the end-user identifier for the request
+ GenAIRequestUser = "gen_ai.request.user"
+)
+
+// Response Attributes
+const (
+ // GenAIResponseModel is the actual model that generated the response
+ GenAIResponseModel = "gen_ai.response.model"
+ // GenAIResponseID is the unique identifier for the response
+ GenAIResponseID = "gen_ai.response.id"
+ // GenAIResponseFinishReasons are the reasons why generation stopped
+ GenAIResponseFinishReasons = "gen_ai.response.finish_reasons"
+ // GenAIResponseChoiceCount is the number of choices in response
+ GenAIResponseChoiceCount = "gen_ai.response.choice_count"
+)
+
+// Usage Attributes
+const (
+ // GenAIUsageInputTokens is the number of input tokens
+ GenAIUsageInputTokens = "gen_ai.usage.input_tokens"
+ // GenAIUsageOutputTokens is the number of output tokens
+ GenAIUsageOutputTokens = "gen_ai.usage.output_tokens"
+ // GenAIUsageTotalTokens is the total number of tokens
+ GenAIUsageTotalTokens = "gen_ai.usage.total_tokens"
+ // GenAIUsageCost is the estimated cost of the operation
+ GenAIUsageCost = "gen_ai.usage.cost"
+ // GenAITokenType distinguishes input vs output on token usage metrics
+ GenAITokenType = "gen_ai.token.type"
+)
+
+// Token Details (OpenAI specific)
+const (
+ // GenAIUsageCompletionTokensDetailsAudio tracks audio output tokens
+ GenAIUsageCompletionTokensDetailsAudio = "gen_ai.usage.completion_tokens_details.audio"
+ // GenAIUsageCompletionTokensDetailsReasoning tracks reasoning tokens
+ GenAIUsageCompletionTokensDetailsReasoning = "gen_ai.usage.completion_tokens_details.reasoning"
+ // GenAIUsagePromptTokensDetailsCacheRead tracks cached tokens read
+ GenAIUsagePromptTokensDetailsCacheRead = "gen_ai.usage.prompt_tokens_details.cache_read"
+ // GenAIUsagePromptTokensDetailsCacheWrite tracks cached tokens written
+ GenAIUsagePromptTokensDetailsCacheWrite = "gen_ai.usage.prompt_tokens_details.cache_write"
+)
+
+// Message Attributes
+const (
+ // GenAIPrompt is the legacy prompt attribute
+ GenAIPrompt = "gen_ai.prompt"
+ // GenAICompletion is the legacy completion attribute
+ GenAICompletion = "gen_ai.completion"
+ // GenAIInputMessages are the structured input messages
+ GenAIInputMessages = "gen_ai.input.messages"
+ // GenAIOutputMessages are the structured output messages
+ GenAIOutputMessages = "gen_ai.output.messages"
+ // GenAISystemInstructions are system-level instructions
+ GenAISystemInstructions = "gen_ai.system_instructions"
+)
+
+// Tool/Function Attributes
+const (
+ // GenAIToolDefinitions are the tool/function definitions provided
+ GenAIToolDefinitions = "gen_ai.tool.definitions"
+ // GenAIToolCalls are the tool/function calls made
+ GenAIToolCalls = "gen_ai.tool.calls"
+ // GenAIToolType is the type of tool (e.g. "function")
+ GenAIToolType = "gen_ai.tool.type"
+ // GenAIToolName is the comma-joined list of tool names called by the model
+ GenAIToolName = "gen_ai.tool.name"
+ // GenAIToolCallID is the comma-joined list of tool call IDs from the model response
+ GenAIToolCallID = "gen_ai.tool.call.id"
+ // GenAIToolCallArguments is the array of JSON argument strings from model tool calls
+ GenAIToolCallArguments = "gen_ai.tool.call.arguments"
+)
+
+// Conversation Attributes
+const (
+ // GenAIConversationID identifies a conversation/session
+ GenAIConversationID = "gen_ai.conversation.id"
+)
+
+// OpenAI Specific Attributes
+const (
+ // GenAIOpenAIAssistantID is the OpenAI assistant ID
+ GenAIOpenAIAssistantID = "gen_ai.openai.assistant.id"
+ // GenAIOpenAIThreadID is the OpenAI thread ID
+ GenAIOpenAIThreadID = "gen_ai.openai.thread.id"
+ // GenAIOpenAIRunID is the OpenAI run ID
+ GenAIOpenAIRunID = "gen_ai.openai.run.id"
+ // GenAIOpenAIRequestServiceTier is the requested service tier
+ GenAIOpenAIRequestServiceTier = "gen_ai.openai.request.service_tier"
+ // GenAIOpenAIResponseServiceTier is the actual service tier used
+ GenAIOpenAIResponseServiceTier = "gen_ai.openai.response.service_tier"
+ // GenAIOpenAIResponseSystemFingerprint is the system fingerprint
+ GenAIOpenAIResponseSystemFingerprint = "gen_ai.openai.response.system_fingerprint"
+)
+
+// Output Type
+const (
+ // GenAIOutputType is the requested content type
+ GenAIOutputType = "gen_ai.output.type"
+ // GenAIOutputTypeText represents text output
+ GenAIOutputTypeText = "text"
+ // GenAIOutputTypeJSON represents JSON output
+ GenAIOutputTypeJSON = "json_object"
+)
+
+// Server Attributes
+const (
+ // ServerAddress is the server address
+ ServerAddress = "server.address"
+ // ServerPort is the server port
+ ServerPort = "server.port"
+)
+
+// Error Attributes
+const (
+ // ErrorType describes the error type
+ ErrorType = "error.type"
+)
+
+// Event Names
+const (
+ // GenAIClientInferenceOperationDetails is the event name for operation details
+ GenAIClientInferenceOperationDetails = "gen_ai.client.inference.operation.details"
+ // GenAIUserMessage is the event name for user messages
+ GenAIUserMessage = "gen_ai.user.message"
+ // GenAISystemMessage is the event name for system messages
+ GenAISystemMessage = "gen_ai.system.message"
+ // GenAIAssistantMessage is the event name for assistant messages
+ GenAIAssistantMessage = "gen_ai.assistant.message"
+ // GenAIToolMessage is the event name for tool messages
+ GenAIToolMessage = "gen_ai.tools.message"
+ // GenAIChoice is the event name for choice details
+ GenAIChoice = "gen_ai.choice"
+)
+
+// Metric Names
+const (
+ // GenAIClientTokenUsage tracks token usage
+ GenAIClientTokenUsage = "gen_ai.client.token.usage"
+ // GenAIClientOperationDuration tracks operation duration
+ GenAIClientOperationDuration = "gen_ai.client.operation.duration"
+ // GenAIServerRequestDuration tracks server request duration
+ GenAIServerRequestDuration = "gen_ai.server.request.duration"
+ // GenAIServerTimePerOutputToken tracks time between tokens
+ GenAIServerTimePerOutputToken = "gen_ai.server.time_per_output_token"
+ // GenAIServerTimeToFirstToken tracks time to first token
+ GenAIServerTimeToFirstToken = "gen_ai.server.time_to_first_token"
+ // GenAIClientOperationTimeToFirstChunk tracks client-side time to first chunk in streaming
+ GenAIClientOperationTimeToFirstChunk = "gen_ai.client.operation.time_to_first_chunk"
+ // GenAIClientOperationTimePerOutputChunk tracks per-chunk output token latency observations in streaming
+ GenAIClientOperationTimePerOutputChunk = "gen_ai.client.operation.time_per_output_chunk"
+)
+
+// Environment Attributes
+const (
+ // OpenLITEnvironment is the deployment environment
+ OpenLITEnvironment = "openlit.environment"
+ // OpenLITApplicationName is the application name
+ OpenLITApplicationName = "openlit.application.name"
+ // OpenLITSDKVersion is the SDK version
+ OpenLITSDKVersion = "openlit.sdk.version"
+)
diff --git a/sdk/go/version.go b/sdk/go/version.go
new file mode 100644
index 00000000..68f2c3a3
--- /dev/null
+++ b/sdk/go/version.go
@@ -0,0 +1,6 @@
+package openlit
+
+// Version is the current version of the OpenLIT Go SDK.
+// This is the single source of truth — update this when cutting a release,
+// then tag the commit as sdk/go/v<Version> to trigger the release workflow.
+const Version = "0.1.0"