/*! * Copyright (c) Microsoft Corporation and contributors. All rights reserved. * Licensed under the MIT License. */ import { BaseTelemetryProperties, type CommonProperties } from "./resources"; /** * @internal */ export interface ITelemetryContextProperties { [BaseTelemetryProperties.tenantId]: string; [BaseTelemetryProperties.documentId]: string; [BaseTelemetryProperties.correlationId]: string; [BaseTelemetryProperties.requestSource]: string; [CommonProperties.serviceName]: string; } /** * @internal */ export function isTelemetryContextProperties(props: unknown): props is ITelemetryContextProperties { return ( typeof props === "object" && props !== null && typeof props[BaseTelemetryProperties.tenantId] === "string" && typeof props[BaseTelemetryProperties.documentId] === "string" && typeof props[BaseTelemetryProperties.correlationId] === "string" ); } /** * @internal */ export interface ITelemetryContext { /** * Bind properties to context where `callback()` is executed. * After this, `getProperties()` within `callback` will include `props`. */ bindProperties(props: Partial, callback: () => void): void; /** * Promisified {@link ITelemetryContext.bindProperties}. */ bindPropertiesAsync( props: Partial, callback: () => Promise, ): Promise; /** * Retrieve contextual properties for telemetry. */ getProperties(): Partial; } export class NullTelemetryContext implements ITelemetryContext { public getProperties(): Partial { return {}; } public bindProperties(props: Partial, callback: () => void): void { callback(); } public async bindPropertiesAsync( props: Partial, callback: () => Promise, ): Promise { return callback(); } } const nullTelemetryContext = new NullTelemetryContext(); export const getGlobal = () => (typeof window !== "undefined" ? window : global); /** * @internal */ export const getGlobalTelemetryContext = () => (getGlobal().telemetryContext as ITelemetryContext | undefined) ?? nullTelemetryContext; /** * @internal */ export const setGlobalTelemetryContext = (telemetryContext: ITelemetryContext) => { getGlobal().telemetryContext = telemetryContext; };