Skip to content

Commit d72182f

Browse files
committed
add unmarshal for experimental peer instrumentation
Signed-off-by: alex boten <[email protected]>
1 parent 0017ddb commit d72182f

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1818
- Add unmarshaling and validation for `TextMapPropagator` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8052)
1919
- Add unmarshaling and validation for `OTLPHttpExporter`, `OTLPGrpcExporter`, `OTLPGrpcMetricExporter` and `OTLPHttpMetricExporter` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8112)
2020
- Add a `WithSpanNameFormatter` option to `go.opentelemetry.io/contrib/instrumentation/go.mongodb.org/mongo-driver/v2/mongo/otelmongo`. (#7986)
21-
- Add unmarshaling and validation for `AttributeType`, `AttributeNameValue`, `SimpleSpanProcessor`, `SimpleLogRecordProcessor`, `ZipkinSpanExporter`, `NameStringValuePair`, `InstrumentType` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8127)
21+
- Add unmarshaling and validation for `AttributeType`, `AttributeNameValue`, `SimpleSpanProcessor`, `SimpleLogRecordProcessor`, `ZipkinSpanExporter`, `NameStringValuePair`, `InstrumentType`, `ExperimentalPeerInstrumentationServiceMappingElem` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8127)
2222

2323
### Changed
2424

otelconf/config_json.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,3 +650,32 @@ func (j *InstrumentType) UnmarshalJSON(b []byte) error {
650650
*j = InstrumentType(v)
651651
return nil
652652
}
653+
654+
// UnmarshalJSON implements json.Unmarshaler.
655+
func (j *ExperimentalPeerInstrumentationServiceMappingElem) UnmarshalJSON(b []byte) error {
656+
type Plain ExperimentalPeerInstrumentationServiceMappingElem
657+
type shadow struct {
658+
Plain
659+
Peer json.RawMessage `json:"peer"`
660+
Service json.RawMessage `json:"service"`
661+
}
662+
var sh shadow
663+
if err := json.Unmarshal(b, &sh); err != nil {
664+
return errors.Join(newErrUnmarshal(j), err)
665+
}
666+
if sh.Peer == nil {
667+
return newErrRequired(j, "peer")
668+
}
669+
if err := json.Unmarshal(sh.Peer, &sh.Plain.Peer); err != nil {
670+
return errors.Join(newErrUnmarshal(j), err)
671+
}
672+
if sh.Service == nil {
673+
return newErrRequired(j, "service")
674+
}
675+
if err := json.Unmarshal(sh.Service, &sh.Plain.Service); err != nil {
676+
return errors.Join(newErrUnmarshal(j), err)
677+
}
678+
679+
*j = ExperimentalPeerInstrumentationServiceMappingElem(sh.Plain)
680+
return nil
681+
}

