feat: allow all NodeSDK options via registerOtel (#11460)

This commit is contained in:
Harminder Virk
2025-02-14 15:37:32 +05:30
committed by GitHub
parent cbfbae42f2
commit bc02fde236
2 changed files with 35 additions and 21 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
feat: allow all NodeSDK options via registerOtel

View File

@@ -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<NodeSDKConfiguration> & {
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<NodeSDKConfiguration>)
sdk.start()
return sdk