Skip to content

Commit f497165

Browse files
committed
add resource detector code
Signed-off-by: alex boten <[email protected]>
1 parent 92a9269 commit f497165

File tree

5 files changed

+317
-6
lines changed

5 files changed

+317
-6
lines changed

otelconf/config_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,3 +2354,28 @@ func TestUnmarshalPullMetricReader(t *testing.T) {
23542354
})
23552355
}
23562356
}
2357+
2358+
func TestUnmarshalResourceJson(t *testing.T) {
2359+
r := ResourceJson{}
2360+
2361+
err := yaml.Unmarshal([]byte("detection/development:\n detectors:\n - container:\n - host:\n - process:\n - service:"), &r)
2362+
require.NoError(t, err)
2363+
require.Equal(t, ResourceJson{
2364+
DetectionDevelopment: &ExperimentalResourceDetection{
2365+
Detectors: []ExperimentalResourceDetector{
2366+
{
2367+
Container: ExperimentalContainerResourceDetector{},
2368+
},
2369+
{
2370+
Host: ExperimentalHostResourceDetector{},
2371+
},
2372+
{
2373+
Process: ExperimentalProcessResourceDetector{},
2374+
},
2375+
{
2376+
Service: ExperimentalServiceResourceDetector{},
2377+
},
2378+
},
2379+
},
2380+
}, r)
2381+
}

otelconf/config_yaml.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ func hasYAMLMapKey(node *yaml.Node, key string) bool {
2626
return false
2727
}
2828

