API tracer to gather usage telemetry for Python libraries
Project description
Telemetric
This library provides lightweight telemetry for Python projects, collecting statistics on function parameter usage to understand how APIs are being used.
Installation
pip install telemetric
Usage
Automatic Function Wrapping
To automatically track parameter usage statistics for existing Python packages,
use the install() function before importing the target modules:
from telemetric import install
# Install telemetry for specific modules
install(["scipy.stats._correlation", "scipy.stats._distn_infrastructure"])
from scipy import stats
# Use functions normally
stats.norm.pdf(x=1, loc=1, scale=0.01)
stats.norm(loc=1, scale=0.02).pdf(1)
# Retrieve statistics for wrapped functions
print("Call counts:", stats.norm.pdf._get_counts())
print("Parameter stats:", stats.norm.pdf._get_param_stats())
The _get_counts() method returns a tuple of:
- Total function calls
- Number of calls that raised errors
- Number of calls with invalid arguments (wrapping issues)
The _get_param_stats() method returns detailed statistics for each parameter:
- Parameter name (or None for positional-only)
- Number of times the parameter was passed
- Tracked parameter values (if specified)
- Counts for each tracked value (if specified)
Manual Function Decoration
For more control, use the stats_deco_auto decorator to automatically track all
parameters:
from telemetric import stats_deco_auto
@stats_deco_auto
def my_function(x, y=10, z="default"):
return x + y
my_function(5)
my_function(5, y=20)
my_function(5, 20, "custom")
print(my_function._get_counts())
print(my_function._get_param_stats())
For fine-grained control over which parameter values to track, use stats_deco:
from telemetric import stats_deco
@stats_deco(x=None, y=(10, 20, 30), z=("default", "custom"))
def my_function(x, y=10, z="default"):
return x + y
# The decorator will track:
# - Whether x was passed (any value)
# - How often y was 10, 20, or 30 (and count other values separately)
# - How often z was "default" or "custom" (and count other values separately)
Printing All Statistics
To print a summary of all wrapped functions and their statistics:
from telemetric.statswrapper import print_all_stats
# After your code has run
print_all_stats()
OpenTelemetry Integration (Legacy)
The library also supports OpenTelemetry-based tracing for distributed systems:
from telemetric import span, start_span_processor
@span
def foo(bar):
print(bar)
if __name__ == "__main__":
start_span_processor("test-service")
foo(bar="baz")
OpenTelemetry Collector Setup (Legacy)
If using the OpenTelemetry integration, you can set up collectors for trace data:
To start a collector that prints each log message to stdout:
cd tests/collector
docker run -p 4317:4317 -p 4318:4318 --rm \
-v $(pwd)/collector-config.yaml:/etc/otelcol/config.yaml \
otel/opentelemetry-collector
To start a Jaeger collector with a dashboard UI:
docker run --name jaeger \
-e COLLECTOR_OTLP_ENABLED=true \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
jaegertracing/all-in-one:1.35
Access the Jaeger UI at http://localhost:16686
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file telemetric-0.0.1rc1.tar.gz.
File metadata
- Download URL: telemetric-0.0.1rc1.tar.gz
- Upload date:
- Size: 30.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
103d2f902268a02c704248aa09959f2531e80658658c2ee2ac5326de94706fff
|
|
| MD5 |
880c1a533362b8d06d1dc2da811018aa
|
|
| BLAKE2b-256 |
886ba9f9cf539b3fcef06510d6702c4e82d35b461f4ce4e19a11259854749e61
|
Provenance
The following attestation bundles were made for telemetric-0.0.1rc1.tar.gz:
Publisher:
cd.yml on scientific-python/telemetric
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
telemetric-0.0.1rc1.tar.gz -
Subject digest:
103d2f902268a02c704248aa09959f2531e80658658c2ee2ac5326de94706fff - Sigstore transparency entry: 669585697
- Sigstore integration time:
-
Permalink:
scientific-python/telemetric@d3a8c9fe33e12259d171263f0bf28be4392b254b -
Branch / Tag:
refs/tags/v0.0.1rc1 - Owner: https://github.com/scientific-python
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@d3a8c9fe33e12259d171263f0bf28be4392b254b -
Trigger Event:
push
-
Statement type: