otelopenai middleware with minimal changes.
Installation
Usage
Set
LANGWATCH_API_KEY and GROQ_API_KEY environment variables before running.Documentation Index
Fetch the complete documentation index at: /docs/llms.txt
Use this file to discover all available pages before exploring further.
Get started with LangWatch Skills in seconds: Set up evals, scenario tests, and tracing just by asking your AI coding assistant.
Instrument Groq API calls in Go using LangWatch for fast LLM observability, cost tracking, and agent evaluation insights.
otelopenai middleware with minimal changes.
go get github.com/langwatch/langwatch/sdk-go github.com/openai/openai-go
LANGWATCH_API_KEY and GROQ_API_KEY environment variables before running.package main
import (
"context"
"log"
"os"
langwatch "github.com/langwatch/langwatch/sdk-go"
otelopenai "github.com/langwatch/langwatch/sdk-go/instrumentation/openai"
"github.com/openai/openai-go"
oaioption "github.com/openai/openai-go/option"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
func main() {
ctx := context.Background()
// Set up LangWatch exporter
exporter, err := langwatch.NewDefaultExporter(ctx)
if err != nil {
log.Fatalf("failed to create exporter: %v", err)
}
tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
otel.SetTracerProvider(tp)
defer tp.Shutdown(ctx) // Critical: ensures traces are flushed
// Create Groq client via OpenAI-compatible API
client := openai.NewClient(
oaioption.WithBaseURL("https://api.groq.com/openai/v1"),
oaioption.WithAPIKey(os.Getenv("GROQ_API_KEY")),
oaioption.WithMiddleware(otelopenai.Middleware("my-app",
otelopenai.WithCaptureInput(),
otelopenai.WithCaptureOutput(),
otelopenai.WithGenAISystem("groq"),
)),
)
response, err := client.Chat.Completions.New(ctx, openai.ChatCompletionNewParams{
Model: "openai/gpt-oss-20b",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage("You are a helpful assistant."),
openai.UserMessage("Hello, Groq!"),
},
})
if err != nil {
log.Fatalf("Groq API call failed: %v", err)
}
log.Printf("Response: %s", response.Choices[0].Message.Content)
}
defer tp.Shutdown(ctx) call is essential. Without it, traces buffered in memory will be lost when your application exits.Was this page helpful?