Skip to content

Commit 4c38bc8

Browse files
committed
[open-telemetryGH-6629] config: Handle null attributes as invalid
1 parent cd819f5 commit 4c38bc8

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

config/testdata/invalid_empty.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The file format version.
2+
file_format: "0.3"
3+
4+
# Configure if the SDK is disabled or not. This is not required to be provided to ensure the SDK isn't disabled, the default value when this is not provided is for the SDK to be enabled.
5+
# disabled: false
6+
7+
resource:
8+
attributes:
9+
-

config/v0.3.0/config.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ func WithOpenTelemetryConfiguration(cfg OpenTelemetryConfiguration) Configuratio
143143
// ParseYAML parses a YAML configuration file into an OpenTelemetryConfiguration.
144144
func ParseYAML(file []byte) (*OpenTelemetryConfiguration, error) {
145145
var cfg OpenTelemetryConfiguration
146-
err := yaml.Unmarshal(file, &cfg)
147-
if err != nil {
146+
if err := yaml.Unmarshal(file, &cfg); err != nil {
147+
return nil, err
148+
}
149+
if err, ok := validateConfig(&cfg); err != nil || !ok {
148150
return nil, err
149151
}
150-
151152
return &cfg, nil
152153
}
153154

config/v0.3.0/config_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,12 @@ func TestParseYAML(t *testing.T) {
399399
FileFormat: ptr("0.1"),
400400
},
401401
},
402+
{
403+
name: "invalid empty config",
404+
input: "invalid_empty.yaml",
405+
wantErr: errors.New(`yaml: unmarshal errors:
406+
line 3: cannot unmarshal null values`),
407+
},
402408
{
403409
name: "invalid config",
404410
input: "invalid_bool.yaml",

config/v0.3.0/validate_config.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config // import "go.opentelemetry.io/contrib/config/v0.3.0"
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
const (
9+
errCtx string = "invalid OpenTelemetryConfiguration:"
10+
)
11+
12+
func validateConfig(config *OpenTelemetryConfiguration) (error, bool) {
13+
if config == nil {
14+
return errors.New("invalid OpenTelemetryConfiguration: nil config"), false
15+
}
16+
// error on non-empty null values
17+
if config.Resource != nil {
18+
for n, attr := range config.Resource.Attributes {
19+
if attr == (AttributeNameValue{}) {
20+
return fmt.Errorf("%s empty Resource.Attribute[%d]", errCtx, n), false
21+
}
22+
if attr.Value == nil {
23+
return fmt.Errorf("%s missing Resource.Attribute[%d] value", errCtx, n), false
24+
}
25+
}
26+
}
27+
return nil, true
28+
}

config/v0.3.0/validate_config_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package config
2+
3+
import (
4+
"errors"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestConfig(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
input *OpenTelemetryConfiguration
15+
wantErr error
16+
wantOK bool
17+
}{
18+
{
19+
name: "empty resource attribute",
20+
input: &OpenTelemetryConfiguration{
21+
Resource: &Resource{
22+
Attributes: []AttributeNameValue{
23+
{},
24+
},
25+
},
26+
},
27+
wantErr: errors.New(errCtx + " empty Resource.Attribute[0]"),
28+
},
29+
{
30+
name: "missing resource attribute value",
31+
input: &OpenTelemetryConfiguration{
32+
Resource: &Resource{
33+
Attributes: []AttributeNameValue{
34+
{
35+
Name: "empty value",
36+
Value: nil,
37+
},
38+
},
39+
},
40+
},
41+
wantErr: errors.New(errCtx + " missing Resource.Attribute[0] value"),
42+
},
43+
}
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
err, ok := validateConfig(tt.input)
47+
if tt.wantErr != nil {
48+
require.Error(t, err)
49+
require.Equal(t, tt.wantErr.Error(), err.Error())
50+
} else {
51+
require.NoError(t, err)
52+
}
53+
assert.Equal(t, tt.wantOK, ok)
54+
})
55+
}
56+
}

0 commit comments

Comments
 (0)