@@ -45,7 +45,7 @@ export class PrebuildUpdaterDB implements PrebuildUpdater {
45
45
const workspaceId = status . metadata ! . metaId ! ;
46
46
const logCtx : LogContext = { instanceId, workspaceId, userId } ;
47
47
48
- log . info ( "Handling prebuild workspace update." , status ) ;
48
+ log . info ( logCtx , "Handling prebuild workspace update." , status ) ;
49
49
50
50
const span = TraceContext . startSpan ( "updatePrebuiltWorkspace" , ctx ) ;
51
51
try {
@@ -57,7 +57,7 @@ export class PrebuildUpdaterDB implements PrebuildUpdater {
57
57
}
58
58
span . setTag ( "updatePrebuiltWorkspace.prebuildId" , prebuild . id ) ;
59
59
span . setTag ( "updatePrebuiltWorkspace.workspaceInstance.statusVersion" , status . statusVersion ) ;
60
- log . info ( "Found prebuild record in database." , prebuild ) ;
60
+ log . info ( logCtx , "Found prebuild record in database." , prebuild ) ;
61
61
62
62
// prebuild.statusVersion = 0 is the default value in the DB, these shouldn't be counted as stale in our metrics
63
63
if ( prebuild . statusVersion > 0 && prebuild . statusVersion >= status . statusVersion ) {
@@ -80,7 +80,15 @@ export class PrebuildUpdaterDB implements PrebuildUpdater {
80
80
span . setTag ( "updatePrebuildWorkspace.prebuild.state" , updatedPrebuild . state ) ;
81
81
span . setTag ( "updatePrebuildWorkspace.prebuild.error" , updatedPrebuild . error ) ;
82
82
83
- if ( writeToDB && updatedPrebuild . state && terminatingStates . includes ( updatedPrebuild . state ) ) {
83
+ // Here we make sure that we increment the counter only when:
84
+ // 1. the instance is governing ("writeToDB"), so that we don't get metrics from multiple pods,
85
+ // 2. the state changes (we can receive multiple events with the same state)
86
+ if (
87
+ writeToDB &&
88
+ updatedPrebuild . state &&
89
+ terminatingStates . includes ( updatedPrebuild . state ) &&
90
+ updatedPrebuild . state !== prebuild . state
91
+ ) {
84
92
this . prometheusExporter . increasePrebuildsCompletedCounter ( updatedPrebuild . state ) ;
85
93
}
86
94
0 commit comments