From 9f8ca5f4326d4832465da07ea5b0203dba01329a Mon Sep 17 00:00:00 2001 From: Jared Tan Date: Tue, 13 Aug 2024 17:02:27 +0800 Subject: [PATCH] Fix dimensions configuration did not take effect for resource attributes Signed-off-by: Jared Tan --- connector/exceptionsconnector/connector.go | 5 ++++- connector/exceptionsconnector/connector_logs.go | 6 +++--- connector/exceptionsconnector/connector_metrics.go | 12 ++++++------ .../exceptionsconnector/connector_metrics_test.go | 6 +++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/connector/exceptionsconnector/connector.go b/connector/exceptionsconnector/connector.go index e6fad832bc47..a4973e99dd9f 100644 --- a/connector/exceptionsconnector/connector.go +++ b/connector/exceptionsconnector/connector.go @@ -47,7 +47,7 @@ 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 @@ -55,6 +55,9 @@ func getDimensionValue(d dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Ma 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 diff --git a/connector/exceptionsconnector/connector_logs.go b/connector/exceptionsconnector/connector_logs.go index a7a58f5294a2..5d81f9a79e30 100644 --- a/connector/exceptionsconnector/connector_logs.go +++ b/connector/exceptionsconnector/connector_logs.go @@ -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) } } } @@ -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()) @@ -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()) } } diff --git a/connector/exceptionsconnector/connector_metrics.go b/connector/exceptionsconnector/connector_metrics.go index 6ed5c0669d94..bf0f4a60c30b 100644 --- a/connector/exceptionsconnector/connector_metrics.go +++ b/connector/exceptionsconnector/connector_metrics.go @@ -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()) } @@ -175,7 +175,7 @@ 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) @@ -183,7 +183,7 @@ func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.S 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)) } } @@ -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) } } diff --git a/connector/exceptionsconnector/connector_metrics_test.go b/connector/exceptionsconnector/connector_metrics_test.go index f196677a78a8..ff2a244f4c11 100644 --- a/connector/exceptionsconnector/connector_metrics_test.go +++ b/connector/exceptionsconnector/connector_metrics_test.go @@ -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) @@ -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()) }) }