OpenTelemetry Aleph Alpha Instrumentation

0.58.0 · active · verified Thu Apr 09

This library provides OpenTelemetry instrumentation for tracing calls to Aleph Alpha's endpoints when using the official Aleph Alpha Client. It is part of the broader OpenLLMetry project, offering observability for Generative AI and LLM applications. The library is actively maintained with frequent releases, currently at version 0.58.0.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the OpenTelemetry SDK with a console exporter and then apply the `AlephAlphaInstrumentor` to automatically trace calls made using the `aleph-alpha-client`. Ensure your `ALEPH_ALPHA_API_KEY` is set as an environment variable or replaced in the code.

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.alephalpha import AlephAlphaInstrumentor

# Ensure Aleph Alpha client is installed: pip install aleph-alpha-client
import aleph_alpha_client

# --- OpenTelemetry SDK Setup ---
# Configure resource (optional, but good practice)
resource = Resource.create({"service.name": "aleph-alpha-app"})

# Set up a TracerProvider
provider = TracerProvider(resource=resource)

# Export spans to console for demonstration
span_processor = SimpleSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(span_processor)

# Set the global TracerProvider
trace.set_tracer_provider(provider)

# --- Aleph Alpha Instrumentation ---
# Instrument the Aleph Alpha client
AlephAlphaInstrumentor().instrument()

# --- Aleph Alpha Client Usage ---
# Replace with your actual Aleph Alpha API key
AA_API_KEY = os.environ.get("ALEPH_ALPHA_API_KEY", "YOUR_AA_API_KEY")

if AA_API_KEY == "YOUR_AA_API_KEY":
    print("Warning: ALEPH_ALPHA_API_KEY not set. Using dummy key. API calls will fail.")

# Example Aleph Alpha API call
print("\n--- Making an Aleph Alpha API call ---")
client = aleph_alpha_client.Client(host="https://api.aleph-alpha.com", token=AA_API_KEY)

try:
    # A simple completion request
    request = aleph_alpha_client.CompletionRequest(
        prompt=aleph_alpha_client.Prompt([aleph_alpha_client.Text("Hello, world!")]),
        model="luminous-extended",
        maximum_tokens=10,
    )
    response = client.complete(request=request)
    print(f"Aleph Alpha Response: {response.completions[0].completion}")
except Exception as e:
    print(f"Aleph Alpha API call failed (this is expected if API key is invalid): {e}")

print("\n--- OpenTelemetry tracing complete ---")

view raw JSON →