Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Telemetry] Adding x-ms-user-agent in Dataverse calls to check the client surface (Vscode Web/Desktop) #911

Merged
merged 13 commits into from
Apr 23, 2024
11 changes: 10 additions & 1 deletion src/common/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


import * as vscode from "vscode";
import { EXTENSION_ID, SETTINGS_EXPERIMENTAL_STORE_NAME } from "../client/constants";
import { EXTENSION_ID, EXTENSION_NAME, SETTINGS_EXPERIMENTAL_STORE_NAME } from "../client/constants";
import { CUSTOM_TELEMETRY_FOR_POWER_PAGES_SETTING_NAME } from "./OneDSLoggerTelemetry/telemetryConstants";

export function getSelectedCode(editor: vscode.TextEditor): string {
Expand Down Expand Up @@ -123,3 +123,12 @@ export function isCustomTelemetryEnabled():boolean {
.get(CUSTOM_TELEMETRY_FOR_POWER_PAGES_SETTING_NAME);
return isCustomTelemetryEnabled as boolean;
}

export function getUserAgent(): string {
const userAgent = "{product}/{product-version} {comment}";

return userAgent
.replace("{product}", EXTENSION_NAME)
.replace("{product-version}", getExtensionVersion())
.replace("{comment}", "(" + getExtensionType()+'; )');
}
3 changes: 3 additions & 0 deletions src/common/copilot/dataverseMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CopilotDataverseMetadataFailureEvent, CopilotDataverseMetadataSuccessEv
import { getEntityMetadata } from "../../web/client/utilities/fileAndEntityUtil";
import { DOMParser } from "@xmldom/xmldom";
import { ATTRIBUTE_CLASSID, ATTRIBUTE_DATAFIELD_NAME, ATTRIBUTE_DESCRIPTION, ControlClassIdMap, SYSTEFORMS_API_PATH } from "./constants";
import { getUserAgent } from "../Utils";


declare const IS_DESKTOP: string | undefined;
Expand All @@ -25,6 +26,7 @@ export async function getEntityColumns(entityName: string, orgUrl: string, apiTo
headers: {
'Content-Type': "application/json",
Authorization: `Bearer ${apiToken}`,
"x-ms-user-agent": getUserAgent()
},
};

Expand Down Expand Up @@ -57,6 +59,7 @@ export async function getFormXml(entityName: string, formName: string, orgUrl:
headers: {
'Content-Type': "application/json",
Authorization: `Bearer ${apiToken}`,
"x-ms-user-agent": getUserAgent()
},
};

Expand Down
10 changes: 5 additions & 5 deletions src/web/client/WebExtensionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from "vscode";
import {
dataverseAuthentication,
getCommonHeaders,
getCommonHeadersForDataverse,
} from "./common/authenticationProvider";
import * as Constants from "./common/constants";
import {
Expand Down Expand Up @@ -360,7 +360,7 @@ class WebExtensionContext implements IWebExtensionContext {
Constants.queryParameters.WEBSITE_ID
) as string;

const headers = getCommonHeaders(this._dataverseAccessToken);
const headers = getCommonHeadersForDataverse(this._dataverseAccessToken);

// Populate shared workspace for Co-Presence
await this.populateSharedWorkspace(headers, dataverseOrgUrl, websiteId);
Expand Down Expand Up @@ -482,7 +482,7 @@ class WebExtensionContext implements IWebExtensionContext {

requestSentAtTime = new Date().getTime();
const response = await this._concurrencyHandler.handleRequest(requestUrl, {
headers: getCommonHeaders(accessToken),
headers: getCommonHeadersForDataverse(accessToken),
});
if (!response?.ok) {
throw new Error(JSON.stringify(response));
Expand Down Expand Up @@ -548,7 +548,7 @@ class WebExtensionContext implements IWebExtensionContext {

requestSentAtTime = new Date().getTime();
const response = await this._concurrencyHandler.handleRequest(requestUrl, {
headers: getCommonHeaders(accessToken),
headers: getCommonHeadersForDataverse(accessToken),
});
if (!response?.ok) {
throw new Error(JSON.stringify(response));
Expand Down Expand Up @@ -610,7 +610,7 @@ class WebExtensionContext implements IWebExtensionContext {

requestSentAtTime = new Date().getTime();
const response = await this._concurrencyHandler.handleRequest(requestUrl, {
headers: getCommonHeaders(accessToken),
headers: getCommonHeadersForDataverse(accessToken),
});

if (!response?.ok) {
Expand Down
19 changes: 18 additions & 1 deletion src/web/client/common/authenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import { ERRORS, showErrorDialog } from "./errorHandler";
import { ITelemetry } from "../../../client/telemetry/ITelemetry";
import { sendTelemetryEvent } from "../../../common/copilot/telemetry/copilotTelemetry";
import { CopilotLoginFailureEvent, CopilotLoginSuccessEvent } from "../../../common/copilot/telemetry/telemetryConstants";
import { getUserAgent } from "../../../common/Utils";


export function getCommonHeaders(
export function getCommonHeadersForDataverse(
accessToken: string,
useOctetStreamContentType?: boolean
) {
Expand All @@ -32,6 +33,22 @@ export function getCommonHeaders(
accept: "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"x-ms-user-agent": getUserAgent()
};
}

export function getCommonHeaders(
accessToken: string,
useOctetStreamContentType?: boolean
) {
return {
authorization: "Bearer " + accessToken,
"content-type": useOctetStreamContentType
? "application/octet-stream"
: "application/json; charset=utf-8",
accept: "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0"
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/web/client/dal/remoteFetchProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
setFileContent,
} from "../utilities/commonUtil";
import { getCustomRequestURL, getMappingEntityContent, getMetadataInfo, getMappingEntityId, getMimeType, getRequestURL } from "../utilities/urlBuilderUtil";
import { getCommonHeaders } from "../common/authenticationProvider";
import { getCommonHeadersForDataverse } from "../common/authenticationProvider";
import * as Constants from "../common/constants";
import { ERRORS, showErrorDialog } from "../common/errorHandler";
import { PortalsFS } from "./fileSystemProvider";
Expand Down Expand Up @@ -94,7 +94,7 @@ async function fetchFromDataverseAndCreateFiles(
requestSentAtTime = new Date().getTime();
const response = await WebExtensionContext.concurrencyHandler.handleRequest(requestUrl, {
headers: {
...getCommonHeaders(
...getCommonHeadersForDataverse(
WebExtensionContext.dataverseAccessToken
),
Prefer: `odata.maxpagesize=${Constants.MAX_ENTITY_FETCH_COUNT}, odata.include-annotations="Microsoft.Dynamics.CRM.*"`,
Expand Down Expand Up @@ -506,7 +506,7 @@ async function fetchMappingEntityContent(
requestSentAtTime = new Date().getTime();

const response = await WebExtensionContext.concurrencyHandler.handleRequest(requestUrl, {
headers: getCommonHeaders(accessToken),
headers: getCommonHeadersForDataverse(accessToken),
});

if (!response.ok) {
Expand Down
4 changes: 2 additions & 2 deletions src/web/client/dal/remoteSaveProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { RequestInit } from "node-fetch";
import * as vscode from "vscode";
import { getCommonHeaders } from "../common/authenticationProvider";
import { getCommonHeadersForDataverse } from "../common/authenticationProvider";
import { BAD_REQUEST, MIMETYPE, queryParameters } from "../common/constants";
import { showErrorDialog } from "../common/errorHandler";
import { FileData } from "../context/fileData";
Expand Down Expand Up @@ -87,7 +87,7 @@ async function getSaveParameters(
webFileV2
);

saveCallParameters.requestInit.headers = getCommonHeaders(
saveCallParameters.requestInit.headers = getCommonHeadersForDataverse(
accessToken,
useOctetStreamContentType(entityName, attributePath.source)
);
Expand Down
6 changes: 3 additions & 3 deletions src/web/client/services/etagHandlerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as vscode from "vscode";
import { RequestInit } from "node-fetch";
import { getCommonHeaders } from "../common/authenticationProvider";
import { getCommonHeadersForDataverse } from "../common/authenticationProvider";
import { httpMethod, ODATA_ETAG, queryParameters } from "../common/constants";
import { IAttributePath } from "../common/interfaces";
import { PortalsFS } from "../dal/fileSystemProvider";
Expand Down Expand Up @@ -53,7 +53,7 @@ export class EtagHandlerService {
try {
const requestInit: RequestInit = {
method: httpMethod.GET,
headers: getCommonHeaders(
headers: getCommonHeadersForDataverse(
WebExtensionContext.dataverseAccessToken
),
};
Expand Down Expand Up @@ -165,7 +165,7 @@ export class EtagHandlerService {
try {
const requestInit: RequestInit = {
method: httpMethod.GET,
headers: getCommonHeaders(
headers: getCommonHeadersForDataverse(
WebExtensionContext.dataverseAccessToken
),
};
Expand Down
8 changes: 4 additions & 4 deletions src/web/client/test/integration/WebExtensionContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as authenticationProvider from "../../common/authenticationProvider";
import { telemetryEventNames } from "../../telemetry/constants";
import * as schemaHelperUtil from "../../utilities/schemaHelperUtil";
import * as urlBuilderUtil from "../../utilities/urlBuilderUtil";
import { getCommonHeaders } from "../../common/authenticationProvider";
import { getCommonHeadersForDataverse } from "../../common/authenticationProvider";
import { IAttributePath } from "../../common/interfaces";

describe("WebExtensionContext", () => {
Expand Down Expand Up @@ -532,7 +532,7 @@ describe("WebExtensionContext", () => {
//#endregion

//#region Fetch
const header = getCommonHeaders(accessToken);
const header = getCommonHeadersForDataverse(accessToken);
assert.callCount(_mockFetch, 3);
const firstFetchCall = _mockFetch.getCalls()[0];
expect(firstFetchCall.args[0], requestUrl);
Expand Down Expand Up @@ -669,7 +669,7 @@ describe("WebExtensionContext", () => {
//#endregion

//#region Fetch
const header = getCommonHeaders(accessToken);
const header = getCommonHeadersForDataverse(accessToken);
assert.calledThrice(_mockFetch);
const firstFetchCall = _mockFetch.getCalls()[0];
expect(firstFetchCall.args[0], requestUrl);
Expand Down Expand Up @@ -759,7 +759,7 @@ describe("WebExtensionContext", () => {

assert.calledOnceWithExactly(dataverseAuthentication, ORG_URL, true);
//#region Fetch
const header = getCommonHeaders(accessToken);
const header = getCommonHeadersForDataverse(accessToken);
assert.calledThrice(_mockFetch);
const firstFetchCall = _mockFetch.getCalls()[0];
expect(firstFetchCall.args[0], requestUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { BAD_REQUEST } from "../../common/constants";
import * as urlBuilderUtil from "../../utilities/urlBuilderUtil";
import { IAttributePath } from "../../common/interfaces";
import { telemetryEventNames } from "../../telemetry/constants";
import { getUserAgent } from "../../../../common/Utils";

describe("remoteSaveProvider", () => {
afterEach(() => {
Expand Down Expand Up @@ -93,6 +94,7 @@ describe("remoteSaveProvider", () => {
accept: "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"x-ms-user-agent": getUserAgent()
},
});

Expand Down Expand Up @@ -211,6 +213,7 @@ describe("remoteSaveProvider", () => {
accept: "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"x-ms-user-agent": getUserAgent()
},
};
const fetchCalls = _mockFetch.getCalls();
Expand Down Expand Up @@ -288,6 +291,7 @@ describe("remoteSaveProvider", () => {
accept: "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
"x-ms-user-agent": getUserAgent()
},
});
assert.calledOnce(getColumnContent);
Expand Down Expand Up @@ -377,7 +381,8 @@ describe("remoteSaveProvider", () => {
accept: "application/json",
"OData-MaxVersion": "4.0",
"OData-Version": "4.0",
'x-ms-file-name': 'testfilename'
'x-ms-file-name': 'testfilename',
"x-ms-user-agent": getUserAgent()
},
}
);
Expand Down
Loading