@@ -66,7 +66,7 @@ export const deploymentCheckJob = {
6666export function deploymentCheckTelemetry ( meter : Meter ) {
6767 return {
6868 decisions : meter . createCounter ( "deployment_checks.decisions_total" , {
69- description : "Deployment check decisions by mode, selection and outcome " ,
69+ description : "Deployment check decisions by mode, selection, outcome and reason " ,
7070 } ) ,
7171 duration : meter . createHistogram ( "deployment_checks.duration_ms" , {
7272 description : "Deployment check duration" ,
@@ -380,6 +380,7 @@ export async function runDeploymentCheck(
380380 telemetry . noPlugin . add ( 1 ) ;
381381 return "no_plugin" ;
382382 }
383+ const reasons = pluginReasons ( controller ) ;
383384
384385 const deployment = await prisma . workerDeployment . findFirst ( {
385386 where : { id : deploymentId } ,
@@ -415,6 +416,7 @@ export async function runDeploymentCheck(
415416 selected_by : "decision_failed" ,
416417 outcome : "skipped" ,
417418 stage : "job" ,
419+ reason : "none" ,
418420 } ) ;
419421 logger . error ( "deploymentChecks.result" , {
420422 deploymentId : deployment . id ,
@@ -445,13 +447,23 @@ export async function runDeploymentCheck(
445447 outcome : DeploymentCheckJobOutcome ,
446448 details : Record < string , unknown > = { }
447449 ) : DeploymentCheckJobOutcome => {
450+ // Only an answer that isn't a pass has a reason. The line carries the one it's counted under.
451+ const refused = outcome === "fail" || outcome === "unavailable" ;
452+ const counted = refused ? reasonLabel ( details , reasons ) : "none" ;
448453 telemetry . decisions . add ( 1 , {
449454 mode : decision . plan ,
450455 selected_by : decision . selectedBy ,
451456 outcome,
452457 stage : "job" ,
458+ reason : counted ,
453459 } ) ;
454- const line = { ...log , outcome, attempt, ...details } ;
460+ const line = {
461+ ...log ,
462+ outcome,
463+ attempt,
464+ ...details ,
465+ ...( refused && { countedReason : counted } ) ,
466+ } ;
455467 // Log a deployment that was selected for a check but could not be checked.
456468 const selectedButSkipped = outcome === "skipped" && decision . plan !== "skip" ;
457469 if ( outcome === "unavailable" || outcome === "fail" || selectedButSkipped )
@@ -575,18 +587,104 @@ export async function runDeploymentCheck(
575587 // pass waits for the lookup, tried again on a later attempt; on the last one the lookup's
576588 // answer stands, so the monitors for checks without an answer see it.
577589 if ( result . outcome !== "pass" ) {
578- return finish ( result . outcome , { checkId : result . id , durationMs, ...lookupDetails } ) ;
590+ return finish ( result . outcome , {
591+ checkId : result . id ,
592+ durationMs,
593+ ...answerReason ( result ) ,
594+ ...lookupDetails ,
595+ } ) ;
579596 }
580597 const checked = { checkId : result . id , checkOutcome : result . outcome } ;
581598 retryLater ( tag , "image tag" , checked ) ;
582599 const [ outcome , details ] = imageNotRead ( tag ) ;
583600 return finish ( outcome , { ...details , step : "tag" , ...checked , durationMs } ) ;
584601 }
585- return finish ( result . outcome , { checkId : result . id , durationMs } ) ;
602+ return finish ( result . outcome , { checkId : result . id , durationMs, ... answerReason ( result ) } ) ;
586603}
587604
588605type ImageNotRead = DeploymentCheckNotRead ;
589606
607+ /**
608+ * The webapp's own reasons (a result line's `reason`), as `deployment_checks.decisions_total`
609+ * counts them. `error` there is the gate failing in the webapp: counted as `webapp_error`, apart
610+ * from any reason of the plugin's.
611+ */
612+ const WEBAPP_REASONS : ReadonlyMap < string , string > = new Map ( [
613+ ...[
614+ "image_not_read" ,
615+ "image_unreadable" ,
616+ "image_read_not_configured" ,
617+ "image_missing" ,
618+ "image_unknown" ,
619+ "no_image_reference" ,
620+ "not_a_single_image" ,
621+ "digest_mismatch" ,
622+ ] . map ( ( reason ) => [ reason , reason ] as const ) ,
623+ [ "error" , "webapp_error" ] ,
624+ ] ) ;
625+
626+ /** The plugin's reasons counted by name: listed, short and lowercase, and only so many. */
627+ const PLUGIN_REASON = / ^ [ a - z ] [ a - z 0 - 9 _ ] { 0 , 39 } $ / ;
628+ const MAX_PLUGIN_REASONS = 32 ;
629+
630+ const pluginReasonsByController = new WeakMap < DeploymentChecksController , ReadonlySet < string > > ( ) ;
631+
632+ /**
633+ * The plugin's own reasons the count may name: the ones its controller lists, never one of the
634+ * webapp's labels, `none` or `other`, read once per controller. Any other reason a plugin sends
635+ * counts as `other`, so the label stays bounded whatever it sends.
636+ */
637+ function pluginReasons ( controller : DeploymentChecksController ) : ReadonlySet < string > {
638+ const known = pluginReasonsByController . get ( controller ) ;
639+ if ( known ) return known ;
640+ const taken = new Set ( [ ...WEBAPP_REASONS . values ( ) , "none" , "other" ] ) ;
641+ let listed : readonly unknown [ ] = [ ] ;
642+ try {
643+ if ( Array . isArray ( controller . reasons ) ) listed = controller . reasons ;
644+ } catch {
645+ // A plugin's list can't stop a check: its reasons then count as `other`.
646+ }
647+ const reasons = new Set (
648+ listed
649+ . filter (
650+ ( reason ) : reason is string =>
651+ typeof reason === "string" && PLUGIN_REASON . test ( reason ) && ! taken . has ( reason )
652+ )
653+ . slice ( 0 , MAX_PLUGIN_REASONS )
654+ ) ;
655+ pluginReasonsByController . set ( controller , reasons ) ;
656+ return reasons ;
657+ }
658+
659+ /**
660+ * What `deployment_checks.decisions_total` counts a refused or unanswered decision under: the
661+ * webapp's own reason, else the plugin's (`checkReason`). One it doesn't know, or none at all,
662+ * counts as `other`. Every other decision counts as `none`.
663+ */
664+ function reasonLabel ( details : Record < string , unknown > , plugin : ReadonlySet < string > ) : string {
665+ const { reason, checkReason } = details ;
666+ if ( reason !== undefined ) {
667+ return ( typeof reason === "string" && WEBAPP_REASONS . get ( reason ) ) || "other" ;
668+ }
669+ if ( checkReason !== undefined ) {
670+ return typeof checkReason === "string" && plugin . has ( checkReason ) ? checkReason : "other" ;
671+ }
672+ return "other" ;
673+ }
674+
675+ /**
676+ * The plugin's reason for an answer, for the result line and the count. It goes on the line as
677+ * `checkReason`, apart from the webapp's own `reason`; one that isn't short and lowercase as
678+ * `other`.
679+ */
680+ function answerReason ( answer : DeploymentCheckResult ) : { checkReason ?: string } {
681+ if ( answer . reason === undefined || answer . outcome === "pass" || answer . outcome === "flagged" ) {
682+ return { } ;
683+ }
684+ const valid = typeof answer . reason === "string" && PLUGIN_REASON . test ( answer . reason ) ;
685+ return { checkReason : valid ? answer . reason : "other" } ;
686+ }
687+
590688const IMAGE_NOT_READ_REASONS : Record < ImageNotRead [ "kind" ] , string > = {
591689 retry : "image_not_read" ,
592690 unreadable : "image_unreadable" ,
@@ -734,6 +832,7 @@ export async function gateDeployment(
734832 return { kind : "proceed" , checked : false } ;
735833 }
736834 const checks = controller ;
835+ const reasons = pluginReasons ( checks ) ;
737836
738837 const log = {
739838 deploymentId : deployment . id ,
@@ -769,6 +868,7 @@ export async function gateDeployment(
769868 selected_by : "decision_failed" ,
770869 outcome : "skipped" ,
771870 stage : "finalize" ,
871+ reason : "none" ,
772872 } ) ;
773873 logger . error ( "deploymentChecks.result" , {
774874 ...log ,
@@ -788,6 +888,7 @@ export async function gateDeployment(
788888 selected_by : decision . selectedBy ,
789889 outcome : "skipped" ,
790890 stage : "finalize" ,
891+ reason : "none" ,
791892 } ) ;
792893 logger . warn ( "deploymentChecks.result" , {
793894 ...log ,
@@ -807,18 +908,24 @@ export async function gateDeployment(
807908 }
808909
809910 const gateLog = { ...log , mode : decision . plan , selectedBy : decision . selectedBy } ;
911+ // Counted under `counted`'s reason: the refusing image's, which may be a machine image's while
912+ // the line's top level is the deployment image's.
810913 const refuse = (
811914 outcome : "fail" | "unavailable" ,
812915 deployAgain : boolean ,
813- details : Record < string , unknown >
916+ details : Record < string , unknown > ,
917+ counted : Record < string , unknown > = details
814918 ) : DeploymentGateResult => {
919+ const countedReason = reasonLabel ( counted , reasons ) ;
815920 telemetry . decisions . add ( 1 , {
816921 mode : "gate" ,
817922 selected_by : decision . selectedBy ,
818923 outcome,
819924 stage : "finalize" ,
925+ reason : countedReason ,
820926 } ) ;
821- logger . warn ( "deploymentChecks.result" , { ...gateLog , outcome, ...details } ) ;
927+ // The line carries the reason it's counted under, so queries group refusals as the metric does.
928+ logger . warn ( "deploymentChecks.result" , { ...gateLog , outcome, ...details , countedReason } ) ;
822929 return refusal ( outcome === "unavailable" && deployAgain ) ;
823930 } ;
824931
@@ -917,7 +1024,7 @@ export async function gateDeployment(
9171024 return {
9181025 outcome : answer . outcome ,
9191026 deployAgain : answer . retryable ,
920- details : { checkId : answer . id , durationMs } ,
1027+ details : { checkId : answer . id , durationMs, ... answerReason ( answer ) } ,
9211028 } ;
9221029 } ;
9231030
@@ -991,8 +1098,9 @@ export async function gateDeployment(
9911098 const refusals = answers . filter ( isRefusal ) ;
9921099 if ( refusals . length > 0 ) {
9931100 const worst = mostFinalRefusal ( refusals ) ;
994- // The message depends on whether retrying may help.
995- return refuse ( worst . outcome , worst . deployAgain , details ) ;
1101+ // The message depends on whether retrying may help. Counted under the refusing image's
1102+ // reason, which may be a machine image's.
1103+ return refuse ( worst . outcome , worst . deployAgain , details , worst . details ) ;
9961104 }
9971105
9981106 const outcome = answers . some ( ( answer ) => answer . outcome === "flagged" ) ? "flagged" : "pass" ;
@@ -1001,6 +1109,7 @@ export async function gateDeployment(
10011109 selected_by : decision . selectedBy ,
10021110 outcome,
10031111 stage : "finalize" ,
1112+ reason : "none" ,
10041113 } ) ;
10051114 logger . info ( "deploymentChecks.result" , { ...gateLog , outcome, ...details } ) ;
10061115 return {
@@ -1012,6 +1121,7 @@ export async function gateDeployment(
10121121 ) ,
10131122 } ;
10141123 } catch ( error ) {
1124+ // Counted as `webapp_error`.
10151125 return refuse ( "unavailable" , true , { reason : "error" , error : errorMessage ( error ) } ) ;
10161126 }
10171127}
0 commit comments