Skip to content

Commit 9559623

Browse files
committed
Extract the function as a common function.
Signed-off-by: Jared Tan <[email protected]>
1 parent 961d46d commit 9559623

13 files changed

+168
-115
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: exceptionsconnector,servicegraphconnector,spanmetricsconnector
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Extract the `getDimensionValue` function as a common function.
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: [34627]
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+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

connector/exceptionsconnector/connector.go

+12-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package exceptionsconnector // import "github.com/open-telemetry/opentelemetry-c
66
import (
77
"go.opentelemetry.io/collector/pdata/pcommon"
88
conventions "go.opentelemetry.io/collector/semconv/v1.18.0"
9+
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
911
)
1012

1113
const (
@@ -20,21 +22,16 @@ const (
2022
eventNameExc = "exception" // OpenTelemetry non-standard constant.
2123
)
2224

23-
type dimension struct {
24-
name string
25-
value *pcommon.Value
26-
}
27-
28-
func newDimensions(cfgDims []Dimension) []dimension {
25+
func newDimensions(cfgDims []Dimension) []pdatautil.Dimension {
2926
if len(cfgDims) == 0 {
3027
return nil
3128
}
32-
dims := make([]dimension, len(cfgDims))
29+
dims := make([]pdatautil.Dimension, len(cfgDims))
3330
for i := range cfgDims {
34-
dims[i].name = cfgDims[i].Name
31+
dims[i].Name = cfgDims[i].Name
3532
if cfgDims[i].Default != nil {
3633
val := pcommon.NewValueStr(*cfgDims[i].Default)
37-
dims[i].value = &val
34+
dims[i].Value = &val
3835
}
3936
}
4037
return dims
@@ -47,21 +44,21 @@ func newDimensions(cfgDims []Dimension) []dimension {
4744
//
4845
// The ok flag indicates if a dimension value was fetched in order to differentiate
4946
// an empty string value from a state where no value was found.
50-
func getDimensionValue(d dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Map, resourceAttr pcommon.Map) (v pcommon.Value, ok bool) {
47+
func getDimensionValue(d pdatautil.Dimension, spanAttrs pcommon.Map, eventAttrs pcommon.Map, resourceAttr pcommon.Map) (v pcommon.Value, ok bool) {
5148
// The more specific span attribute should take precedence.
52-
if attr, exists := spanAttrs.Get(d.name); exists {
49+
if attr, exists := spanAttrs.Get(d.Name); exists {
5350
return attr, true
5451
}
55-
if attr, exists := eventAttrs.Get(d.name); exists {
52+
if attr, exists := eventAttrs.Get(d.Name); exists {
5653
return attr, true
5754
}
5855
// falling back to searching in resource attributes
59-
if attr, exists := resourceAttr.Get(d.name); exists {
56+
if attr, exists := resourceAttr.Get(d.Name); exists {
6057
return attr, true
6158
}
6259
// Set the default if configured, otherwise this metric will have no value set for the dimension.
63-
if d.value != nil {
64-
return *d.value, true
60+
if d.Value != nil {
61+
return *d.Value, true
6562
}
6663
return v, ok
6764
}

connector/exceptionsconnector/connector_logs.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package exceptionsconnector // import "github.com/open-telemetry/opentelemetry-c
66
import (
77
"context"
88

9+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
10+
911
"go.opentelemetry.io/collector/component"
1012
"go.opentelemetry.io/collector/consumer"
1113
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -21,7 +23,7 @@ type logsConnector struct {
2123
config Config
2224

2325
// Additional dimensions to add to logs.
24-
dimensions []dimension
26+
dimensions []pdatautil.Dimension
2527

2628
logsConsumer consumer.Logs
2729
component.StartFunc
@@ -113,20 +115,13 @@ func (c *logsConnector) attrToLogRecord(sl plog.ScopeLogs, serviceName string, s
113115

114116
// Add configured dimension attributes to the log record.
115117
for _, d := range c.dimensions {
116-
if v, ok := getDimensionValue(d, spanAttrs, eventAttrs, resourceAttrs); ok {
117-
logRecord.Attributes().PutStr(d.name, v.Str())
118+
if v, ok := pdatautil.GetDimensionValue(d, spanAttrs, eventAttrs, resourceAttrs); ok {
119+
logRecord.Attributes().PutStr(d.Name, v.Str())
118120
}
119121
}
120122

121123
// Add stacktrace to the log record.
122-
logRecord.Attributes().PutStr(exceptionStacktraceKey, getValue(eventAttrs, exceptionStacktraceKey))
124+
attrVal, _ := pdatautil.GetAttributeValue(exceptionStacktraceKey, eventAttrs)
125+
logRecord.Attributes().PutStr(exceptionStacktraceKey, attrVal)
123126
return logRecord
124127
}
125-
126-
// getValue returns the value of the attribute with the given key.
127-
func getValue(attr pcommon.Map, key string) string {
128-
if attrVal, ok := attr.Get(key); ok {
129-
return attrVal.Str()
130-
}
131-
return ""
132-
}

connector/exceptionsconnector/connector_metrics.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
13+
1214
"go.opentelemetry.io/collector/component"
1315
"go.opentelemetry.io/collector/consumer"
1416
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -29,7 +31,7 @@ type metricsConnector struct {
2931
config Config
3032

3133
// Additional dimensions to add to metrics.
32-
dimensions []dimension
34+
dimensions []pdatautil.Dimension
3335

3436
keyBuf *bytes.Buffer
3537

@@ -175,16 +177,16 @@ func (c *metricsConnector) addExemplar(exc *exception, traceID pcommon.TraceID,
175177
e.SetDoubleValue(float64(exc.count))
176178
}
177179

178-
func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.Span, eventAttrs pcommon.Map, resourceAttrs pcommon.Map) pcommon.Map {
180+
func buildDimensionKVs(dimensions []pdatautil.Dimension, serviceName string, span ptrace.Span, eventAttrs pcommon.Map, resourceAttrs pcommon.Map) pcommon.Map {
179181
dims := pcommon.NewMap()
180182
dims.EnsureCapacity(3 + len(dimensions))
181183
dims.PutStr(serviceNameKey, serviceName)
182184
dims.PutStr(spanNameKey, span.Name())
183185
dims.PutStr(spanKindKey, traceutil.SpanKindStr(span.Kind()))
184186
dims.PutStr(statusCodeKey, traceutil.StatusCodeStr(span.Status().Code()))
185187
for _, d := range dimensions {
186-
if v, ok := getDimensionValue(d, span.Attributes(), eventAttrs, resourceAttrs); ok {
187-
v.CopyTo(dims.PutEmpty(d.name))
188+
if v, ok := pdatautil.GetDimensionValue(d, span.Attributes(), eventAttrs, resourceAttrs); ok {
189+
v.CopyTo(dims.PutEmpty(d.Name))
188190
}
189191
}
190192
return dims
@@ -195,7 +197,7 @@ func buildDimensionKVs(dimensions []dimension, serviceName string, span ptrace.S
195197
// or resource attributes. If the dimension exists in both, the span's attributes, being the most specific, takes precedence.
196198
//
197199
// 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, resourceAttrs pcommon.Map) {
200+
func buildKey(dest *bytes.Buffer, serviceName string, span ptrace.Span, optionalDims []pdatautil.Dimension, eventAttrs pcommon.Map, resourceAttrs pcommon.Map) {
199201
concatDimensionValue(dest, serviceName, false)
200202
concatDimensionValue(dest, span.Name(), true)
201203
concatDimensionValue(dest, traceutil.SpanKindStr(span.Kind()), true)

connector/exceptionsconnector/connector_metrics_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
13+
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
1416
"go.opentelemetry.io/collector/component/componenttest"
@@ -277,7 +279,7 @@ func TestBuildKeyWithDimensions(t *testing.T) {
277279
defaultFoo := pcommon.NewValueStr("bar")
278280
for _, tc := range []struct {
279281
name string
280-
optionalDims []dimension
282+
optionalDims []pdatautil.Dimension
281283
resourceAttrMap map[string]any
282284
spanAttrMap map[string]any
283285
wantKey string
@@ -288,22 +290,22 @@ func TestBuildKeyWithDimensions(t *testing.T) {
288290
},
289291
{
290292
name: "neither span nor resource contains key, dim provides default",
291-
optionalDims: []dimension{
292-
{name: "foo", value: &defaultFoo},
293+
optionalDims: []pdatautil.Dimension{
294+
{Name: "foo", Value: &defaultFoo},
293295
},
294296
wantKey: "ab\u0000c\u0000SPAN_KIND_UNSPECIFIED\u0000STATUS_CODE_UNSET\u0000bar",
295297
},
296298
{
297299
name: "neither span nor resource contains key, dim provides no default",
298-
optionalDims: []dimension{
299-
{name: "foo"},
300+
optionalDims: []pdatautil.Dimension{
301+
{Name: "foo"},
300302
},
301303
wantKey: "ab\u0000c\u0000SPAN_KIND_UNSPECIFIED\u0000STATUS_CODE_UNSET",
302304
},
303305
{
304306
name: "span attribute contains dimension",
305-
optionalDims: []dimension{
306-
{name: "foo"},
307+
optionalDims: []pdatautil.Dimension{
308+
{Name: "foo"},
307309
},
308310
spanAttrMap: map[string]any{
309311
"foo": 99,
@@ -312,8 +314,8 @@ func TestBuildKeyWithDimensions(t *testing.T) {
312314
},
313315
{
314316
name: "resource attribute contains dimension",
315-
optionalDims: []dimension{
316-
{name: "foo"},
317+
optionalDims: []pdatautil.Dimension{
318+
{Name: "foo"},
317319
},
318320
resourceAttrMap: map[string]any{
319321
"foo": 99,
@@ -322,8 +324,8 @@ func TestBuildKeyWithDimensions(t *testing.T) {
322324
},
323325
{
324326
name: "both span and resource attribute contains dimension, should prefer span attribute",
325-
optionalDims: []dimension{
326-
{name: "foo"},
327+
optionalDims: []pdatautil.Dimension{
328+
{Name: "foo"},
327329
},
328330
spanAttrMap: map[string]any{
329331
"foo": 100,

connector/exceptionsconnector/factory_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"context"
88
"testing"
99

10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
11+
1012
"github.com/stretchr/testify/assert"
1113
"go.opentelemetry.io/collector/connector/connectortest"
1214
"go.opentelemetry.io/collector/consumer/consumertest"
@@ -19,7 +21,7 @@ func TestNewConnector(t *testing.T) {
1921
for _, tc := range []struct {
2022
name string
2123
dimensions []Dimension
22-
wantDimensions []dimension
24+
wantDimensions []pdatautil.Dimension
2325
}{
2426
{
2527
name: "simplest config (use defaults)",
@@ -30,8 +32,8 @@ func TestNewConnector(t *testing.T) {
3032
{Name: "http.method", Default: &defaultMethod},
3133
{Name: "http.status_code"},
3234
},
33-
wantDimensions: []dimension{
34-
{name: "http.method", value: &defaultMethodValue},
35+
wantDimensions: []pdatautil.Dimension{
36+
{Name: "http.method", Value: &defaultMethodValue},
3537
{"http.status_code", nil},
3638
},
3739
},

connector/servicegraphconnector/connector.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"sync"
1414
"time"
1515

16+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
17+
1618
"go.opentelemetry.io/collector/component"
1719
"go.opentelemetry.io/collector/consumer"
1820
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -262,7 +264,7 @@ func (p *serviceGraphConnector) aggregateMetrics(ctx context.Context, td ptrace.
262264

263265
// A database request will only have one span, we don't wait for the server
264266
// span but just copy details from the client span
265-
if dbName, ok := findAttributeValue(p.config.DatabaseNameAttribute, rAttributes, span.Attributes()); ok {
267+
if dbName, ok := pdatautil.GetAttributeValue(p.config.DatabaseNameAttribute, rAttributes, span.Attributes()); ok {
266268
e.ConnectionType = store.Database
267269
e.ServerService = dbName
268270
e.ServerLatencySec = spanDuration(span)
@@ -310,15 +312,15 @@ func (p *serviceGraphConnector) aggregateMetrics(ctx context.Context, td ptrace.
310312

311313
func (p *serviceGraphConnector) upsertDimensions(kind string, m map[string]string, resourceAttr pcommon.Map, spanAttr pcommon.Map) {
312314
for _, dim := range p.config.Dimensions {
313-
if v, ok := findAttributeValue(dim, resourceAttr, spanAttr); ok {
315+
if v, ok := pdatautil.GetAttributeValue(dim, resourceAttr, spanAttr); ok {
314316
m[kind+"_"+dim] = v
315317
}
316318
}
317319
}
318320

319321
func (p *serviceGraphConnector) upsertPeerAttributes(m []string, peers map[string]string, spanAttr pcommon.Map) {
320322
for _, s := range m {
321-
if v, ok := findAttributeValue(s, spanAttr); ok {
323+
if v, ok := pdatautil.GetAttributeValue(s, spanAttr); ok {
322324
peers[s] = v
323325
break
324326
}

connector/servicegraphconnector/util.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,10 @@ package servicegraphconnector // import "github.com/open-telemetry/opentelemetry
66
import (
77
"go.opentelemetry.io/collector/pdata/pcommon"
88
semconv "go.opentelemetry.io/collector/semconv/v1.9.0"
9-
)
109

11-
func findAttributeValue(key string, attributes ...pcommon.Map) (string, bool) {
12-
for _, attr := range attributes {
13-
if v, ok := attr.Get(key); ok {
14-
return v.AsString(), true
15-
}
16-
}
17-
return "", false
18-
}
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil"
11+
)
1912

2013
func findServiceName(attributes pcommon.Map) (string, bool) {
21-
return findAttributeValue(semconv.AttributeServiceName, attributes)
14+
return pdatautil.GetAttributeValue(semconv.AttributeServiceName, attributes)
2215
}

0 commit comments

Comments
 (0)