Constantize metric name label name.

This commit is contained in:
Julius Volz 2013-03-26 15:32:48 +01:00
parent 567a998e63
commit 2b8f0b2cc7
15 changed files with 132 additions and 127 deletions

View file

@ -13,6 +13,9 @@
package model
// The label name used to indicate the metric name of a timeseries.
const MetricNameLabel = LabelName("name")
// A LabelName is a key for a LabelSet or Metric. It has a value associated
// therewith.
type LabelName string

View file

@ -37,125 +37,125 @@ func testProcessor001Process(t test.Tester) {
out: []Result{
{
Sample: model.Sample{
Metric: model.Metric{"service": "zed", "name": "rpc_calls_total"},
Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total"},
Value: 25,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"service": "bar", "name": "rpc_calls_total"},
Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total"},
Value: 25,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"service": "foo", "name": "rpc_calls_total"},
Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total"},
Value: 25,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.010000", "name": "rpc_latency_microseconds", "service": "zed"},
Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
Value: 0.04598141,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.010000", "name": "rpc_latency_microseconds", "service": "bar"},
Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
Value: 78.485634,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.010000", "name": "rpc_latency_microseconds", "service": "foo"},
Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
Value: 15.890724674774395,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.050000", "name": "rpc_latency_microseconds", "service": "zed"},
Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
Value: 0.04598141,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.050000", "name": "rpc_latency_microseconds", "service": "bar"},
Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
Value: 78.485634,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.050000", "name": "rpc_latency_microseconds", "service": "foo"},
Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
Value: 15.890724674774395,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.500000", "name": "rpc_latency_microseconds", "service": "zed"},
Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
Value: 0.61204565,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.500000", "name": "rpc_latency_microseconds", "service": "bar"},
Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
Value: 97.317986,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.500000", "name": "rpc_latency_microseconds", "service": "foo"},
Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
Value: 84.63044,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.900000", "name": "rpc_latency_microseconds", "service": "zed"},
Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
Value: 1.3559151,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.900000", "name": "rpc_latency_microseconds", "service": "bar"},
Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
Value: 109.89202,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.900000", "name": "rpc_latency_microseconds", "service": "foo"},
Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
Value: 160.21101,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.990000", "name": "rpc_latency_microseconds", "service": "zed"},
Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed"},
Value: 1.7727332,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.990000", "name": "rpc_latency_microseconds", "service": "bar"},
Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar"},
Value: 109.99626,
},
},
{
Sample: model.Sample{
Metric: model.Metric{"percentile": "0.990000", "name": "rpc_latency_microseconds", "service": "foo"},
Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo"},
Value: 172.49829,
},
},

View file

@ -503,7 +503,7 @@ func labelsEqual(labels1, labels2 model.Metric) bool {
return false
}
for label, value := range labels1 {
if labels2[label] != value && label != "name" {
if labels2[label] != value && label != model.MetricNameLabel {
return false
}
}

View file

@ -128,67 +128,67 @@ func sampleVectorImpl(timestamp *time.Time, view *viewAdapter, args []Node) inte
return Vector{
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "0",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "0",
},
Value: 10,
Timestamp: *timestamp,
},
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "1",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "1",
},
Value: 20,
Timestamp: *timestamp,
},
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "2",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "2",
},
Value: 30,
Timestamp: *timestamp,
},
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "3",
"group": "canary",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "3",
"group": "canary",
},
Value: 40,
Timestamp: *timestamp,
},
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "2",
"group": "canary",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "2",
"group": "canary",
},
Value: 40,
Timestamp: *timestamp,
},
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "3",
"group": "mytest",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "3",
"group": "mytest",
},
Value: 40,
Timestamp: *timestamp,
},
&model.Sample{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "3",
"group": "mytest",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "3",
"group": "mytest",
},
Value: 40,
Timestamp: *timestamp,

View file

