Skip to content

Commit 0a0b6cd

Browse files
authored
Fix SourceMapper typing (#101)
* Add whitespace * Use Pyroscope `SourceMapper` type externally Only convert to DDog `SourceMapper` type internally * yarn lint:fix
1 parent b11063e commit 0a0b6cd

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/profilers/pyroscope-profiler.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { SourceMapper as DDSourceMapper } from '@datadog/pprof';
2+
13
import { PyroscopeApiExporter } from '../pyroscope-api-exporter.js';
4+
import { PyroscopeConfig } from '../pyroscope-config.js';
5+
import { SourceMapper } from '../sourcemapper.js';
26
import { ContinuousProfiler } from './continuous-profiler.js';
3-
import { WallProfiler, WallProfilerStartArgs } from './wall-profiler.js';
47
import { HeapProfiler, HeapProfilerStartArgs } from './heap-profiler.js';
5-
import { PyroscopeConfig } from '../pyroscope-config.js';
8+
import { WallProfiler, WallProfilerStartArgs } from './wall-profiler.js';
69

710
const MICROS_PER_SECOND = 1e6;
811
const 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
}

src/pyroscope-config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { SourceMapper, LabelSet } from '@datadog/pprof';
1+
import { LabelSet } from '@datadog/pprof';
2+
import { SourceMapper } from './sourcemapper.js';
3+
24
export { LabelSet } from '@datadog/pprof';
35

46
export interface PyroscopeConfig {

src/sourcemapper.ts

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export class SourceMapper {
180180
`Looking for source map files in dirs: [${searchDirs.join(', ')}]`
181181
);
182182
}
183+
183184
const mapFiles: string[] = [];
184185
for (const dir of searchDirs) {
185186
try {
@@ -190,6 +191,7 @@ export class SourceMapper {
190191
} catch (e) {
191192
throw error(`failed to get source maps from ${dir}: ${e}`);
192193
}
194+
193195
try {
194196
const sf = await getSourceCodeFiles(dir);
195197
sf.forEach((sourceCodeFile) => {
@@ -199,9 +201,11 @@ export class SourceMapper {
199201
throw error(`failed to get source maps from ${dir}: ${e}`);
200202
}
201203
}
204+
202205
if (debug) {
203206
logger.debug(`Found source map files: [${mapFiles.join(', ')}]`);
204207
}
208+
205209
return createFromMapFiles(mapFiles, debug);
206210
}
207211

0 commit comments

Comments
 (0)