-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: only convert monotonic to wall upon export
- Loading branch information
Showing
2 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
addHrTimes, | ||
millisToHrTime, | ||
OTLPTraceExporter, | ||
ReadableSpan, | ||
} from '../deps.ts'; | ||
|
||
const LIMIT = new Date('2000-01-01').getTime() / 1000; | ||
const isMonotonicHrTime = (hrTime: [number, number]): boolean => | ||
hrTime[0] < LIMIT; | ||
// @ts-ignore | ||
const variableTimeOrigin = () => new Date() - performance.now(); | ||
|
||
export class CustomOTLPTraceExporter extends OTLPTraceExporter { | ||
export(spans: ReadableSpan[], resultCallback: (result: any) => void): void { | ||
// Current timeOrigin | ||
const hrTime = millisToHrTime(variableTimeOrigin()); | ||
// Just before exporting | ||
for (const span of spans) { | ||
// Only if the spans were created with the monotonic clock | ||
if (!isMonotonicHrTime(span.startTime)) { | ||
return; | ||
} | ||
// Convert the times to wall-clock | ||
// @ts-ignore | ||
span.startTime = addHrTimes(span.startTime, hrTime); | ||
// @ts-ignore | ||
span.endTime = addHrTimes(span.endTime, hrTime); | ||
for (const event of span.events) { | ||
event.time = addHrTimes(event.time, hrTime); | ||
} | ||
} | ||
// And proceed to the regular export | ||
return super.export(spans, resultCallback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters