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
This commit is contained in:
Julius Volz 2014-03-14 01:41:19 +01:00 committed by Julius Volz
parent 29ebb580db
commit bb957bc145
6 changed files with 29 additions and 21 deletions

View File

@ -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": {

View File

@ -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,
},

View File

@ -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"

View File

@ -37,7 +37,6 @@ const (
baseLabelsKey = "baseLabels"
docstringKey = "docstring"
metricKey = "metric"
nameLabel = "name"
counterTypeValue = "counter"
floatBitCount = 64

View File

@ -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 {

View File

@ -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":[]}}]`),
},
}