OpenTelemetry Groq Instrumentation

0.58.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up the OpenTelemetry SDK with a console exporter, instrument the Groq client, and make a sample API call. Traces generated by the Groq client will be printed to the console.

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}")

view raw JSON →