Registry / observability / netflix-spectator-py

netflix-spectator-py

JSON →
library1.1.2pypypi✓ verified 84d ago

netflix-spectator-py is a thin-client library for reporting metrics from Python applications to SpectatorD and the Netflix Atlas Timeseries Database. It is currently at version 1.1.2 and maintains an active release cadence, providing continuous bug fixes and feature enhancements.

pip install netflix-spectator-py
INSTALL
IMPORT
SIG · NETFLIX-SPECTATOR-
N
netflix-spectator-py
observabilitypythonv1.1.2
Install
1.6s avg
Import
67ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.065s · 18MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.069s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Registry
from spectator import Registry
Config
from spectator import Config
GlobalRegistry
from spectator import GlobalRegistry
from spectator.main import GlobalRegistry
While GlobalRegistry is still supported for legacy reasons, direct import from 'spectator' is the correct modern path. It's a hold-over from the thick-client and stateless in this thin-client version.
StopWatch
from spectator import StopWatch

Initializes a `Registry` with an optional `Config` for common tags, then demonstrates basic usage of `Counter`, `Timer`, and `Gauge` to report metrics. By default, metrics are sent via UDP to a local SpectatorD agent.

from spectator import Registry, Config import time import os # Configure with optional extra common tags (e.g., for environment, service) config = Config(extra_common_tags={'env': os.environ.get('SPECTATOR_ENV', 'dev')}) registry = Registry(config) # Create and interact with a counter request_counter = registry.counter('my_service.request_count') request_counter.increment() print(f"Request Count: {request_counter.get()}") # Create and interact with a timer processing_timer = registry.timer('my_service.processing_latency') with processing_timer.start(): time.sleep(0.1) # Simulate work print(f"Processing Latency recorded: {processing_timer.get_count()} events") # Create and interact with a gauge (manual update) current_items = registry.gauge('my_service.current_items') current_items.set(5) print(f"Current Items: {current_items.get()}") # Note: Metrics are typically sent to a SpectatorD agent via UDP by default. Ensure SpectatorD is running.
Debug
Known issues
gotchaWhen using `netflix-spectator-py` in a multiprocessing environment, especially with 'fork' or 'forkserver' start methods (Linux default), you must create a new `Registry` instance *after* each child process has started. Failing to do so can lead to deadlocks or incorrect metric reporting due to shared socket descriptors and background threads.
fix
Initialize `Registry` within each child process's main execution block, or use a multiprocessing-safe method for managing the registry instance, such as passing metrics back to a single reporting process.
affects: All versions
gotchaFor high-volume metric reporting (tens of thousands to millions of operations per second), direct per-operation metric updates can lead to high CPU usage for both your application and SpectatorD. This client sends a UDP packet for each update.
fix
Pre-aggregate metrics within your application and report them at a cadence closer to SpectatorD's publish interval (e.g., every 5 seconds).
affects: All versions
gotchaAll tag keys and values for `MeterId` objects must be strings. If non-string values are provided, the `MeterId` class will validate them, potentially dropping or changing invalid values and reporting a warning log. This can lead to unexpected metric dimensions or missing data.
fix
Ensure all tag keys and values are explicitly cast to strings before being passed to `with_tag()` or `with_tags()` methods. Monitor `spectator.meter.meter_id` logger at `WARNING` or `DEBUG` level for validation issues.
affects: All versions
deprecatedThe `GlobalRegistry` is a legacy concept inherited from a prior thick-client version. While still functional and supported for backward compatibility, new code should prefer instantiating `Registry` objects directly with an explicit `Config`.
fix
Replace `from spectator import GlobalRegistry` and `GlobalRegistry.counter(...)` with `from spectator import Registry, Config` and `registry = Registry(Config(...)); registry.counter(...)`.
affects: <=1.1.2
Errors
Common errors & fixes
ResourceWarning: unclosed socket <socket.socket fd=X, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('0.0.0.0', 0)>
The SpectatorD client opens a UDP or Unix domain socket to send metrics. If a `Registry` (especially `GlobalRegistry` in short-lived scripts) is not explicitly shut down or if the script exits abruptly, the socket may not be closed properly.
fix
For explicit `Registry` instances, ensure `.stop()` is called (e.g., in a `finally` block or application shutdown hook). For `GlobalRegistry` in tests or short scripts, consider `spectator.GlobalRegistry.stop()` explicitly, or switch to an explicit `Registry` instance for better lifecycle management.
ModuleNotFoundError: No module named 'spectator'
The `netflix-spectator-py` library is not installed or the Python environment is not correctly configured.
fix
Install the library using pip: `pip install netflix-spectator-py`. Ensure you are running your script with the Python interpreter where the library was installed.
TypeError: 'int' object is not subscriptable (or similar errors with non-string tags)
Spectator requires all tag keys and values to be strings. Passing integers, booleans, or other non-string types directly as tag values will cause type errors or silent tag dropping after validation.
fix
Always cast tag keys and values to strings: `tags={'status': str(status_code)}` or `id.with_tag('value', str(my_int_value))`.
Upgrade
Version history
1.1.2latest on PyPI · released Oct 2, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
12
OpenAI (training)
2
Resources
netflix-spectator-py — pip install netflix-spectator-py · libregistry