From bb957bc1457da53db3fca742c4195b06208fa5b3 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Fri, 14 Mar 2014 01:41:19 +0100 Subject: [PATCH] Change internal metric name label to __name__. This also adds a check that forbids any user-supplied metrics to start with the reserved label name prefix "__". Change-Id: I2fe94c740b685ad05c4c670613cf2af7b9e1c1c0 --- extraction/fixtures/test0_0_1-0_0_2.json | 4 ++-- extraction/metricfamilyprocessor_test.go | 10 +++++----- model/labelname.go | 6 +++++- prometheus/constants.go | 1 - prometheus/registry.go | 21 ++++++++++++--------- prometheus/registry_test.go | 8 +++++--- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/extraction/fixtures/test0_0_1-0_0_2.json b/extraction/fixtures/test0_0_1-0_0_2.json index d14297c..1ac5be7 100644 --- a/extraction/fixtures/test0_0_1-0_0_2.json +++ b/extraction/fixtures/test0_0_1-0_0_2.json @@ -1,7 +1,7 @@ [ { "baseLabels": { - "name": "rpc_calls_total", + "__name__": "rpc_calls_total", "job": "batch_job" }, "docstring": "RPC calls.", @@ -31,7 +31,7 @@ }, { "baseLabels": { - "name": "rpc_latency_microseconds" + "__name__": "rpc_latency_microseconds" }, "docstring": "RPC latency.", "metric": { diff --git a/extraction/metricfamilyprocessor_test.go b/extraction/metricfamilyprocessor_test.go index 6c47387..7bc6c7e 100644 --- a/extraction/metricfamilyprocessor_test.go +++ b/extraction/metricfamilyprocessor_test.go @@ -70,12 +70,12 @@ func TestMetricFamilyProcessor(t *testing.T) { { Samples: model.Samples{ &model.Sample{ - Metric: model.Metric{"name": "request_count", "some_label_name": "some_label_value"}, + Metric: model.Metric{model.MetricNameLabel: "request_count", "some_label_name": "some_label_value"}, Value: -42, Timestamp: testTime, }, &model.Sample{ - Metric: model.Metric{"name": "request_count", "another_label_name": "another_label_value"}, + Metric: model.Metric{model.MetricNameLabel: "request_count", "another_label_name": "another_label_value"}, Value: 84, Timestamp: testTime, }, @@ -89,17 +89,17 @@ func TestMetricFamilyProcessor(t *testing.T) { { Samples: model.Samples{ &model.Sample{ - Metric: model.Metric{"name": "request_count", "some_label_name": "some_label_value", "quantile": "0.99"}, + Metric: model.Metric{model.MetricNameLabel: "request_count", "some_label_name": "some_label_value", "quantile": "0.99"}, Value: -42, Timestamp: testTime, }, &model.Sample{ - Metric: model.Metric{"name": "request_count", "some_label_name": "some_label_value", "quantile": "0.999"}, + Metric: model.Metric{model.MetricNameLabel: "request_count", "some_label_name": "some_label_value", "quantile": "0.999"}, Value: -84, Timestamp: testTime, }, &model.Sample{ - Metric: model.Metric{"name": "request_count", "another_label_name": "another_label_value", "quantile": "0.5"}, + Metric: model.Metric{model.MetricNameLabel: "request_count", "another_label_name": "another_label_value", "quantile": "0.5"}, Value: 10, Timestamp: testTime, }, diff --git a/model/labelname.go b/model/labelname.go index 08a0b29..310bd72 100644 --- a/model/labelname.go +++ b/model/labelname.go @@ -23,7 +23,11 @@ const ( ExporterLabelPrefix LabelName = "exporter_" // The label name indicating the metric name of a timeseries. - MetricNameLabel LabelName = "name" + MetricNameLabel LabelName = "__name__" + + // ReservedLabelPrefix is a prefix which is not legal in user-supplied label + // names. + ReservedLabelPrefix = "__" // The label name indicating the job from which a timeseries was scraped. JobLabel LabelName = "job" diff --git a/prometheus/constants.go b/prometheus/constants.go index a5197cc..5b97966 100644 --- a/prometheus/constants.go +++ b/prometheus/constants.go @@ -37,7 +37,6 @@ const ( baseLabelsKey = "baseLabels" docstringKey = "docstring" metricKey = "metric" - nameLabel = "name" counterTypeValue = "counter" floatBitCount = 64 diff --git a/prometheus/registry.go b/prometheus/registry.go index 8d085cb..f72342a 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -25,6 +25,7 @@ import ( "code.google.com/p/goprotobuf/proto" "github.com/matttproud/golang_protobuf_extensions/ext" + "github.com/prometheus/client_golang/model" "github.com/prometheus/client_golang/vendor/goautoneg" ) @@ -124,19 +125,21 @@ func (r *registry) isValidCandidate(name string, baseLabels map[string]string) ( } } - if _, contains := baseLabels[nameLabel]; contains { - err = fmt.Errorf("metric named %s with baseLabels %s contains reserved label name %s in baseLabels", name, baseLabels, nameLabel) + for label := range baseLabels { + if strings.HasPrefix(label, model.ReservedLabelPrefix) { + err = fmt.Errorf("metric named %s with baseLabels %s contains reserved label name %s in baseLabels", name, baseLabels, label) - if *abortOnMisuse { - panic(err) - } else if *debugRegistration { - log.Println(err) + if *abortOnMisuse { + panic(err) + } else if *debugRegistration { + log.Println(err) + } + + return signature, err } - - return signature, err } - baseLabels[nameLabel] = name + baseLabels[string(model.MetricNameLabel)] = name signature = labelsToSignature(baseLabels) if _, contains := r.signatureContainers[signature]; contains { diff --git a/prometheus/registry_test.go b/prometheus/registry_test.go index 2dd669e..299b3ea 100644 --- a/prometheus/registry_test.go +++ b/prometheus/registry_test.go @@ -15,6 +15,8 @@ import ( "testing" "code.google.com/p/goprotobuf/proto" + + "github.com/prometheus/client_golang/model" ) func testRegister(t tester) { @@ -83,7 +85,7 @@ func testRegister(t tester) { inputs: []input{ { name: "valid_name", - baseLabels: map[string]string{"name": "illegal_duplicate_name"}, + baseLabels: map[string]string{model.ReservedLabelPrefix + "internal": "illegal_internal_name"}, }, }, outputs: []bool{ @@ -303,7 +305,7 @@ func testDumpToWriter(t tester) { "foo": NewCounter(), }, }, - out: []byte(`[{"baseLabels":{"label_foo":"foo","name":"foo"},"docstring":"metric foo","metric":{"type":"counter","value":[]}}]`), + out: []byte(`[{"baseLabels":{"__name__":"foo","label_foo":"foo"},"docstring":"metric foo","metric":{"type":"counter","value":[]}}]`), }, { in: input{ @@ -312,7 +314,7 @@ func testDumpToWriter(t tester) { "bar": NewCounter(), }, }, - out: []byte(`[{"baseLabels":{"label_bar":"bar","name":"bar"},"docstring":"metric bar","metric":{"type":"counter","value":[]}},{"baseLabels":{"label_foo":"foo","name":"foo"},"docstring":"metric foo","metric":{"type":"counter","value":[]}}]`), + out: []byte(`[{"baseLabels":{"__name__":"bar","label_bar":"bar"},"docstring":"metric bar","metric":{"type":"counter","value":[]}},{"baseLabels":{"__name__":"foo","label_foo":"foo"},"docstring":"metric foo","metric":{"type":"counter","value":[]}}]`), }, }