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

Added HTTP telemetry logging in restWrapper #23599

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ const uploadBlob = async (
getGlobalTelemetryContext().getProperties().correlationId ??
uuid() /* getCorrelationId */,
() => getGlobalTelemetryContext().getProperties() /* getTelemetryContextProperties */,
undefined /* refreshTokenIfNeeded */,
"alfred",
true /* enableTelemetry */,
);
return restWrapper.post(uri, blobData, undefined, {
"Content-Type": "application/json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { SummaryObject } from '@fluidframework/protocol-definitions';

// @internal (undocumented)
export class BasicRestWrapper extends RestWrapper {
constructor(baseurl?: string, defaultQueryString?: Record<string, string | number | boolean>, maxBodyLength?: number, maxContentLength?: number, defaultHeaders?: RawAxiosRequestHeaders, axios?: AxiosInstance, refreshDefaultQueryString?: (() => Record<string, string | number | boolean>) | undefined, refreshDefaultHeaders?: (() => RawAxiosRequestHeaders) | undefined, getCorrelationId?: (() => string | undefined) | undefined, getTelemetryContextProperties?: (() => Record<string, string | number | boolean> | undefined) | undefined, refreshTokenIfNeeded?: ((authorizationHeader: RawAxiosRequestHeaders) => Promise<RawAxiosRequestHeaders | undefined>) | undefined);
constructor(baseurl?: string, defaultQueryString?: Record<string, string | number | boolean>, maxBodyLength?: number, maxContentLength?: number, defaultHeaders?: RawAxiosRequestHeaders, axios?: AxiosInstance, refreshDefaultQueryString?: (() => Record<string, string | number | boolean>) | undefined, refreshDefaultHeaders?: (() => RawAxiosRequestHeaders) | undefined, getCorrelationId?: (() => string | undefined) | undefined, getTelemetryContextProperties?: (() => Record<string, string | number | boolean> | undefined) | undefined, refreshTokenIfNeeded?: ((authorizationHeader: RawAxiosRequestHeaders) => Promise<RawAxiosRequestHeaders | undefined>) | undefined, serviceName?: string | undefined, enableTelemetry?: boolean);
// (undocumented)
protected request<T>(requestConfig: AxiosRequestConfig, statusCode: number, canRetry?: boolean): Promise<T>;
}
Expand Down
1 change: 1 addition & 0 deletions server/routerlicious/packages/services-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@fluidframework/gitresources": "workspace:~",
"@fluidframework/protocol-base": "workspace:~",
"@fluidframework/protocol-definitions": "^3.2.0",
"@fluidframework/server-services-telemetry": "workspace:~",
"axios": "^1.7.7",
"crc-32": "1.2.0",
"debug": "^4.3.4",
Expand Down
41 changes: 40 additions & 1 deletion server/routerlicious/packages/services-client/src/restWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import {
} from "axios";
import { v4 as uuid } from "uuid";
import { debug } from "./debug";
import {
Lumberjack,
LumberEventName,
HttpProperties,
BaseTelemetryProperties,
CommonProperties,
} from "@fluidframework/server-services-telemetry";
import { createFluidServiceNetworkError, INetworkErrorDetails } from "./error";
import { CorrelationIdHeaderName, TelemetryContextHeaderName } from "./constants";

Expand Down Expand Up @@ -170,6 +177,8 @@ export class BasicRestWrapper extends RestWrapper {
private readonly refreshTokenIfNeeded?: (
authorizationHeader: RawAxiosRequestHeaders,
) => Promise<RawAxiosRequestHeaders | undefined>,
private readonly serviceName?: string,
private readonly enableTelemetry = false,
) {
super(baseurl, defaultQueryString, maxBodyLength, maxContentLength);
}
Expand All @@ -180,9 +189,10 @@ export class BasicRestWrapper extends RestWrapper {
canRetry = true,
): Promise<T> {
const options = { ...requestConfig };
const correlationId = this.getCorrelationId?.() ?? uuid();
options.headers = this.generateHeaders(
options.headers,
this.getCorrelationId?.() ?? uuid(),
correlationId,
this.getTelemetryContextProperties?.(),
);

Expand All @@ -202,6 +212,8 @@ export class BasicRestWrapper extends RestWrapper {
}

return new Promise<T>((resolve, reject) => {
const startTime = performance.now();
let axiosError: AxiosError<any>;
this.axios
.request<T>(options)
.then((response) => {
Expand Down Expand Up @@ -248,6 +260,7 @@ export class BasicRestWrapper extends RestWrapper {

this.request<T>(retryConfig, statusCode, false).then(resolve).catch(reject);
} else {
axiosError = error;
const errorSourceMessage = `[${error?.config?.method ?? ""}] request to [${
error?.config?.baseURL ?? options.baseURL ?? ""
}] failed with [${error.response?.status}] status code`;
Expand Down Expand Up @@ -292,6 +305,32 @@ export class BasicRestWrapper extends RestWrapper {
reject(createFluidServiceNetworkError(500, details));
}
}
})
.finally(() => {
if (this.enableTelemetry) {
const properties = {
[HttpProperties.method]: options.method ?? "METHOD_UNAVAILABLE",
[HttpProperties.url]: options.url ?? "URL_UNAVAILABLE",
[BaseTelemetryProperties.correlationId]: correlationId,
[CommonProperties.serviceName]: this.serviceName,
[CommonProperties.telemetryGroupName]: "rest_wrapper",
};
const httpMetric = Lumberjack.newLumberMetric(
LumberEventName.RestWrapper,
properties,
);
const endTime = performance.now();
httpMetric.setProperty("durationInMs", endTime - startTime);
if (axiosError) {
httpMetric.setProperty(
HttpProperties.status,
axiosError?.response?.status ?? "STATUS_UNAVAILABLE",
);
httpMetric.error("HttpRequest failed");
} else {
httpMetric.success("HttpRequest completed");
}
}
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export enum LumberEventName {
LivenessProbe = "LivenessProbe",
ReadinessProbe = "ReadinessProbe",
CircuitBreaker = "CircuitBreaker",
RestWrapper = "RestWrapper",
}
2 changes: 2 additions & 0 deletions server/routerlicious/packages/services/src/deltaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ export class DeltaManager implements IDeltaService {
() => getGlobalTelemetryContext().getProperties().correlationId /* getCorrelationId */,
() => getGlobalTelemetryContext().getProperties() /* getTelemetryContextProperties */,
refreshTokenIfNeeded,
"deltaManager" /* service */,
true /* enableTelemetry */,
);
return restWrapper;
}
Expand Down
2 changes: 2 additions & 0 deletions server/routerlicious/packages/services/src/documentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export class DocumentManager implements IDocumentManager {
() => getGlobalTelemetryContext().getProperties().correlationId /* getCorrelationId */,
() => getGlobalTelemetryContext().getProperties() /* getTelemetryContextProperties */,
refreshTokenIfNeeded /* refreshTokenIfNeeded */,
"documentManager" /* service */,
true /* enableTelemetry */,
);
return restWrapper;
}
Expand Down
17 changes: 17 additions & 0 deletions server/routerlicious/packages/services/src/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ export class TenantManager implements core.ITenantManager, core.ITenantConfigMan
undefined /* refreshDefaultHeaders */,
() => getGlobalTelemetryContext().getProperties().correlationId,
() => getGlobalTelemetryContext().getProperties(),
undefined /* refreshTokenIfNeeded */,
"tenant" /* service */,
true /* enableTelemetry */,
);
const result = await restWrapper.post<core.ITenantConfig & { key: string }>(
`${this.endpoint}/api/tenants/${encodeURIComponent(tenantId || "")}`,
Expand Down Expand Up @@ -231,6 +234,8 @@ export class TenantManager implements core.ITenantManager, core.ITenantConfigMan
() => getGlobalTelemetryContext().getProperties().correlationId,
() => getGlobalTelemetryContext().getProperties(),
refreshTokenIfNeeded,
"tenant" /* service */,
true /* enableTelemetry */,
);
const historian = new Historian(baseUrl, true, false, tenantRestWrapper);
const gitManager = new GitManager(historian);
Expand All @@ -250,6 +255,9 @@ export class TenantManager implements core.ITenantManager, core.ITenantConfigMan
undefined /* refreshDefaultHeaders */,
() => getGlobalTelemetryContext().getProperties().correlationId,
() => getGlobalTelemetryContext().getProperties(),
undefined /* refreshTokenIfNeeded */,
"tenant" /* service */,
true /* enableTelemetry */,
);
await restWrapper.post(
`${this.endpoint}/api/tenants/${encodeURIComponent(tenantId)}/validate`,
Expand All @@ -269,6 +277,9 @@ export class TenantManager implements core.ITenantManager, core.ITenantConfigMan
undefined /* refreshDefaultHeaders */,
() => getGlobalTelemetryContext().getProperties().correlationId,
() => getGlobalTelemetryContext().getProperties(),
undefined /* refreshTokenIfNeeded */,
"tenant" /* service */,
true /* enableTelemetry */,
);
const result = await restWrapper.get<core.ITenantKeys>(
`${this.endpoint}/api/tenants/${encodeURIComponent(tenantId)}/keys`,
Expand Down Expand Up @@ -298,6 +309,9 @@ export class TenantManager implements core.ITenantManager, core.ITenantConfigMan
undefined /* refreshDefaultHeaders */,
() => getGlobalTelemetryContext().getProperties().correlationId,
() => getGlobalTelemetryContext().getProperties(),
undefined /* refreshTokenIfNeeded */,
"tenant" /* service */,
true /* enableTelemetry */,
);
const result = await restWrapper.post<core.IFluidAccessToken>(
`${this.endpoint}/api/tenants/${encodeURIComponent(tenantId)}/accesstoken`,
Expand Down Expand Up @@ -337,6 +351,9 @@ export class TenantManager implements core.ITenantManager, core.ITenantConfigMan
undefined /* refreshDefaultHeaders */,
() => getGlobalTelemetryContext().getProperties().correlationId,
() => getGlobalTelemetryContext().getProperties(),
undefined /* refreshTokenIfNeeded */,
"tenant" /* service */,
true /* enableTelemetry */,
);
return restWrapper.get<core.ITenantConfig>(`${this.endpoint}/api/tenants/${tenantId}`, {
includeDisabledTenant,
Expand Down
Loading
Loading