Skip to content

feat(instrumentation-openai): add instrumentation of openai SDK #2941

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

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

anuraaga
Copy link
Contributor

@anuraaga anuraaga commented Jul 17, 2025

Which problem is this PR solving?

  • openai SDK usage should have gen ai conventions but there is no instrumentation yet

Short description of the changes

  • Adds instrumentation targeting openai SDK 4 for chat and embeddings

Future PRs will add support for responses API and SDK 5

/cc @trentm if you can help review
/cc @lmolkova because I heard you are also interested in this instrumentation. Notably, I'd like this instrumentation to exist upstream so people can use it but need potential component owners I believe

This instrumentation is adapted from the one in https://github.com/elastic/elastic-otel-node/tree/main/packages/instrumentation-openai

@anuraaga anuraaga requested a review from a team as a code owner July 17, 2025 09:23
@anuraaga anuraaga marked this pull request as draft July 17, 2025 09:23
})
).rejects.toThrow(OpenAI.APIConnectionError);

// TODO: Figure out why it takes so long to get this span. trace.getTracerProvider()._delegate.forceFlush() didn't help.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could use some help debugging this

Copy link

codecov bot commented Jul 18, 2025

Codecov Report

Attention: Patch coverage is 88.21752% with 39 lines in your changes missing coverage. Please review.

Project coverage is 89.03%. Comparing base (e9987a7) to head (22e806a).

Files with missing lines Patch % Lines
...ages/instrumentation-openai/src/instrumentation.ts 90.90% 25 Missing ⚠️
packages/instrumentation-openai/src/utils.ts 53.33% 14 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2941      +/-   ##
==========================================
- Coverage   89.06%   89.03%   -0.03%     
==========================================
  Files         188      191       +3     
  Lines        9219     9550     +331     
  Branches     1900     1977      +77     
==========================================
+ Hits         8211     8503     +292     
- Misses       1008     1047      +39     
Files with missing lines Coverage Δ
packages/instrumentation-openai/src/semconv.ts 100.00% <100.00%> (ø)
packages/instrumentation-openai/src/utils.ts 53.33% <53.33%> (ø)
...ages/instrumentation-openai/src/instrumentation.ts 90.90% <90.90%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

nx.json Outdated
@@ -31,6 +31,9 @@
]
},
"lint": {
"dependsOn": [
"version:update",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure on this infrastructure change but I couldn't think of another way to solve

> npx --yes [email protected] --dependencies --production --tags=-knipignore

Unresolved imports (1)
./version  packages/instrumentation-openai/src/instrumentation.ts:70:9
Error: Process completed with exit code 1.

I notice there's some cache setup so I wonder if the existing projects are passing thanks to caching?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is what has been done for the other packages:

diff --git a/packages/instrumentation-openai/src/instrumentation.ts b/packages/instrumentation-openai/src/instrumentation.ts
index 6b7d3abf..c448b918 100644
--- a/packages/instrumentation-openai/src/instrumentation.ts
+++ b/packages/instrumentation-openai/src/instrumentation.ts
@@ -67,6 +67,7 @@ import {
   METRIC_GEN_AI_CLIENT_OPERATION_DURATION,
   METRIC_GEN_AI_CLIENT_TOKEN_USAGE,
 } from './semconv';
+/** @knipignore */
 import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
 import { getEnvBool, getAttrsFromBaseURL } from './utils';
 import { OpenAIInstrumentationConfig } from './types';

Let's do that instead so we can avoid the version:update time for linting in CI.

Copy link
Contributor

@trentm trentm left a comment

Choose a reason for hiding this comment

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

Thanks for starting this, Anuraag. This is just a quick start at looking. I haven't looked at src/ or test/ yet.

},
"scripts": {
"clean": "rimraf build/*",
"setup:dev": "nx run-many -t compile -p @opentelemetry/instrumentation-aws-sdk",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"setup:dev": "nx run-many -t compile -p @opentelemetry/instrumentation-aws-sdk",
"setup:dev": "nx run-many -t compile -p @opentelemetry/instrumentation-openai",

"lint:readme": "node ../../scripts/lint-readme.js",
"prepublishOnly": "npm run compile",
"tdd": "npm run test -- --watch-extensions ts --watch",
"test": "nyc mocha --require '@opentelemetry/contrib-test-utils' 'test/**/*.test.ts'",
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't dug into the tests yet. Are the tests relying on the require-hook environment setup from contrib-test-utils? Or could this be copy-pasta?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah they currently are. When debugging #2941 (comment) too TBH I did find it a bit confusing to be out-of-control of the lifecycle of the tracer provider but thought it's the pattern for the repo. Let me know if it's better to manually set it up

Comment on lines 47 to 50
"@opentelemetry/api": "^1.3.0"
},
"devDependencies": {
"@opentelemetry/api": "^1.9.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious if dev usage really does require the newer API version than the runtime (peerDep).

},
"devDependencies": {
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/context-async-hooks": "^2.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"@opentelemetry/context-async-hooks": "^2.0.0",

"cross-env": "7.0.3",
"nock": "^14.0.0",
"nyc": "17.1.0",
"openai": "4.57.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps bump this to 4.104.0

"repository": {
"type": "git",
"url": "https://github.com/open-telemetry/opentelemetry-js-contrib.git",
"directory": "packages/instrumentation-oracledb"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"directory": "packages/instrumentation-oracledb"
"directory": "packages/instrumentation-openai"

nx.json Outdated
@@ -31,6 +31,9 @@
]
},
"lint": {
"dependsOn": [
"version:update",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is what has been done for the other packages:

diff --git a/packages/instrumentation-openai/src/instrumentation.ts b/packages/instrumentation-openai/src/instrumentation.ts
index 6b7d3abf..c448b918 100644
--- a/packages/instrumentation-openai/src/instrumentation.ts
+++ b/packages/instrumentation-openai/src/instrumentation.ts
@@ -67,6 +67,7 @@ import {
   METRIC_GEN_AI_CLIENT_OPERATION_DURATION,
   METRIC_GEN_AI_CLIENT_TOKEN_USAGE,
 } from './semconv';
+/** @knipignore */
 import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
 import { getEnvBool, getAttrsFromBaseURL } from './utils';
 import { OpenAIInstrumentationConfig } from './types';

Let's do that instead so we can avoid the version:update time for linting in CI.

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.

2 participants