Register copies the provided baseLabels

This ensures that you can pass the same base label set into multiple
Register() calls, e.g.:

    labels := map[string]string{"key": "value"}
    prometheus.Register("metric_1", "", labels, ...)
    prometheus.Register("metric_2", "", labels, ...)

Change-Id: I951e5c2ed7844c74eb3716d1bf07126ce558f266
This commit is contained in:
Bernerd Schaefer 2013-09-11 17:22:00 +02:00
parent 148fde894b
commit a9b3602cea
2 changed files with 26 additions and 4 deletions

View File

@ -172,17 +172,21 @@ func (r *registry) Register(name, docstring string, baseLabels map[string]string
r.mutex.Lock() r.mutex.Lock()
defer r.mutex.Unlock() defer r.mutex.Unlock()
if baseLabels == nil { labels := map[string]string{}
baseLabels = map[string]string{}
if baseLabels != nil {
for k, v := range baseLabels {
labels[k] = v
}
} }
signature, err := r.isValidCandidate(name, baseLabels) signature, err := r.isValidCandidate(name, labels)
if err != nil { if err != nil {
return err return err
} }
r.signatureContainers[signature] = &container{ r.signatureContainers[signature] = &container{
BaseLabels: baseLabels, BaseLabels: labels,
Docstring: docstring, Docstring: docstring,
Metric: metric, Metric: metric,
name: name, name: name,

View File

@ -38,6 +38,8 @@ func testRegister(t tester) {
baseLabels map[string]string baseLabels map[string]string
} }
validLabels := map[string]string{"label": "value"}
var scenarios = []struct { var scenarios = []struct {
inputs []input inputs []input
outputs []bool outputs []bool
@ -118,6 +120,22 @@ func testRegister(t tester) {
false, false,
}, },
}, },
{
inputs: []input{
{
name: "metric_1_with_identical_labels",
baseLabels: validLabels,
},
{
name: "metric_2_with_identical_labels",
baseLabels: validLabels,
},
},
outputs: []bool{
true,
true,
},
},
{ {
inputs: []input{ inputs: []input{
{ {