1+ import { SourceMapper as DDSourceMapper } from '@datadog/pprof' ;
2+
13import { PyroscopeApiExporter } from '../pyroscope-api-exporter.js' ;
4+ import { PyroscopeConfig } from '../pyroscope-config.js' ;
5+ import { SourceMapper } from '../sourcemapper.js' ;
26import { ContinuousProfiler } from './continuous-profiler.js' ;
3- import { WallProfiler , WallProfilerStartArgs } from './wall-profiler.js' ;
47import { HeapProfiler , HeapProfilerStartArgs } from './heap-profiler.js' ;
5- import { PyroscopeConfig } from '../pyroscope-config .js' ;
8+ import { WallProfiler , WallProfilerStartArgs } from './wall-profiler .js' ;
69
710const MICROS_PER_SECOND = 1e6 ;
811const MS_PER_SECOND = 1e3 ;
@@ -76,7 +79,7 @@ export class PyroscopeProfiler {
7679 flushIntervalMs : flushIntervalMs ,
7780 profiler : new HeapProfiler ( ) ,
7881 startArgs : {
79- sourceMapper : config . sourceMapper ,
82+ sourceMapper : this . toDDSourceMapper ( config . sourceMapper ) ,
8083 samplingIntervalBytes :
8184 config . heap ?. samplingIntervalBytes ?? DEFAULT_SAMPLING_INTERVAL_BYTES ,
8285 stackDepth : config . heap ?. stackDepth ?? DEFAULT_STACK_DEPTH ,
@@ -95,7 +98,7 @@ export class PyroscopeProfiler {
9598 flushIntervalMs : flushIntervalMs ,
9699 profiler : new WallProfiler ( ) ,
97100 startArgs : {
98- sourceMapper : config . sourceMapper ,
101+ sourceMapper : this . toDDSourceMapper ( config . sourceMapper ) ,
99102 samplingDurationMs :
100103 config . wall ?. samplingDurationMs ?? DEFAULT_SAMPLING_DURATION_MS ,
101104 samplingIntervalMicros :
@@ -105,4 +108,28 @@ export class PyroscopeProfiler {
105108 } ,
106109 } ) ;
107110 }
111+
112+ /**
113+ * Converts a (Pyroscope) `SourceMapper` to a (DataDog) `SourceMapper`. These
114+ * two types have the same shape, but since the DataDog SourceMapper has a
115+ * private field `getMappingInfo`, we cannot use these two classes
116+ * interchangeably.
117+ *
118+ * For a more detailed explanation as to why the private method makes the
119+ * typical TypeScript type conversion impossible, see:
120+ *
121+ * https://github.com/microsoft/TypeScript/issues/7755#issuecomment-204161372
122+ *
123+ * @param sourceMapper A Pyroscope `SourceMapper`.
124+ * @return A DataDog `SourceMapper`.
125+ */
126+ private toDDSourceMapper (
127+ sourceMapper : SourceMapper | undefined
128+ ) : DDSourceMapper | undefined {
129+ if ( ! sourceMapper ) {
130+ return undefined ;
131+ }
132+
133+ return sourceMapper as unknown as DDSourceMapper ;
134+ }
108135}
0 commit comments