Skip to content

Commit acba2c3

Browse files
Add resource attributes to Datadog OtelCollector payload
Co-authored-by: jackgopack4 <[email protected]>
1 parent ded0ad4 commit acba2c3

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

extension/datadogextension/extension.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package datadogextension // import "github.com/open-telemetry/opentelemetry-coll
55

66
import (
77
"context"
8+
"sort"
89
"sync"
910
"time"
1011

@@ -16,6 +17,7 @@ import (
1617
"go.opentelemetry.io/collector/confmap"
1718
"go.opentelemetry.io/collector/extension"
1819
"go.opentelemetry.io/collector/extension/extensioncapabilities"
20+
"go.opentelemetry.io/collector/pdata/pcommon"
1921
"go.opentelemetry.io/collector/service"
2022
"go.opentelemetry.io/collector/service/hostcapabilities"
2123
"go.uber.org/zap"
@@ -49,11 +51,12 @@ type configs struct {
4951
}
5052

5153
type info struct {
52-
host source.Source
53-
hostnameSource string
54-
uuid string
55-
build component.BuildInfo
56-
modules service.ModuleInfos
54+
host source.Source
55+
hostnameSource string
56+
uuid string
57+
build component.BuildInfo
58+
modules service.ModuleInfos
59+
resourceAttributes []string
5760
}
5861

5962
type payloadSender struct {
@@ -120,6 +123,9 @@ func (e *datadogExtension) NotifyConfig(_ context.Context, conf *confmap.Conf) e
120123
buildInfo,
121124
)
122125

126+
// Populate resource attributes collected from TelemetrySettings.Resource
127+
otelCollectorPayload.CollectorResourceAttributes = e.info.resourceAttributes
128+
123129
// Populate the full list of components available in the collector build
124130
moduleInfoJSON, err := componentchecker.PopulateFullComponentsJSON(e.info.modules, e.configs.collector)
125131
if err != nil {
@@ -313,6 +319,23 @@ func newExtension(
313319
logComponent := agentcomponents.NewLogComponent(set.TelemetrySettings)
314320
serializer := agentcomponents.NewSerializerComponent(configComponent, logComponent, host.Identifier)
315321

322+
// Collect resource attributes from TelemetrySettings.Resource
323+
// Format: ["key:value", ...]
324+
resourceMap := make(map[string]string, set.Resource.Attributes().Len())
325+
set.Resource.Attributes().Range(func(k string, v pcommon.Value) bool {
326+
resourceMap[k] = v.Str()
327+
return true
328+
})
329+
keys := make([]string, 0, len(resourceMap))
330+
for k := range resourceMap {
331+
keys = append(keys, k)
332+
}
333+
sort.Strings(keys)
334+
resourceList := make([]string, 0, len(keys))
335+
for _, k := range keys {
336+
resourceList = append(resourceList, k+":"+resourceMap[k])
337+
}
338+
316339
// configure payloadSender struct
317340
ctxWithCancel, cancel := context.WithCancel(ctx)
318341
ticker := time.NewTicker(payloadSendingInterval)
@@ -323,11 +346,12 @@ func newExtension(
323346
logger: set.Logger,
324347
serializer: serializer,
325348
info: &info{
326-
host: host,
327-
hostnameSource: hostnameSource,
328-
uuid: uuidProvider.NewString(),
329-
build: set.BuildInfo,
330-
modules: service.ModuleInfos{}, // moduleInfos will be populated in Start()
349+
host: host,
350+
hostnameSource: hostnameSource,
351+
uuid: uuidProvider.NewString(),
352+
build: set.BuildInfo,
353+
modules: service.ModuleInfos{}, // moduleInfos will be populated in Start()
354+
resourceAttributes: resourceList,
331355
},
332356
payloadSender: &payloadSender{
333357
ctx: ctxWithCancel,

extension/datadogextension/internal/payload/payload.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,19 @@ type CustomBuildInfo struct {
2323
}
2424

2525
type OtelCollector struct {
26-
HostKey string `json:"host_key"`
27-
Hostname string `json:"hostname"`
28-
HostnameSource string `json:"hostname_source"`
29-
CollectorID string `json:"collector_id"`
30-
CollectorVersion string `json:"collector_version"`
31-
ConfigSite string `json:"config_site"`
32-
APIKeyUUID string `json:"api_key_uuid"`
33-
FullComponents []CollectorModule `json:"full_components"`
34-
ActiveComponents []ServiceComponent `json:"active_components"`
35-
BuildInfo CustomBuildInfo `json:"build_info"`
36-
FullConfiguration string `json:"full_configuration"` // JSON passed as string
37-
HealthStatus string `json:"health_status"` // JSON passed as string
26+
HostKey string `json:"host_key"`
27+
Hostname string `json:"hostname"`
28+
HostnameSource string `json:"hostname_source"`
29+
CollectorID string `json:"collector_id"`
30+
CollectorVersion string `json:"collector_version"`
31+
ConfigSite string `json:"config_site"`
32+
APIKeyUUID string `json:"api_key_uuid"`
33+
FullComponents []CollectorModule `json:"full_components"`
34+
ActiveComponents []ServiceComponent `json:"active_components"`
35+
BuildInfo CustomBuildInfo `json:"build_info"`
36+
FullConfiguration string `json:"full_configuration"` // JSON passed as string
37+
HealthStatus string `json:"health_status"` // JSON passed as string
38+
CollectorResourceAttributes []string `json:"collector_resource_attributes"`
3839
}
3940

4041
type CollectorModule struct {

0 commit comments

Comments
 (0)