diff --git a/.changeset/kind-lobsters-swim.md b/.changeset/kind-lobsters-swim.md new file mode 100644 index 0000000000000..6a285a5d99734 --- /dev/null +++ b/.changeset/kind-lobsters-swim.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +feat: allow all NodeSDK options via registerOtel diff --git a/packages/medusa/src/instrumentation/index.ts b/packages/medusa/src/instrumentation/index.ts index 3fad5a1ca2777..3a5344f16b4ea 100644 --- a/packages/medusa/src/instrumentation/index.ts +++ b/packages/medusa/src/instrumentation/index.ts @@ -8,7 +8,7 @@ import { import { ApiRoutesLoader } from "@medusajs/framework/http" import { Tracer } from "@medusajs/framework/telemetry" import type { SpanExporter } from "@opentelemetry/sdk-trace-node" -import type { Instrumentation } from "@opentelemetry/instrumentation" +import type { NodeSDKConfiguration } from "@opentelemetry/sdk-node" import { TransactionOrchestrator } from "@medusajs/framework/orchestration" const EXCLUDED_RESOURCES = [".vite", "virtual:"] @@ -270,24 +270,34 @@ export function instrumentWorkflows() { * - @opentelemetry/instrumentation-pg * - @opentelemetry/instrumentation */ -export function registerOtel(options: { - serviceName: string - exporter: SpanExporter - instrument?: Partial<{ - http: boolean - query: boolean - workflows: boolean - db: boolean - }> - instrumentations?: Instrumentation[] -}) { +export function registerOtel( + options: Partial & { + serviceName: string + exporter?: SpanExporter + instrument?: Partial<{ + http: boolean + query: boolean + workflows: boolean + db: boolean + }> + } +) { + const { + exporter, + serviceName, + instrument, + instrumentations, + ...nodeSdkOptions + } = { + instrument: {}, + instrumentations: [], + ...options, + } + const { Resource } = require("@opentelemetry/resources") const { NodeSDK } = require("@opentelemetry/sdk-node") const { SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-node") - const instrument = options.instrument || {} - const instrumentations = options.instrumentations || [] - if (instrument.db) { const { PgInstrumentation } = require("@opentelemetry/instrumentation-pg") instrumentations.push(new PgInstrumentation()) @@ -303,13 +313,12 @@ export function registerOtel(options: { } const sdk = new NodeSDK({ - serviceName: options.serviceName, - resource: new Resource({ - "service.name": options.serviceName, - }), - spanProcessor: new SimpleSpanProcessor(options.exporter), + serviceName, + resource: new Resource({ "service.name": serviceName }), + spanProcessor: new SimpleSpanProcessor(exporter), + ...nodeSdkOptions, instrumentations: instrumentations, - }) + } satisfies Partial) sdk.start() return sdk