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.6.tar.gz (52.1 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.6-py3-none-any.whl (68.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plain_observer-0.34.6.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.6.tar.gz
Algorithm Hash digest
SHA256 b07cc6fb4ae68c347dd83bc0944f14dc7791004bb1e83ceadfac56efb8bcee84
MD5 d15cc457d18929b23536ed73d37504c8
BLAKE2b-256 e8b67f6bceb682b7b183669ff663a3b34ad616797855b812edb362626ad15ef4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plain_observer-0.34.6-py3-none-any.whl
  • Upload date:
  • Size: 68.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 e9e9100bd247b6275d8b9b8db120dd3093be4432a17288b798421db9de7edb51
MD5 bdb2dd1d94376b465167d1547a572f03
BLAKE2b-256 93978c0c7187c236cb36b3ddc91095abba133d80d4608b61d44e8affeee7d2d3

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