-
Notifications
You must be signed in to change notification settings - Fork 233
Add telemetry only for requests to graph.microsoft.com #336
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
Changes from 4 commits
c412e86
5bafd11
cfeb4a0
790eb30
f9a0d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| * @module TelemetryHandler | ||
| */ | ||
|
|
||
| import { graphURLs } from "../GraphRequestUtil"; | ||
| import { Context } from "../IContext"; | ||
| import { PACKAGE_VERSION } from "../Version"; | ||
|
|
||
|
|
@@ -64,23 +65,54 @@ export class TelemetryHandler implements Middleware { | |
| * @param {Context} context - The context object of the request | ||
| * @returns A Promise that resolves to nothing | ||
| */ | ||
|
|
||
| /** | ||
| * @private | ||
| * Checks if the request url points to the Graph service endpoints | ||
| * @param {string} url - The request url string | ||
| * @returns true if request url is a Graph url | ||
| */ | ||
| private isGraphURL(url: string): boolean { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Benefits:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also consider inlining the whole function as it will be a simple call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for suggesting an optimal approach! I have used the approach you suggested with some modifications. Implemented a set object with the host names. I haven't used the URL object as some browsers do not support of the URL object and I am extracting the host name as it is done in GraphRequest.ts during the url parsing process. |
||
| // Valid Graph URL pattern - https://graph.microsoft.com/{version}/{resource}?{query-parameters} | ||
| // Valid Graph URL example - https://graph.microsoft.com/v1.0/me | ||
|
|
||
| if (url.indexOf("https://") !== -1) { | ||
| url = url.replace("https://", ""); | ||
|
|
||
| // Find where the host ends | ||
| const endOfHostStrPos = url.indexOf("/"); | ||
| if (endOfHostStrPos !== -1) { | ||
| // Parse out the host | ||
| const hostName = url.substring(0, endOfHostStrPos); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't cover the scenario of |
||
| return graphURLs.has(hostName); | ||
nikithauc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return false; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| public async execute(context: Context): Promise<void> { | ||
| try { | ||
| let clientRequestId: string = getRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER); | ||
| if (clientRequestId === null) { | ||
| clientRequestId = generateUUID(); | ||
| setRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER, clientRequestId); | ||
| } | ||
| let sdkVersionValue: string = `${TelemetryHandler.PRODUCT_NAME}/${PACKAGE_VERSION}`; | ||
| let options: TelemetryHandlerOptions; | ||
| if (context.middlewareControl instanceof MiddlewareControl) { | ||
| options = context.middlewareControl.getMiddlewareOptions(TelemetryHandlerOptions) as TelemetryHandlerOptions; | ||
| } | ||
| if (typeof options !== "undefined") { | ||
| const featureUsage: string = options.getFeatureUsage(); | ||
| sdkVersionValue += ` (${TelemetryHandler.FEATURE_USAGE_STRING}=${featureUsage})`; | ||
| if (typeof context.request === "string" && this.isGraphURL(context.request)) { | ||
| let clientRequestId: string = getRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER); | ||
| if (clientRequestId === null) { | ||
| clientRequestId = generateUUID(); | ||
| setRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER, clientRequestId); | ||
| } | ||
| let sdkVersionValue: string = `${TelemetryHandler.PRODUCT_NAME}/${PACKAGE_VERSION}`; | ||
| let options: TelemetryHandlerOptions; | ||
| if (context.middlewareControl instanceof MiddlewareControl) { | ||
| options = context.middlewareControl.getMiddlewareOptions(TelemetryHandlerOptions) as TelemetryHandlerOptions; | ||
| } | ||
| if (typeof options !== "undefined") { | ||
| const featureUsage: string = options.getFeatureUsage(); | ||
| sdkVersionValue += ` (${TelemetryHandler.FEATURE_USAGE_STRING}=${featureUsage})`; | ||
| } | ||
|
|
||
| appendRequestHeader(context.request, context.options, TelemetryHandler.SDK_VERSION_HEADER, sdkVersionValue); | ||
| } | ||
| appendRequestHeader(context.request, context.options, TelemetryHandler.SDK_VERSION_HEADER, sdkVersionValue); | ||
|
|
||
| return await this.nextMiddleware.execute(context); | ||
| } catch (error) { | ||
| throw error; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could initialize the set directly from the array, see the edit in the response.
https://stackoverflow.com/a/30902786/3808675
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used an IIFE with set.add() instead as IE does not support Set(array). IE creates an empty set when I passed in new Set(object/array).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting, and I'm guessing this is an edge case the transpilation is not catching?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
additionally this is something that people (or we) can polyfill using core-js, see the first example. https://www.npmjs.com/package/core-js