Merge pull request #86213 from RainbowMango/pr_customcollector_clear_guarantee

Guarantee ClearState will fully clear a collector
This commit is contained in:
Kubernetes Prow Robot 2019-12-13 09:15:10 -08:00 committed by GitHub
commit b1177f4755
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -175,6 +175,7 @@ func (bsc *BaseStableCollector) ClearState() {
bsc.descriptors = nil bsc.descriptors = nil
bsc.registrable = nil bsc.registrable = nil
bsc.hidden = nil bsc.hidden = nil
bsc.self = nil
} }
// HiddenMetrics tells the list of hidden metrics with fqName. // HiddenMetrics tells the list of hidden metrics with fqName.

View File

@ -18,6 +18,7 @@ package metrics
import ( import (
"fmt" "fmt"
"reflect"
"strings" "strings"
"testing" "testing"
@ -134,3 +135,36 @@ func TestInvalidCustomCollector(t *testing.T) {
}) })
} }
} }
// TestCustomCollectorClearState guarantees `ClearState()` will fully clear a collector.
// It is necessary because we may forget to clear some new-added fields in the future.
func TestCustomCollectorClearState(t *testing.T) {
var currentVersion = parseVersion(apimachineryversion.Info{
Major: "1",
Minor: "17",
GitVersion: "v1.17.0-alpha-1.12345",
})
var (
alphaDesc = NewDesc("metric_alpha", "alpha metric", []string{"name"}, nil,
ALPHA, "")
stableDesc = NewDesc("metric_stable", "stable metrics", []string{"name"}, nil,
STABLE, "")
deprecatedDesc = NewDesc("metric_deprecated", "stable deprecated metrics", []string{"name"}, nil,
STABLE, "1.17.0")
hiddenDesc = NewDesc("metric_hidden", "stable hidden metrics", []string{"name"}, nil,
STABLE, "1.16.0")
)
benchmarkA := newTestCustomCollector(alphaDesc, stableDesc, deprecatedDesc, hiddenDesc)
benchmarkB := newTestCustomCollector(alphaDesc, stableDesc, deprecatedDesc, hiddenDesc)
if benchmarkA.Create(&currentVersion, benchmarkA) == false {
t.Fatal("collector should be created")
}
benchmarkA.ClearState()
if !reflect.DeepEqual(*benchmarkA, *benchmarkB) {
t.Fatal("custom collector state hasn't be fully cleared")
}
}