Skip to content

Commit

Permalink
only used detectors when used
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael-theriault-swi committed Feb 28, 2024
1 parent 53088b5 commit 3d4e8d4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
43 changes: 24 additions & 19 deletions packages/instrumentations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,8 @@ import { type SocketIoInstrumentation } from "@opentelemetry/instrumentation-soc
import { type TediousInstrumentation } from "@opentelemetry/instrumentation-tedious"
import { type WinstonInstrumentation } from "@opentelemetry/instrumentation-winston"
import {
awsEc2Detector,
awsLambdaDetector,
} from "@opentelemetry/resource-detector-aws"
import { containerDetector } from "@opentelemetry/resource-detector-container"
import {
type DetectorSync,
detectResourcesSync,
envDetectorSync,
hostDetectorSync,
osDetectorSync,
processDetectorSync,
type Resource,
} from "@opentelemetry/resources"
import { load } from "@solarwinds-apm/module/load"
Expand Down Expand Up @@ -152,6 +144,18 @@ const INSTRUMENTATION_NAMES: Record<string, string> = {
"@opentelemetry/instrumentation-winston": "WinstonInstrumentation",
}

// map of resource detector package names to the names of their exported detectors
const RESOURCE_DETECTOR_NAMES: Record<string, string[]> = {
"@opentelemetry/resource-detector-aws": ["awsEc2Detector"],
"@opentelemetry/resource-detector-container": ["containerDetector"],
"@opentelemetry/resources": [
"envDetectorSync",
"hostDetectorSync",
"osDetectorSync",
"processDetectorSync",
],
}

export type InstrumentationConfigMap = {
[I in keyof InstrumentationTypes]?: InstrumentationTypes[I] extends {
setConfig(config: infer C): unknown
Expand Down Expand Up @@ -194,16 +198,17 @@ export function getInstrumentations(
else return instrumentations as Instrumentation[]
}

export function getDetectedResource(): Resource {
export async function getDetectedResource(): Promise<Resource> {
const detectors = await Promise.all(
Object.entries(RESOURCE_DETECTOR_NAMES).map(async ([name, detectors]) => {
const imported = (await import(name)) as object
return Object.entries(imported)
.filter(([name]) => detectors.includes(name))
.map(([, detector]) => detector as DetectorSync)
}),
)

return detectResourcesSync({
detectors: [
containerDetector,
awsEc2Detector,
awsLambdaDetector,
envDetectorSync,
hostDetectorSync,
osDetectorSync,
processDetectorSync,
],
detectors: detectors.flat(),
})
}
2 changes: 1 addition & 1 deletion packages/solarwinds-apm/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function init() {
}),
)
if (config.dev.resourceDetection) {
resource = resource.merge(getDetectedResource())
resource = resource.merge(await getDetectedResource())
}

const [reporter, serverlessApi] = IS_SERVERLESS
Expand Down

0 comments on commit 3d4e8d4

Please sign in to comment.