Skip to content

Commit c47606c

Browse files
committed
add histogram aggregation
Signed-off-by: alex boten <[email protected]>
1 parent d72182f commit c47606c

File tree

5 files changed

+94
-9
lines changed

5 files changed

+94
-9
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` 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` to v1.0.0 model in `go.opentelemetry.io/contrib/otelconf`. (#8127)
2222

2323
### Changed
2424

otelconf/config_common.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ var enumValuesViewSelectorInstrumentType = []any{
4242
"up_down_counter",
4343
}
4444

45+
var enumValuesOTLPMetricDefaultHistogramAggregation = []any{
46+
"explicit_bucket_histogram",
47+
"base2_exponential_bucket_histogram",
48+
}
49+
4550
type configOptions struct {
4651
ctx context.Context
4752
opentelemetryConfig OpenTelemetryConfiguration
@@ -273,12 +278,22 @@ func createHeadersConfig(headers []NameStringValuePair, headersList *string) (ma
273278
return result, nil
274279
}
275280

276-
// supportedInstrumentType return true if the instrument type is supported.
277-
func supportedInstrumentType(in InstrumentType) bool {
281+
// supportedInstrumentType return an error if the instrument type is not supported.
282+
func supportedInstrumentType(in InstrumentType) error {
278283
for _, expected := range enumValuesViewSelectorInstrumentType {
279284
if string(in) == fmt.Sprintf("%s", expected) {
280-
return true
285+
return nil
286+
}
287+
}
288+
return newErrInvalid(fmt.Sprintf("invalid selector (expected one of %#v): %#v", enumValuesViewSelectorInstrumentType, in))
289+
}
290+
291+
// supportedHistogramAggregation return an error if the histogram aggregation is not supported.
292+
func supportedHistogramAggregation(in ExporterDefaultHistogramAggregation) error {
293+
for _, expected := range enumValuesOTLPMetricDefaultHistogramAggregation {
294+
if string(in) == fmt.Sprintf("%s", expected) {
295+
return nil
281296
}
282297
}
283-
return false
298+
return newErrInvalid(fmt.Sprintf("invalid histogram aggregation (expected one of %#v): %#v", enumValuesOTLPMetricDefaultHistogramAggregation, in))
284299
}

otelconf/config_json.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ func (j *InstrumentType) UnmarshalJSON(b []byte) error {
644644
if err := json.Unmarshal(b, &v); err != nil {
645645
return errors.Join(newErrUnmarshal(j), err)
646646
}
647-
if !supportedInstrumentType(InstrumentType(v)) {
648-
return newErrInvalid(fmt.Sprintf("invalid selector (expected one of %#v): %#v", enumValuesViewSelectorInstrumentType, v))
647+
if err := supportedInstrumentType(InstrumentType(v)); err != nil {
648+
return err
649649
}
650650
*j = InstrumentType(v)
651651
return nil
@@ -679,3 +679,16 @@ func (j *ExperimentalPeerInstrumentationServiceMappingElem) UnmarshalJSON(b []by
679679
*j = ExperimentalPeerInstrumentationServiceMappingElem(sh.Plain)
680680
return nil
681681
}
682+
683+
// UnmarshalJSON implements json.Unmarshaler.
684+
func (j *ExporterDefaultHistogramAggregation) UnmarshalJSON(b []byte) error {
685+
var v string
686+
if err := json.Unmarshal(b, &v); err != nil {
687+
return errors.Join(newErrUnmarshal(j), err)
688+
}
689+
if err := supportedHistogramAggregation(ExporterDefaultHistogramAggregation(v)); err != nil {
690+
return err
691+
}
692+
*j = ExporterDefaultHistogramAggregation(v)
693+
return nil
694+
}

otelconf/config_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,3 +1376,45 @@ func TestUnmarshalExperimentalPeerInstrumentationServiceMappingElemType(t *testi
13761376
})
13771377
}
13781378
}
1379+
1380+
func TestUnmarshalExporterDefaultHistogramAggregation(t *testing.T) {
1381+
var exporterDefaultHistogramAggregation ExporterDefaultHistogramAggregation
1382+
for _, tt := range []struct {
1383+
name string
1384+
yamlConfig []byte
1385+
jsonConfig []byte
1386+
wantErrT error
1387+
wantExporterDefaultHistogramAggregation ExporterDefaultHistogramAggregation
1388+
}{
1389+
{
1390+
name: "invalid data",
1391+
jsonConfig: []byte(`{:2000}`),
1392+
yamlConfig: []byte("name: []\nvalue: true\ntype: bool\n"),
1393+
wantErrT: newErrUnmarshal(&exporterDefaultHistogramAggregation),
1394+
},
1395+
{
1396+
name: "invalid histogram aggregation",
1397+
jsonConfig: []byte(`"test"`),
1398+
yamlConfig: []byte("test"),
1399+
wantErrT: newErrInvalid(`invalid histogram aggregation (expected one of []interface {}{"explicit_bucket_histogram", "base2_exponential_bucket_histogram"}): "test""`),
1400+
},
1401+
{
1402+
name: "valid histogram aggregation",
1403+
jsonConfig: []byte(`"base2_exponential_bucket_histogram"`),
1404+
yamlConfig: []byte("base2_exponential_bucket_histogram"),
1405+
wantExporterDefaultHistogramAggregation: ExporterDefaultHistogramAggregationBase2ExponentialBucketHistogram,
1406+
},
1407+
} {
1408+
t.Run(tt.name, func(t *testing.T) {
1409+
val := ExporterDefaultHistogramAggregation("")
1410+
err := val.UnmarshalJSON(tt.jsonConfig)
1411+
assert.ErrorIs(t, err, tt.wantErrT)
1412+
assert.Equal(t, tt.wantExporterDefaultHistogramAggregation, val)
1413+
1414+
val = ExporterDefaultHistogramAggregation("")
1415+
err = yaml.Unmarshal(tt.yamlConfig, &val)
1416+
assert.ErrorIs(t, err, tt.wantErrT)
1417+
assert.Equal(t, tt.wantExporterDefaultHistogramAggregation, val)
1418+
})
1419+
}
1420+
}

otelconf/config_yaml.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,8 @@ func (j *InstrumentType) UnmarshalYAML(node *yaml.Node) error {
388388
if err := node.Decode(&plain); err != nil {
389389
return errors.Join(newErrUnmarshal(j), err)
390390
}
391-
if !supportedInstrumentType(InstrumentType(plain)) {
392-
return newErrInvalid(fmt.Sprintf("invalid selector (expected one of %#v): %#v", enumValuesViewSelectorInstrumentType, plain))
391+
if err := supportedInstrumentType(InstrumentType(plain)); err != nil {
392+
return err
393393
}
394394

395395
*j = InstrumentType(plain)
@@ -414,3 +414,18 @@ func (j *ExperimentalPeerInstrumentationServiceMappingElem) UnmarshalYAML(node *
414414
*j = ExperimentalPeerInstrumentationServiceMappingElem(plain)
415415
return nil
416416
}
417+
418+
// UnmarshalYAML implements yaml.Unmarshaler.
419+
func (j *ExporterDefaultHistogramAggregation) UnmarshalYAML(node *yaml.Node) error {
420+
type Plain ExporterDefaultHistogramAggregation
421+
var plain Plain
422+
if err := node.Decode(&plain); err != nil {
423+
return errors.Join(newErrUnmarshal(j), err)
424+
}
425+
if err := supportedHistogramAggregation(ExporterDefaultHistogramAggregation(plain)); err != nil {
426+
return err
427+
}
428+
429+
*j = ExporterDefaultHistogramAggregation(plain)
430+
return nil
431+
}

0 commit comments

Comments
 (0)