Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/util/common/telemetryCorrelationId.ts
13397 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { generateUuid } from '../vs/base/common/uuid';
7
8
/**
9
* Tracks a chain of calls for telemetry purposes.
10
*
11
* The list of callers is printed in reverse order, so the most recent caller is at the start.
12
*/
13
export class CallTracker {
14
private static readonly joiner = ' <- ';
15
16
public readonly value: string;
17
18
constructor(...parts: string[]) {
19
this.value = parts.join(CallTracker.joiner);
20
}
21
22
public toString(): string {
23
return this.value;
24
}
25
26
public toAscii(): string {
27
return this.value.replace(/[\u{0080}-\u{FFFF}]/gu, '');
28
}
29
30
public add(...parts: string[]): CallTracker {
31
return new CallTracker(...parts, this.value);
32
}
33
}
34
35
export class TelemetryCorrelationId {
36
public readonly callTracker: CallTracker;
37
public readonly correlationId: string;
38
39
constructor(caller: CallTracker | string | readonly string[], correlationId?: string) {
40
if (caller instanceof CallTracker) {
41
this.callTracker = caller;
42
} else {
43
this.callTracker = typeof caller === 'string' ? new CallTracker(caller) : new CallTracker(...caller);
44
}
45
46
this.correlationId = correlationId || generateUuid();
47
}
48
49
public addCaller(...parts: string[]): TelemetryCorrelationId {
50
return new TelemetryCorrelationId(this.callTracker.add(...parts), this.correlationId);
51
}
52
}
53
54