Skip to content

Commit b1b3b63

Browse files
florianldmathieu
andauthored
[service/telemetry] replace panic() with proper error handling (#15171)
#### Description replace panic() with proper error handling <!-- Issue number if applicable --> #### Link to tracking issue Fixes # <!--Describe what testing was performed and which tests were added.--> #### Testing <!--Describe the documentation added.--> #### Documentation <!--Please delete paragraphs that you did not use before submitting.--> --------- Signed-off-by: Florian Lehner <florian.lehner@elastic.co> Co-authored-by: Damien Mathieu <42@dmathieu.com>
1 parent a54d19e commit b1b3b63

4 files changed

Lines changed: 72 additions & 5 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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. receiver/otlp)
7+
component: pkg/service
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Non-string resource attributes in telemetry configuration now return an error instead of panicking
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [15171]
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+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [user]

service/telemetry/otelconftelemetry/resource.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ func createResource(
2525
pcommonRes := pcommon.NewResource()
2626
for _, keyValue := range res.Attributes() {
2727
key := string(keyValue.Key)
28-
pcommonRes.Attributes().PutStr(key, mustAttributeValueString(key, keyValue.Value))
28+
value, err := attributeValueString(key, keyValue.Value)
29+
if err != nil {
30+
return pcommon.Resource{}, err
31+
}
32+
pcommonRes.Attributes().PutStr(key, value)
2933
}
3034
return pcommonRes, nil
3135
}
@@ -34,12 +38,12 @@ func newResource(set telemetry.Settings, cfg *Config) *sdkresource.Resource {
3438
return resource.New(set.BuildInfo, cfg.Resource)
3539
}
3640

37-
func mustAttributeValueString(k string, v attribute.Value) string {
41+
func attributeValueString(k string, v attribute.Value) (string, error) {
3842
if v.Type() != attribute.STRING {
3943
// We only support string-type resource attributes in the configuration.
40-
panic(fmt.Errorf("attribute %q: expected string, got %s", k, v.Type()))
44+
return "", fmt.Errorf("attribute %q: expected string, got %s", k, v.Type())
4145
}
42-
return v.AsString()
46+
return v.AsString(), nil
4347
}
4448

4549
// pcommonAttrsToOTelAttrs gets the Resource attributes to OpenTelemetry attribute.KeyValue slice.

service/telemetry/otelconftelemetry/resource_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
11+
"go.opentelemetry.io/otel/attribute"
1112

1213
"go.opentelemetry.io/collector/component"
1314
"go.opentelemetry.io/collector/service/telemetry"
@@ -48,3 +49,36 @@ func TestCreateResource(t *testing.T) {
4849
}, raw)
4950
})
5051
}
52+
53+
func TestAttributeValueString(t *testing.T) {
54+
t.Run("string attribute", func(t *testing.T) {
55+
val := attribute.StringValue("test-value")
56+
result, err := attributeValueString("test.attr", val)
57+
require.NoError(t, err)
58+
assert.Equal(t, "test-value", result)
59+
})
60+
61+
t.Run("int64 attribute returns error", func(t *testing.T) {
62+
val := attribute.Int64Value(42)
63+
result, err := attributeValueString("test.attr", val)
64+
require.Error(t, err)
65+
assert.Empty(t, result)
66+
assert.Contains(t, err.Error(), `attribute "test.attr": expected string, got INT64`)
67+
})
68+
69+
t.Run("bool attribute returns error", func(t *testing.T) {
70+
val := attribute.BoolValue(true)
71+
result, err := attributeValueString("test.attr", val)
72+
require.Error(t, err)
73+
assert.Empty(t, result)
74+
assert.Contains(t, err.Error(), `attribute "test.attr": expected string, got BOOL`)
75+
})
76+
77+
t.Run("float64 attribute returns error", func(t *testing.T) {
78+
val := attribute.Float64Value(3.14)
79+
result, err := attributeValueString("test.attr", val)
80+
require.Error(t, err)
81+
assert.Empty(t, result)
82+
assert.Contains(t, err.Error(), `attribute "test.attr": expected string, got FLOAT64`)
83+
})
84+
}

service/telemetry/otelconftelemetry/sdk.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ func newSDK(ctx context.Context, res *sdkresource.Resource, conf config.OpenTele
1515
resourceAttrs := make([]config.AttributeNameValue, 0, res.Len())
1616
for _, r := range res.Attributes() {
1717
key := string(r.Key)
18+
value, err := attributeValueString(key, r.Value)
19+
if err != nil {
20+
return config.SDK{}, err
21+
}
1822
resourceAttrs = append(resourceAttrs, config.AttributeNameValue{
1923
Name: key,
20-
Value: mustAttributeValueString(key, r.Value),
24+
Value: value,
2125
})
2226
}
2327
conf.Resource = &config.Resource{

0 commit comments

Comments
 (0)