MCPcopy
hub / github.com/prometheus/client_golang / TestAlreadyRegisteredCollision

Function TestAlreadyRegisteredCollision

prometheus/registry_test.go:1127–1184  ·  view source on GitHub ↗

TestAlreadyRegistered will fail with the old, weaker hash function. It is taken from https://play.golang.org/p/HpV7YE6LI_4 , authored by @awilliams.

(t *testing.T)

Source from the content-addressed store, hash-verified

1125// TestAlreadyRegistered will fail with the old, weaker hash function. It is
1126// taken from https://play.golang.org/p/HpV7YE6LI_4 , authored by @awilliams.
1127func TestAlreadyRegisteredCollision(t *testing.T) {
1128 reg := prometheus.NewRegistry()
1129
1130 for i := 0; i < 10000; i++ {
1131 // A collector should be considered unique if its name and const
1132 // label values are unique.
1133
1134 name := fmt.Sprintf("test-collector-%010d", i)
1135
1136 collector := collidingCollector{
1137 i: i,
1138 name: name,
1139
1140 a: prometheus.NewCounter(prometheus.CounterOpts{
1141 Name: "my_collector_a",
1142 ConstLabels: prometheus.Labels{
1143 "name": name,
1144 "type": "test",
1145 },
1146 }),
1147 b: prometheus.NewCounter(prometheus.CounterOpts{
1148 Name: "my_collector_b",
1149 ConstLabels: prometheus.Labels{
1150 "name": name,
1151 "type": "test",
1152 },
1153 }),
1154 c: prometheus.NewCounter(prometheus.CounterOpts{
1155 Name: "my_collector_c",
1156 ConstLabels: prometheus.Labels{
1157 "name": name,
1158 "type": "test",
1159 },
1160 }),
1161 d: prometheus.NewCounter(prometheus.CounterOpts{
1162 Name: "my_collector_d",
1163 ConstLabels: prometheus.Labels{
1164 "name": name,
1165 "type": "test",
1166 },
1167 }),
1168 }
1169
1170 // Register should not fail, since each collector has a unique
1171 // set of sub-collectors, determined by their names and const label values.
1172 if err := reg.Register(&collector); err != nil {
1173 are := &prometheus.AlreadyRegisteredError{}
1174 if !errors.As(err, are) {
1175 t.Fatal(err)
1176 }
1177
1178 previous := are.ExistingCollector.(*collidingCollector)
1179 current := are.NewCollector.(*collidingCollector)
1180
1181 t.Errorf("Unexpected registration error: %q\nprevious collector: %s (i=%d)\ncurrent collector %s (i=%d)", are, previous.name, previous.i, current.name, current.i)
1182 }
1183 }
1184}

Callers

nothing calls this directly

Calls 3

RegisterMethod · 0.95
NewRegistryFunction · 0.92
NewCounterFunction · 0.92

Tested by

no test coverage detected