Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(metrics): sidecar injection false positives #716

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
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
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))
}
})
}
}
Loading