@ -16,6 +16,7 @@ package ast
import (
"encoding/json"
"fmt"
"github.com/prometheus/prometheus/model"
"github.com/prometheus/prometheus/utility"
"sort"
"strings"
@ -69,13 +70,13 @@ func exprTypeToString(exprType ExprType) string {
func (vector Vector) String() string {
metricStrings := []string{}
for _, sample := range vector {
metricName, ok := sample.Metric["name"]
metricName, ok := sample.Metric[model.MetricNameLabel]
if !ok {
panic("Tried to print vector without metric name")
}
labelStrings := []string{}
for label, value := range sample.Metric {
if label != "name" {
if label != model.MetricNameLabel {
// TODO escape special chars in label values here and elsewhere.
labelStrings = append(labelStrings, fmt.Sprintf("%v='%v'", label, value))
}
@ -94,13 +95,13 @@ func (vector Vector) String() string {
func (matrix Matrix) String() string {
metricStrings := []string{}
for _, sampleSet := range matrix {
metricName, ok := sampleSet.Metric["name"]
metricName, ok := sampleSet.Metric[model.MetricNameLabel]
if !ok {
panic("Tried to print matrix without metric name")
}
labelStrings := []string{}
for label, value := range sampleSet.Metric {
if label != "name" {
if label != model.MetricNameLabel {
labelStrings = append(labelStrings, fmt.Sprintf("%v='%v'", label, value))
}
}
@ -195,13 +196,13 @@ func EvalToString(node Node, timestamp *time.Time, format OutputFormat) string {
}
func (node *VectorLiteral) String() string {
metricName, ok := node.labels["name"]
metricName, ok := node.labels[model.MetricNameLabel]
if !ok {
panic("Tried to print vector without metric name")
}
labelStrings := []string{}
for label, value := range node.labels {
if label != "name" {
if label != model.MetricNameLabel {
labelStrings = append(labelStrings, fmt.Sprintf("%v='%v'", label, value))
}
}

View file

@ -100,7 +100,7 @@ label_assign : IDENTIFIER '=' STRING
rule_expr : '(' rule_expr ')'
{ $$ = $2 }
| IDENTIFIER rule_labels
{ $2["name"] = model.LabelValue($1); $$ = ast.NewVectorLiteral($2) }
{ $2[model.MetricNameLabel] = model.LabelValue($1); $$ = ast.NewVectorLiteral($2) }
| IDENTIFIER '(' func_arg_list ')'
{
var err error

View file

@ -419,7 +419,7 @@ yydefault:
{ yyVAL.ruleNode = yyS[yypt-1].ruleNode }
case 16:
//line parser.y:103
{ yyS[yypt-0].labelSet["name"] = model.LabelValue(yyS[yypt-1].str); yyVAL.ruleNode = ast.NewVectorLiteral(yyS[yypt-0].labelSet) }
{ yyS[yypt-0].labelSet[model.MetricNameLabel] = model.LabelValue(yyS[yypt-1].str); yyVAL.ruleNode = ast.NewVectorLiteral(yyS[yypt-0].labelSet) }
case 17:
//line parser.y:105
{

View file

@ -40,7 +40,7 @@ func (rule *Rule) Eval(timestamp *time.Time) ast.Vector {
// Override the metric name and labels.
for _, sample := range vector {
sample.Metric["name"] = model.LabelValue(rule.name)
sample.Metric[model.MetricNameLabel] = model.LabelValue(rule.name)
for label, value := range rule.labels {
if value == "" {
delete(sample.Metric, label)

View file

@ -70,73 +70,73 @@ func storeMatrix(storage metric.Storage, matrix ast.Matrix) error {
var testMatrix = ast.Matrix{
{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "0",
"group": "production",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "0",
"group": "production",
},
Values: getTestValueStream(0, 100, 10),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "1",
"group": "production",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "1",
"group": "production",
},
Values: getTestValueStream(0, 200, 20),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "0",
"group": "canary",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "0",
"group": "canary",
},
Values: getTestValueStream(0, 300, 30),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "api-server",
"instance": "1",
"group": "canary",
model.MetricNameLabel: "http_requests",
"job": "api-server",
"instance": "1",
"group": "canary",
},
Values: getTestValueStream(0, 400, 40),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "app-server",
"instance": "0",
"group": "production",
model.MetricNameLabel: "http_requests",
"job": "app-server",
"instance": "0",
"group": "production",
},
Values: getTestValueStream(0, 500, 50),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "app-server",
"instance": "1",
"group": "production",
model.MetricNameLabel: "http_requests",
"job": "app-server",
"instance": "1",
"group": "production",
},
Values: getTestValueStream(0, 600, 60),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "app-server",
"instance": "0",
"group": "canary",
model.MetricNameLabel: "http_requests",
"job": "app-server",
"instance": "0",
"group": "canary",
},
Values: getTestValueStream(0, 700, 70),
},
{
Metric: model.Metric{
"name": "http_requests",
"job": "app-server",
"instance": "1",
"group": "canary",
model.MetricNameLabel: "http_requests",
"job": "app-server",
"instance": "1",
"group": "canary",
},
Values: getTestValueStream(0, 800, 80),
},

View file

@ -25,8 +25,8 @@ func GetFingerprintsForLabelSetTests(p MetricPersistence, t test.Tester) {
Value: 0,
Timestamp: time.Time{},
Metric: model.Metric{
"name": "my_metric",
"request_type": "your_mom",
model.MetricNameLabel: "my_metric",
"request_type": "your_mom",
},
}, t)
@ -34,13 +34,13 @@ func GetFingerprintsForLabelSetTests(p MetricPersistence, t test.Tester) {
Value: 0,
Timestamp: time.Time{},
Metric: model.Metric{
"name": "my_metric",
"request_type": "your_dad",
model.MetricNameLabel: "my_metric",
"request_type": "your_dad",
},
}, t)
result, err := p.GetFingerprintsForLabelSet(model.LabelSet{
model.LabelName("name"): model.LabelValue("my_metric"),
model.MetricNameLabel: model.LabelValue("my_metric"),
})
if err != nil {
@ -81,9 +81,9 @@ func GetFingerprintsForLabelNameTests(p MetricPersistence, t test.Tester) {
Value: 0,
Timestamp: time.Time{},
Metric: model.Metric{
"name": "my_metric",
"request_type": "your_mom",
"language": "english",
model.MetricNameLabel: "my_metric",
"request_type": "your_mom",
"language": "english",
},
}, t)
@ -91,13 +91,13 @@ func GetFingerprintsForLabelNameTests(p MetricPersistence, t test.Tester) {
Value: 0,
Timestamp: time.Time{},
Metric: model.Metric{
"name": "my_metric",
"request_type": "your_dad",
"sprache": "deutsch",
model.MetricNameLabel: "my_metric",
"request_type": "your_dad",
"sprache": "deutsch",
},
}, t)
b := model.LabelName("name")
b := model.MetricNameLabel
result, err := p.GetFingerprintsForLabelName(b)
if err != nil {
@ -230,9 +230,9 @@ func GetMetricForFingerprintTests(p MetricPersistence, t test.Tester) {
func AppendRepeatingValuesTests(p MetricPersistence, t test.Tester) {
metric := model.Metric{
"controller": "foo",
"name": "errors_total",
"operation": "bar",
model.MetricNameLabel: "errors_total",
"controller": "foo",
"operation": "bar",
}
increments := 10
@ -255,9 +255,9 @@ func AppendRepeatingValuesTests(p MetricPersistence, t test.Tester) {
}
labelSet := model.LabelSet{
"controller": "foo",
"name": "errors_total",
"operation": "bar",
model.MetricNameLabel: "errors_total",
"controller": "foo",
"operation": "bar",
}
for i := 0; i < increments; i++ {
@ -290,9 +290,9 @@ func AppendRepeatingValuesTests(p MetricPersistence, t test.Tester) {
func AppendsRepeatingValuesTests(p MetricPersistence, t test.Tester) {
metric := model.Metric{
"controller": "foo",
"name": "errors_total",
"operation": "bar",
model.MetricNameLabel: "errors_total",
"controller": "foo",
"operation": "bar",
}
increments := 10
@ -318,9 +318,9 @@ func AppendsRepeatingValuesTests(p MetricPersistence, t test.Tester) {
}
labelSet := model.LabelSet{
"controller": "foo",
"name": "errors_total",
"operation": "bar",
model.MetricNameLabel: "errors_total",
"controller": "foo",
"operation": "bar",
}
for i := 0; i < increments; i++ {

View file

@ -21,12 +21,12 @@ import (
)
func GetFingerprintsForLabelSetUsesAndForLabelMatchingTests(p MetricPersistence, t test.Tester) {
metrics := []map[string]string{
{"name": "request_metrics_latency_equal_tallying_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{"name": "requests_metrics_latency_equal_accumulating_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{"name": "requests_metrics_latency_logarithmic_accumulating_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{"name": "requests_metrics_latency_logarithmic_tallying_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{"name": "targets_healthy_scrape_latency_ms", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
metrics := []model.LabelSet{
{model.MetricNameLabel: "request_metrics_latency_equal_tallying_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{model.MetricNameLabel: "requests_metrics_latency_equal_accumulating_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{model.MetricNameLabel: "requests_metrics_latency_logarithmic_accumulating_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{model.MetricNameLabel: "requests_metrics_latency_logarithmic_tallying_microseconds", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
{model.MetricNameLabel: "targets_healthy_scrape_latency_ms", "instance": "http://localhost:9090/metrics.json", "percentile": "0.010000"},
}
for _, metric := range metrics {
@ -44,8 +44,8 @@ func GetFingerprintsForLabelSetUsesAndForLabelMatchingTests(p MetricPersistence,
}
labelSet := model.LabelSet{
"name": "targets_healthy_scrape_latency_ms",
"percentile": "0.010000",
model.MetricNameLabel: "targets_healthy_scrape_latency_ms",
"percentile": "0.010000",
}
fingerprints, err := p.GetFingerprintsForLabelSet(labelSet)

View file

@ -567,7 +567,7 @@ func GetValueAtTimeTests(persistenceMaker func() (MetricPersistence, io.Closer),
}()
m := model.Metric{
"name": "age_in_years",
model.MetricNameLabel: "age_in_years",
}
for _, value := range context.values {
@ -1012,7 +1012,7 @@ func GetBoundaryValuesTests(persistenceMaker func() (MetricPersistence, io.Close
}()
m := model.Metric{
"name": "age_in_years",
model.MetricNameLabel: "age_in_years",
}
for _, value := range context.values {
@ -1369,7 +1369,7 @@ func GetRangeValuesTests(persistenceMaker func() (MetricPersistence, io.Closer),
}()
m := model.Metric{
"name": "age_in_years",
model.MetricNameLabel: "age_in_years",
}
for _, value := range context.values {

View file

@ -173,7 +173,7 @@ func AppendSampleAsPureSingleEntityAppendTests(p MetricPersistence, t test.Teste
sample := model.Sample{
Value: model.SampleValue(x),
Timestamp: time.Unix(int64(x), 0),
Metric: model.Metric{"name": "my_metric"},
Metric: model.Metric{model.MetricNameLabel: "my_metric"},
}
err := p.AppendSample(sample)
@ -215,7 +215,7 @@ func StochasticTests(persistenceMaker func() MetricPersistence, t test.Tester) {
}
v := model.LabelValue(fmt.Sprintf("metric_index_%d", metricIndex))
sample.Metric["name"] = v
sample.Metric[model.MetricNameLabel] = v
for sharedLabelIndex := 0; sharedLabelIndex < numberOfSharedLabels; sharedLabelIndex++ {
l := model.LabelName(fmt.Sprintf("shared_label_%d", sharedLabelIndex))
@ -348,7 +348,7 @@ func StochasticTests(persistenceMaker func() MetricPersistence, t test.Tester) {
}
metric := model.Metric{}
metric["name"] = model.LabelValue(fmt.Sprintf("metric_index_%d", metricIndex))
metric[model.MetricNameLabel] = model.LabelValue(fmt.Sprintf("metric_index_%d", metricIndex))
for i := 0; i < numberOfSharedLabels; i++ {
l := model.LabelName(fmt.Sprintf("shared_label_%d", i))

View file

@ -81,7 +81,7 @@ func testMakeView(t test.Tester) {
}
var (
instant = time.Date(1984, 3, 30, 0, 0, 0, 0, time.Local)
metric = model.Metric{"name": "request_count"}
metric = model.Metric{model.MetricNameLabel: "request_count"}
fingerprint = model.NewFingerprintFromMetric(metric)
scenarios = []struct {
data []model.Sample
@ -498,7 +498,7 @@ func TestGetAllValuesForLabel(t *testing.T) {
defer closer.Close()
for j, metric := range scenario.in {
sample := model.Sample{
Metric: model.Metric{"name": model.LabelValue(metric.metricName)},
Metric: model.Metric{model.MetricNameLabel: model.LabelValue(metric.metricName)},
}
if metric.appendToMemory {
if err := tiered.(*tieredStorage).memoryArena.AppendSample(sample); err != nil {
@ -511,7 +511,7 @@ func TestGetAllValuesForLabel(t *testing.T) {
}
}
}
metricNames, err := tiered.GetAllValuesForLabel("name")
metricNames, err := tiered.GetAllValuesForLabel(model.MetricNameLabel)
if err != nil {
t.Fatalf("%d. Error getting metric names: %s", i, err)
}

View file

@ -17,6 +17,7 @@ import (
"code.google.com/p/gorest"
"encoding/json"
"errors"
"github.com/prometheus/prometheus/model"
"github.com/prometheus/prometheus/rules"
"github.com/prometheus/prometheus/rules/ast"
"log"
@ -86,7 +87,7 @@ func (serv MetricsService) QueryRange(expr string, end int64, duration int64, st
}
func (serv MetricsService) Metrics() string {
metricNames, err := serv.appState.Storage.GetAllValuesForLabel("name")
metricNames, err := serv.appState.Storage.GetAllValuesForLabel(model.MetricNameLabel)
rb := serv.ResponseBuilder()
rb.SetContentType(gorest.Application_Json)
if err != nil {