otelconf/config_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,12 @@ func TestUnmarshalNameStringValuePairType(t *testing.T) {
12371237
yamlConfig: []byte("name: test"),
12381238
wantErrT: newErrRequired(&NameStringValuePair{}, "value"),
12391239
},
1240+
{
1241+
name: "invalid array name",
1242+
jsonConfig: []byte(`{"name":[], "value": ["test-val", "test-val-2"], "type": "string_array"}`),
1243+
yamlConfig: []byte("name: []\nvalue: [test-val, test-val-2]\ntype: string_array\n"),
1244+
wantErrT: newErrUnmarshal(&NameStringValuePair{}),
1245+
},
12401246
{
12411247
name: "valid string value",
12421248
jsonConfig: []byte(`{"name":"test", "value": "test-val", "type": "string"}`),
@@ -1308,3 +1314,65 @@ func TestUnmarshalInstrumentType(t *testing.T) {
13081314
})
13091315
}
13101316
}
1317+
1318+
func TestUnmarshalExperimentalPeerInstrumentationServiceMappingElemType(t *testing.T) {
1319+
for _, tt := range []struct {
1320+
name string
1321+
yamlConfig []byte
1322+
jsonConfig []byte
1323+
wantErrT error
1324+
wantExperimentalPeerInstrumentationServiceMappingElem ExperimentalPeerInstrumentationServiceMappingElem
1325+
}{
1326+
{
1327+
name: "invalid data",
1328+
jsonConfig: []byte(`{:2000}`),
1329+
yamlConfig: []byte("peer: []\nservice: true"),
1330+
wantErrT: newErrUnmarshal(&ExperimentalPeerInstrumentationServiceMappingElem{}),
1331+
},
1332+
{
1333+
name: "missing required peer field",
1334+
jsonConfig: []byte(`{}`),
1335+
yamlConfig: []byte("{}"),
1336+
wantErrT: newErrRequired(&ExperimentalPeerInstrumentationServiceMappingElem{}, "peer"),
1337+
},
1338+
{
1339+
name: "missing required service field",
1340+
jsonConfig: []byte(`{"peer":"test"}`),
1341+
yamlConfig: []byte("peer: test"),
1342+
wantErrT: newErrRequired(&ExperimentalPeerInstrumentationServiceMappingElem{}, "service"),
1343+
},
1344+
{
1345+
name: "invalid string_array peer",
1346+
jsonConfig: []byte(`{"peer":[], "service": ["test-val", "test-val-2"], "type": "string_array"}`),
1347+
yamlConfig: []byte("peer: []\nservice: [test-val, test-val-2]\ntype: string_array\n"),
1348+
wantErrT: newErrUnmarshal(&ExperimentalPeerInstrumentationServiceMappingElem{}),
1349+
},
1350+
{
1351+
name: "valid string service",
1352+
jsonConfig: []byte(`{"peer":"test", "service": "test-val"}`),
1353+
yamlConfig: []byte("peer: test\nservice: test-val"),
1354+
wantExperimentalPeerInstrumentationServiceMappingElem: ExperimentalPeerInstrumentationServiceMappingElem{
1355+
Peer: "test",
1356+
Service: "test-val",
1357+
},
1358+
},
1359+
{
1360+
name: "invalid string_array service",
1361+
jsonConfig: []byte(`{"peer":"test", "service": ["test-val", "test-val-2"], "type": "string_array"}`),
1362+
yamlConfig: []byte("peer: test\nservice: [test-val, test-val-2]\ntype: string_array\n"),
1363+
wantErrT: newErrUnmarshal(&ExperimentalPeerInstrumentationServiceMappingElem{}),
1364+
},
1365+
} {
1366+
t.Run(tt.name, func(t *testing.T) {
1367+
val := ExperimentalPeerInstrumentationServiceMappingElem{}
1368+
err := val.UnmarshalJSON(tt.jsonConfig)
1369+
assert.ErrorIs(t, err, tt.wantErrT)
1370+
assert.Equal(t, tt.wantExperimentalPeerInstrumentationServiceMappingElem, val)
1371+
1372+
val = ExperimentalPeerInstrumentationServiceMappingElem{}
1373+
err = yaml.Unmarshal(tt.yamlConfig, &val)
1374+
assert.ErrorIs(t, err, tt.wantErrT)
1375+
assert.Equal(t, tt.wantExperimentalPeerInstrumentationServiceMappingElem, val)
1376+
})
1377+
}
1378+
}

otelconf/config_yaml.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,22 @@ func (j *InstrumentType) UnmarshalYAML(node *yaml.Node) error {
395395
*j = InstrumentType(plain)
396396
return nil
397397
}
398+
399+
// UnmarshalYAML implements yaml.Unmarshaler.
400+
func (j *ExperimentalPeerInstrumentationServiceMappingElem) UnmarshalYAML(node *yaml.Node) error {
401+
if !hasYAMLMapKey(node, "peer") {
402+
return newErrRequired(j, "peer")
403+
}
404+
if !hasYAMLMapKey(node, "service") {
405+
return newErrRequired(j, "service")
406+
}
407+
408+
type Plain ExperimentalPeerInstrumentationServiceMappingElem
409+
var plain Plain
410+
if err := node.Decode(&plain); err != nil {
411+
return errors.Join(newErrUnmarshal(j), err)
412+
}
413+
414+
*j = ExperimentalPeerInstrumentationServiceMappingElem(plain)
415+
return nil
416+
}

0 commit comments

Comments
 (0)