Skip to content

Commit 36bb3a2

Browse files
committed
add metric reader
Signed-off-by: alex boten <[email protected]>
1 parent c47606c commit 36bb3a2

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-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`, `ExperimentalPeerInstrumentationServiceMappingElem`, `ExporterDefaultHistogramAggregation` 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`, `ExporterDefaultHistogramAggregation`, `PullMetricReader` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8127)
2222

2323
### Changed
2424

otelconf/config_json.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,3 +692,25 @@ func (j *ExporterDefaultHistogramAggregation) UnmarshalJSON(b []byte) error {
692692
*j = ExporterDefaultHistogramAggregation(v)
693693
return nil
694694
}
695+
696+
// UnmarshalJSON implements json.Unmarshaler.
697+
func (j *PullMetricReader) UnmarshalJSON(b []byte) error {
698+
type Plain PullMetricReader
699+
type shadow struct {
700+
Plain
701+
Exporter json.RawMessage `json:"exporter"`
702+
}
703+
var sh shadow
704+
if err := json.Unmarshal(b, &sh); err != nil {
705+
return errors.Join(newErrUnmarshal(j), err)
706+
}
707+
if sh.Exporter == nil {
708+
return newErrRequired(j, "exporter")
709+
}
710+
// Hydrate the exporter into the underlying field.
711+
if err := json.Unmarshal(sh.Exporter, &sh.Plain.Exporter); err != nil {
712+
return err
713+
}
714+
*j = PullMetricReader(sh.Plain)
715+
return nil
716+
}

otelconf/config_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,3 +1418,44 @@ func TestUnmarshalExporterDefaultHistogramAggregation(t *testing.T) {
14181418
})
14191419
}
14201420
}
1421+
1422+
func TestUnmarshalPullMetricReader(t *testing.T) {
1423+
for _, tt := range []struct {
1424+
name string
1425+
yamlConfig []byte
1426+
jsonConfig []byte
1427+
wantErrT error
1428+
wantExporter PullMetricExporter
1429+
}{
1430+
{
1431+
name: "valid with proemtheus exporter",
1432+
jsonConfig: []byte(`{"exporter":{"prometheus/development":{}}}`),
1433+
yamlConfig: []byte("exporter:\n prometheus/development: {}"),
1434+
wantExporter: PullMetricExporter{PrometheusDevelopment: &ExperimentalPrometheusMetricExporter{}},
1435+
},
1436+
{
1437+
name: "missing required exporter field",
1438+
jsonConfig: []byte(`{}`),
1439+
yamlConfig: []byte("{}"),
1440+
wantErrT: newErrRequired(&PullMetricReader{}, "exporter"),
1441+
},
1442+
{
1443+
name: "invalid data",
1444+
jsonConfig: []byte(`{:2000}`),
1445+
yamlConfig: []byte("exporter:\n prometheus/development: []"),
1446+
wantErrT: newErrUnmarshal(&PullMetricReader{}),
1447+
},
1448+
} {
1449+
t.Run(tt.name, func(t *testing.T) {
1450+
cl := PullMetricReader{}
1451+
err := cl.UnmarshalJSON(tt.jsonConfig)
1452+
assert.ErrorIs(t, err, tt.wantErrT)
1453+
assert.Equal(t, tt.wantExporter, cl.Exporter)
1454+
1455+
cl = PullMetricReader{}
1456+
err = yaml.Unmarshal(tt.yamlConfig, &cl)
1457+
assert.ErrorIs(t, err, tt.wantErrT)
1458+
assert.Equal(t, tt.wantExporter, cl.Exporter)
1459+
})
1460+
}
1461+
}

otelconf/config_yaml.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,17 @@ func (j *ExporterDefaultHistogramAggregation) UnmarshalYAML(node *yaml.Node) err
429429
*j = ExporterDefaultHistogramAggregation(plain)
430430
return nil
431431
}
432+
433+
// UnmarshalYAML implements yaml.Unmarshaler.
434+
func (j *PullMetricReader) UnmarshalYAML(node *yaml.Node) error {
435+
if !hasYAMLMapKey(node, "exporter") {
436+
return newErrRequired(j, "exporter")
437+
}
438+
type Plain PullMetricReader
439+
var plain Plain
440+
if err := node.Decode(&plain); err != nil {
441+
return errors.Join(newErrUnmarshal(j), err)
442+
}
443+
*j = PullMetricReader(plain)
444+
return nil
445+
}

0 commit comments

Comments
 (0)