Skip to content

Commit 2f33ee9

Browse files
JaredTan95f7o
authored andcommitted
[connector/exceptionsconnector] Fix dimensions configuration did not take effect for resource attributes (open-telemetry#34604)
**Description:** Fix dimensions configuration did not take effect for resource attributes <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> **Link to tracking Issue:** open-telemetry#34603 **Testing:** <Describe what testing was performed and which tests were added.> **Documentation:** <Describe the documentation added.> Signed-off-by: Jared Tan <jian.tan@daocloud.io>
1 parent 2d57147 commit 2f33ee9

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: bug_fix
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: connector/exceptionsconnector
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Fix dimensions configuration did not take effect for resource attributes
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [34603]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: ""
19+
# If your change doesn't affect end users or the exported elements of any package,
20+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
21+
# Optional: The change log or logs in which this entry should be included.
22+
# e.g. '[user]' or '[user, api]'
23+
# Include 'user' if the change is relevant to end users.
24+
# Include 'api' if there is a change to a library API.
25+
# Default: '[user]'
26+
change_logs: []

connector/exceptionsconnector/connector.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ func newDimensions(cfgDims []Dimension) []dimension {
4747
//
4848
// The ok flag indicates if a dimension value was fetched in order to differentiate
4949
// an empty string value from a state where no value was found.
50-
func getDimensionValue(d dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Map) (v pcommon.Value, ok bool) {
50+
func getDimensionValue(d dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Map, resourceAttr pcommon.Map) (v pcommon.Value, ok bool) {
5151
// The more specific span attribute should take precedence.
5252
if attr, exists := spanAttrs.Get(d.name); exists {
5353
return attr, true
5454
}
5555
if attr, exists := eventAttrs.Get(d.name); exists {
5656
return attr, true
5757
}
58+
// falling back to searching in resource attributes
59+
if attr, exists := resourceAttr.Get(d.name); exists {
60+
return attr, true
61+
}
5862
// Set the default if configured, otherwise this metric will have no value set for the dimension.
5963
if d.value != nil {
6064
return *d.value, true

connector/exceptionsconnector/connector_logs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *logsConnector) ConsumeTraces(ctx context.Context, traces ptrace.Traces)
6868
for l := 0; l < span.Events().Len(); l++ {
6969
event := span.Events().At(l)
7070
if event.Name() == eventNameExc {
71-
c.attrToLogRecord(sl, serviceName, span, event)
71+
c.attrToLogRecord(sl, serviceName, span, event, resourceAttr)
7272
}
7373
}
7474
}
@@ -91,7 +91,7 @@ func (c *logsConnector) newScopeLogs(ld plog.Logs) plog.ScopeLogs {
9191
return sl
9292
}
9393

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

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

114114
// Add configured dimension attributes to the log record.
115115
for _, d := range c.dimensions {
116-
if v, ok := getDimensionValue(d, spanAttrs, eventAttrs); ok {
116+
if v, ok := getDimensionValue(d, spanAttrs, eventAttrs, resourceAttrs); ok {
117117
logRecord.Attributes().PutStr(d.name, v.Str())
118118
}
119119
}

connector/exceptionsconnector/connector_metrics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ func (c *metricsConnector) ConsumeTraces(ctx context.Context, traces ptrace.Trac
9292
eventAttrs := event.Attributes()
9393

9494
c.keyBuf.Reset()
95-
buildKey(c.keyBuf, serviceName, span, c.dimensions, eventAttrs)
95+
buildKey(c.keyBuf, serviceName, span, c.dimensions, eventAttrs, resourceAttr)
9696
key := c.keyBuf.String()
9797

98-
attrs := buildDimensionKVs(c.dimensions, serviceName, span, eventAttrs)
98+
attrs := buildDimensionKVs(c.dimensions, serviceName, span, eventAttrs, resourceAttr)
9999
exc := c.addException(key, attrs)
100100
c.addExemplar(exc, span.TraceID(), span.SpanID())
101101
}
@@ -175,15 +175,15 @@ func (c *metricsConnector) addExemplar(exc *exception, traceID pcommon.TraceID,
175175
e.SetDoubleValue(float64(exc.count))
176176
}
177177

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

204204
for _, d := range optionalDims {
205-
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs); ok {
205+
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs, resourceAttrs); ok {
206206
concatDimensionValue(dest, v.AsString(), true)
207207
}
208208
}

connector/exceptionsconnector/connector_metrics_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ func TestBuildKeySameServiceOperationCharSequence(t *testing.T) {
261261
span0 := ptrace.NewSpan()
262262
span0.SetName("c")
263263
buf := &bytes.Buffer{}
264-
buildKey(buf, "ab", span0, nil, pcommon.NewMap())
264+
buildKey(buf, "ab", span0, nil, pcommon.NewMap(), pcommon.NewMap())
265265
k0 := buf.String()
266266
buf.Reset()
267267
span1 := ptrace.NewSpan()
268268
span1.SetName("bc")
269-
buildKey(buf, "a", span1, nil, pcommon.NewMap())
269+
buildKey(buf, "a", span1, nil, pcommon.NewMap(), pcommon.NewMap())
270270
k1 := buf.String()
271271
assert.NotEqual(t, k0, k1)
272272
assert.Equal(t, "ab\u0000c\u0000SPAN_KIND_UNSPECIFIED\u0000STATUS_CODE_UNSET", k0)
@@ -341,7 +341,7 @@ func TestBuildKeyWithDimensions(t *testing.T) {
341341
assert.NoError(t, span0.Attributes().FromRaw(tc.spanAttrMap))
342342
span0.SetName("c")
343343
buf := &bytes.Buffer{}
344-
buildKey(buf, "ab", span0, tc.optionalDims, resAttr)
344+
buildKey(buf, "ab", span0, tc.optionalDims, pcommon.NewMap(), resAttr)
345345
assert.Equal(t, tc.wantKey, buf.String())
346346
})
347347
}

0 commit comments

Comments
 (0)