From 475c2f07af85960e0a5197285032dfdce67b7b12 Mon Sep 17 00:00:00 2001 From: BidishaMS <97606074+BidishaMS@users.noreply.github.com> Date: Wed, 24 Jan 2024 00:22:09 +0530 Subject: [PATCH] Populate ContextInfo, TenantId and ObjectId attributes for Vscode Web (#816) * Populate ContextInfo object values * Populate ContextInfo object values --- .../OneDSLoggerTelemetry/IEventTypes.ts | 12 ++++ .../OneDSLoggerTelemetry/oneDSLogger.ts | 68 +++++++++++++++---- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/src/common/OneDSLoggerTelemetry/IEventTypes.ts b/src/common/OneDSLoggerTelemetry/IEventTypes.ts index 9eb4c926a..1bc547d30 100644 --- a/src/common/OneDSLoggerTelemetry/IEventTypes.ts +++ b/src/common/OneDSLoggerTelemetry/IEventTypes.ts @@ -10,6 +10,7 @@ import {IProDevCopilotTelemetryData} from '../copilot/telemetry/ITelemetry'; export interface IUserInfo { oid: string; tid: string; + puid ?: string; } export interface IEvent { @@ -34,4 +35,15 @@ export interface ICustomEvent { exceptionSource?: string; exceptionCauseCode?: string | number; exceptionDetails?: string; + } + + export interface IContextInfo { + orgId?: string; + portalId?: string; + websiteId?: string; + dataSource?: string; + schema?: string; + correlationId?: string; + referrer? :string; + envId?: string; } \ No newline at end of file diff --git a/src/common/OneDSLoggerTelemetry/oneDSLogger.ts b/src/common/OneDSLoggerTelemetry/oneDSLogger.ts index 41e2e97e6..a7cb8d30e 100644 --- a/src/common/OneDSLoggerTelemetry/oneDSLogger.ts +++ b/src/common/OneDSLoggerTelemetry/oneDSLogger.ts @@ -3,16 +3,18 @@ * Licensed under the MIT License. See License.txt in the project root for license information. */ -/* eslint-disable @typescript-eslint/no-non-null-assertion*/ +/* eslint-disable @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-inferrable-types*/ import { AppInsightsCore, type IExtendedConfiguration } from "@microsoft/1ds-core-js"; import { PostChannel, type IChannelConfiguration, type IXHROverride } from "@microsoft/1ds-post-js"; import { ITelemetryLogger } from "./ITelemetryLogger"; +import { IContextInfo, IUserInfo } from "./IEventTypes"; import { EventType, Severity } from "./telemetryConstants"; import * as vscode from "vscode"; import {getExtensionType, getExtensionVersion} from "../../common/Utils"; import { EXTENSION_ID } from "../../client/constants"; import {OneDSCollectorEventName} from "./EventContants"; +import { telemetryEventNames } from "../../web/client/telemetry/constants"; interface IInstrumentationSettings { endpointURL: string; @@ -24,6 +26,10 @@ export class OneDSLogger implements ITelemetryLogger{ private readonly appInsightsCore :AppInsightsCore; private readonly postChannel: PostChannel; + private static userInfo: IUserInfo = {oid: "", tid: "", puid: ""}; + private static contextInfo: IContextInfo ; + private static userRegion : string = ""; + private readonly regexPatternsToRedact = [ /key["\\ ']*[:=]+["\\ ']*([a-zA-Z0-9]*)/igm, /token["\\ ']*[:=]+["\\ ']*([a-zA-Z0-9]*)/igm, @@ -106,12 +112,25 @@ export class OneDSLogger implements ITelemetryLogger{ if ((coreConfig.instrumentationKey ?? "") !== "") { this.appInsightsCore.initialize(coreConfig, []); } + this.intitializeContextInfo(); // eslint-disable-next-line @typescript-eslint/no-explicit-any this.appInsightsCore.addTelemetryInitializer(this.populateCommonAttributes()); } + private intitializeContextInfo(){ + OneDSLogger.contextInfo = { + orgId: "", + portalId: "", + websiteId: "", + dataSource: "", + schema: "", + correlationId: "", + referrer: "", + envId: "" + } + } + private static getInstrumentationSettings(geo?:string): IInstrumentationSettings { - // eslint-disable-next-line @typescript-eslint/no-inferrable-types const region:string = "test"; // TODO: Remove it from here and replace it with value getting from build. Check gulp.mjs (setTelemetryTarget) const instrumentationSettings:IInstrumentationSettings = { endpointURL: 'https://self.pipe.aria.int.microsoft.com/OneCollector/1.0/', @@ -237,14 +256,9 @@ export class OneDSLogger implements ITelemetryLogger{ envelope.data.domain = vscode.env.appHost; // Adding below attributes so they get populated in Geneva. // TODO: It needs implementation for populating the actual value - envelope.data.tenantId = "test"; - envelope.data.principalObjectId = "test"; - envelope.data.puid = "test"; envelope.data.eventSubType = "test"; envelope.data.scenarioId = "test"; envelope.data.eventModifier = "test"; - envelope.data.userRegion = "test"; - envelope.data.context = "test"; envelope.data.timestamp = "test"; envelope.data.country = "test"; envelope.data.userLocale = "test"; @@ -257,11 +271,16 @@ export class OneDSLogger implements ITelemetryLogger{ envelope.data.screenResolution = "test"; envelope.data.osName = "test"; envelope.data.osVersion = "test"; - // envelope.data.timestamp = envelope.ext.user.locale; - // envelope.data.userLocale = envelope.ext.user.locale; - // envelope.data.userTimeZone = envelope.ext.loc.tz; - // envelope.data.appLanguage = envelope.ext.app.locale; - + if (getExtensionType() == 'Web'){ + this.populateVscodeWebAttributes(envelope); + }else{ + this.populateVscodeDesktopAttributes(envelope); + } + envelope.data.tenantId = OneDSLogger.userInfo?.tid; + envelope.data.principalObjectId = OneDSLogger.userInfo?.oid; + envelope.data.puid = OneDSLogger.userInfo?.puid; + envelope.data.context = JSON.stringify(OneDSLogger.contextInfo); + envelope.data.userRegion = OneDSLogger.userRegion; // At the end of event enrichment, redact the sensitive data for all the applicable fields // envelope = this.redactSensitiveDataFromEvent(envelope); } @@ -280,6 +299,31 @@ export class OneDSLogger implements ITelemetryLogger{ } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private populateVscodeWebAttributes(envelope: any) { + if (envelope.data.eventName == telemetryEventNames.WEB_EXTENSION_INIT_QUERY_PARAMETERS){ + OneDSLogger.userInfo.tid= JSON.parse(envelope.data.eventInfo).tenantId; + OneDSLogger.userRegion = JSON.parse(envelope.data.eventInfo).geo; + OneDSLogger.contextInfo.orgId = JSON.parse(envelope.data.eventInfo).orgId; + OneDSLogger.contextInfo.portalId = JSON.parse(envelope.data.eventInfo).portalId; + OneDSLogger.contextInfo.websiteId = JSON.parse(envelope.data.eventInfo).websiteId; + OneDSLogger.contextInfo.dataSource = JSON.parse(envelope.data.eventInfo).dataSource; + OneDSLogger.contextInfo.schema = JSON.parse(envelope.data.eventInfo).schema; + OneDSLogger.contextInfo.correlationId = JSON.parse(envelope.data.eventInfo).referrerSessionId; + OneDSLogger.contextInfo.referrer = JSON.parse(envelope.data.eventInfo).referrer; + OneDSLogger.contextInfo.envId = JSON.parse(envelope.data.eventInfo).envId; + } + if (envelope.data.eventName == telemetryEventNames.WEB_EXTENSION_DATAVERSE_AUTHENTICATION_COMPLETED){ + OneDSLogger.userInfo.oid= JSON.parse(envelope.data.eventInfo).userId; + } + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private populateVscodeDesktopAttributes(envelope: any){ + // TODO: this method helps in populating desktop attributes. + console.log(envelope); + } + //// Redact Sensitive data for the fields susceptible to contain codes/tokens/keys/secrets etc. //// This is done post event enrichment is complete to not impact the dependencies (if any) on actual values like Uri etc. // eslint-disable-next-line @typescript-eslint/no-explicit-any