Skip to main content

Real-time OpenTelemetry streaming components for FastHTML

Project description

FastHTML OpenTelemetry Streamer

Real-time OpenTelemetry streaming components for FastHTML applications. Stream telemetry data as beautiful, nested, collapsible components that update live as spans are created and completed.

Features

  • One-line setup: Add real-time telemetry streaming with a single function call
  • Beautiful UI: Collapsible, nested span visualization with status colors
  • Real-time updates: See spans appear and update live via Server-Sent Events
  • Thread-safe: Works seamlessly with any OpenTelemetry instrumentation
  • Customizable: Bring your own renderers or extend the defaults
  • AI-optimized: Specialized rendering for AI/LLM spans with rich metrics
  • Attribute-based rendering: Custom renderers for specific span types
  • Zero config: Sensible defaults, optional customization

Quick Start

from fasthtml.common import *
import fasthtml_otel as ft_otel
from opentelemetry.sdk.trace import TracerProvider

# Create your FastHTML app
app = FastHTML()

# Set up OpenTelemetry
provider = TracerProvider()
ft_otel.configure(app, provider)

# Your routes automatically get telemetry
@app.get("/")
def index():
    return Div(
        H1("My App"),
        ft_otel.telemetry_container(),
    )

serve()

That's it! Visit your app and you'll see live telemetry data streaming in.

Installation

pip install fasthtml-otel

Advanced Usage

Custom Container

import fasthtml_otel as ft_otel
from opentelemetry.sdk.trace import TracerProvider

# Configure with custom container ID
provider = TracerProvider()
ft_otel.configure(app, provider, container_id="my-telemetry")

@app.get("/")
def index():
    return Div(
        H1("My App"),
        ft_otel.telemetry_container(title="Custom Telemetry"),
    )

Custom Renderers

import fasthtml_otel as ft_otel
from fasthtml_otel.renderers import SpanRenderer
from opentelemetry.sdk.trace import TracerProvider

class MySpanRenderer(SpanRenderer):
    def render_header(self, span):
        return Div(f"SPAN: {span.name}", cls="custom-header")

provider = TracerProvider()
ft_otel.configure(app, provider, renderer=MySpanRenderer())

Integration with Existing Telemetry

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
import fasthtml_otel as ft_otel

# Set up your existing OpenTelemetry
provider = TracerProvider()
# Don't set global provider yet - let ft_otel.configure() do it

# Add FastHTML streaming
ft_otel.configure(app, provider)

Example with Manual Tracing

from fasthtml.common import *
import fasthtml_otel as ft_otel
from opentelemetry.sdk.trace import TracerProvider

app = FastHTML()
provider = TracerProvider()
ft_otel.configure(app, provider)

# Get a tracer for manual instrumentation
tracer = provider.get_tracer("my-app")

@app.get("/")
def index():
    return Div(
        H1("Telemetry Demo"),
        Button("Trigger Operation", hx_post="/process", hx_target="#result"),
        Div(id="result"),
        ft_otel.telemetry_container(),
    )

@app.post("/process")
def process():
    with tracer.start_as_current_span("user_operation") as span:
        span.set_attribute("operation.type", "data_processing")

        # Simulate some work with nested spans
        with tracer.start_as_current_span("validate_data") as validate_span:
            validate_span.set_attribute("data.size", 1024)
            import time
            time.sleep(0.1)  # Simulate validation work

        with tracer.start_as_current_span("process_data") as process_span:
            process_span.set_attribute("algorithm", "example")
            time.sleep(0.2)  # Simulate processing work

        span.set_attribute("success", True)

    return Div("Operation completed! Check the telemetry panel.", cls="text-green-600")

Example with Pydantic AI

from fasthtml.common import *
import fasthtml_otel as ft_otel
from opentelemetry.sdk.trace import TracerProvider
from pydantic_ai import Agent

app = FastHTML()
provider = TracerProvider()
ft_otel.configure(app, provider)

# Create specialized AI renderer for better AI span visualization
ai_renderer = ft_otel.AISpanRenderer(auto_expand_patterns=["chat", "Tool:"])

# Instrument Pydantic AI with custom renderer
ft_otel.instrument_pydantic_ai(provider, renderer=ai_renderer)
agent = Agent("gpt-4o-mini")

@app.get("/")
def index():
    return Div(
        H1("Chat App"),
        ft_otel.telemetry_container(),
    )

@app.post("/chat")
def chat(msg: str):
    result = agent.run(msg)
    return Div(result.data)

Attribute-Based Custom Renderers

# Register custom renderer for specific span types
database_renderer = MyDatabaseRenderer()
ft_otel.register_attribute_renderer("db.operation", database_renderer)

# AI spans automatically get rich formatting with token counts, costs, etc.
ai_renderer = ft_otel.AISpanRenderer()
ft_otel.register_attribute_renderer("gen_ai.operation.name", ai_renderer)

Development

git clone https://github.com/Novia-RDI-Seafaring/ft-otel
cd ft-otel
pip install -e ".[dev]"

# Run example
cd example
python app.py

Citation

If you use this software in your research or projects, please cite it as:

@software{fasthtml_otel,
  title={FastHTML OpenTelemetry Streamer},
  author={Christoffer Björkskog},
  year={2025},
  url={https://github.com/Novia-RDI-Seafaring/ft-otel}
}

Acknowledgments

This project is part of the Virtual Sea Trial Project (VST), funded by Business Finland.

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

fasthtml_otel-0.1.0.tar.gz (208.8 kB view details)

Uploaded Source

Built Distribution

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

fasthtml_otel-0.1.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file fasthtml_otel-0.1.0.tar.gz.

File metadata

  • Download URL: fasthtml_otel-0.1.0.tar.gz
  • Upload date:
  • Size: 208.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for fasthtml_otel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6381ef11e23e912551e16eea5956eef8843b79dc031b4a5bbb1a9c3a92ea69a9
MD5 306415079a3279dd9afe047dbd5418b2
BLAKE2b-256 97f4da71783c34275fd30695b568083bf2274520aeefe32ca0893381e3cc087d

See more details on using hashes here.

File details

Details for the file fasthtml_otel-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fasthtml_otel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6c69bf2a453e3b2785e7b9c7f5b40a637aaccd06b3e918e3d07187ed7018657d
MD5 adc3f390ca7a60e1411ef174f09e9461
BLAKE2b-256 e593cd65fccb2b53bad151cc0f8d28cdcfa80c36890f02fce31b50184fbeafdb

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