29+
// UnmarshalYAML implements yaml.Unmarshaler.
30+
func (j *ExperimentalResourceDetector) UnmarshalYAML(node *yaml.Node) error {
31+
type Plain ExperimentalResourceDetector
32+
var plain Plain
33+
if err := node.Decode(&plain); err != nil {
34+
return errors.Join(newErrUnmarshal(j), err)
35+
}
36+
// container can be nil, must check and set here
37+
if hasYAMLMapKey(node, "container") && plain.Container == nil {
38+
plain.Container = ExperimentalContainerResourceDetector{}
39+
}
40+
// host can be nil, must check and set here
41+
if hasYAMLMapKey(node, "host") && plain.Host == nil {
42+
plain.Host = ExperimentalHostResourceDetector{}
43+
}
44+
// process can be nil, must check and set here
45+
if hasYAMLMapKey(node, "process") && plain.Process == nil {
46+
plain.Process = ExperimentalProcessResourceDetector{}
47+
}
48+
// service can be nil, must check and set here
49+
if hasYAMLMapKey(node, "service") && plain.Service == nil {
50+
plain.Service = ExperimentalServiceResourceDetector{}
51+
}
52+
*j = ExperimentalResourceDetector(plain)
53+
return nil
54+
}
55+
2956
// UnmarshalYAML implements yaml.Unmarshaler.
3057
func (j *PushMetricExporter) UnmarshalYAML(node *yaml.Node) error {
3158
type Plain PushMetricExporter

otelconf/go.mod

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/prometheus/client_golang v1.23.2
77
github.com/prometheus/otlptranslator v1.0.0
88
github.com/stretchr/testify v1.11.1
9+
go.opentelemetry.io/contrib/detectors/autodetect v0.10.0
910
go.opentelemetry.io/otel v1.38.0
1011
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.14.0
1112
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.14.0
@@ -30,27 +31,80 @@ require (
3031
)
3132

3233
require (
34+
cloud.google.com/go/compute/metadata v0.9.0 // indirect
35+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.30.0 // indirect
36+
github.com/aws/aws-sdk-go-v2 v1.38.3 // indirect
37+
github.com/aws/aws-sdk-go-v2/config v1.31.6 // indirect
38+
github.com/aws/aws-sdk-go-v2/credentials v1.18.10 // indirect
39+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.6 // indirect
40+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.6 // indirect
41+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.6 // indirect
42+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect
43+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.1 // indirect
44+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.6 // indirect
45+
github.com/aws/aws-sdk-go-v2/service/sso v1.29.1 // indirect
46+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.34.2 // indirect
47+
github.com/aws/aws-sdk-go-v2/service/sts v1.38.2 // indirect
48+
github.com/aws/smithy-go v1.23.0 // indirect
3349
github.com/beorn7/perks v1.0.1 // indirect
50+
github.com/brunoscheufler/aws-ecs-metadata-go v0.0.0-20221221133751-67e37ae746cd // indirect
3451
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
3552
github.com/cespare/xxhash/v2 v2.3.0 // indirect
36-
github.com/davecgh/go-spew v1.1.1 // indirect
53+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
54+
github.com/emicklei/go-restful/v3 v3.13.0 // indirect
55+
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
3756
github.com/go-logr/logr v1.4.3 // indirect
3857
github.com/go-logr/stdr v1.2.2 // indirect
58+
github.com/go-openapi/jsonpointer v0.21.2 // indirect
59+
github.com/go-openapi/jsonreference v0.21.0 // indirect
60+
github.com/go-openapi/swag v0.23.1 // indirect
61+
github.com/gogo/protobuf v1.3.2 // indirect
62+
github.com/golang/protobuf v1.5.4 // indirect
63+
github.com/google/gnostic-models v0.7.0 // indirect
64+
github.com/google/go-cmp v0.7.0 // indirect
65+
github.com/google/gofuzz v1.2.0 // indirect
3966
github.com/google/uuid v1.6.0 // indirect
4067
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 // indirect
68+
github.com/josharian/intern v1.0.0 // indirect
69+
github.com/json-iterator/go v1.1.12 // indirect
70+
github.com/mailru/easyjson v0.9.0 // indirect
71+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
72+
github.com/modern-go/reflect2 v1.0.2 // indirect
4173
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
42-
github.com/pmezard/go-difflib v1.0.0 // indirect
74+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
4375
github.com/prometheus/client_model v0.6.2 // indirect
4476
github.com/prometheus/common v0.67.4 // indirect
4577
github.com/prometheus/procfs v0.19.2 // indirect
78+
github.com/x448/float16 v0.8.4 // indirect
4679
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
80+
go.opentelemetry.io/contrib/detectors/aws/ec2/v2 v2.0.0 // indirect
81+
go.opentelemetry.io/contrib/detectors/aws/ecs v1.38.0 // indirect
82+
go.opentelemetry.io/contrib/detectors/aws/eks v1.38.0 // indirect
83+
go.opentelemetry.io/contrib/detectors/aws/lambda v0.63.0 // indirect
84+
go.opentelemetry.io/contrib/detectors/azure/azurevm v0.10.0 // indirect
85+
go.opentelemetry.io/contrib/detectors/gcp v1.38.0 // indirect
4786
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.38.0 // indirect
4887
go.yaml.in/yaml/v2 v2.4.3 // indirect
4988
golang.org/x/net v0.47.0 // indirect
89+
golang.org/x/oauth2 v0.32.0 // indirect
5090
golang.org/x/sys v0.38.0 // indirect
91+
golang.org/x/term v0.37.0 // indirect
5192
golang.org/x/text v0.31.0 // indirect
93+
golang.org/x/time v0.12.0 // indirect
5294
google.golang.org/genproto/googleapis/api v0.0.0-20251124214823-79d6a2a48846 // indirect
5395
google.golang.org/genproto/googleapis/rpc v0.0.0-20251124214823-79d6a2a48846 // indirect
5496
google.golang.org/protobuf v1.36.10 // indirect
97+
gopkg.in/evanphx/json-patch.v4 v4.13.0 // indirect
98+
gopkg.in/inf.v0 v0.9.1 // indirect
5599
gopkg.in/yaml.v3 v3.0.1 // indirect
100+
k8s.io/api v0.32.4 // indirect
101+
k8s.io/apimachinery v0.32.4 // indirect
102+
k8s.io/client-go v0.32.4 // indirect
103+
k8s.io/klog/v2 v2.130.1 // indirect
104+
k8s.io/kube-openapi v0.0.0-20250701173324-9bd5c66d9911 // indirect
105+
k8s.io/utils v0.0.0-20250820121507-0af2bda4dd1d // indirect
106+
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
107+
sigs.k8s.io/randfill v1.0.0 // indirect
108+
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
109+
sigs.k8s.io/yaml v1.6.0 // indirect
56110
)

0 commit comments

Comments
 (0)