Skip to content

Commit

Permalink
feat: allow all NodeSDK options via registerOtel (#11460)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Feb 14, 2025
1 parent cbfbae4 commit bc02fde
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-lobsters-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat: allow all NodeSDK options via registerOtel
51 changes: 30 additions & 21 deletions packages/medusa/src/instrumentation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:"]
Expand Down Expand Up @@ -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())
Expand All @@ -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
Expand Down

0 comments on commit bc02fde

Please sign in to comment.