forked from prometheus/prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We copy files from the otel-collector-contrib. See the README in `storage/remote/otlptranslator/README.md`. This supersedes: prometheus#11965 Signed-off-by: gouthamve <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,774 additions
and
7 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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,23 @@ | ||
## Copying from opentelemetry/opentelemetry-collector-contrib | ||
|
||
This files in the `prometheus/` and `prometheusremotewrite/` are copied from the OpenTelemetry Project[^1]. | ||
|
||
This is done instead of adding a go.mod dependency because OpenTelemetry depends on `prometheus/prometheus` and a cyclic dependency will be created. This is just a temporary solution and the long-term solution is to move the required packages from OpenTelemetry into `prometheus/prometheus`. | ||
We don't copy in `./prometheus` through this script because that package imports a collector specific featuregate package we don't want to import. The featuregate package is being removed now, and in the future we will copy this folder too. | ||
|
||
To update the dependency is a multi-step process: | ||
1. Vendor the latest `prometheus/prometheus`@`main` into [`opentelemetry/opentelemetry-collector-contrib`](https://github.com/open-telemetry/opentelemetry-collector-contrib) | ||
1. Update the VERSION in `update-copy.sh`. | ||
1. Run `./update-copy.sh`. | ||
|
||
### Why copy? | ||
|
||
This is because the packages we copy depend on the [`prompb`](https://github.com/prometheus/prometheus/blob/main/prompb) package. While the package is relatively stable, there are still changes. For example, https://github.com/prometheus/prometheus/pull/11935 changed the types. | ||
This means if we depend on the upstream packages directly, we will never able to make the changes like above. Hence we're copying the code for now. | ||
|
||
### I need to manually change these files | ||
|
||
When we do want to make changes to the types in `prompb`, we might need to edit the files directly. That is OK, please let @gouthamve or @jesusvazquez know so they can take care of updating the upstream code (by vendoring in `prometheus/prometheus` upstream and resolving conflicts) and then will run the copy | ||
script again to keep things updated. | ||
|
||
[^1]: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/translator/prometheus and https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/translator/prometheusremotewrite |
41 changes: 41 additions & 0 deletions
41
storage/remote/otlptranslator/prometheus/normalize_label.go
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,41 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package normalize | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// Normalizes the specified label to follow Prometheus label names standard | ||
// | ||
// See rules at https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | ||
// | ||
// Labels that start with non-letter rune will be prefixed with "key_" | ||
// | ||
// Exception is made for double-underscores which are allowed | ||
func NormalizeLabel(label string) string { | ||
// Trivial case | ||
if len(label) == 0 { | ||
return label | ||
} | ||
|
||
// Replace all non-alphanumeric runes with underscores | ||
label = strings.Map(sanitizeRune, label) | ||
|
||
// If label starts with a number, prepend with "key_" | ||
if unicode.IsDigit(rune(label[0])) { | ||
label = "key_" + label | ||
} | ||
|
||
return label | ||
} | ||
|
||
// Return '_' for anything non-alphanumeric | ||
func sanitizeRune(r rune) rune { | ||
if unicode.IsLetter(r) || unicode.IsDigit(r) { | ||
return r | ||
} | ||
return '_' | ||
} |
19 changes: 19 additions & 0 deletions
19
storage/remote/otlptranslator/prometheus/normalize_label_test.go
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,19 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package normalize | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSanitizeDropSanitization(t *testing.T) { | ||
require.Equal(t, "", NormalizeLabel("")) | ||
require.Equal(t, "_test", NormalizeLabel("_test")) | ||
require.Equal(t, "key_0test", NormalizeLabel("0test")) | ||
require.Equal(t, "test", NormalizeLabel("test")) | ||
require.Equal(t, "test__", NormalizeLabel("test_/")) | ||
require.Equal(t, "__test", NormalizeLabel("__test")) | ||
} |
Oops, something went wrong.