Skip to content

Commit 54118aa

Browse files
committed
[PRW-2.0] (part2) Moved to latest basic negotiation & spec semantics.
Spec: prometheus/docs#2462 Supersedes #13968 Signed-off-by: bwplotka <bwplotka@gmail.com> # Conflicts: # config/config.go # docs/configuration/configuration.md # storage/remote/queue_manager_test.go # storage/remote/write.go # web/api/v1/api.go
1 parent 2b348d4 commit 54118aa

File tree

17 files changed

+970
-1045
lines changed

17 files changed

+970
-1045
lines changed

cmd/promtool/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func parseAndPushMetrics(client *remote.Client, data []byte, labels map[string]s
116116

117117
// Encode the request body into snappy encoding.
118118
compressed := snappy.Encode(nil, raw)
119-
err = client.Store(context.Background(), compressed, 0, remote.Version1, "snappy")
119+
err = client.Store(context.Background(), compressed, 0)
120120
if err != nil {
121121
fmt.Fprintln(os.Stderr, " FAILED:", err)
122122
return false

config/config.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,9 +1026,6 @@ func CheckTargetAddress(address model.LabelValue) error {
10261026
return nil
10271027
}
10281028

1029-
// TODO(bwplotka): Remove in the next PRs (review split PR for readability).
1030-
type RemoteWriteFormat int64
1031-
10321029
// RemoteWriteProtoMsg represents the known protobuf message for the remote write
10331030
// 1.0 and 2.0 specs.
10341031
type RemoteWriteProtoMsg string
@@ -1054,7 +1051,7 @@ func (m RemoteWriteProtoMsgs) Strings() []string {
10541051
}
10551052

10561053
func (m RemoteWriteProtoMsgs) String() string {
1057-
return strings.Join(m.Strings(), ",")
1054+
return strings.Join(m.Strings(), ", ")
10581055
}
10591056

10601057
var (

docs/configuration/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3557,7 +3557,7 @@ url: <string>
35573557
# Before changing this value, consult with your remote storage provider (or test) what message it supports.
35583558
# Read more on https://prometheus.io/docs/specs/remote_write_spec_2_0/#io-prometheus-write-v2-request
35593559
[ protobuf_message: <prometheus.WriteRequest | io.prometheus.write.v2.Request > | default = prometheus.WriteRequest ]
3560-
3560+
35613561
# Timeout for requests to the remote write endpoint.
35623562
[ remote_timeout: <duration> | default = 30s ]
35633563

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2024 Prometheus Team
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package writev2
15+
16+
import "github.com/prometheus/prometheus/model/labels"
17+
18+
// SymbolsTable implements table for easy symbol use.
19+
type SymbolsTable struct {
20+
strings []string
21+
symbolsMap map[string]uint32
22+
}
23+
24+
// NewSymbolTable returns a symbol table.
25+
func NewSymbolTable() SymbolsTable {
26+
return SymbolsTable{
27+
// Empty string is required as a first element.
28+
symbolsMap: map[string]uint32{"": 0},
29+
strings: []string{""},
30+
}
31+
}
32+
33+
// Symbolize adds (if not added before) a string to the symbols table,
34+
// while returning its reference number.
35+
func (t *SymbolsTable) Symbolize(str string) uint32 {
36+
if ref, ok := t.symbolsMap[str]; ok {
37+
return ref
38+
}
39+
ref := uint32(len(t.strings))
40+
t.strings = append(t.strings, str)
41+
t.symbolsMap[str] = ref
42+
return ref
43+
}
44+
45+
// SymbolizeLabels symbolize Prometheus labels.
46+
func (t *SymbolsTable) SymbolizeLabels(lbls labels.Labels, buf []uint32) []uint32 {
47+
result := buf[:0]
48+
lbls.Range(func(l labels.Label) {
49+
off := t.Symbolize(l.Name)
50+
result = append(result, off)
51+
off = t.Symbolize(l.Value)
52+
result = append(result, off)
53+
})
54+
return result
55+
}
56+
57+
// Symbols returns computes symbols table to put in e.g. Request.Symbols.
58+
// As per spec, order does not matter.
59+
func (t *SymbolsTable) Symbols() []string {
60+
return t.strings
61+
}
62+
63+
// Reset clears symbols table.
64+
func (t *SymbolsTable) Reset() {
65+
// NOTE: Make sure to keep empty symbol.
66+
t.strings = t.strings[:1]
67+
for k := range t.symbolsMap {
68+
if k == "" {
69+
continue
70+
}
71+
delete(t.symbolsMap, k)
72+
}
73+
}
74+
75+
// DesymbolizeLabels decodes label references, with given symbols to labels.
76+
func DesymbolizeLabels(labelRefs []uint32, symbols []string) labels.Labels {
77+
b := labels.NewScratchBuilder(len(labelRefs))
78+
for i := 0; i < len(labelRefs); i += 2 {
79+
b.Add(symbols[labelRefs[i]], symbols[labelRefs[i+1]])
80+
}
81+
b.Sort()
82+
return b.Labels()
83+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024 Prometheus Team
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package writev2
15+
16+
import (
17+
"testing"
18+
19+
"github.com/stretchr/testify/require"
20+
21+
"github.com/prometheus/prometheus/model/labels"
22+
)
23+
24+
func TestSymbolsTable(t *testing.T) {
25+
s := NewSymbolTable()
26+
require.Equal(t, []string{""}, s.Symbols(), "required empty reference does not exist")
27+
require.Equal(t, uint32(0), s.Symbolize(""))
28+
require.Equal(t, []string{""}, s.Symbols())
29+
30+
require.Equal(t, uint32(1), s.Symbolize("abc"))
31+
require.Equal(t, []string{"", "abc"}, s.Symbols())
32+
33+
require.Equal(t, uint32(2), s.Symbolize("__name__"))
34+
require.Equal(t, []string{"", "abc", "__name__"}, s.Symbols())
35+
36+
require.Equal(t, uint32(3), s.Symbolize("foo"))
37+
require.Equal(t, []string{"", "abc", "__name__", "foo"}, s.Symbols())
38+
39+
s.Reset()
40+
require.Equal(t, []string{""}, s.Symbols(), "required empty reference does not exist")
41+
require.Equal(t, uint32(0), s.Symbolize(""))
42+
43+
require.Equal(t, uint32(1), s.Symbolize("__name__"))
44+
require.Equal(t, []string{"", "__name__"}, s.Symbols())
45+
46+
require.Equal(t, uint32(2), s.Symbolize("abc"))
47+
require.Equal(t, []string{"", "__name__", "abc"}, s.Symbols())
48+
49+
ls := labels.FromStrings("__name__", "qwer", "zxcv", "1234")
50+
encoded := s.SymbolizeLabels(ls, nil)
51+
require.Equal(t, []uint32{1, 3, 4, 5}, encoded)
52+
decoded := DesymbolizeLabels(encoded, s.Symbols())
53+
require.Equal(t, ls, decoded)
54+
55+
// Different buf.
56+
ls = labels.FromStrings("__name__", "qwer", "zxcv2222", "1234")
57+
encoded = s.SymbolizeLabels(ls, []uint32{1, 3, 4, 5})
58+
require.Equal(t, []uint32{1, 3, 6, 5}, encoded)
59+
}

0 commit comments

Comments
 (0)