-
Notifications
You must be signed in to change notification settings - Fork 67
fix(sdk): defensive resource creation for otel v1, v2 resolution #647
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11ad43a
fix(sdk): defensive resource creation for otel v1, v2 resolution
galkleinman cf44602
lint
galkleinman 8f95bd5
more convenient impl
galkleinman cbd1577
Apply suggestion from @ellipsis-dev[bot]
nirga 42a627b
fix: remove unused envDetector and processDetector imports
nirga db16cd1
fix: apply prettier formatting to tracing index.ts
nirga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,12 @@ import { NodeSDK } from "@opentelemetry/sdk-node"; | |
| import { SpanProcessor } from "@opentelemetry/sdk-trace-node"; | ||
| import { context, diag } from "@opentelemetry/api"; | ||
| import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto"; | ||
| import { resourceFromAttributes } from "@opentelemetry/resources"; | ||
| import { | ||
| resourceFromAttributes, | ||
| Resource, | ||
| envDetector, | ||
| processDetector, | ||
| } from "@opentelemetry/resources"; | ||
| import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; | ||
| import { Instrumentation } from "@opentelemetry/instrumentation"; | ||
| import { InitializeOptions } from "../interfaces"; | ||
|
|
@@ -307,10 +312,38 @@ export const startTracing = (options: InitializeOptions) => { | |
| spanProcessors.push(options.processor); | ||
| } | ||
|
|
||
| // Create resource with proper detection and defensive handling for OTLP serialization | ||
| const serviceName = | ||
| options.appName || process.env.npm_package_name || "unknown-service"; | ||
| let resource: Resource; | ||
|
|
||
| try { | ||
| // Create our custom resource with service name and let NodeSDK handle default detection | ||
| resource = resourceFromAttributes({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider leveraging the newly imported detectResources (with envDetector and processDetector) to merge additional default resource attributes into your custom resource for enhanced resilience. |
||
| [ATTR_SERVICE_NAME]: serviceName, | ||
| }); | ||
|
|
||
| // Defensive check to prevent OTLP serialization errors | ||
| if (!resource || typeof resource !== "object") { | ||
| throw new Error("Invalid resource object"); | ||
| } | ||
|
|
||
| if (!resource.attributes || typeof resource.attributes !== "object") { | ||
| throw new Error("Resource missing attributes"); | ||
| } | ||
| } catch (error) { | ||
| // Fallback: create a basic resource manually | ||
| diag.warn( | ||
| "Failed to create resource with resourceFromAttributes, using fallback", | ||
| error, | ||
| ); | ||
| resource = resourceFromAttributes({ | ||
| [ATTR_SERVICE_NAME]: serviceName, | ||
| }); | ||
| } | ||
|
|
||
| _sdk = new NodeSDK({ | ||
| resource: resourceFromAttributes({ | ||
| [ATTR_SERVICE_NAME]: options.appName || process.env.npm_package_name, | ||
| }), | ||
| resource, | ||
| spanProcessors, | ||
| contextManager: options.contextManager, | ||
| textMapPropagator: options.propagator, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Peer ranges should reflect v1 and v2 compatibility to match the PR objective
Declaring only v2 for several OpenTelemetry peers contradicts the “v1, v2 resolution” goal and can force consumers onto v2. Use union ranges to support both v1 and v2 where applicable.
Apply this diff (adjust minimal v1 versions to those you actually support):
"peerDependencies": { "@opentelemetry/api": "^1.9.0", - "@opentelemetry/resources": "^2.0.0", - "@opentelemetry/sdk-trace-base": "^2.0.0", - "@opentelemetry/exporter-trace-otlp-proto": "^0.203.0" + "@opentelemetry/resources": "^1.10.0 || ^2.0.0", + "@opentelemetry/sdk-trace-base": "^1.10.0 || ^2.0.0", + "@opentelemetry/exporter-trace-otlp-proto": "^0.40.0 || ^0.203.0" },Also, consider removing these packages from "dependencies" to avoid duplicate installs and version skew. If needed for local builds/tests, move them to "devDependencies".
Please verify Node.js engine compatibility: OTel v2 typically requires Node >= 18. If you retain v2 ranges, the
"engines.node": ">=14"may be misleading for consumers on Node 14/16.🌐 Web query:
💡 Result:
@opentelemetry/resources v2.x and @opentelemetry/sdk-trace-base v2.x require a minimum Node.js version of 18.19.0 or 20.6.0 and above[1]. This requirement applies to all stable OpenTelemetry JS SDK v2.x packages, including these two[1].
Node.js:
^18.19.0 || >=20.6.0@opentelemetry/resourcesand@opentelemetry/sdk-trace-baseare part[1].For context:
>=2.0.0for stable packages (including these)[1].If you intend to use these packages, ensure your application is running Node 18.19.0+, or 20.6.0+ to guarantee support and compatibility[1][7].
Citations:
Bump OpenTelemetry peer ranges for v1/v2 support and enforce Node ≥18.19.0
"dependencies"to"devDependencies"to avoid duplicate installs and version skew."engines.node"in package.json from" >=14"to" >=18.19.0"(v2.x requires Node 18.19.0 or newer)."peerDependencies": { "@opentelemetry/api": "^1.9.0", - "@opentelemetry/resources": "^2.0.0", - "@opentelemetry/sdk-trace-base": "^2.0.0", - "@opentelemetry/exporter-trace-otlp-proto": "^0.203.0" + "@opentelemetry/resources": "^1.10.0 || ^2.0.0", + "@opentelemetry/sdk-trace-base": "^1.10.0 || ^2.0.0", + "@opentelemetry/exporter-trace-otlp-proto": "^0.40.0 || ^0.203.0" }, - "dependencies": { - /* …OTel packages… */ - }, - "engines": { - "node": ">=14" - } + "devDependencies": { + /* Move OTel packages here if used only for build/tests */ + }, + "engines": { + "node": ">=18.19.0" + }🤖 Prompt for AI Agents