Skip to content

Commit

Permalink
[Metrics Rewrite] attribute to label mapping (#243)
Browse files Browse the repository at this point in the history
[Metrics Rewrite] attribute to label mapping
  • Loading branch information
aabmass authored Dec 14, 2021
1 parent aabe3ac commit a64ae0c
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
13 changes: 13 additions & 0 deletions exporter/collector/breaking-changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Breaking changes vs old googlecloud exporter

The new pdata based exporter has some breaking changes from the original OpenCensus stackdriver
based `googlecloud` exporter:

## Labels

Original label key mapping code is
[here](https://github.com/census-ecosystem/opencensus-go-exporter-stackdriver/blob/42e7e58efdb937e8477f827d3fba022212335dbc/sanitize.go#L26).
The new code does not:

- truncate label keys longer than 100 characters.
- prepend `key` when the first character is `_`.
32 changes: 31 additions & 1 deletion exporter/collector/metricsexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package collector

import (
"context"
"strings"
"unicode"

monitoring "cloud.google.com/go/monitoring/apiv3/v2"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -173,7 +175,35 @@ func attributesToLabels(
attrs pdata.AttributeMap,
) labels {
// TODO
return nil
ls := make(labels, attrs.Len())
attrs.Range(func(k string, v pdata.AttributeValue) bool {
ls[sanitizeKey(k)] = v.AsString()
return true
})
return ls
}

// Replaces non-alphanumeric characters to underscores. Note, this does not truncate label keys
// longer than 100 characters or prepend "key" when the first character is "_" like OpenCensus
// did.
func sanitizeKey(s string) string {
if len(s) == 0 {
return s
}
s = strings.Map(sanitizeRune, s)
if unicode.IsDigit(rune(s[0])) {
s = "key_" + s
}
return s
}

// converts anything that is not a letter or digit to an underscore
func sanitizeRune(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) {
return r
}
// Everything else turns into an underscore
return '_'
}

func mergeLabels(mergeInto labels, others ...labels) labels {
Expand Down
62 changes: 62 additions & 0 deletions exporter/collector/metricsexporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,65 @@ func TestMergeLabels(t *testing.T) {
"Should merge intersecting labels, keeping the last value",
)
}

func TestAttributesToLabels(t *testing.T) {
// string to string
assert.Equal(
t,
attributesToLabels(pdata.NewAttributeMapFromMap(map[string]pdata.AttributeValue{
"foo": pdata.NewAttributeValueString("bar"),
"bar": pdata.NewAttributeValueString("baz"),
})),
labels{"foo": "bar", "bar": "baz"},
)

// various key special cases
assert.Equal(
t,
attributesToLabels(pdata.NewAttributeMapFromMap(map[string]pdata.AttributeValue{
"foo.bar": pdata.NewAttributeValueString("bar"),
"_foo": pdata.NewAttributeValueString("bar"),
"123.hello": pdata.NewAttributeValueString("bar"),
"": pdata.NewAttributeValueString("bar"),
})),
labels{
"foo_bar": "bar",
"_foo": "bar",
"key_123_hello": "bar",
"": "bar",
},
)

attribSlice := pdata.NewAttributeValueArray()
attribSlice.SliceVal().AppendEmpty().SetStringVal("x")
attribSlice.SliceVal().AppendEmpty()
attribSlice.SliceVal().AppendEmpty().SetStringVal("y")

attribMap := pdata.NewAttributeValueMap()
attribMap.MapVal().InsertString("a", "b")

// value special cases
assert.Equal(
t,
attributesToLabels(pdata.NewAttributeMapFromMap(map[string]pdata.AttributeValue{
"a": pdata.NewAttributeValueBool(true),
"b": pdata.NewAttributeValueBool(false),
"c": pdata.NewAttributeValueInt(12),
"d": pdata.NewAttributeValueDouble(12.3),
"e": pdata.NewAttributeValueEmpty(),
"f": pdata.NewAttributeValueBytes([]byte{0xde, 0xad, 0xbe, 0xef}),
"g": attribSlice,
"h": attribMap,
})),
labels{
"a": "true",
"b": "false",
"c": "12",
"d": "12.3",
"e": "",
"f": "3q2+7w==",
"g": `["x",null,"y"]`,
"h": `{"a":"b"}`,
},
)
}

0 comments on commit a64ae0c

Please sign in to comment.