Skip to content

[prebuilds] increment metric only with state change #10621

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

Merged
merged 1 commit into from
Jun 13, 2022
Merged
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
14 changes: 11 additions & 3 deletions components/ws-manager-bridge/ee/src/prebuild-updater-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class PrebuildUpdaterDB implements PrebuildUpdater {
const workspaceId = status.metadata!.metaId!;
const logCtx: LogContext = { instanceId, workspaceId, userId };

log.info("Handling prebuild workspace update.", status);
log.info(logCtx, "Handling prebuild workspace update.", status);

const span = TraceContext.startSpan("updatePrebuiltWorkspace", ctx);
try {
Expand All @@ -57,7 +57,7 @@ export class PrebuildUpdaterDB implements PrebuildUpdater {
}
span.setTag("updatePrebuiltWorkspace.prebuildId", prebuild.id);
span.setTag("updatePrebuiltWorkspace.workspaceInstance.statusVersion", status.statusVersion);
log.info("Found prebuild record in database.", prebuild);
log.info(logCtx, "Found prebuild record in database.", prebuild);

// prebuild.statusVersion = 0 is the default value in the DB, these shouldn't be counted as stale in our metrics
if (prebuild.statusVersion > 0 && prebuild.statusVersion >= status.statusVersion) {
Expand All @@ -80,7 +80,15 @@ export class PrebuildUpdaterDB implements PrebuildUpdater {
span.setTag("updatePrebuildWorkspace.prebuild.state", updatedPrebuild.state);
span.setTag("updatePrebuildWorkspace.prebuild.error", updatedPrebuild.error);

if (writeToDB && updatedPrebuild.state && terminatingStates.includes(updatedPrebuild.state)) {
// Here we make sure that we increment the counter only when:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

// 1. the instance is governing ("writeToDB"), so that we don't get metrics from multiple pods,
// 2. the state changes (we can receive multiple events with the same state)
if (
writeToDB &&
updatedPrebuild.state &&
terminatingStates.includes(updatedPrebuild.state) &&
updatedPrebuild.state !== prebuild.state
) {
this.prometheusExporter.increasePrebuildsCompletedCounter(updatedPrebuild.state);
}

Expand Down