Path: blob/main/extensions/copilot/src/util/common/telemetryCorrelationId.ts
13397 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { generateUuid } from '../vs/base/common/uuid';67/**8* Tracks a chain of calls for telemetry purposes.9*10* The list of callers is printed in reverse order, so the most recent caller is at the start.11*/12export class CallTracker {13private static readonly joiner = ' <- ';1415public readonly value: string;1617constructor(...parts: string[]) {18this.value = parts.join(CallTracker.joiner);19}2021public toString(): string {22return this.value;23}2425public toAscii(): string {26return this.value.replace(/[\u{0080}-\u{FFFF}]/gu, '');27}2829public add(...parts: string[]): CallTracker {30return new CallTracker(...parts, this.value);31}32}3334export class TelemetryCorrelationId {35public readonly callTracker: CallTracker;36public readonly correlationId: string;3738constructor(caller: CallTracker | string | readonly string[], correlationId?: string) {39if (caller instanceof CallTracker) {40this.callTracker = caller;41} else {42this.callTracker = typeof caller === 'string' ? new CallTracker(caller) : new CallTracker(...caller);43}4445this.correlationId = correlationId || generateUuid();46}4748public addCaller(...parts: string[]): TelemetryCorrelationId {49return new TelemetryCorrelationId(this.callTracker.add(...parts), this.correlationId);50}51}525354