Skip to content

Commit

Permalink
[chore] [exporter/datadogexporter] Use opentelemetry-mapping-go paylo…
Browse files Browse the repository at this point in the history
…ad definitions for host metadata and gohai (open-telemetry#24267)

**Description:** 

Host metadata and gohai payloads were moved to a new `pkg/inframetadata`
Go module. This PR is just an internal refactor using the new payloads.
  • Loading branch information
mx-psi authored Jul 14, 2023
1 parent e51f5c1 commit 2b2c967
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 182 deletions.
1 change: 1 addition & 0 deletions cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ require (
github.com/DataDog/datadog-go/v5 v5.1.1 // indirect
github.com/DataDog/go-tuf v0.3.0--fix-localmeta-fork // indirect
github.com/DataDog/gohai v0.0.0-20220718130825-1776f9beb9cc // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata v0.5.1 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/logs v0.5.1 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/quantile v0.5.1 // indirect
github.com/DataDog/sketches-go v1.4.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/configschema/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ require (
github.com/DataDog/datadog-go/v5 v5.1.1 // indirect
github.com/DataDog/go-tuf v0.3.0--fix-localmeta-fork // indirect
github.com/DataDog/gohai v0.0.0-20220718130825-1776f9beb9cc // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata v0.5.1 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.5.1 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/logs v0.5.1 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics v0.5.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/otelcontribcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions exporter/datadogexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"testing"

"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata/payload"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
Expand All @@ -19,7 +20,6 @@ import (
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/pdata/ptrace"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/testutil"
)
Expand Down Expand Up @@ -617,7 +617,7 @@ func TestOnlyMetadata(t *testing.T) {
require.NoError(t, err)

body := <-server.MetadataChan
var recvMetadata hostmetadata.HostMetadata
var recvMetadata payload.HostMetadata
err = json.Unmarshal(body, &recvMetadata)
require.NoError(t, err)
assert.Equal(t, recvMetadata.InternalHostname, "custom-hostname")
Expand Down
1 change: 1 addition & 0 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/DataDog/datadog-agent/pkg/trace v0.47.0-rc.2
github.com/DataDog/datadog-api-client-go/v2 v2.14.0
github.com/DataDog/gohai v0.0.0-20220718130825-1776f9beb9cc
github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata v0.5.1
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.5.1
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/logs v0.5.1
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics v0.5.1
Expand Down
2 changes: 2 additions & 0 deletions exporter/datadogexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ import (
"github.com/DataDog/gohai/memory"
"github.com/DataDog/gohai/network"
"github.com/DataDog/gohai/platform"
"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata/gohai"
"go.uber.org/zap"
)

// NewPayload builds a payload of every metadata collected with gohai except processes metadata.
// Parts of this are based on datadog-agent code
// https://github.com/DataDog/datadog-agent/blob/94a28d9cee3f1c886b3866e8208be5b2a8c2c217/pkg/metadata/internal/gohai/gohai.go#L27-L32
func NewPayload(logger *zap.Logger) Payload {
return Payload{
Gohai: gohaiMarshaler{
gohai: newGohai(logger),
},
}
func NewPayload(logger *zap.Logger) gohai.Payload {
payload := gohai.NewEmpty()
payload.Gohai.Gohai = newGohai(logger)
return payload
}

func newGohai(logger *zap.Logger) *gohai {
res := new(gohai)
func newGohai(logger *zap.Logger) *gohai.Gohai {
res := new(gohai.Gohai)

if p, err := new(cpu.Cpu).Collect(); err != nil {
logger.Info("Failed to retrieve cpu metadata", zap.Error(err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
func TestGetPayload(t *testing.T) {
logger := zap.NewNop()
gohai := NewPayload(logger)
assert.NotNil(t, gohai.Gohai.gohai.CPU)
assert.NotNil(t, gohai.Gohai.gohai.FileSystem)
assert.NotNil(t, gohai.Gohai.gohai.Memory)
assert.NotNil(t, gohai.Gohai.gohai.Network)
assert.NotNil(t, gohai.Gohai.gohai.Platform)
assert.NotNil(t, gohai.Gohai.Gohai.CPU)
assert.NotNil(t, gohai.Gohai.Gohai.FileSystem)
assert.NotNil(t, gohai.Gohai.Gohai.Memory)
assert.NotNil(t, gohai.Gohai.Gohai.Network)
assert.NotNil(t, gohai.Gohai.Gohai.Platform)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ package gohai // import "github.com/open-telemetry/opentelemetry-collector-contr

import (
"github.com/DataDog/gohai/processes"
"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata/gohai"
"go.uber.org/zap"
)

// NewProcessesPayload builds a payload of processes metadata collected from gohai.
func NewProcessesPayload(hostname string, logger *zap.Logger) *ProcessesPayload {
func NewProcessesPayload(hostname string, logger *zap.Logger) *gohai.ProcessesPayload {
// Get processes metadata from gohai
proc, err := new(processes.Processes).Collect()
if err != nil {
Expand All @@ -22,7 +23,7 @@ func NewProcessesPayload(hostname string, logger *zap.Logger) *ProcessesPayload
processesPayload := map[string]interface{}{
"snaps": []interface{}{proc},
}
return &ProcessesPayload{
return &gohai.ProcessesPayload{
Processes: processesPayload,
Meta: map[string]string{
"host": hostname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

package gohai // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/internal/gohai"

import "go.uber.org/zap"
import (
"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata/gohai"
"go.uber.org/zap"
)

// GetProcessesPayload currently just a stub.
func NewProcessesPayload(_ string, _ *zap.Logger) *ProcessesPayload {
func NewProcessesPayload(_ string, _ *zap.Logger) *gohai.ProcessesPayload {
// unimplemented for misc platforms.
return &ProcessesPayload{}
return &gohai.ProcessesPayload{}
}

This file was deleted.

75 changes: 7 additions & 68 deletions exporter/datadogexporter/internal/hostmetadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"time"

"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata/payload"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes"
ec2Attributes "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/ec2"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/gcp"
Expand All @@ -29,72 +30,10 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/scrub"
)

// HostMetadata includes metadata about the host tags,
// host aliases and identifies the host as an OpenTelemetry host
type HostMetadata struct {
// Meta includes metadata about the host.
Meta *Meta `json:"meta"`

// InternalHostname is the canonical hostname
InternalHostname string `json:"internalHostname"`

// Version is the OpenTelemetry Collector version.
// This is used for correctly identifying the Collector in the backend,
// and for telemetry purposes.
Version string `json:"otel_version"`

// Flavor is always set to "opentelemetry-collector".
// It is used for telemetry purposes in the backend.
Flavor string `json:"agent-flavor"`

// Tags includes the host tags
Tags *HostTags `json:"host-tags"`

// Payload contains inventory of system information provided by gohai
// this is embedded because of special serialization requirements
// the field `gohai` is JSON-formatted string
gohai.Payload

// Processes contains the process payload devired by gohai
// Because of legacy reasons this is called resources in datadog intake
Processes *gohai.ProcessesPayload `json:"resources"`
}

// HostTags are the host tags.
// Currently only system (configuration) tags are considered.
type HostTags struct {
// OTel are host tags set in the configuration
OTel []string `json:"otel,omitempty"`

// GCP are Google Cloud Platform tags
GCP []string `json:"google cloud platform,omitempty"`
}

// Meta includes metadata about the host aliases
type Meta struct {
// InstanceID is the EC2 instance id the Collector is running on, if available
InstanceID string `json:"instance-id,omitempty"`

// EC2Hostname is the hostname from the EC2 metadata API
EC2Hostname string `json:"ec2-hostname,omitempty"`

// Hostname is the canonical hostname
Hostname string `json:"hostname"`

// SocketHostname is the OS hostname
SocketHostname string `json:"socket-hostname,omitempty"`

// SocketFqdn is the FQDN hostname
SocketFqdn string `json:"socket-fqdn,omitempty"`

// HostAliases are other available host names
HostAliases []string `json:"host_aliases,omitempty"`
}

// metadataFromAttributes gets metadata info from attributes following
// OpenTelemetry semantic conventions
func metadataFromAttributes(attrs pcommon.Map) *HostMetadata {
hm := &HostMetadata{Meta: &Meta{}, Tags: &HostTags{}}
func metadataFromAttributes(attrs pcommon.Map) *payload.HostMetadata {
hm := &payload.HostMetadata{Meta: &payload.Meta{}, Tags: &payload.HostTags{}}

if src, ok := attributes.SourceFromAttrs(attrs); ok && src.Kind == source.HostnameKind {
hm.InternalHostname = src.Identifier
Expand All @@ -118,7 +57,7 @@ func metadataFromAttributes(attrs pcommon.Map) *HostMetadata {
return hm
}

func fillHostMetadata(params exporter.CreateSettings, pcfg PusherConfig, p source.Provider, hm *HostMetadata) {
func fillHostMetadata(params exporter.CreateSettings, pcfg PusherConfig, p source.Provider, hm *payload.HostMetadata) {
// Could not get hostname from attributes
if hm.InternalHostname == "" {
if src, err := p.Source(context.TODO()); err == nil && src.Kind == source.HostnameKind {
Expand Down Expand Up @@ -149,7 +88,7 @@ func fillHostMetadata(params exporter.CreateSettings, pcfg PusherConfig, p sourc
}
}

func pushMetadata(pcfg PusherConfig, params exporter.CreateSettings, metadata *HostMetadata) error {
func pushMetadata(pcfg PusherConfig, params exporter.CreateSettings, metadata *payload.HostMetadata) error {
if metadata.Meta.Hostname == "" {
// if the hostname is empty, don't send metadata; we don't need it.
params.Logger.Debug("Skipping host metadata since the hostname is empty")
Expand Down Expand Up @@ -181,7 +120,7 @@ func pushMetadata(pcfg PusherConfig, params exporter.CreateSettings, metadata *H
return nil
}

func pushMetadataWithRetry(retrier *clientutil.Retrier, params exporter.CreateSettings, pcfg PusherConfig, hostMetadata *HostMetadata) {
func pushMetadataWithRetry(retrier *clientutil.Retrier, params exporter.CreateSettings, pcfg PusherConfig, hostMetadata *payload.HostMetadata) {
params.Logger.Debug("Sending host metadata payload", zap.Any("payload", hostMetadata))

_, err := retrier.DoWithRetries(context.Background(), func(context.Context) error {
Expand Down Expand Up @@ -211,7 +150,7 @@ func Pusher(ctx context.Context, params exporter.CreateSettings, pcfg PusherConf
// All fields that are being filled in by our exporter
// do not change over time. If this ever changes `hostMetadata`
// *must* be deep copied before calling `fillHostMetadata`.
hostMetadata := &HostMetadata{Meta: &Meta{}, Tags: &HostTags{}}
hostMetadata := &payload.HostMetadata{Meta: &payload.Meta{}, Tags: &payload.HostTags{}}
if pcfg.UseResourceMetadata {
hostMetadata = metadataFromAttributes(attrs)
}
Expand Down
Loading

0 comments on commit 2b2c967

Please sign in to comment.