Skip to content

Commit

Permalink
Fix dimensions configuration did not take effect for resource attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Jared Tan <[email protected]>
  • Loading branch information
JaredTan95 committed Aug 13, 2024
1 parent be6cabf commit 9f8ca5f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
5 changes: 4 additions & 1 deletion connector/exceptionsconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,17 @@ 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
}
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, resourceAttr 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, resourceAttr); 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

0 comments on commit 9f8ca5f

Please sign in to comment.