Skip to content

Commit

Permalink
model/labels: Add Label.Identifying field
Browse files Browse the repository at this point in the history
Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
  • Loading branch information
aknuds1 committed Feb 6, 2024
1 parent 975ab59 commit c7c6d7d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 29 deletions.
8 changes: 6 additions & 2 deletions model/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,12 @@ func (b *ScratchBuilder) Reset() {

// Add a name/value pair.
// Note if you Add the same name twice you will get a duplicate label, which is invalid.
func (b *ScratchBuilder) Add(name, value string) {
b.add = append(b.add, Label{Name: name, Value: value})
func (b *ScratchBuilder) Add(name, value string, identifyingLabels ...string) {
identifying := false
if len(identifyingLabels) > 0 && slices.Contains(identifyingLabels, name) {
identifying = true
}
b.add = append(b.add, Label{Name: name, Value: value, Identifying: identifying})
}

// Add a name/value pair, using []byte instead of string.
Expand Down
8 changes: 8 additions & 0 deletions model/labels/labels_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package labels
import (
"bytes"
"encoding/json"
"fmt"
"strconv"

"github.com/prometheus/common/model"
Expand All @@ -36,6 +37,13 @@ var seps = []byte{'\xff'}
// Label is a key/value pair of strings.
type Label struct {
Name, Value string
// Identifying signifies whether this is an identifying label for an info metric.
Identifying bool
}

// String returns l's string representation.
func (l Label) String() string {
return fmt.Sprintf("%s=%s", l.Name, l.Value)
}

func (ls Labels) String() string {
Expand Down
8 changes: 6 additions & 2 deletions model/labels/labels_stringlabels.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,12 @@ func (b *ScratchBuilder) Reset() {

// Add a name/value pair.
// Note if you Add the same name twice you will get a duplicate label, which is invalid.
func (b *ScratchBuilder) Add(name, value string) {
b.add = append(b.add, Label{Name: name, Value: value})
func (b *ScratchBuilder) Add(name, value string, identifyingLabels ...string) {
identifying := false
if len(identifyingLabels) > 0 && slices.Contains(identifyingLabels, name) {
identifying = true
}
b.add = append(b.add, Label{Name: name, Value: value, Identifying: identifying})
}

// Add a name/value pair, using []byte instead of string to reduce memory allocations.
Expand Down
40 changes: 20 additions & 20 deletions model/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func TestBuilder(t *testing.T) {
},
{
base: FromStrings("aaa", "111", "bbb", "222", "ccc", "333"),
set: []Label{{"aaa", "444"}, {"bbb", "555"}, {"ccc", "666"}},
set: []Label{{Name: "aaa", Value: "444"}, {Name: "bbb", Value: "555"}, {Name: "ccc", Value: "666"}},
want: FromStrings("aaa", "444", "bbb", "555", "ccc", "666"),
},
{
Expand All @@ -591,24 +591,24 @@ func TestBuilder(t *testing.T) {
want: FromStrings("aaa", "111", "ccc", "333"),
},
{
set: []Label{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}},
set: []Label{{Name: "aaa", Value: "111"}, {Name: "bbb", Value: "222"}, {Name: "ccc", Value: "333"}},
del: []string{"bbb"},
want: FromStrings("aaa", "111", "ccc", "333"),
},
{
base: FromStrings("aaa", "111"),
set: []Label{{"bbb", "222"}},
set: []Label{{Name: "bbb", Value: "222"}},
want: FromStrings("aaa", "111", "bbb", "222"),
},
{
base: FromStrings("aaa", "111"),
set: []Label{{"bbb", "222"}, {"bbb", "333"}},
set: []Label{{Name: "bbb", Value: "222"}, {Name: "bbb", Value: "333"}},
want: FromStrings("aaa", "111", "bbb", "333"),
},
{
base: FromStrings("aaa", "111", "bbb", "222", "ccc", "333"),
del: []string{"bbb"},
set: []Label{{"ddd", "444"}},
set: []Label{{Name: "ddd", Value: "444"}},
want: FromStrings("aaa", "111", "ccc", "333", "ddd", "444"),
},
{ // Blank value is interpreted as delete.
Expand All @@ -617,7 +617,7 @@ func TestBuilder(t *testing.T) {
},
{
base: FromStrings("aaa", "111", "bbb", "222", "ccc", "333"),
set: []Label{{"bbb", ""}},
set: []Label{{Name: "bbb", Value: ""}},
want: FromStrings("aaa", "111", "ccc", "333"),
},
{
Expand All @@ -633,7 +633,7 @@ func TestBuilder(t *testing.T) {
{
base: FromStrings("aaa", "111", "bbb", "222", "ccc", "333"),
del: []string{"bbb"},
set: []Label{{"ddd", "444"}},
set: []Label{{Name: "ddd", Value: "444"}},
keep: []string{"aaa", "ddd"},
want: FromStrings("aaa", "111", "ddd", "444"),
},
Expand Down Expand Up @@ -677,19 +677,19 @@ func TestScratchBuilder(t *testing.T) {
want: EmptyLabels(),
},
{
add: []Label{{"aaa", "111"}},
add: []Label{{Name: "aaa", Value: "111"}},
want: FromStrings("aaa", "111"),
},
{
add: []Label{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}},
add: []Label{{Name: "aaa", Value: "111"}, {Name: "bbb", Value: "222"}, {Name: "ccc", Value: "333"}},
want: FromStrings("aaa", "111", "bbb", "222", "ccc", "333"),
},
{
add: []Label{{"bbb", "222"}, {"aaa", "111"}, {"ccc", "333"}},
add: []Label{{Name: "bbb", Value: "222"}, {Name: "aaa", Value: "111"}, {Name: "ccc", Value: "333"}},
want: FromStrings("aaa", "111", "bbb", "222", "ccc", "333"),
},
{
add: []Label{{"ddd", "444"}},
add: []Label{{Name: "ddd", Value: "444"}},
want: FromStrings("ddd", "444"),
},
} {
Expand Down Expand Up @@ -769,15 +769,15 @@ func BenchmarkLabels_Hash(b *testing.B) {

func BenchmarkBuilder(b *testing.B) {
m := []Label{
{"job", "node"},
{"instance", "123.123.1.211:9090"},
{"path", "/api/v1/namespaces/<namespace>/deployments/<name>"},
{"method", "GET"},
{"namespace", "system"},
{"status", "500"},
{"prometheus", "prometheus-core-1"},
{"datacenter", "eu-west-1"},
{"pod_name", "abcdef-99999-defee"},
{Name: "job", Value: "node"},
{Name: "instance", Value: "123.123.1.211:9090"},
{Name: "path", Value: "/api/v1/namespaces/<namespace>/deployments/<name>"},
{Name: "method", Value: "GET"},
{Name: "namespace", Value: "system"},
{Name: "status", Value: "500"},
{Name: "prometheus", Value: "prometheus-core-1"},
{Name: "datacenter", Value: "eu-west-1"},
{Name: "pod_name", Value: "abcdef-99999-defee"},
}

var l Labels
Expand Down
4 changes: 2 additions & 2 deletions storage/remote/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,10 +760,10 @@ func LabelProtosToMetric(labelPairs []*prompb.Label) model.Metric {
return metric
}

func labelProtosToLabels(labelPairs []prompb.Label) labels.Labels {
func labelProtosToLabels(labelPairs []prompb.Label, identifyingLabels ...string) labels.Labels {
b := labels.ScratchBuilder{}
for _, l := range labelPairs {
b.Add(l.Name, l.Value)
b.Add(l.Name, l.Value, identifyingLabels...)
}
b.Sort()
return b.Labels()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"log"
"math"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -86,9 +87,10 @@ func addSample(tsMap map[string]*prompb.TimeSeries, sample *prompb.Sample, label
var identifyingLabels []string
if isTargetMetric {
identifyingLabels = []string{
model.JobLabel,
model.InstanceLabel,
model.JobLabel,
}
slices.Sort(identifyingLabels)
}
newTs := &prompb.TimeSeries{
Labels: labels,
Expand Down
5 changes: 3 additions & 2 deletions storage/remote/write_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,16 @@ func (h *writeHandler) write(ctx context.Context, req *prompb.WriteRequest) (err

var exemplarErr error
for _, ts := range req.Timeseries {
labels := labelProtosToLabels(ts.Labels)
labels := labelProtosToLabels(ts.Labels, ts.IdentifyingLabels...)
if !labels.IsValid() {
level.Warn(h.logger).Log("msg", "Invalid metric names or labels", "got", labels.String())
samplesWithInvalidLabels++
continue
}
var ref storage.SeriesRef
for _, s := range ts.Samples {
// When recording a sample for an info type metric with identifying labels,
// make sure also to persist which of the labels are identifying
ref, err = app.Append(ref, labels, s.Timestamp, s.Value)
if err != nil {
unwrappedErr := errors.Unwrap(err)
Expand Down Expand Up @@ -225,7 +227,6 @@ func (h *otlpWriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

prwMetrics := make([]prompb.TimeSeries, 0, len(prwMetricsMap))

for _, ts := range prwMetricsMap {
prwMetrics = append(prwMetrics, *ts)
}
Expand Down

0 comments on commit c7c6d7d

Please sign in to comment.