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

[connector/exceptionsconnector] Fix dimensions configuration did not take effect for resource attributes #34604

Merged
Merged
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
26 changes: 26 additions & 0 deletions .chloggen/fix_exceptionconnector_dimension_not_works.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: connector/exceptionsconnector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix dimensions configuration did not take effect for resource attributes

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [34603]

# (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: []
6 changes: 5 additions & 1 deletion connector/exceptionsconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ func newDimensions(cfgDims []Dimension) []dimension {
//
// The ok flag indicates if a dimension value was fetched in order to differentiate
// an empty string value from a state where no value was found.
func getDimensionValue(d dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Map) (v pcommon.Value, ok bool) {
func getDimensionValue(d dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Map, resourceAttr pcommon.Map) (v pcommon.Value, ok bool) {
// The more specific span attribute should take precedence.
if attr, exists := spanAttrs.Get(d.name); exists {
return attr, true
}
if attr, exists := eventAttrs.Get(d.name); exists {
return attr, true
}
// falling back to searching in resource attributes
if attr, exists := resourceAttr.Get(d.name); exists {
return attr, true
}
// Set the default if configured, otherwise this metric will have no value set for the dimension.
if d.value != nil {
return *d.value, true
Expand Down
6 changes: 3 additions & 3 deletions connector/exceptionsconnector/connector_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *logsConnector) ConsumeTraces(ctx context.Context, traces ptrace.Traces)
for l := 0; l < span.Events().Len(); l++ {
event := span.Events().At(l)
if event.Name() == eventNameExc {
c.attrToLogRecord(sl, serviceName, span, event)
c.attrToLogRecord(sl, serviceName, span, event, resourceAttr)
}
}
}
Expand All @@ -91,7 +91,7 @@ func (c *logsConnector) newScopeLogs(ld plog.Logs) plog.ScopeLogs {
return sl
}

func (c *logsConnector) attrToLogRecord(sl plog.ScopeLogs, serviceName string, span ptrace.Span, event ptrace.SpanEvent) plog.LogRecord {
func (c *logsConnector) attrToLogRecord(sl plog.ScopeLogs, serviceName string, span ptrace.Span, event ptrace.SpanEvent, resourceAttrs pcommon.Map) plog.LogRecord {
logRecord := sl.LogRecords().AppendEmpty()

logRecord.SetTimestamp(event.Timestamp())
Expand All @@ -113,7 +113,7 @@ func (c *logsConnector) attrToLogRecord(sl plog.ScopeLogs, serviceName string, s

// Add configured dimension attributes to the log record.
for _, d := range c.dimensions {
if v, ok := getDimensionValue(d, spanAttrs, eventAttrs); ok {
if v, ok := getDimensionValue(d, spanAttrs, eventAttrs, resourceAttrs); ok {
logRecord.Attributes().PutStr(d.name, v.Str())
}
}
Expand Down
12 changes: 6 additions & 6 deletions connector/exceptionsconnector/connector_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func (c *metricsConnector) ConsumeTraces(ctx context.Context, traces ptrace.Trac
eventAttrs := event.Attributes()

c.keyBuf.Reset()
buildKey(c.keyBuf, serviceName, span, c.dimensions, eventAttrs)
buildKey(c.keyBuf, serviceName, span, c.dimensions, eventAttrs, resourceAttr)
key := c.keyBuf.String()

attrs := buildDimensionKVs(c.dimensions, serviceName, span, eventAttrs)
attrs := buildDimensionKVs(c.dimensions, serviceName, span, eventAttrs, resourceAttr)
exc := c.addException(key, attrs)
c.addExemplar(exc, span.TraceID(), span.SpanID())
}
Expand Down Expand Up @@ -175,15 +175,15 @@ func (c *metricsConnector) addExemplar(exc *exception, traceID pcommon.TraceID,
e.SetDoubleValue(float64(exc.count))
}

func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.Span, eventAttrs pcommon.Map) pcommon.Map {
func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.Span, eventAttrs pcommon.Map, resourceAttrs pcommon.Map) pcommon.Map {
dims := pcommon.NewMap()
dims.EnsureCapacity(3 + len(dimensions))
dims.PutStr(serviceNameKey, serviceName)
dims.PutStr(spanNameKey, span.Name())
dims.PutStr(spanKindKey, traceutil.SpanKindStr(span.Kind()))
dims.PutStr(statusCodeKey, traceutil.StatusCodeStr(span.Status().Code()))
for _, d := range dimensions {
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs); ok {
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs, resourceAttrs); ok {
v.CopyTo(dims.PutEmpty(d.name))
}
}
Expand All @@ -195,14 +195,14 @@ func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.S
// or resource attributes. If the dimension exists in both, the span's attributes, being the most specific, takes precedence.
//
// The metric key is a simple concatenation of dimension values, delimited by a null character.
func buildKey(dest *bytes.Buffer, serviceName string, span ptrace.Span, optionalDims []dimension, eventAttrs pcommon.Map) {
func buildKey(dest *bytes.Buffer, serviceName string, span ptrace.Span, optionalDims []dimension, eventAttrs pcommon.Map, resourceAttrs pcommon.Map) {
concatDimensionValue(dest, serviceName, false)
concatDimensionValue(dest, span.Name(), true)
concatDimensionValue(dest, traceutil.SpanKindStr(span.Kind()), true)
concatDimensionValue(dest, traceutil.StatusCodeStr(span.Status().Code()), true)

for _, d := range optionalDims {
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs); ok {
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs, resourceAttrs); ok {
concatDimensionValue(dest, v.AsString(), true)
}
}
Expand Down
6 changes: 3 additions & 3 deletions connector/exceptionsconnector/connector_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ func TestBuildKeySameServiceOperationCharSequence(t *testing.T) {
span0 := ptrace.NewSpan()
span0.SetName("c")
buf := &bytes.Buffer{}
buildKey(buf, "ab", span0, nil, pcommon.NewMap())
buildKey(buf, "ab", span0, nil, pcommon.NewMap(), pcommon.NewMap())
k0 := buf.String()
buf.Reset()
span1 := ptrace.NewSpan()
span1.SetName("bc")
buildKey(buf, "a", span1, nil, pcommon.NewMap())
buildKey(buf, "a", span1, nil, pcommon.NewMap(), pcommon.NewMap())
k1 := buf.String()
assert.NotEqual(t, k0, k1)
assert.Equal(t, "ab\u0000c\u0000SPAN_KIND_UNSPECIFIED\u0000STATUS_CODE_UNSET", k0)
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestBuildKeyWithDimensions(t *testing.T) {
assert.NoError(t, span0.Attributes().FromRaw(tc.spanAttrMap))
span0.SetName("c")
buf := &bytes.Buffer{}
buildKey(buf, "ab", span0, tc.optionalDims, resAttr)
buildKey(buf, "ab", span0, tc.optionalDims, pcommon.NewMap(), resAttr)
assert.Equal(t, tc.wantKey, buf.String())
})
}
Expand Down