Skip to content

Commit ef90add

Browse files
committed
otelconf: add support for parsing resource detectors
Fixes #7252 Signed-off-by: alex boten <[email protected]>
1 parent ad8ca18 commit ef90add

File tree

4 files changed

+72
-14
lines changed

4 files changed

+72
-14
lines changed

otelconf/config_json.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,56 @@ func (j *TraceContextPropagator) UnmarshalJSON(b []byte) error {
122122
return nil
123123
}
124124

125+
// UnmarshalJSON implements json.Unmarshaler.
126+
func (j *ExperimentalResourceDetector) UnmarshalJSON(b []byte) error {
127+
// Use a shadow struct with a RawMessage field to detect key presence.
128+
type Plain ExperimentalResourceDetector
129+
type shadow struct {
130+
Plain
131+
Container json.RawMessage `json:"container"`
132+
Host json.RawMessage `json:"host"`
133+
Process json.RawMessage `json:"process"`
134+
Service json.RawMessage `json:"service"`
135+
}
136+
var sh shadow
137+
if err := json.Unmarshal(b, &sh); err != nil {
138+
return errors.Join(newErrUnmarshal(j), err)
139+
}
140+
141+
if sh.Container != nil {
142+
var c ExperimentalContainerResourceDetector
143+
if err := json.Unmarshal(sh.Container, &c); err != nil {
144+
return err
145+
}
146+
sh.Plain.Container = c
147+
}
148+
149+
if sh.Host != nil {
150+
var c ExperimentalHostResourceDetector
151+
if err := json.Unmarshal(sh.Host, &c); err != nil {
152+
return err
153+
}
154+
sh.Plain.Host = c
155+
}
156+
157+
if sh.Process != nil {
158+
var c ExperimentalProcessResourceDetector
159+
if err := json.Unmarshal(sh.Process, &c); err != nil {
160+
return err
161+
}
162+
sh.Plain.Process = c
163+
}
164+
165+
if sh.Service != nil {
166+
var c ExperimentalServiceResourceDetector
167+
if err := json.Unmarshal(sh.Service, &c); err != nil {
168+
return err
169+
}
170+
sh.Plain.Service = c
171+
}
172+
return nil
173+
}
174+
125175
// UnmarshalJSON implements json.Unmarshaler.
126176
func (j *PushMetricExporter) UnmarshalJSON(b []byte) error {
127177
// Use a shadow struct with a RawMessage field to detect key presence.

otelconf/config_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,12 @@ var v10OpenTelemetryConfig = OpenTelemetryConfiguration{
828828
Excluded: []string{"process.command_args"},
829829
Included: []string{"process.*"},
830830
},
831-
// TODO: implement resource detectors
832-
// Detectors: []ExperimentalResourceDetector{}
833-
// },
831+
Detectors: []ExperimentalResourceDetector{
832+
{Container: ExperimentalContainerResourceDetector{}},
833+
{Host: ExperimentalHostResourceDetector{}},
834+
{Process: ExperimentalProcessResourceDetector{}},
835+
{Service: ExperimentalServiceResourceDetector{}},
836+
},
834837
},
835838
SchemaUrl: ptr("https://opentelemetry.io/schemas/1.16.0"),
836839
},

otelconf/testdata/v1.0.0.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,13 @@
515515
"excluded": [
516516
"process.command_args"
517517
]
518-
}
518+
},
519+
"detectors": [
520+
{"container": null},
521+
{"host": null},
522+
{"process": null},
523+
{"service": null}
524+
]
519525
},
520526
"schema_url": "https://opentelemetry.io/schemas/1.16.0"
521527
},

otelconf/testdata/v1.0.0.yaml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -841,16 +841,15 @@ resource:
841841
# Configure resource detectors.
842842
# Resource detector names are dependent on the SDK language ecosystem. Please consult documentation for each respective language.
843843
# If omitted or null, no resource detectors are enabled.
844-
# TODO: implement resource detectors
845-
# detectors:
846-
# - # Enable the container resource detector, which populates container.* attributes.
847-
# container:
848-
# - # Enable the host resource detector, which populates host.* and os.* attributes.
849-
# host:
850-
# - # Enable the process resource detector, which populates process.* attributes.
851-
# process:
852-
# - # Enable the service detector, which populates service.name based on the OTEL_SERVICE_NAME environment variable and service.instance.id.
853-
# service:
844+
detectors:
845+
- # Enable the container resource detector, which populates container.* attributes.
846+
container:
847+
- # Enable the host resource detector, which populates host.* and os.* attributes.
848+
host:
849+
- # Enable the process resource detector, which populates process.* attributes.
850+
process:
851+
- # Enable the service detector, which populates service.name based on the OTEL_SERVICE_NAME environment variable and service.instance.id.
852+
service:
854853
# Configure resource schema URL.
855854
# If omitted or null, no schema URL is used.
856855
schema_url: https://opentelemetry.io/schemas/1.16.0

0 commit comments

Comments
 (0)