Skip to content

Commit

Permalink
Populate ContextInfo, TenantId and ObjectId attributes for Vscode Web (
Browse files Browse the repository at this point in the history
…#816)

* Populate ContextInfo object values

* Populate ContextInfo object values
  • Loading branch information
BidishaMS authored Jan 23, 2024
1 parent 556a9c5 commit 475c2f0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/common/OneDSLoggerTelemetry/IEventTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {IProDevCopilotTelemetryData} from '../copilot/telemetry/ITelemetry';
export interface IUserInfo {
oid: string;
tid: string;
puid ?: string;
}

export interface IEvent {
Expand All @@ -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;
}
68 changes: 56 additions & 12 deletions src/common/OneDSLoggerTelemetry/oneDSLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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/',
Expand Down Expand Up @@ -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";
Expand All @@ -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);
}
Expand All @@ -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
Expand Down

0 comments on commit 475c2f0

Please sign in to comment.