Skip to content

Commit

Permalink
fix metrics and trace public api calls
Browse files Browse the repository at this point in the history
  • Loading branch information
akosyakov committed Oct 2, 2023
1 parent 7f9d889 commit 22be512
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
3 changes: 1 addition & 2 deletions components/dashboard/src/service/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* See License.AGPL.txt in the project root for license information.
*/

import { GitpodHostUrl } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";
import { MetricsReporter } from "@gitpod/public-api/lib/metrics";
import { getExperimentsClient } from "../experiments/client";
import { v4 } from "uuid";
Expand All @@ -13,7 +12,7 @@ const commit = require("./config.json").commit;
const originalConsoleError = console.error;

const metricsReporter = new MetricsReporter({
gitpodUrl: new GitpodHostUrl(window.location.href).withoutWorkspacePrefix().toString(),
gitpodUrl: window.location.href,
clientName: "dashboard",
clientVersion: commit,
log: {
Expand Down
21 changes: 18 additions & 3 deletions components/server/src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ export class API {
return {
get(target, prop) {
return (...args: any[]) => {
const startedAt = performance.now();
const method = type.methods[prop as any];
if (!method) {
// Increment metrics for unknown method attempts
console.warn("public api: unknown method", grpc_service, prop);
log.warn("public api: unknown method", grpc_service, prop);
const code = Code.Unimplemented;
grpcServerStarted.labels(grpc_service, "unknown", "unknown").inc();
grpcServerHandled.labels(grpc_service, "unknown", "unknown", Code[code]).inc();
Expand All @@ -134,11 +135,24 @@ export class API {
const grpc_code = err ? Code[err.code] : "OK";
grpcServerHandled.labels(grpc_service, grpc_method, grpc_type, grpc_code).inc();
stopTimer({ grpc_code });
let callDuration;
if (callStartedAt) {
callDuration = performance.now() - callStartedAt;
}
withRequestContext(log.debug, log, [
"public api: done",
{
grpc_code,
duration: performance.now() - startedAt,
verifyDuration,
callDuration,
},
]);
};
const handleError = (reason: unknown) => {
let err = ConnectError.from(reason, Code.Internal);
if (reason != err && err.code === Code.Internal) {
console.error("public api: unexpected internal error", reason);
withRequestContext(log.error, log, [`public api: unexpected internal error`, reason]);
err = ConnectError.from(
`Oops! Something went wrong. Please quote the request ID ${requestId} when reaching out to Gitpod Support.`,
Code.Internal,
Expand All @@ -149,6 +163,7 @@ export class API {
};

let verifyDuration: number | undefined;
let callStartedAt: number | undefined;
const context = args[1] as HandlerContext;
function withRequestContext<T>(
target: Function,
Expand All @@ -162,7 +177,6 @@ export class API {
requestId,
grpc_service,
grpc_method,
verifyDuration,
},
() => Reflect.apply(target, thisArgument, argumentsList),
);
Expand All @@ -174,6 +188,7 @@ export class API {
verifyDuration = performance.now() - verifyStartedAt;
context.user = user;

callStartedAt = performance.now();
if (grpc_type === "unary" || grpc_type === "client_stream") {
return withRequestContext(target[prop as any], target, args);
}
Expand Down

0 comments on commit 22be512

Please sign in to comment.