Skip to content

le-codeur-rapide/opentelemetry-instrumentation-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenTelemetry Python Autoinstrumentation

Problem we are trying to solve

When we want to instrument our python code, the opentelemetry python library provides a convenient way to do so.

In the repo opentelemetry-python-contrib, there are numerous autoinstrumentation libraries for different frameworks and libraries.

Examples are:

But it would be a good thing to be able to instrument any python code without having to use a specific library. For example it would be convenient to create a span for any function call in a python script. In this span, we could have the function name, the arguments and the return value.

Solution

The @tracer.start_as_current_span decorator

The opentelemetry.tracer module provides a start_as_current_span decorator that can be used to create a span for any function call.

from opentelemetry import trace

tracer = trace.get_tracer(__name__)

@tracer.start_as_current_span
def my_function(arg1, arg2):

This is the standard way to create a span for a function call.

Automatize the creation of the span

One way to automate the creation of the span would be to use the sys.monitoring module. Using this module, we can register a callback that will be called when a function call is made, and that would create a span for the function call. We can also register a callback that will be called when a function is returned, and that would end the span.

When implementing these callbacks, we can also get the args and the return value of the function call and add them to the span.

See examples/basic_example.py for an example.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages