Skip to content

Commit 7e5b565

Browse files
committed
moved json/yaml unmarshaling into their own files
This will give us more flexibility to ensure unmarshaling in both yaml/json returns a consistent structure. Signed-off-by: Alex Boten <[email protected]>
1 parent 92ae5bf commit 7e5b565

File tree

5 files changed

+419
-348
lines changed

5 files changed

+419
-348
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ genjsonschema: genjsonschema-cleanup $(GOJSONSCHEMA)
326326
--capitalization OTLP \
327327
--struct-name-from-title \
328328
--package config \
329+
--only-models \
329330
--output ${GENERATED_CONFIG} \
330331
${OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_SRC_DIR}/schema/opentelemetry_configuration.json
331332
@echo Modify jsonschema generated files.

config/config_json.go

Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package config // import "go.opentelemetry.io/contrib/config"
5+
6+
import (
7+
"encoding/json"
8+
"fmt"
9+
"reflect"
10+
)
11+
12+
// MarshalJSON implements json.Marshaler.
13+
func (j *AttributeNameValueType) MarshalJSON() ([]byte, error) {
14+
return json.Marshal(j.Value)
15+
}
16+
17+
var enumValuesAttributeNameValueType = []interface{}{
18+
nil,
19+
"string",
20+
"bool",
21+
"int",
22+
"double",
23+
"string_array",
24+
"bool_array",
25+
"int_array",
26+
"double_array",
27+
}
28+
29+
// UnmarshalJSON implements json.Unmarshaler.
30+
func (j *AttributeNameValueType) UnmarshalJSON(b []byte) error {
31+
var v struct {
32+
Value interface{}
33+
}
34+
if err := json.Unmarshal(b, &v.Value); err != nil {
35+
return err
36+
}
37+
var ok bool
38+
for _, expected := range enumValuesAttributeNameValueType {
39+
if reflect.DeepEqual(v.Value, expected) {
40+
ok = true
41+
break
42+
}
43+
}
44+
if !ok {
45+
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValuesAttributeNameValueType, v.Value)
46+
}
47+
*j = AttributeNameValueType(v)
48+
return nil
49+
}
50+
51+
// UnmarshalJSON implements json.Unmarshaler.
52+
func (j *BatchLogRecordProcessor) UnmarshalJSON(b []byte) error {
53+
var raw map[string]interface{}
54+
if err := json.Unmarshal(b, &raw); err != nil {
55+
return err
56+
}
57+
if _, ok := raw["exporter"]; raw != nil && !ok {
58+
return fmt.Errorf("field exporter in BatchLogRecordProcessor: required")
59+
}
60+
type Plain BatchLogRecordProcessor
61+
var plain Plain
62+
if err := json.Unmarshal(b, &plain); err != nil {
63+
return err
64+
}
65+
*j = BatchLogRecordProcessor(plain)
66+
return nil
67+
}
68+
69+
// UnmarshalJSON implements json.Unmarshaler.
70+
func (j *BatchSpanProcessor) UnmarshalJSON(b []byte) error {
71+
var raw map[string]interface{}
72+
if err := json.Unmarshal(b, &raw); err != nil {
73+
return err
74+
}
75+
if _, ok := raw["exporter"]; raw != nil && !ok {
76+
return fmt.Errorf("field exporter in BatchSpanProcessor: required")
77+
}
78+
type Plain BatchSpanProcessor
79+
var plain Plain
80+
if err := json.Unmarshal(b, &plain); err != nil {
81+
return err
82+
}
83+
*j = BatchSpanProcessor(plain)
84+
return nil
85+
}
86+
87+
// UnmarshalJSON implements json.Unmarshaler.
88+
func (j *GeneralInstrumentationPeerServiceMappingElem) UnmarshalJSON(b []byte) error {
89+
var raw map[string]interface{}
90+
if err := json.Unmarshal(b, &raw); err != nil {
91+
return err
92+
}
93+
if _, ok := raw["peer"]; raw != nil && !ok {
94+
return fmt.Errorf("field peer in GeneralInstrumentationPeerServiceMappingElem: required")
95+
}
96+
if _, ok := raw["service"]; raw != nil && !ok {
97+
return fmt.Errorf("field service in GeneralInstrumentationPeerServiceMappingElem: required")
98+
}
99+
type Plain GeneralInstrumentationPeerServiceMappingElem
100+
var plain Plain
101+
if err := json.Unmarshal(b, &plain); err != nil {
102+
return err
103+
}
104+
*j = GeneralInstrumentationPeerServiceMappingElem(plain)
105+
return nil
106+
}
107+
108+
// UnmarshalJSON implements json.Unmarshaler.
109+
func (j *NameStringValuePair) UnmarshalJSON(b []byte) error {
110+
var raw map[string]interface{}
111+
if err := json.Unmarshal(b, &raw); err != nil {
112+
return err
113+
}
114+
if _, ok := raw["name"]; raw != nil && !ok {
115+
return fmt.Errorf("field name in NameStringValuePair: required")
116+
}
117+
if _, ok := raw["value"]; raw != nil && !ok {
118+
return fmt.Errorf("field value in NameStringValuePair: required")
119+
}
120+
type Plain NameStringValuePair
121+
var plain Plain
122+
if err := json.Unmarshal(b, &plain); err != nil {
123+
return err
124+
}
125+
*j = NameStringValuePair(plain)
126+
return nil
127+
}
128+
129+
var enumValuesOTLPMetricDefaultHistogramAggregation = []interface{}{
130+
"explicit_bucket_histogram",
131+
"base2_exponential_bucket_histogram",
132+
}
133+
134+
// UnmarshalJSON implements json.Unmarshaler.
135+
func (j *OTLPMetricDefaultHistogramAggregation) UnmarshalJSON(b []byte) error {
136+
var v string
137+
if err := json.Unmarshal(b, &v); err != nil {
138+
return err
139+
}
140+
var ok bool
141+
for _, expected := range enumValuesOTLPMetricDefaultHistogramAggregation {
142+
if reflect.DeepEqual(v, expected) {
143+
ok = true
144+
break
145+
}
146+
}
147+
if !ok {
148+
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValuesOTLPMetricDefaultHistogramAggregation, v)
149+
}
150+
*j = OTLPMetricDefaultHistogramAggregation(v)
151+
return nil
152+
}
153+
154+
// UnmarshalJSON implements json.Unmarshaler.
155+
func (j *OTLPMetric) UnmarshalJSON(b []byte) error {
156+
var raw map[string]interface{}
157+
if err := json.Unmarshal(b, &raw); err != nil {
158+
return err
159+
}
160+
if _, ok := raw["endpoint"]; raw != nil && !ok {
161+
return fmt.Errorf("field endpoint in OTLPMetric: required")
162+
}
163+
if _, ok := raw["protocol"]; raw != nil && !ok {
164+
return fmt.Errorf("field protocol in OTLPMetric: required")
165+
}
166+
type Plain OTLPMetric
167+
var plain Plain
168+
if err := json.Unmarshal(b, &plain); err != nil {
169+
return err
170+
}
171+
*j = OTLPMetric(plain)
172+
return nil
173+
}
174+
175+
// UnmarshalJSON implements json.Unmarshaler.
176+
func (j *OTLP) UnmarshalJSON(b []byte) error {
177+
var raw map[string]interface{}
178+
if err := json.Unmarshal(b, &raw); err != nil {
179+
return err
180+
}
181+
if _, ok := raw["endpoint"]; raw != nil && !ok {
182+
return fmt.Errorf("field endpoint in OTLP: required")
183+
}
184+
if _, ok := raw["protocol"]; raw != nil && !ok {
185+
return fmt.Errorf("field protocol in OTLP: required")
186+
}
187+
type Plain OTLP
188+
var plain Plain
189+
if err := json.Unmarshal(b, &plain); err != nil {
190+
return err
191+
}
192+
*j = OTLP(plain)
193+
return nil
194+
}
195+
196+
// UnmarshalJSON implements json.Unmarshaler.
197+
func (j *OpenTelemetryConfiguration) UnmarshalJSON(b []byte) error {
198+
var raw map[string]interface{}
199+
if err := json.Unmarshal(b, &raw); err != nil {
200+
return err
201+
}
202+
if _, ok := raw["file_format"]; raw != nil && !ok {
203+
return fmt.Errorf("field file_format in OpenTelemetryConfiguration: required")
204+
}
205+
type Plain OpenTelemetryConfiguration
206+
var plain Plain
207+
if err := json.Unmarshal(b, &plain); err != nil {
208+
return err
209+
}
210+
*j = OpenTelemetryConfiguration(plain)
211+
return nil
212+
}
213+
214+
// UnmarshalJSON implements json.Unmarshaler.
215+
func (j *PeriodicMetricReader) UnmarshalJSON(b []byte) error {
216+
var raw map[string]interface{}
217+
if err := json.Unmarshal(b, &raw); err != nil {
218+
return err
219+
}
220+
if _, ok := raw["exporter"]; raw != nil && !ok {
221+
return fmt.Errorf("field exporter in PeriodicMetricReader: required")
222+
}
223+
type Plain PeriodicMetricReader
224+
var plain Plain
225+
if err := json.Unmarshal(b, &plain); err != nil {
226+
return err
227+
}
228+
*j = PeriodicMetricReader(plain)
229+
return nil
230+
}
231+
232+
// UnmarshalJSON implements json.Unmarshaler.
233+
func (j *PullMetricReader) UnmarshalJSON(b []byte) error {
234+
var raw map[string]interface{}
235+
if err := json.Unmarshal(b, &raw); err != nil {
236+
return err
237+
}
238+
if _, ok := raw["exporter"]; raw != nil && !ok {
239+
return fmt.Errorf("field exporter in PullMetricReader: required")
240+
}
241+
type Plain PullMetricReader
242+
var plain Plain
243+
if err := json.Unmarshal(b, &plain); err != nil {
244+
return err
245+
}
246+
*j = PullMetricReader(plain)
247+
return nil
248+
}
249+
250+
// UnmarshalJSON implements json.Unmarshaler.
251+
func (j *SimpleLogRecordProcessor) UnmarshalJSON(b []byte) error {
252+
var raw map[string]interface{}
253+
if err := json.Unmarshal(b, &raw); err != nil {
254+
return err
255+
}
256+
if _, ok := raw["exporter"]; raw != nil && !ok {
257+
return fmt.Errorf("field exporter in SimpleLogRecordProcessor: required")
258+
}
259+
type Plain SimpleLogRecordProcessor
260+
var plain Plain
261+
if err := json.Unmarshal(b, &plain); err != nil {
262+
return err
263+
}
264+
*j = SimpleLogRecordProcessor(plain)
265+
return nil
266+
}
267+
268+
// UnmarshalJSON implements json.Unmarshaler.
269+
func (j *SimpleSpanProcessor) UnmarshalJSON(b []byte) error {
270+
var raw map[string]interface{}
271+
if err := json.Unmarshal(b, &raw); err != nil {
272+
return err
273+
}
274+
if _, ok := raw["exporter"]; raw != nil && !ok {
275+
return fmt.Errorf("field exporter in SimpleSpanProcessor: required")
276+
}
277+
type Plain SimpleSpanProcessor
278+
var plain Plain
279+
if err := json.Unmarshal(b, &plain); err != nil {
280+
return err
281+
}
282+
*j = SimpleSpanProcessor(plain)
283+
return nil
284+
}
285+
286+
var enumValuesViewSelectorInstrumentType = []interface{}{
287+
"counter",
288+
"histogram",
289+
"observable_counter",
290+
"observable_gauge",
291+
"observable_up_down_counter",
292+
"up_down_counter",
293+
}
294+
295+
// UnmarshalJSON implements json.Unmarshaler.
296+
func (j *ViewSelectorInstrumentType) UnmarshalJSON(b []byte) error {
297+
var v string
298+
if err := json.Unmarshal(b, &v); err != nil {
299+
return err
300+
}
301+
var ok bool
302+
for _, expected := range enumValuesViewSelectorInstrumentType {
303+
if reflect.DeepEqual(v, expected) {
304+
ok = true
305+
break
306+
}
307+
}
308+
if !ok {
309+
return fmt.Errorf("invalid value (expected one of %#v): %#v", enumValuesViewSelectorInstrumentType, v)
310+
}
311+
*j = ViewSelectorInstrumentType(v)
312+
return nil
313+
}
314+
315+
// UnmarshalJSON implements json.Unmarshaler.
316+
func (j *Zipkin) UnmarshalJSON(b []byte) error {
317+
var raw map[string]interface{}
318+
if err := json.Unmarshal(b, &raw); err != nil {
319+
return err
320+
}
321+
if _, ok := raw["endpoint"]; raw != nil && !ok {
322+
return fmt.Errorf("field endpoint in Zipkin: required")
323+
}
324+
type Plain Zipkin
325+
var plain Plain
326+
if err := json.Unmarshal(b, &plain); err != nil {
327+
return err
328+
}
329+
*j = Zipkin(plain)
330+
return nil
331+
}
332+
333+
// UnmarshalJSON implements json.Unmarshaler.
334+
func (j *AttributeNameValue) UnmarshalJSON(b []byte) error {
335+
var raw map[string]interface{}
336+
if err := json.Unmarshal(b, &raw); err != nil {
337+
return err
338+
}
339+
if _, ok := raw["name"]; raw != nil && !ok {
340+
return fmt.Errorf("field name in AttributeNameValue: required")
341+
}
342+
if _, ok := raw["value"]; raw != nil && !ok {
343+
return fmt.Errorf("field value in AttributeNameValue: required")
344+
}
345+
type Plain AttributeNameValue
346+
var plain Plain
347+
if err := json.Unmarshal(b, &plain); err != nil {
348+
return err
349+
}
350+
if plain.Type != nil && plain.Type.Value == "int" {
351+
val, ok := plain.Value.(float64)
352+
if ok {
353+
plain.Value = int(val)
354+
}
355+
}
356+
if plain.Type != nil && plain.Type.Value == "int_array" {
357+
m, ok := plain.Value.([]interface{})
358+
if ok {
359+
var vals []interface{}
360+
for _, v := range m {
361+
val, ok := v.(float64)
362+
if ok {
363+
vals = append(vals, int(val))
364+
} else {
365+
vals = append(vals, val)
366+
}
367+
}
368+
plain.Value = vals
369+
}
370+
}
371+
372+
*j = AttributeNameValue(plain)
373+
return nil
374+
}

config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,11 @@ var v03OpenTelemetryConfig = OpenTelemetryConfiguration{
291291
{Name: "service.name", Value: "unknown_service"},
292292
{Name: "string_key", Type: &AttributeNameValueType{Value: "string"}, Value: "value"},
293293
{Name: "bool_key", Type: &AttributeNameValueType{Value: "bool"}, Value: true},
294-
{Name: "int_key", Type: &AttributeNameValueType{Value: "int"}, Value: float64(1)},
294+
{Name: "int_key", Type: &AttributeNameValueType{Value: "int"}, Value: 1},
295295
{Name: "double_key", Type: &AttributeNameValueType{Value: "double"}, Value: 1.1},
296296
{Name: "string_array_key", Type: &AttributeNameValueType{Value: "string_array"}, Value: []interface{}{"value1", "value2"}},
297297
{Name: "bool_array_key", Type: &AttributeNameValueType{Value: "bool_array"}, Value: []interface{}{true, false}},
298-
{Name: "int_array_key", Type: &AttributeNameValueType{Value: "int_array"}, Value: []interface{}{float64(1), float64(2)}},
298+
{Name: "int_array_key", Type: &AttributeNameValueType{Value: "int_array"}, Value: []interface{}{1, 2}},
299299
{Name: "double_array_key", Type: &AttributeNameValueType{Value: "double_array"}, Value: []interface{}{1.1, 2.2}},
300300
},
301301
AttributesList: ptr("service.namespace=my-namespace,service.version=1.0.0"),

0 commit comments

Comments
 (0)