Skip to main content

On-page telemetry and observability tools for Plain.

Project description

plain.observer

Request tracing and debugging tools built on OpenTelemetry.

Overview

You can use Observer to trace requests and debug performance issues in your Plain application. Observer integrates with OpenTelemetry to capture spans, database queries, and logs for individual requests.

When enabled, Observer shows you a real-time summary of each request including query counts, duplicate queries, and total duration. You can also persist traces to the database for later analysis.

# Access the Observer from any request
from plain.observer import Observer

observer = Observer.from_request(request)

# Check the current mode
if observer.is_enabled():
    print("Observer is tracking this request")

if observer.is_persisting():
    print("Traces will be saved to the database")

# Get a summary of the current trace
summary = observer.get_current_trace_summary()
# Returns something like: "5 queries (2 duplicates) • 45.2ms"

The Observer class provides methods to check the current mode and enable/disable tracing via cookies.

Observer modes

Observer has three modes that control how traces are captured.

Summary mode

Summary mode captures spans in memory for real-time monitoring but does not save them to the database. This is useful for debugging during development without filling up your database.

from plain.observer import Observer

def my_view(request):
    observer = Observer.from_request(request)
    response = Response("OK")
    observer.enable_summary_mode(response)
    return response

The summary cookie lasts for 1 week.

Persist mode

Persist mode captures spans and saves them to the database. This includes full trace data, spans, and log entries. Use this when you need to analyze traces after the request completes.

observer.enable_persist_mode(response)

The persist cookie lasts for 1 day.

Disabled mode

You can explicitly disable Observer to prevent any tracing, even if a parent trace exists.

observer.disable(response)

Toolbar integration

If you have plain.toolbar installed, Observer automatically adds a panel showing the current mode and trace summary. You can toggle between modes directly from the toolbar.

The toolbar panel displays:

  • Current observer mode (Summary, Persist, or Disabled)
  • Query count with duplicate detection
  • Total request duration
  • Link to view persisted traces

CLI commands

Observer provides CLI commands for capturing, managing, and viewing traces.

# Capture a trace and return structured JSON analysis
plain observer request /path
plain observer request /path --user 1
plain observer request /path --method POST --data '{"key": "value"}'

# List recent traces
plain observer traces
plain observer traces --limit 50
plain observer traces --user-id 123
plain observer traces --json

# View a specific trace
plain observer trace <trace_id>
plain observer trace <trace_id> --json

# List spans
plain observer spans
plain observer spans --trace-id <trace_id>

# View a specific span
plain observer span <span_id>

# Clear all trace data
plain observer clear
plain observer clear --yes

The observer request command makes a request with tracing automatically enabled and returns structured JSON output including query counts, duplicate detection, issue analysis, and a span tree. The --user flag accepts a user ID or email.

The traces and spans commands support filtering by user ID, session ID, or request ID, and can output JSON for programmatic use.

Admin integration

When plain.admin is installed, Observer registers viewsets for browsing Traces, Spans, and Logs. You can find these under the "Observer" section in the admin navigation.

The admin views let you:

  • Browse and search traces by request ID, user ID, or session ID
  • View span hierarchies and timing
  • Filter spans by parent status
  • Search and filter log entries by level or message

Settings

Setting Default Env var
OBSERVER_IGNORE_URL_PATTERNS [...] PLAIN_OBSERVER_IGNORE_URL_PATTERNS (JSON)
OBSERVER_TRACE_LIMIT 100 PLAIN_OBSERVER_TRACE_LIMIT

See default_settings.py for more details.

FAQs

How do I enable Observer in production?

Observer is controlled by a signed cookie, so you can enable it for specific users or sessions. The toolbar provides an easy way to toggle modes, or you can set the cookie programmatically in a view.

Can I use Observer with an external OpenTelemetry collector?

Yes. Observer uses the ObserverSampler and ObserverSpanProcessor which integrate with OpenTelemetry's standard APIs. You can combine Observer with other samplers using ObserverCombinedSampler.

Why are some URLs not being traced?

Observer ignores certain URL patterns by default (assets, observer routes, etc.) to reduce noise. You can customize this with the OBSERVER_IGNORE_URL_PATTERNS setting.

How do I get the trace summary in a template?

In persist or summary mode, you can access the summary from the Observer instance:

# In your view
context["trace_summary"] = Observer.from_request(request).get_current_trace_summary()

What data is stored when persisting traces?

The Trace model stores trace ID, timing, request ID, user ID, and session ID. Each trace has related Span records with full OpenTelemetry span data (including SQL queries and attributes) and Log entries captured during the request.

Installation

Install the plain.observer package from PyPI:

uv add plain.observer

Add plain.observer to your INSTALLED_PACKAGES:

# app/settings.py
INSTALLED_PACKAGES = [
    # ...
    "plain.observer",
]

Include the observer URLs in your URL configuration:

# app/urls.py
from plain.observer.urls import ObserverRouter
from plain.urls import Router, include

class AppRouter(Router):
    namespace = ""
    urls = [
        # ...
        include("observer/", ObserverRouter),
    ]

Sync the database to create the necessary tables:

plain postgres sync

After installation, Observer will automatically integrate with your application's toolbar (if using plain.toolbar). You can access the web interface at /observer/traces/ or use the CLI commands to analyze traces.

Content Security Policy (CSP)

If you're using a Content Security Policy (CSP), the Observer toolbar panel requires frame-ancestors 'self' to display trace information in an iframe.

Without this directive, the toolbar panel will fail to load with a CSP error: "Refused to frame... because an ancestor violates the following Content Security Policy directive: 'frame-ancestors 'none'".

Example CSP configuration:

DEFAULT_RESPONSE_HEADERS = {
    "Content-Security-Policy": (
        "default-src 'self'; "
        "script-src 'self' 'nonce-{request.csp_nonce}'; "
        "style-src 'self' 'nonce-{request.csp_nonce}'; "
        "frame-ancestors 'self'; "  # Required for Observer toolbar
        # ... other directives
    ),
}

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

plain_observer-0.34.2.tar.gz (51.5 kB view details)

Uploaded Source

Built Distribution

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

plain_observer-0.34.2-py3-none-any.whl (67.6 kB view details)

Uploaded Python 3

File details

Details for the file plain_observer-0.34.2.tar.gz.

File metadata

  • Download URL: plain_observer-0.34.2.tar.gz
  • Upload date:
  • Size: 51.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","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 plain_observer-0.34.2.tar.gz
Algorithm Hash digest
SHA256 a1fd6f550ac7d7d22c35b200a43230d93e3c2d5e0face0345b8f24ae42808f02
MD5 b8ce96950900b481336f834c61fb8ebe
BLAKE2b-256 76e0185298aae2522f0557dc811c32858c39472c583cdc08c6bd68db90af9606

See more details on using hashes here.

File details

Details for the file plain_observer-0.34.2-py3-none-any.whl.

File metadata

  • Download URL: plain_observer-0.34.2-py3-none-any.whl
  • Upload date:
  • Size: 67.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","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 plain_observer-0.34.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c069575e2b3f691169a572f88b5c9b5fd1af2cc9fc5eca8ae77c40f99d45675f
MD5 4275a3dd3fcab48f03db12ecebf5a004
BLAKE2b-256 22367a2206efa5d8b91f474de2e3815c57a7db60b2fdd4358732fdf83ba0ce71

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