Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry-instrumentation: expose a way to init autoinstrumentation programmatically #3273

Merged
merged 14 commits into from
Feb 24, 2025

Conversation

xrmx
Copy link
Contributor

@xrmx xrmx commented Feb 17, 2025

Description

Expose the initialize function used to init auto-instrumentation programmatically in a more conventient way instead of importing sitecustomize and expect its side effects. While at it also update the code to handle the case of PYTHONPATH not set.

from opentelemetry.instrumentation import auto_instrumentation
auto_instrumentation.initialize()

Fixes #3263
Closes #2886

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • tox -e py310-test-opentelemetry-instrumentation

Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

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

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

@xrmx
Copy link
Contributor Author

xrmx commented Feb 17, 2025

This is a draft because I haven't tested it yet outside of unit tests.

@xrmx xrmx force-pushed the manual-autoinstrumentation branch from b1511ec to fd93744 Compare February 17, 2025 11:16
@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Feb 18, 2025

This works, thanks. Here is how I tested it.


I checked out your branch gh pr checkout 3273, then navigated to the instrumentation-genai/opentelemetry-instrumentation-openai-v2/examples/manual directory

Inside there, I replaced requirements.txt with this

openai~=1.63.2

opentelemetry-api@git+https://github.com/open-telemetry/opentelemetry-python.git#egg=opentelemetry-api&subdirectory=opentelemetry-api
opentelemetry-sdk@git+https://github.com/open-telemetry/opentelemetry-python.git#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk
opentelemetry-semantic-conventions@git+https://github.com/open-telemetry/opentelemetry-python.git#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions
opentelemetry-proto@git+https://github.com/open-telemetry/opentelemetry-python.git#egg=opentelemetry-proto&subdirectory=opentelemetry-proto
opentelemetry-exporter-otlp-proto-common@git+https://github.com/open-telemetry/opentelemetry-python.git#egg=opentelemetry-exporter-otlp-proto-common&subdirectory=exporter/opentelemetry-exporter-otlp-proto-common
opentelemetry-exporter-otlp-proto-grpc@git+https://github.com/open-telemetry/opentelemetry-python.git#egg=opentelemetry-exporter-otlp-proto-grpc&subdirectory=exporter/opentelemetry-exporter-otlp-proto-grpc
../../../../opentelemetry-instrumentation
../../../../opentelemetry-distro
../../../opentelemetry-instrumentation-openai-v2

Then, I replaced main.py with this:

# pylint: skip-file
import os

from openai import OpenAI

from opentelemetry.instrumentation import auto_instrumentation
auto_instrumentation.initialize()

def main():
    client = OpenAI()
    chat_completion = client.chat.completions.create(
        model=os.getenv("CHAT_MODEL", "gpt-4o-mini"),
        messages=[
            {
                "role": "user",
                "content": "Write a short poem on OpenTelemetry.",
            },
        ],
    )
    print(chat_completion.choices[0].message.content)


if __name__ == "__main__":
    main()

Then, I ran dotenv run -- python main.py and it worked!

@codefromthecrypt
Copy link
Contributor

This works, I was missing one dep and adjusted my comment!

@xrmx
Copy link
Contributor Author

xrmx commented Feb 18, 2025

Tested this with a flask app, opentelemetry-instrument still work, in order to work manually the initialize() must be called before importing Flask, I guess the developer experience will depend on the instrumentation.

@xrmx xrmx force-pushed the manual-autoinstrumentation branch from d444489 to 39f81ce Compare February 18, 2025 08:54
@xrmx xrmx marked this pull request as ready for review February 18, 2025 08:54
@xrmx xrmx requested a review from a team as a code owner February 18, 2025 08:54
@codefromthecrypt
Copy link
Contributor

Tested this with a flask app, opentelemetry-instrument still work, in order to work manually the initialize() must be called before importing Flask, I guess the developer experience will depend on the instrumentation.

yeah maybe in general we suggest to init before other imports, though code style might interfere with this.

Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

per earlier comments, this is exactly what I was looking for and happy to see it!

@xrmx xrmx requested a review from emdneto February 18, 2025 16:41
@xrmx xrmx changed the title opentelemetry-instrumentation: expose a way to init autoinstrumentation opentelemetry-instrumentation: expose a way to init autoinstrumentation programmatically Feb 18, 2025
@xrmx xrmx enabled auto-merge (squash) February 24, 2025 09:13
@xrmx xrmx merged commit 38006e8 into open-telemetry:main Feb 24, 2025
694 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

library-based alternative to opentelemetry-instrument for easy integration in Jupyter notebooks
4 participants