Skip to content

Commit

Permalink
fix: mos api telemetry (#11936)
Browse files Browse the repository at this point in the history
* fix: mos api telemetry

* fix: mos api telemetry

* test: ut

* test: ut
  • Loading branch information
jayzhang authored Jul 2, 2024
1 parent 6e43342 commit 530dccd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
4 changes: 4 additions & 0 deletions packages/fx-core/src/common/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ export enum TelemetryProperty {
HasAzureOpenAIEndpoint = "has-azure-openai-endpoint",
HasAzureOpenAIDeploymentName = "has-azure-openai-deployment-name",
HasOpenAIKey = "has-openai-key",

TDPTraceId = "tdp-trace-id",
MOSTraceId = "mos-trace-id",
}

export const TelemetryConstants = {
Expand Down Expand Up @@ -156,6 +159,7 @@ export enum TelemetryEvent {
ProjectType = "project-type",
DependencyApi = "dependency-api",
AppStudioApi = "app-studio-api",
MOSApi = "ttk-mos-api",
}

export enum ProjectTypeProps {
Expand Down
45 changes: 26 additions & 19 deletions packages/fx-core/src/common/wrappedAxiosClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ export class WrappedAxiosClient {
params: this.generateParameters(request.params),
...this.generateExtraProperties(fullPath, request.data),
};

let eventName: string;
if (this.isTDPApi(fullPath)) {
eventName = TelemetryEvent.AppStudioApi;
} else {
eventName = TelemetryEvent.DependencyApi;
}

const eventName = this.getEventName(fullPath);
TOOLS?.telemetryReporter?.sendTelemetryEvent(`${eventName}-start`, properties);
return request;
}
Expand All @@ -80,12 +73,7 @@ export class WrappedAxiosClient {
...this.generateExtraProperties(fullPath, response.data),
};

let eventName: string;
if (this.isTDPApi(fullPath)) {
eventName = TelemetryEvent.AppStudioApi;
} else {
eventName = TelemetryEvent.DependencyApi;
}
const eventName = this.getEventName(fullPath);
TOOLS?.telemetryReporter?.sendTelemetryEvent(eventName, properties);
return response;
}
Expand Down Expand Up @@ -122,8 +110,8 @@ export class WrappedAxiosClient {
...this.generateExtraProperties(fullPath, requestData),
};

let eventName: string;
if (this.isTDPApi(fullPath)) {
const eventName = this.getEventName(fullPath);
if (eventName === TelemetryEvent.AppStudioApi) {
const correlationId = error.response?.headers[Constants.CORRELATION_ID] ?? "undefined";
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
const extraData = error.response?.data ? `data: ${JSON.stringify(error.response.data)}` : "";
Expand All @@ -137,9 +125,16 @@ export class WrappedAxiosClient {
TelemetryProperty.ErrorCode
] = `${TDPApiFailedError.source}.${TDPApiFailedError.name}`;
properties[TelemetryProperty.ErrorMessage] = TDPApiFailedError.message;
eventName = TelemetryEvent.AppStudioApi;
} else {
eventName = TelemetryEvent.DependencyApi;
properties[TelemetryProperty.TDPTraceId] = correlationId;
} else if (eventName === TelemetryEvent.MOSApi) {
const tracingId = (error.response?.headers?.traceresponse ?? "undefined") as string;
const originalMessage = error.message;
const innerError = (error.response?.data as any).error || { code: "", message: "" };
const finalMessage = `${originalMessage} (tracingId: ${tracingId}) ${
innerError.code as string
}: ${innerError.message as string} `;
properties[TelemetryProperty.ErrorMessage] = finalMessage;
properties[TelemetryProperty.MOSTraceId] = tracingId;
}

TOOLS?.telemetryReporter?.sendTelemetryErrorEvent(eventName, properties);
Expand Down Expand Up @@ -295,6 +290,18 @@ export class WrappedAxiosClient {
return matches != null && matches.length > 0;
}

private static getEventName(
baseUrl: string
): TelemetryEvent.MOSApi | TelemetryEvent.AppStudioApi | TelemetryEvent.DependencyApi {
if (this.isTDPApi(baseUrl)) {
return TelemetryEvent.AppStudioApi;
} else if (baseUrl.includes("titles.prod.mos.microsoft.com")) {
return TelemetryEvent.MOSApi;
} else {
return TelemetryEvent.DependencyApi;
}
}

/**
* Flattern query parameters to string, e.g. {a: 1, b: 2} => a:1;b:2
* @param params
Expand Down
21 changes: 21 additions & 0 deletions packages/fx-core/tests/common/wrappedAxiosClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,27 @@ describe("Wrapped Axios Client Test", () => {
chai.expect(telemetryChecker.calledOnce).to.be.true;
});

it("MOS API error response", async () => {
const mockedError = {
request: {
method: "GET",
host: "https://titles.prod.mos.microsoft.com",
path: "/users/packages",
},
config: {},
response: {
status: 400,
data: {
code: "BadRequest",
message: "Invalid request",
},
},
} as any;
const telemetryChecker = sinon.spy(mockTools.telemetryReporter, "sendTelemetryErrorEvent");
WrappedAxiosClient.onRejected(mockedError);
chai.expect(telemetryChecker.calledOnce).to.be.true;
});

it("Create bot API start telemetry", async () => {
const mockedRequest = {
method: "POST",
Expand Down

0 comments on commit 530dccd

Please sign in to comment.