Packages
snakepit
0.6.7
0.13.0
0.12.0
0.11.1
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
High-performance pooler and session manager for external language integrations. Supports Python, Node.js, Ruby, and more with gRPC streaming, session management, and production-ready process cleanup.
Current section
Files
Jump to
Current section
Files
priv/python/snakepit_bridge/telemetry/__init__.py
"""Telemetry module for Snakepit Bridge.
This module provides a high-level API for emitting telemetry events from Python
workers. Events are captured by Elixir and re-emitted as :telemetry events.
The module supports pluggable backends:
- gRPC backend (default): Streams events over gRPC
- stderr backend (fallback): Writes JSON to stderr
Example usage:
from snakepit_bridge import telemetry
# Emit a simple event
telemetry.emit(
"tool.execution.start",
{"system_time": time.time_ns()},
{"tool": "predict"},
correlation_id="abc-123"
)
# Use span context manager
with telemetry.span("tool.execution", {"tool": "predict"}, "abc-123"):
result = do_work()
telemetry.emit("tool.result_size", {"bytes": len(result)})
"""
from __future__ import annotations
import time
from contextlib import contextmanager
from typing import Any, Dict, Optional
from .manager import get_backend, set_backend
# Import OpenTelemetry tracing functions from the old module
from ..otel_tracing import (
setup_tracing,
get_tracer,
correlation_filter,
new_correlation_id,
set_correlation_id,
reset_correlation_id,
get_correlation_id,
outgoing_metadata,
CORRELATION_HEADER,
span as otel_span # Rename to avoid conflict with new span()
)
# Re-export key functions
__all__ = [
# Event streaming (new)
"emit",
"span", # New event streaming span
"set_backend",
"get_backend",
# OpenTelemetry (old, for backward compatibility)
"setup_tracing",
"get_tracer",
"correlation_filter",
"new_correlation_id",
"set_correlation_id",
"reset_correlation_id",
"get_correlation_id",
"outgoing_metadata",
"CORRELATION_HEADER",
"otel_span" # Old OpenTelemetry span (renamed)
]
def emit(
event: str,
measurements: Dict[str, Any],
metadata: Optional[Dict[str, Any]] = None,
correlation_id: Optional[str] = None,
) -> None:
"""Emit a telemetry event.
Events are sent to the active backend (gRPC stream, stderr, etc.).
Args:
event: Event name in dotted notation (e.g., "tool.execution.start")
measurements: Numeric measurements (duration, size, count, etc.)
metadata: Contextual metadata (tool name, operation, etc.)
correlation_id: Optional correlation ID for distributed tracing
Example:
telemetry.emit(
"tool.execution.stop",
{"duration": 1234, "bytes": 5000},
{"tool": "predict", "model": "gpt-4"},
correlation_id="abc-123"
)
"""
backend = get_backend()
if backend:
backend.emit(event, measurements, metadata, correlation_id)
@contextmanager
def span(
operation: str,
metadata: Optional[Dict[str, Any]] = None,
correlation_id: Optional[str] = None,
):
"""Context manager for timing operations and emitting start/stop/exception events.
Automatically emits:
- {operation}.start at entry
- {operation}.stop at exit (with duration)
- {operation}.exception on error (with duration and error info)
Args:
operation: Operation name (e.g., "tool.execution")
metadata: Metadata to include in all events
correlation_id: Optional correlation ID for distributed tracing
Example:
with telemetry.span("inference", {"model": "gpt-4"}, correlation_id):
result = model.predict(input_data)
Yields:
None
"""
start_time = time.time_ns()
# Emit start event
emit(
f"{operation}.start",
{"system_time": start_time},
metadata,
correlation_id
)
try:
yield
# Emit stop event with duration
end_time = time.time_ns()
duration = end_time - start_time
emit(
f"{operation}.stop",
{"duration": duration},
metadata,
correlation_id
)
except Exception as e:
# Emit exception event
end_time = time.time_ns()
duration = end_time - start_time
error_metadata = dict(metadata or {})
error_metadata["error_type"] = type(e).__name__
error_metadata["error_message"] = str(e)
emit(
f"{operation}.exception",
{"duration": duration},
error_metadata,
correlation_id
)
raise