A Python decorator to add OTEL auto-instrumentation to your functions
Project description
otelize
Add OTEL auto-instrumentation to your functions.
Introduction
This is a simple package intended for the use of lazy developers that want to included basic OTEL telemetry to their project without bothering much with adding a lot of boilerplate.
How it works
This package provides the otelize decorator that wraps a function and adds all your parameters (with their values) and the returning value as span attributes.
How to use it
The otelize decorator
Just add the @otelize decorator to your functions:
from otelize import otelize
@otelize
def your_function(a_param: str, another_param: int, a_list: list[float], a_dict: dict[str, str]):
...
All the parameters and the return value will be added as attributes to the OTEL span created for the function.
In this case it would be equivalent to do this:
import json
from opentelemetry import trace
tracer = trace.get_tracer(__name__)
def your_function(a_param: str, another_param: int, a_list: list[float], a_dict: dict[str, str]):
with tracer.start_as_current_span('your_function') as span:
span.set_attributes({
'a_param': a_param,
'another_param': another_param,
'a_list': json.dumps(a_list),
'a_dict': json.dumps(a_dict),
})
Avoiding leaking secrets
To avoid leaking values of sensitive parameters, define the following environment values:
OTELIZE_SPAN_REDACTABLE_ATTRIBUTES: JSON array of attributes that need to be redacted in your OTEL. By default is'[]'OTELIZE_SPAN_REDACTABLE_ATTRIBUTES_REGEX: string with a Python regex that will redact all attributes that match the regulax expression. By default, is an impossible regex:'(?!)'.OTELIZE_SPAN_RETURN_VALUE_IS_INCLUDED: Truthy or falsy value. By default, it is False.
The redacted attributes will have the '[REDACTED]' value.
Adding additional information from the decorated function
Call trace.get_current_span() to get the current span from inside the function:
from typing import Any
from opentelemetry import trace
from otelize import otelize
@otelize
def your_function(a_param: str, another_param: int, and_another_one: Any):
span = trace.get_current_span()
span.set_attribute('custom_attr', 'your value')
Examples
There are more examples in the test folder.
Dependencies
The runtime depends only on opentelemetry-api, and for testing it depends on opentelemetry-sdk and other test coverage and formatting packages (coverage, black, flake8...).
Python version support
The minimum Python supported version is 3.10.
Collaborations
This project is open to collaborations. Make a PR or an issue, and I'll take a look to it.
License
MIT license, but if you need any other contact me.
Disclaimer
This project is not affiliated with, endorsed by, or sponsored by the OpenTelemetry project owners, the Cloud Native Computing Foundation.
The use of the names "OTEL" and "otelize" in this repository is solely for descriptive purposes and does not imply any association or intent to infringe on any trademarks.
The project is named "otelize" for purposes of having a short name that can be used as a Python decorator.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file otelize-0.0.2.tar.gz.
File metadata
- Download URL: otelize-0.0.2.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05972f918d65ae95d0eba17501a30e8d47899b3207565b6b5e54c06ebed27733
|
|
| MD5 |
73a88c1b54d9619626f95a9abcc27507
|
|
| BLAKE2b-256 |
7c05d8cc411eaefeec44c24d308373b9f776300fec43b7b134832b09290598da
|
Provenance
The following attestation bundles were made for otelize-0.0.2.tar.gz:
Publisher:
publish_on_pypi.yml on diegojromerolopez/otelize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
otelize-0.0.2.tar.gz -
Subject digest:
05972f918d65ae95d0eba17501a30e8d47899b3207565b6b5e54c06ebed27733 - Sigstore transparency entry: 454151449
- Sigstore integration time:
-
Permalink:
diegojromerolopez/otelize@b42c7f0eab9b8dc40c552706e3c1bf088f3cb7ce -
Branch / Tag:
refs/tags/0.0.2 - Owner: https://github.com/diegojromerolopez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_on_pypi.yml@b42c7f0eab9b8dc40c552706e3c1bf088f3cb7ce -
Trigger Event:
release
-
Statement type:
File details
Details for the file otelize-0.0.2-py3-none-any.whl.
File metadata
- Download URL: otelize-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d05cd2c9adbb9d7b120ac6116aeeab8a7ece9ae94afba97732a0d5df9d8ad134
|
|
| MD5 |
a98e9553e0b158bfedb08db0fa8d1255
|
|
| BLAKE2b-256 |
0293151beef2e7601315674eb775a1a4a7b76b99830fb8657c3704cabe732f74
|
Provenance
The following attestation bundles were made for otelize-0.0.2-py3-none-any.whl:
Publisher:
publish_on_pypi.yml on diegojromerolopez/otelize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
otelize-0.0.2-py3-none-any.whl -
Subject digest:
d05cd2c9adbb9d7b120ac6116aeeab8a7ece9ae94afba97732a0d5df9d8ad134 - Sigstore transparency entry: 454151454
- Sigstore integration time:
-
Permalink:
diegojromerolopez/otelize@b42c7f0eab9b8dc40c552706e3c1bf088f3cb7ce -
Branch / Tag:
refs/tags/0.0.2 - Owner: https://github.com/diegojromerolopez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish_on_pypi.yml@b42c7f0eab9b8dc40c552706e3c1bf088f3cb7ce -
Trigger Event:
release
-
Statement type: