-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: Add remote API with write client; add remote handler.
Signed-off-by: bwplotka <[email protected]>
- Loading branch information
Showing
17 changed files
with
5,071 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT | ||
|
||
go 1.22.6 | ||
|
||
require github.com/bufbuild/buf v1.39.0 // cmd/buf |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# buf.gen.yaml | ||
version: v2 | ||
|
||
plugins: | ||
- remote: buf.build/protocolbuffers/go:v1.31.0 | ||
out: . | ||
opt: | ||
- Mio/prometheus/write/v2/types.proto=./v2 | ||
|
||
# vtproto for efficiency utilities like pooling etc. | ||
# https://buf.build/community/planetscale-vtprotobuf?version=v0.6.0 | ||
- remote: buf.build/community/planetscale-vtprotobuf:v0.6.0 | ||
out: . | ||
opt: | ||
- Mio/prometheus/write/v2/types.proto=./v2 | ||
- features=marshal+unmarshal+size | ||
|
||
inputs: | ||
- module: buf.build/prometheus/prometheus:5b212ab78fb7460e831cf7ff2d83e385 | ||
types: | ||
- "io.prometheus.write.v2.Request" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright (c) Bartłomiej Płotka @bwplotka | ||
// Licensed under the Apache License 2.0. | ||
|
||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Copyright 2024 Prometheus Team | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package writev2 | ||
|
||
// SymbolsTable implements table for easy symbol use. | ||
type SymbolsTable struct { | ||
strings []string | ||
symbolsMap map[string]uint32 | ||
} | ||
|
||
// NewSymbolTable returns a symbol table. | ||
func NewSymbolTable() SymbolsTable { | ||
return SymbolsTable{ | ||
// Empty string is required as a first element. | ||
symbolsMap: map[string]uint32{"": 0}, | ||
strings: []string{""}, | ||
} | ||
} | ||
|
||
// Symbolize adds (if not added before) a string to the symbols table, | ||
// while returning its reference number. | ||
func (t *SymbolsTable) Symbolize(str string) uint32 { | ||
if ref, ok := t.symbolsMap[str]; ok { | ||
return ref | ||
} | ||
ref := uint32(len(t.strings)) | ||
t.strings = append(t.strings, str) | ||
t.symbolsMap[str] = ref | ||
return ref | ||
} | ||
|
||
// SymbolizeLabels symbolize Prometheus labels. | ||
func (t *SymbolsTable) SymbolizeLabels(lbls []string, buf []uint32) []uint32 { | ||
result := buf[:0] | ||
for i := 0; i < len(lbls); i += 2 { | ||
off := t.Symbolize(lbls[i]) | ||
result = append(result, off) | ||
off = t.Symbolize(lbls[i+1]) | ||
result = append(result, off) | ||
} | ||
return result | ||
} | ||
|
||
// Symbols returns computes symbols table to put in e.g. Request.Symbols. | ||
// As per spec, order does not matter. | ||
func (t *SymbolsTable) Symbols() []string { | ||
return t.strings | ||
} | ||
|
||
// Reset clears symbols table. | ||
func (t *SymbolsTable) Reset() { | ||
// NOTE: Make sure to keep empty symbol. | ||
t.strings = t.strings[:1] | ||
for k := range t.symbolsMap { | ||
if k == "" { | ||
continue | ||
} | ||
delete(t.symbolsMap, k) | ||
} | ||
} | ||
|
||
// DesymbolizeLabels decodes label references, with given symbols to labels. | ||
func DesymbolizeLabels(labelRefs []uint32, symbols, buf []string) []string { | ||
result := buf[:0] | ||
for i := 0; i < len(labelRefs); i += 2 { | ||
result = append(result, symbols[labelRefs[i]], symbols[labelRefs[i+1]]) | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Bartłomiej Płotka @bwplotka | ||
// Licensed under the Apache License 2.0. | ||
|
||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Copyright 2024 Prometheus Team | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package writev2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func requireEqual(t testing.TB, expected, got any) { | ||
if diff := cmp.Diff(expected, got); diff != "" { | ||
t.Fatal(diff) | ||
} | ||
} | ||
|
||
func TestSymbolsTable(t *testing.T) { | ||
s := NewSymbolTable() | ||
requireEqual(t, []string{""}, s.Symbols()) | ||
requireEqual(t, uint32(0), s.Symbolize("")) | ||
requireEqual(t, []string{""}, s.Symbols()) | ||
|
||
requireEqual(t, uint32(1), s.Symbolize("abc")) | ||
requireEqual(t, []string{"", "abc"}, s.Symbols()) | ||
|
||
requireEqual(t, uint32(2), s.Symbolize("__name__")) | ||
requireEqual(t, []string{"", "abc", "__name__"}, s.Symbols()) | ||
|
||
requireEqual(t, uint32(3), s.Symbolize("foo")) | ||
requireEqual(t, []string{"", "abc", "__name__", "foo"}, s.Symbols()) | ||
|
||
s.Reset() | ||
requireEqual(t, []string{""}, s.Symbols()) | ||
requireEqual(t, uint32(0), s.Symbolize("")) | ||
|
||
requireEqual(t, uint32(1), s.Symbolize("__name__")) | ||
requireEqual(t, []string{"", "__name__"}, s.Symbols()) | ||
|
||
requireEqual(t, uint32(2), s.Symbolize("abc")) | ||
requireEqual(t, []string{"", "__name__", "abc"}, s.Symbols()) | ||
|
||
ls := []string{"__name__", "qwer", "zxcv", "1234"} | ||
encoded := s.SymbolizeLabels(ls, nil) | ||
requireEqual(t, []uint32{1, 3, 4, 5}, encoded) | ||
decoded := DesymbolizeLabels(encoded, s.Symbols(), nil) | ||
requireEqual(t, ls, decoded) | ||
|
||
// Different buf. | ||
ls = []string{"__name__", "qwer", "zxcv2222", "1234"} | ||
encoded = s.SymbolizeLabels(ls, []uint32{1, 3, 4, 5}) | ||
requireEqual(t, []uint32{1, 3, 6, 5}, encoded) | ||
} |
Oops, something went wrong.