OpenTelemetry Groq Instrumentation
OpenTelemetry Groq instrumentation for Python allows automatic tracing of calls to the Groq API. It integrates with the OpenTelemetry Python SDK to capture LLM-related operations, requests, responses, and metadata, conforming to the OpenTelemetry Generative AI semantic conventions. The current version is 0.58.0, with frequent releases to keep up with Groq API changes and OpenTelemetry semantic convention updates.
Warnings
- breaking The instrumentation has migrated to the OpenTelemetry Generative AI semantic conventions (gen_ai), starting from version 0.53.0. This change renames many span attributes (e.g., `llm.request.type` to `gen_ai.request.type`) and introduces new ones. Custom dashboards, alerts, or queries relying on older attribute names will break.
- gotcha The instrumentation must be initialized (`GroqInstrumentor().instrument()`) *before* the `groq` library is imported or its client is instantiated for automatic tracing to work correctly. If `groq` is imported first, its methods might not be patched.
- gotcha This package only provides the OpenTelemetry instrumentation. It requires the `groq` library itself to be installed separately for the instrumentation to function. If `groq` is not installed, the instrumentation will be enabled but won't find the target methods to patch.
Install
-
pip install opentelemetry-instrumentation-groq groq
Imports
- GroqInstrumentor
from opentelemetry.instrumentation.groq import GroqInstrumentor
Quickstart
import os
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from opentelemetry.instrumentation.groq import GroqInstrumentor
# 1. Configure OpenTelemetry SDK
resource = Resource.create({"service.name": "groq-llm-app"})
provider = TracerProvider(resource=resource)
processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
# 2. Instrument the Groq library
GroqInstrumentor().instrument()
# 3. Ensure Groq API key is set
groq_api_key = os.environ.get("GROQ_API_KEY", "
# In a real application, use proper secret management.
# For this quickstart, ensure the env var is set or provide a dummy key for testing.
")
if not groq_api_key:
print("Warning: GROQ_API_KEY environment variable not set.")
print("Skipping Groq API call. Please set GROQ_API_KEY to run the example.")
else:
print("Making Groq API call...")
try:
from groq import Groq
client = Groq(api_key=groq_api_key)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Explain the concept of 'observability' in one sentence."
}
],
model="llama3-8b-8192",
temperature=0.7,
max_tokens=100
)
print("Groq Response:", chat_completion.choices[0].message.content)
print("Traces should be printed to console.")
except Exception as e:
print(f"An error occurred during Groq API call: {e}")