mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #86213 from RainbowMango/pr_customcollector_clear_guarantee
Guarantee ClearState will fully clear a collector
This commit is contained in:
commit
b1177f4755
@ -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.
|
||||||
|
@ -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(¤tVersion, 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user