@@ -4,6 +4,7 @@ import assert from 'node:assert';
44import { config } from 'dotenv' ;
55import { readPackageUp } from 'read-package-up' ;
66import type { NormalizedPackageJson } from 'read-package-up' ;
7+ import { NodeSDKConfiguration } from '@opentelemetry/sdk-node' ;
78
89import type {
910 AnyServiceLocals ,
@@ -64,6 +65,7 @@ async function getServiceDetails(argv: BootstrapArguments = {}) {
6465 rootDirectory : path . dirname ( pkg . path ) ,
6566 name : parts [ parts . length - 1 ] ,
6667 version : pkg . packageJson . version ,
68+ customizer : ( pkg . packageJson . config ?. telemetry as { customizer ?: string } ) ?. customizer ,
6769 } ;
6870}
6971
@@ -81,7 +83,7 @@ export async function bootstrap<
8183 SLocals extends AnyServiceLocals = ServiceLocals < ConfigurationSchema > ,
8284 RLocals extends RequestLocals = RequestLocals ,
8385> ( argv ?: BootstrapArguments ) {
84- const { main, rootDirectory, name, version } = await getServiceDetails ( argv ) ;
86+ const { main, rootDirectory, name, version, customizer } = await getServiceDetails ( argv ) ;
8587
8688 let entrypoint : string ;
8789 let codepath : 'build' | 'dist' | 'src' = 'build' ;
@@ -104,12 +106,23 @@ export async function bootstrap<
104106
105107 const absoluteEntrypoint = path . resolve ( rootDirectory , entrypoint ) ;
106108 if ( argv ?. telemetry ) {
109+ let otelCustomizer :
110+ | ( ( options : Partial < NodeSDKConfiguration > ) => Partial < NodeSDKConfiguration > )
111+ | undefined = undefined ;
112+ if ( customizer ) {
113+ // Customize OTEL with a dynamic import based on the codePath (so put it in src, generally)
114+ otelCustomizer = ( await import ( `$(codePath}/${ customizer } ` ) ) . NodeSDKConfiguration ;
115+ if ( typeof otelCustomizer === 'object' ) {
116+ otelCustomizer = ( v ) => ( { ...v , ...( otelCustomizer as Partial < NodeSDKConfiguration > ) } ) ;
117+ }
118+ }
107119 return startWithTelemetry < SLocals , RLocals > ( {
108120 name,
109121 rootDirectory,
110122 service : absoluteEntrypoint ,
111123 codepath,
112124 version,
125+ customizer : otelCustomizer ,
113126 } ) ;
114127 }
115128
0 commit comments