Skip to main content

Latitude Telemetry for Python

Project description

Latitude Telemetry for Python

Instrument your AI application and send traces to Latitude. Built on OpenTelemetry.

Installation

pip install latitude-telemetry

Requires Python 3.11+.

Quick Start

from openai import OpenAI
from latitude_telemetry import Telemetry, TelemetryOptions, Instrumentors

telemetry = Telemetry("your-api-key", "your-project-slug", TelemetryOptions(
    instrumentors=[Instrumentors.OpenAI],
))

# All OpenAI calls are now automatically traced
client = OpenAI()
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}],
)

# Ensure traces are sent before process exits
telemetry.flush()

Constructor

Telemetry(api_key: str, project_slug: str, options: TelemetryOptions | None = None)
Parameter Type Description
api_key str Your Latitude API key
project_slug str Your Latitude project slug
options TelemetryOptions Optional configuration

Options

Option Type Default Description
service_name str "latitude-telemetry-python" Service name reported in traces
instrumentors list[Instrumentors] [] Providers to auto-instrument
disable_batch bool False Send spans immediately instead of batching

Auto-Instrumentation

Pass the provider names to automatically instrument all calls:

from latitude_telemetry import Telemetry, TelemetryOptions, Instrumentors

telemetry = Telemetry("your-api-key", "your-project-slug", TelemetryOptions(
    instrumentors=[
        Instrumentors.OpenAI,
        Instrumentors.Anthropic,
    ],
))

Supported Providers

Instrumentor Package
Instrumentors.OpenAI openai
Instrumentors.Anthropic anthropic
Instrumentors.Bedrock boto3
Instrumentors.Cohere cohere
Instrumentors.CrewAI crewai
Instrumentors.DSPy dspy / dspy-ai
Instrumentors.GoogleGenerativeAI google-generativeai
Instrumentors.Groq groq
Instrumentors.Haystack haystack-ai
Instrumentors.Langchain langchain-core
Instrumentors.LiteLLM litellm
Instrumentors.LlamaIndex llama-index
Instrumentors.MistralAI mistralai
Instrumentors.Ollama ollama
Instrumentors.Replicate replicate
Instrumentors.Sagemaker boto3
Instrumentors.Together together
Instrumentors.Transformers transformers
Instrumentors.VertexAI google-cloud-aiplatform
Instrumentors.Watsonx ibm-watsonx-ai

Capture

Use capture() to set trace-wide context attributes. Works as a decorator or context manager. All spans created within the scope inherit the attributes as baggage:

Decorator

@telemetry.capture(
    tags=["production", "chat"],
    metadata={"environment": "prod", "version": "1.2.0"},
    session_id="session-abc-123",
    user_id="user-456",
)
def generate_reply(message: str) -> str:
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": message}],
    )
    return response.choices[0].message.content

Context Manager

with telemetry.capture(session_id="session-abc", user_id="user-123"):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Hello!"}],
    )

Async

@telemetry.capture(tags=["async"])
async def generate_reply(message: str) -> str:
    response = await async_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": message}],
    )
    return response.choices[0].message.content

Capture Options

Option Type OTel Attribute Description
tags list[str] latitude.tags Tags for filtering traces
metadata dict[str, Any] latitude.metadata Arbitrary key-value metadata
session_id str session.id Group traces by session
user_id str user.id Associate traces with a user

Custom Spans

The SDK exposes the underlying OpenTelemetry tracer for creating custom spans:

# Simple span
with telemetry.tracer.start_as_current_span("my-operation") as span:
    span.set_attribute("custom.key", "value")
    # your code here

# Manual span management
span = telemetry.tracer.start_span("my-operation")
span.set_attribute("custom.key", "value")
span.end()

Lifecycle

# Force flush pending spans
telemetry.flush()

# Shutdown (flushes then closes)
telemetry.shutdown()

# Disable all instrumentors
telemetry.uninstrument()

Environment Variables

Variable Default Description
LATITUDE_TELEMETRY_URL http://localhost:3002 OTLP exporter endpoint

Development

Requires uv 0.5.10+.

uv venv && uv sync --all-extras --all-groups
uv run scripts/test.py      # run tests
uv run scripts/lint.py      # run linter
uv run scripts/format.py    # run formatter

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

latitude_telemetry-3.0.0a0.tar.gz (152.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

latitude_telemetry-3.0.0a0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file latitude_telemetry-3.0.0a0.tar.gz.

File metadata

  • Download URL: latitude_telemetry-3.0.0a0.tar.gz
  • Upload date:
  • Size: 152.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for latitude_telemetry-3.0.0a0.tar.gz
Algorithm Hash digest
SHA256 9c24a21d33ac96560d021527c7e95f22bf387076cdbc511cc4af798d5e7aa29d
MD5 942a8c45c0f7d9b1ace8190a06c21a78
BLAKE2b-256 e851b2321efac9590fa28e39886e0c1eaba7fc0c1fa231e31ca7c31409b1fc30

See more details on using hashes here.

File details

Details for the file latitude_telemetry-3.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: latitude_telemetry-3.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for latitude_telemetry-3.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f5576552b7d5baf9b81f66278695e80974ebb75cfab0ef3c01f833c00b19747
MD5 e7ded0a97001d955762cfb80055a5664
BLAKE2b-256 2b53f9333d11393830de2a8c268cd07f5a4942e3434e023e58aa30fd6d8ac5cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page