-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/translator/prometheus] Add option to keep UTF-8 characters
Signed-off-by: Arthur Silva Sens <[email protected]>
- Loading branch information
1 parent
e2b8f38
commit dd96ad5
Showing
18 changed files
with
330 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/translator/prometheus | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Optionally allow UTF-8 characters in Prometheus metric and label names. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35459] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [api] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Provenance-includes-location: https://github.com/golang/go/blob/f2d118fd5f7e872804a5825ce29797f81a28b0fa/src/strings/strings.go | ||
// Provenance-includes-license: BSD-3-Clause | ||
// Provenance-includes-copyright: Copyright The Go Authors. | ||
|
||
package prometheus | ||
|
||
import "strings" | ||
|
||
// fieldsFunc is a copy of strings.FieldsFunc from the Go standard library, | ||
// but it also returns the separators as part of the result. | ||
func fieldsFunc(s string, f func(rune) bool) ([]string, []string) { | ||
// A span is used to record a slice of s of the form s[start:end]. | ||
// The start index is inclusive and the end index is exclusive. | ||
type span struct { | ||
start int | ||
end int | ||
} | ||
spans := make([]span, 0, 32) | ||
separators := make([]string, 0, 32) | ||
|
||
// Find the field start and end indices. | ||
// Doing this in a separate pass (rather than slicing the string s | ||
// and collecting the result substrings right away) is significantly | ||
// more efficient, possibly due to cache effects. | ||
start := -1 // valid span start if >= 0 | ||
for end, rune := range s { | ||
if f(rune) { | ||
if start >= 0 { | ||
spans = append(spans, span{start, end}) | ||
// Set start to a negative value. | ||
// Note: using -1 here consistently and reproducibly | ||
// slows down this code by a several percent on amd64. | ||
start = ^start | ||
separators = append(separators, string(s[end])) | ||
} | ||
} else { | ||
if start < 0 { | ||
start = end | ||
} | ||
} | ||
} | ||
|
||
// Last field might end at EOF. | ||
if start >= 0 { | ||
spans = append(spans, span{start, len(s)}) | ||
} | ||
|
||
// Create strings from recorded field indices. | ||
a := make([]string, len(spans)) | ||
for i, span := range spans { | ||
a[i] = s[span.start:span.end] | ||
} | ||
|
||
return a, separators | ||
} | ||
|
||
// join is a copy of strings.Join from the Go standard library, | ||
// but it also accepts a slice of separators to join the elements with. | ||
// If the slice of separators is shorter than the slice of elements, use a default value. | ||
// We also don't check for integer overflow. | ||
func join(elems []string, separators []string, def string) string { | ||
switch len(elems) { | ||
case 0: | ||
return "" | ||
case 1: | ||
return elems[0] | ||
} | ||
|
||
var n int | ||
var sep string | ||
sepLen := len(separators) | ||
for i, elem := range elems { | ||
if i >= sepLen { | ||
sep = def | ||
} else { | ||
sep = separators[i] | ||
} | ||
n += len(sep) + len(elem) | ||
} | ||
|
||
var b strings.Builder | ||
b.Grow(n) | ||
b.WriteString(elems[0]) | ||
for i, s := range elems[1:] { | ||
if i >= sepLen { | ||
sep = def | ||
} else { | ||
sep = separators[i] | ||
} | ||
b.WriteString(sep) | ||
b.WriteString(s) | ||
} | ||
return b.String() | ||
} |
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
Oops, something went wrong.