Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/otlp/metrics] Add WithSplatArrayAttributes #331

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .chloggen/mx-psi_splat-tags.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component (e.g. pkg/quantile)
component: pkg/otlp/metrics

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `WithSplatArrayAttributes` translator option

# The PR related to this change
issues: [329]

# (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: |
- This option destructures an array valued attribute into multiple tags with the same key and different values.
17 changes: 17 additions & 0 deletions .chloggen/mx-psi_splat-tags2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component (e.g. pkg/quantile)
component: pkg/otlp/metrics

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `splat` option to `WithAttributeMap`

# The PR related to this change
issues: [329]

# (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: |
- This option destructures an array valued attribute into multiple tags with the same key and different values.
9 changes: 9 additions & 0 deletions pkg/otlp/metrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type translatorConfig struct {
InitialCumulMonoValueMode InitialCumulMonoValueMode
InstrumentationLibraryMetadataAsTags bool
InstrumentationScopeMetadataAsTags bool
SplatArrayAttributes bool

originProduct OriginProduct

Expand Down Expand Up @@ -215,3 +216,11 @@ func WithInitialCumulMonoValueMode(mode InitialCumulMonoValueMode) TranslatorOpt
return nil
}
}

// WithSplatArrayAttributes destructures an array attribute into multiple Datadog tags.
func WithSplatArrayAttributes() TranslatorOption {
return func(t *translatorConfig) error {
t.SplatArrayAttributes = true
return nil
}
}
33 changes: 30 additions & 3 deletions pkg/otlp/metrics/dimensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,38 @@ func (d *Dimensions) OriginProductDetail() OriginProductDetail {
// getTags maps an attributeMap into a slice of Datadog tags
func getTags(labels pcommon.Map) []string {
tags := make([]string, 0, labels.Len())
labels.Range(func(key string, value pcommon.Value) bool {
v := value.AsString()
labels.Range(func(key string, val pcommon.Value) bool {
v := val.AsString()
tags = append(tags, utils.FormatKeyValueTag(key, v))
return true
})
return tags
}

// getTagsWithSplat maps an attributeMap into a slice of Datadog tags.
// It will flatten the map values if they are slices.
func getTagsWithSplat(labels pcommon.Map) []string {
// Allocation is a lower bound since we are not traversing the whole map.
tags := make([]string, 0, labels.Len())

var addToTags func(string, pcommon.Value) bool
addToTags = func(key string, val pcommon.Value) bool {
switch val.Type() {
case pcommon.ValueTypeSlice:
for i := 0; i < val.Slice().Len(); i++ {
_ = addToTags(key, val.Slice().At(i))
}
default:
v := val.AsString()
tags = append(tags, utils.FormatKeyValueTag(key, v))
}
return true
}

labels.Range(addToTags)
return tags
}

// AddTags to metrics dimensions.
func (d *Dimensions) AddTags(tags ...string) *Dimensions {
// defensively copy the tags
Expand All @@ -106,7 +130,10 @@ func (d *Dimensions) AddTags(tags ...string) *Dimensions {
}

// WithAttributeMap creates a new metricDimensions struct with additional tags from attributes.
func (d *Dimensions) WithAttributeMap(labels pcommon.Map) *Dimensions {
func (d *Dimensions) WithAttributeMap(labels pcommon.Map, splat bool) *Dimensions {
if splat {
return d.AddTags(getTagsWithSplat(labels)...)
}
return d.AddTags(getTags(labels)...)
}

Expand Down
35 changes: 32 additions & 3 deletions pkg/otlp/metrics/dimensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,41 @@ func TestWithAttributeMap(t *testing.T) {
"key1": "val1",
"key2": "val2",
"key3": "",
"key4": []any{"val4a", "val4b"},
"key5": []any{"val5a", []any{"val5b", "val5c", []any{"val5d"}}},
})

dims := Dimensions{}
assert.ElementsMatch(t,
dims.WithAttributeMap(attributes).tags,
[...]string{"key1:val1", "key2:val2", "key3:n/a"},
dims.WithAttributeMap(attributes, false).tags,
[...]string{"key1:val1", "key2:val2", "key3:n/a", `key4:["val4a","val4b"]`, `key5:["val5a",["val5b","val5c",["val5d"]]]`},
)
}

func TestWithAttributeMapSplat(t *testing.T) {
attributes := pcommon.NewMap()
attributes.FromRaw(map[string]interface{}{
"key1": "val1",
"key2": "val2",
"key3": "",
"key4": []any{"val4a", "val4b"},
"key5": []any{"val5a", []any{"val5b", "val5c", []any{"val5d"}}},
})

dims := Dimensions{}
assert.ElementsMatch(t,
dims.WithAttributeMap(attributes, true).tags,
[...]string{
"key1:val1",
"key2:val2",
"key3:n/a",
"key4:val4a",
"key4:val4b",
"key5:val5a",
"key5:val5b",
"key5:val5c",
"key5:val5d",
},
)
}

Expand Down Expand Up @@ -115,7 +144,7 @@ func TestAllFieldsAreCopied(t *testing.T) {
newDims := dims.
AddTags("tagThree:c").
WithSuffix("suffix").
WithAttributeMap(attributes)
WithAttributeMap(attributes, false)

assert.Equal(t, "example.name.suffix", newDims.Name())
assert.Equal(t, "hostname", newDims.Host())
Expand Down
2 changes: 1 addition & 1 deletion pkg/otlp/metrics/exponential_histograms_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (t *Translator) mapExponentialHistogramMetrics(
p := slice.At(i)
startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
pointDims := dims.WithAttributeMap(p.Attributes(), t.cfg.SplatArrayAttributes)

histInfo := histogramInfo{ok: true}

Expand Down
8 changes: 4 additions & 4 deletions pkg/otlp/metrics/metrics_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (t *Translator) mapNumberMetrics(
continue
}

pointDims := dims.WithAttributeMap(p.Attributes())
pointDims := dims.WithAttributeMap(p.Attributes(), t.cfg.SplatArrayAttributes)
var val float64
switch p.ValueType() {
case pmetric.NumberDataPointValueTypeDouble:
Expand Down Expand Up @@ -215,7 +215,7 @@ func (t *Translator) mapNumberMonotonicMetrics(

ts := uint64(p.Timestamp())
startTs := uint64(p.StartTimestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
pointDims := dims.WithAttributeMap(p.Attributes(), t.cfg.SplatArrayAttributes)

var val float64
switch p.ValueType() {
Expand Down Expand Up @@ -467,7 +467,7 @@ func (t *Translator) mapHistogramMetrics(

startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
pointDims := dims.WithAttributeMap(p.Attributes(), t.cfg.SplatArrayAttributes)

histInfo := histogramInfo{ok: true}

Expand Down Expand Up @@ -576,7 +576,7 @@ func (t *Translator) mapSummaryMetrics(

startTs := uint64(p.StartTimestamp())
ts := uint64(p.Timestamp())
pointDims := dims.WithAttributeMap(p.Attributes())
pointDims := dims.WithAttributeMap(p.Attributes(), t.cfg.SplatArrayAttributes)

// count and sum are increasing; we treat them as cumulative monotonic sums.
{
Expand Down
Loading