opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

RFC: Create opentelemetry-auto-instrument package

Open DaGenix opened this issue 1 year ago • 0 comments
trafficstars

Summary

Create a package that when installed causes opentelemetry setup code to run when Python starts without the need for running the opentelemetry-instrument command.

This PR isn't ready for merging. I'd like feedback on if this change is likely to be accepted.

Longer Description

One way to setup opentelemetry libraries and run the installed instrumentors is to use the opentelemetry-instrument command to start the Python process. This will add the a directory with a "sitecustomize.py" file to the PYTHONPATH and run Python. Python will then run this module at startup which will run the setup code.

One challenge of this approach is that there can only be a single "sitecustomize.py" file for the entire Python process - so, if an environment already uses this file, using opentelemetry-instrument will hide the existing file which is probably not desired. A second challenge is that using the opentelemetry-instrument command inherently requires modifying startup scripts which may not be desired.

What this commit does is to create a new package, opentelemetry-auto-instrument. What this package does is to provide a .pth file. When Python starts up, it reads all .pth files that it can find and, if the file contains an import, it runs the import. So, the .pth file that we create does an import - and the module we import then does all of the opentelemetry setup. In this way, someone that wants to setup opentelemetry without modifying their startup scripts or who already has a "sitecustomize.py" file has another option: they can add the new package into their virtual environment and that will cause setup to run when Python starts.

One thing I don't know how to do is to create a Python package using the hatch build system that just contains a .pth file. I know how to do it via setuptools - so, I used setuptools for the new package.

I made some minor tweeks to the existing opentelemetry-instrumentation package - primarily I moved most of the code from "sitecustomize.py" to a new module, "auto_instrument_on_import.py". Then, both sitecustomize.py and the new .pth file both import auto_instrument_on_import.py - which means that if somehow someone were to both use the new package AND the opentelemetry-instrument command, things should work fine since Python will ensure the module code is run only once.

Unlike the "sitecustomize.py" file which prevents instrumentation of child python processes, the new package will instrument child processes. I think this is the expected behavior of installing a package into the virtual environment.

Type of change

  • [X] New feature (non-breaking change which adds functionality)
  • [X] This change requires a documentation update

How Has This Been Tested?

Tested by installing the new package. But, at this point, what I mostly want is feedback as to if the general approach will be accepted.

Does This PR Require a Core Repo Change?

  • [X] No.

Checklist:

See contributing.md for styleguide, changelog guidelines, and more.

  • [X] Followed the style guidelines of this project
  • [ ] Changelogs have been updated
  • [ ] Unit tests have been added
  • [ ] Documentation has been updated

DaGenix avatar Dec 26 '23 20:12 DaGenix