opentelemetry-instrumentation: expose a way to init autoinstrumentation programmatically#3273
Conversation
|
This is a draft because I haven't tested it yet outside of unit tests. |
b1511ec to
fd93744
Compare
|
This works, thanks. Here is how I tested it. I checked out your branch Inside there, I replaced requirements.txt with this 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 |
|
This works, I was missing one dep and adjusted my comment! |
|
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. |
d444489 to
39f81ce
Compare
yeah maybe in general we suggest to init before other imports, though code style might interfere with this. |
codefromthecrypt
left a comment
There was a problem hiding this comment.
per earlier comments, this is exactly what I was looking for and happy to see it!
Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com>
Co-authored-by: Leighton Chen <lechen@microsoft.com>
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.
Fixes #3263
Closes #2886
Type of change
Please delete options that are not relevant.
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
Does This PR Require a Core Repo Change?
Checklist:
See contributing.md for styleguide, changelog guidelines, and more.