Update LabelSet and Metric String() methods from server.

This commit is contained in:
Julius Volz 2013-06-11 12:56:37 +02:00
parent 14cbd6b8ce
commit 2c8b7cc19b
6 changed files with 88 additions and 57 deletions

View File

@ -20,18 +20,6 @@ import (
"github.com/prometheus/client_golang/model" "github.com/prometheus/client_golang/model"
) )
const (
// The label name prefix to prepend if a synthetic label is already present
// in the exported metrics.
ExporterLabelPrefix model.LabelName = "exporter_"
// The label name indicating the metric name of a timeseries.
MetricNameLabel = "name"
// The label name indicating the job from which a timeseries was scraped.
JobLabel = "job"
)
// ProcessOptions dictates how the interpreted stream should be rendered for // ProcessOptions dictates how the interpreted stream should be rendered for
// consumption. // consumption.
type ProcessOptions struct { type ProcessOptions struct {
@ -82,7 +70,7 @@ func mergeTargetLabels(entityLabels, targetLabels model.LabelSet) model.LabelSet
for label, labelValue := range targetLabels { for label, labelValue := range targetLabels {
if _, exists := result[label]; exists { if _, exists := result[label]; exists {
result[ExporterLabelPrefix+label] = labelValue result[model.ExporterLabelPrefix+label] = labelValue
} else { } else {
result[label] = labelValue result[label] = labelValue
} }

View File

@ -21,8 +21,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/prometheus/client_golang/test"
"github.com/prometheus/client_golang/model" "github.com/prometheus/client_golang/model"
"github.com/prometheus/client_golang/test"
) )
func testProcessor001Process(t test.Tester) { func testProcessor001Process(t test.Tester) {
@ -39,84 +39,84 @@ func testProcessor001Process(t test.Tester) {
{ {
in: "test0_0_1-0_0_2.json", in: "test0_0_1-0_0_2.json",
baseLabels: model.LabelSet{ baseLabels: model.LabelSet{
JobLabel: "batch_exporter", model.JobLabel: "batch_exporter",
}, },
out: model.Samples{ out: model.Samples{
&model.Sample{ &model.Sample{
Metric: model.Metric{"service": "zed", MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"},
Value: 25, Value: 25,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"service": "bar", MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"},
Value: 25, Value: 25,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"service": "foo", MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"},
Value: 25, Value: 25,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.010000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 0.0459814091918713, Value: 0.0459814091918713,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.010000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 78.48563317257356, Value: 78.48563317257356,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.010000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 15.890724674774395, Value: 15.890724674774395,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.050000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 0.0459814091918713, Value: 0.0459814091918713,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.050000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 78.48563317257356, Value: 78.48563317257356,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.050000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 15.890724674774395, Value: 15.890724674774395,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.500000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 0.6120456642749681, Value: 0.6120456642749681,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.500000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 97.31798360385088, Value: 97.31798360385088,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.500000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 84.63044031436561, Value: 84.63044031436561,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.900000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 1.355915069887731, Value: 1.355915069887731,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.900000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 109.89202084295582, Value: 109.89202084295582,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.900000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 160.21100853053224, Value: 160.21100853053224,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.990000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 1.772733213161236, Value: 1.772733213161236,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.990000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 109.99626121011262, Value: 109.99626121011262,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.990000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 172.49828748957728, Value: 172.49828748957728,
}, },
}, },

View File

@ -22,8 +22,8 @@ import (
"testing" "testing"
"time" "time"
"github.com/prometheus/client_golang/test"
"github.com/prometheus/client_golang/model" "github.com/prometheus/client_golang/model"
"github.com/prometheus/client_golang/test"
) )
func testProcessor002Process(t test.Tester) { func testProcessor002Process(t test.Tester) {
@ -40,84 +40,84 @@ func testProcessor002Process(t test.Tester) {
{ {
in: "test0_0_1-0_0_2.json", in: "test0_0_1-0_0_2.json",
baseLabels: model.LabelSet{ baseLabels: model.LabelSet{
JobLabel: "batch_exporter", model.JobLabel: "batch_exporter",
}, },
out: model.Samples{ out: model.Samples{
&model.Sample{ &model.Sample{
Metric: model.Metric{"service": "zed", MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, Metric: model.Metric{"service": "zed", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"},
Value: 25, Value: 25,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"service": "bar", MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, Metric: model.Metric{"service": "bar", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"},
Value: 25, Value: 25,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"service": "foo", MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"}, Metric: model.Metric{"service": "foo", model.MetricNameLabel: "rpc_calls_total", "job": "batch_job", "exporter_job": "batch_exporter"},
Value: 25, Value: 25,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.010000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 0.0459814091918713, Value: 0.0459814091918713,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.010000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 78.48563317257356, Value: 78.48563317257356,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.010000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.010000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 15.890724674774395, Value: 15.890724674774395,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.050000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 0.0459814091918713, Value: 0.0459814091918713,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.050000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 78.48563317257356, Value: 78.48563317257356,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.050000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.050000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 15.890724674774395, Value: 15.890724674774395,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.500000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 0.6120456642749681, Value: 0.6120456642749681,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.500000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 97.31798360385088, Value: 97.31798360385088,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.500000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.500000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 84.63044031436561, Value: 84.63044031436561,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.900000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 1.355915069887731, Value: 1.355915069887731,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.900000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 109.89202084295582, Value: 109.89202084295582,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.900000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.900000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 160.21100853053224, Value: 160.21100853053224,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.990000", MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "zed", "job": "batch_exporter"},
Value: 1.772733213161236, Value: 1.772733213161236,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.990000", MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "bar", "job": "batch_exporter"},
Value: 109.99626121011262, Value: 109.99626121011262,
}, },
&model.Sample{ &model.Sample{
Metric: model.Metric{"percentile": "0.990000", MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"}, Metric: model.Metric{"percentile": "0.990000", model.MetricNameLabel: "rpc_latency_microseconds", "service": "foo", "job": "batch_exporter"},
Value: 172.49828748957728, Value: 172.49828748957728,
}, },
}, },

View File

@ -17,6 +17,18 @@ import (
"strings" "strings"
) )
const (
// The label name prefix to prepend if a synthetic label is already present
// in the exported metrics.
ExporterLabelPrefix LabelName = "exporter_"
// The label name indicating the metric name of a timeseries.
MetricNameLabel = "name"
// The label name indicating the job from which a timeseries was scraped.
JobLabel = "job"
)
// A LabelName is a key for a LabelSet or Metric. It has a value associated // A LabelName is a key for a LabelSet or Metric. It has a value associated
// therewith. // therewith.
type LabelName string type LabelName string

View File

@ -44,10 +44,14 @@ func (l LabelSet) Merge(other LabelSet) LabelSet {
func (l LabelSet) String() string { func (l LabelSet) String() string {
labelStrings := make([]string, 0, len(l)) labelStrings := make([]string, 0, len(l))
for label, value := range l { for label, value := range l {
labelStrings = append(labelStrings, fmt.Sprintf("%s='%s'", label, value)) labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
} }
sort.Strings(labelStrings) switch len(labelStrings) {
case 0:
return fmt.Sprintf("{%s}", strings.Join(labelStrings, ", ")) return ""
default:
sort.Strings(labelStrings)
return fmt.Sprintf("{%s}", strings.Join(labelStrings, ", "))
}
} }

View File

@ -13,6 +13,12 @@
package model package model
import (
"fmt"
"sort"
"strings"
)
// A Metric is similar to a LabelSet, but the key difference is that a Metric is // A Metric is similar to a LabelSet, but the key difference is that a Metric is
// a singleton and refers to one and only one stream of samples. // a singleton and refers to one and only one stream of samples.
type Metric map[LabelName]LabelValue type Metric map[LabelName]LabelValue
@ -36,3 +42,24 @@ func (m Metric) Before(o Metric) bool {
return m.Before(o) return m.Before(o)
} }
func (m Metric) String() string {
metricName, ok := m[MetricNameLabel]
if !ok {
panic("Tried to print metric without name")
}
labelStrings := make([]string, 0, len(m)-1)
for label, value := range m {
if label != MetricNameLabel {
labelStrings = append(labelStrings, fmt.Sprintf("%s=%q", label, value))
}
}
switch len(labelStrings) {
case 0:
return string(metricName)
default:
sort.Strings(labelStrings)
return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
}
}