11# Telemetric
22
33[ ![ Actions Status] [ actions-badge ]] [ actions-link ]
4- [ ![ Documentation Status] [ rtd-badge ]] [ rtd-link ]
5-
64[ ![ PyPI version] [ pypi-version ]] [ pypi-link ]
7- [ ![ Conda-Forge] [ conda-badge ]] [ conda-link ]
8- [ ![ PyPI platforms] [ pypi-platforms ]] [ pypi-link ]
9-
10- [ ![ GitHub Discussion] [ github-discussions-badge ]] [ github-discussions-link ]
115
126<!-- SPHINX-START -->
137
148<!-- prettier-ignore-start -->
159[ actions-badge ] : https://github.com/scientific-python/telemetric/workflows/CI/badge.svg
1610[ actions-link ] : https://github.com/scientific-python/telemetric/actions
17- [ conda-badge ] : https://img.shields.io/conda/vn/conda-forge/telemetric
18- [ conda-link ] : https://github.com/conda-forge/telemetric-feedstock
19- [ github-discussions-badge ] : https://img.shields.io/static/v1?label=Discussions&message=Ask&color=blue&logo=github
20- [ github-discussions-link ] : https://github.com/scientific-python/telemetric/discussions
2111[ pypi-link ] : https://pypi.org/project/telemetric/
22- [ pypi-platforms ] : https://img.shields.io/pypi/pyversions/telemetric
2312[ pypi-version ] : https://img.shields.io/pypi/v/telemetric
24- [ rtd-badge ] : https://readthedocs.org/projects/telemetric/badge/?version=latest
25- [ rtd-link ] : https://telemetric.readthedocs.io/en/latest/?badge=latest
2613
2714<!-- prettier-ignore-end -->
2815
29- This library adds basic telemetry to Python projects that traces the usage and
30- run time of Python functions within a given scope .
16+ This library provides lightweight telemetry for Python projects, collecting
17+ statistics on function parameter usage to understand how APIs are being used .
3118
3219## Installation
3320
34- Prerequisites:
35-
36- ```
37- pip install opentelemetry-distro
38- pip install opentelemetry-exporter-otlp
39- opentelemetry-bootstrap --action=install
21+ ``` bash
22+ pip install telemetric
4023```
4124
4225## Usage
4326
44- To track usage of one or more existing Python projects, run:
27+ ### Automatic Function Wrapping
28+
29+ To automatically track parameter usage statistics for existing Python packages,
30+ use the ` install() ` function before importing the target modules:
4531
4632``` python
47- from opentelemetry.instrumentation.auto_instrumentation import initialize
4833from telemetric import install
4934
50- install([my_project.my_module])
51- initialize()
52- start_span_processor(" my-project-service" )
35+ # Install telemetry for specific modules
36+ install([" scipy.stats._correlation" , " scipy.stats._distn_infrastructure" ])
37+
38+ from scipy import stats
39+
40+ # Use functions normally
41+ stats.norm.pdf(x = 1 , loc = 1 , scale = 0.01 )
42+ stats.norm(loc = 1 , scale = 0.02 ).pdf(1 )
43+
44+ # Retrieve statistics for wrapped functions
45+ print (" Call counts:" , stats.norm.pdf._get_counts())
46+ print (" Parameter stats:" , stats.norm.pdf._get_param_stats())
47+ ```
48+
49+ The ` _get_counts() ` method returns a tuple of:
50+
51+ - Total function calls
52+ - Number of calls that raised errors
53+ - Number of calls with invalid arguments (wrapping issues)
54+
55+ The ` _get_param_stats() ` method returns detailed statistics for each parameter:
56+
57+ - Parameter name (or None for positional-only)
58+ - Number of times the parameter was passed
59+ - Tracked parameter values (if specified)
60+ - Counts for each tracked value (if specified)
61+
62+ ### Manual Function Decoration
63+
64+ For more control, use the ` stats_deco_auto ` decorator to automatically track all
65+ parameters:
66+
67+ ``` python
68+ from telemetric import stats_deco_auto
69+
70+
71+ @stats_deco_auto
72+ def my_function (x , y = 10 , z = " default" ):
73+ return x + y
74+
75+
76+ my_function(5 )
77+ my_function(5 , y = 20 )
78+ my_function(5 , 20 , " custom" )
79+
80+ print (my_function._get_counts())
81+ print (my_function._get_param_stats())
82+ ```
83+
84+ For fine-grained control over which parameter values to track, use ` stats_deco ` :
85+
86+ ``` python
87+ from telemetric import stats_deco
88+
89+
90+ @stats_deco (x = None , y = (10 , 20 , 30 ), z = (" default" , " custom" ))
91+ def my_function (x , y = 10 , z = " default" ):
92+ return x + y
93+
94+
95+ # The decorator will track:
96+ # - Whether x was passed (any value)
97+ # - How often y was 10, 20, or 30 (and count other values separately)
98+ # - How often z was "default" or "custom" (and count other values separately)
99+ ```
100+
101+ ### Printing All Statistics
102+
103+ To print a summary of all wrapped functions and their statistics:
104+
105+ ``` python
106+ from telemetric.statswrapper import print_all_stats
107+
108+ # After your code has run
109+ print_all_stats()
53110```
54111
55- To explicitly add instrumentation to functions you want to trace, use the ` span `
56- decorator:
112+ ### OpenTelemetry Integration (Legacy)
113+
114+ The library also supports OpenTelemetry-based tracing for distributed systems:
57115
58116``` python
59117from telemetric import span, start_span_processor
@@ -69,16 +127,21 @@ if __name__ == "__main__":
69127 foo(bar = " baz" )
70128```
71129
72- ## Start collector
130+ ## OpenTelemetry Collector Setup (Legacy)
73131
74- To start a collector that prints each log message to stdout, run
75- ` cd tests/collector ` and run
132+ If using the OpenTelemetry integration, you can set up collectors for trace
133+ data:
134+
135+ To start a collector that prints each log message to stdout:
76136
77137``` bash
78- docker run -p 4317:4317 -p 4318:4318 --rm -v $( pwd) /collector-config.yaml:/etc/otelcol/config.yaml otel/opentelemetry-collector
138+ cd tests/collector
139+ docker run -p 4317:4317 -p 4318:4318 --rm \
140+ -v $( pwd) /collector-config.yaml:/etc/otelcol/config.yaml \
141+ otel/opentelemetry-collector
79142```
80143
81- To start a Jaeger collector that starts a basic dashboard, run :
144+ To start a Jaeger collector with a dashboard UI :
82145
83146``` bash
84147docker run --name jaeger \
@@ -88,3 +151,5 @@ docker run --name jaeger \
88151 -p 4318:4318 \
89152 jaegertracing/all-in-one:1.35
90153```
154+
155+ Access the Jaeger UI at http://localhost:16686
0 commit comments