Skip to main content

Observability integration with Spinal

Project description

SP-OBS: Spinal OpenTelemetry Integration

SP-OBS is Spinal's cost tracking library built on top of open telemetry. It works by adding isolated tracers to libraries that have not been instrumented and attached a processor to libraries that aloready have been instrumented. This means we can also play nice with other observability libraries out there.

Features

  • Seamlessly integrates with existing OpenTelemetry setups
  • Works with Logfire, vanilla OpenTelemetry, or any OTEL-compatible framework
  • Adds user and workflow context to spans for better tracking
  • Selective span processing - only sends relevant AI/billing spans

Installation

pip install sp-obs

With AI Provider Support

# For OpenAI support
pip install sp-obs[openai]

# For Anthropic support  
pip install sp-obs[anthropic]

# For all providers
pip install sp-obs[all]

Quick Start

Configuration

First, configure SP-OBS with your endpoint and API key:

import sp_obs

# Configure globally (recommended)
sp_obs.configure(
    endpoint="https://your-endpoint.com/spans",
    api_key="your-api-key"
)

Or use environment variables:

  • SPINAL_TRACING_ENDPOINT
  • SPINAL_API_KEY

Instrumenting AI Providers

import sp_obs

# Configure SP-OBS
sp_obs.configure()

# Instrument providers
sp_obs.instrument_openai()
sp_obs.instrument_anthropic()
sp_obs.instrument_httpx()
sp_obs.instrument_requests()

Adding Context to Traces

Use the context manager to add user and workflow information:

import sp_obs

# Add context using context manager
with sp_obs.add_context(
    workflow_id="workflow-123",
    user_id="user-456",
    aggregation_id="session-789"  # optional
):
    # All spans created here will have this context
    response = client.chat.completions.create(...)

Configuration Options

Environment Variables

  • SPINAL_TRACING_ENDPOINT: HTTP endpoint to send spans to
  • SPINAL_API_KEY: API key for authentication
  • SPINAL_PROCESS_MAX_QUEUE_SIZE: Max spans in queue (default: 2048)
  • SPINAL_PROCESS_SCHEDULE_DELAY: Export delay in ms (default: 5000)
  • SPINAL_PROCESS_MAX_EXPORT_BATCH_SIZE: Batch size (default: 512)
  • SPINAL_PROCESS_EXPORT_TIMEOUT: Export timeout in ms (default: 30000)

Advanced Configuration

sp_obs.configure(
    endpoint="https://your-endpoint.com/spans",
    api_key="your-api-key",
    headers={"Custom-Header": "value"},
    timeout=5,
    max_queue_size=2048,
    max_export_batch_size=512,
    schedule_delay_millis=5000,
    export_timeout_millis=30000,
    scrubber=my_custom_scrubber  # Optional
)

Data Scrubbing

SP-OBS includes automatic scrubbing of sensitive data:

from sp_obs import DefaultScrubber, NoOpScrubber

# Use default scrubber (redacts tokens, keys, passwords)
sp_obs.configure(scrubber=DefaultScrubber())

# Or disable scrubbing
sp_obs.configure(scrubber=NoOpScrubber())

# Or implement custom scrubbing
class MyCustomScrubber:
    def scrub_attributes(self, attributes: dict) -> dict:
        # Your scrubbing logic
        return attributes

sp_obs.configure(scrubber=MyCustomScrubber())

Performance Considerations

SP-OBS uses a BatchSpanProcessor to minimize performance impact:

  • Spans are batched and sent asynchronously in a background thread
  • Default batch size: 512 spans
  • Default flush interval: 5 seconds
  • Spans are dropped if queue exceeds max size (default: 2048)

To tune for high-volume applications:

sp_obs.configure(
    max_queue_size=5000,          # Increase queue size
    max_export_batch_size=1000,   # Larger batches
    schedule_delay_millis=2000    # More frequent exports
)

What Spans Are Captured?

SP-OBS automatically captures:

  • AI/LLM spans (identified by gen_ai.system attribute)
  • HTTPX and request spans
  • Explicitly created billing event spans
  • Spans with attached user/workflow context

All other spans are ignored to minimize overhead and data transfer.

Integration Examples

FastAPI Application

from fastapi import FastAPI
import sp_obs
from openai import AsyncOpenAI

app = FastAPI()
client = AsyncOpenAI()

# Configure on startup
@app.on_event("startup")
async def startup():
    sp_obs.configure()
    sp_obs.instrument_openai()

@app.post("/generate")
async def generate(user_id: str, workflow_id: str):
    with sp_obs.add_context(user_id=user_id, workflow_id=workflow_id):
        response = await client.chat.completions.create(
            model="gpt-4",
            messages=[{"role": "user", "content": "Hello"}]
        )
        return response

License

MIT License - see LICENSE file for details.

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

sp_obs-0.1.1.tar.gz (58.6 kB view details)

Uploaded Source

Built Distribution

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

sp_obs-0.1.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file sp_obs-0.1.1.tar.gz.

File metadata

  • Download URL: sp_obs-0.1.1.tar.gz
  • Upload date:
  • Size: 58.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for sp_obs-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6da4a91d25d153f5885df663cc6cb6e077dac413d56b8e9ccc3cccd7a50f3c96
MD5 69d78d375d4fe0f1f934f92c22deae34
BLAKE2b-256 a2213d716d31d97c0ecfe6e630f41fda6fe5b1b3321313039f284b9233355e7d

See more details on using hashes here.

File details

Details for the file sp_obs-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: sp_obs-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for sp_obs-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f069ff7170d8750b6084095da3e0a2e7d444edcb804fdbae16223c58583d7d99
MD5 c266b6789c7c7f949bc2d4e3099c9631
BLAKE2b-256 d5a9b2405324a8f0b3a5f2cd4c746249138189565cc7892dadc192ef69afc167

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