Skip to content

Commit

Permalink
fix(metrics): sidecar injection false positives (#716)
Browse files Browse the repository at this point in the history
Do not increment the `injections_by_namespace` metric when no injection
is performed.
  • Loading branch information
gsantos-hc authored Dec 12, 2024
1 parent c46ef47 commit 9f0f1bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions agent-inject/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ var (
)

func incrementInjections(namespace string, res MutateResponse) {
if !res.InjectedInit && !res.InjectedSidecar {
return
}

// Injection type can be one of: init_and_sidecar (default); init_only; or sidecar_only
typeLabel := metricsLabelTypeBoth
if res.InjectedInit && !res.InjectedSidecar {
Expand Down
24 changes: 21 additions & 3 deletions agent-inject/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Test_incrementInjections(t *testing.T) {
namespace string
mutateResponse MutateResponse
expectedLabels map[string]string
noIncrement bool
}{
"init_only": {
namespace: "init",
Expand Down Expand Up @@ -52,16 +53,33 @@ func Test_incrementInjections(t *testing.T) {
metricsLabelType: metricsLabelTypeBoth,
},
},
"no_injection": {
namespace: "none",
mutateResponse: MutateResponse{
InjectedInit: false,
InjectedSidecar: false,
},
expectedLabels: nil,
noIncrement: true,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
t.Cleanup(func() {
injectionsByNamespace.Reset()
})

expInc := 1
if test.noIncrement {
expInc = 0
}

incrementInjections(test.namespace, test.mutateResponse)
assert.Equal(t, 1, testutil.CollectAndCount(injectionsByNamespace))
check := injectionsByNamespace.With(prometheus.Labels(test.expectedLabels))
assert.Equal(t, float64(1), testutil.ToFloat64(check))
assert.Equal(t, expInc, testutil.CollectAndCount(injectionsByNamespace))
if !test.noIncrement {
check := injectionsByNamespace.With(prometheus.Labels(test.expectedLabels))
assert.Equal(t, float64(1), testutil.ToFloat64(check))
}
})
}
}

0 comments on commit 9f0f1bc

Please sign in to comment.