Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporter/datadogexporter: Graduate exporter.datadog.hostname.preview to stable. #20286

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/datadog-exporter-span-links-support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: datadogexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds support for OpenTelemetry span links when converting OTLP spans (span links are added as metadata to the converted span).
mackjmr marked this conversation as resolved.
Show resolved Hide resolved

# One or more tracking issues related to the change
issues: [20286]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: datadogexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Graduate exporter.datadog.hostname.preview feature gate to Stable.

# One or more tracking issues related to the change
issues: [20286]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
7 changes: 1 addition & 6 deletions exporter/datadogexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ func defaulttimeoutSettings() exporterhelper.TimeoutSettings {

// createDefaultConfig creates the default exporter configuration
func (f *factory) createDefaultConfig() component.Config {
hostnameSource := HostnameSourceFirstResource
if hostmetadata.HostnamePreviewFeatureGate.IsEnabled() {
hostnameSource = HostnameSourceConfigOrSystem
}

return &Config{
TimeoutSettings: defaulttimeoutSettings(),
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
Expand Down Expand Up @@ -169,7 +164,7 @@ func (f *factory) createDefaultConfig() component.Config {

HostMetadata: HostMetadataConfig{
Enabled: true,
HostnameSource: hostnameSource,
HostnameSource: HostnameSourceConfigOrSystem,
},
}
}
Expand Down
20 changes: 10 additions & 10 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.19

require (
github.com/DataDog/agent-payload/v5 v5.0.76
github.com/DataDog/datadog-agent/pkg/trace v0.44.0-rc.3
github.com/DataDog/datadog-agent/pkg/trace v0.44.0-rc.4.0.20230322085447-8b27e87df01c
github.com/DataDog/datadog-api-client-go/v2 v2.10.0
github.com/DataDog/gohai v0.0.0-20220718130825-1776f9beb9cc
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.1.2
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.1.3
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics v0.1.2
github.com/DataDog/opentelemetry-mapping-go/pkg/quantile v0.1.3
github.com/DataDog/sketches-go v1.4.1
Expand Down Expand Up @@ -46,15 +46,15 @@ require (
)

require (
cloud.google.com/go/compute v1.15.1 // indirect
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.44.0-rc.3 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.44.0-rc.3 // indirect
github.com/DataDog/datadog-agent/pkg/util/cgroups v0.44.0-rc.3 // indirect
github.com/DataDog/datadog-agent/pkg/util/log v0.44.0-rc.3 // indirect
github.com/DataDog/datadog-agent/pkg/util/pointer v0.44.0-rc.3 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.44.0-rc.3 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.44.0-rc.4 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.44.0-rc.4 // indirect
github.com/DataDog/datadog-agent/pkg/util/cgroups v0.44.0-rc.4 // indirect
github.com/DataDog/datadog-agent/pkg/util/log v0.44.0-rc.4 // indirect
github.com/DataDog/datadog-agent/pkg/util/pointer v0.44.0-rc.4 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.44.0-rc.4 // indirect
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/zstd v1.5.2 // indirect
Expand Down Expand Up @@ -178,7 +178,7 @@ require (
golang.org/x/time v0.3.0 // indirect
gonum.org/v1/gonum v0.12.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/grpc v1.53.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
40 changes: 20 additions & 20 deletions exporter/datadogexporter/go.sum

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

29 changes: 0 additions & 29 deletions exporter/datadogexporter/internal/hostmetadata/cache.go

This file was deleted.

85 changes: 4 additions & 81 deletions exporter/datadogexporter/internal/hostmetadata/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
package hostmetadata // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata"

import (
"context"
"fmt"

"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
gocache "github.com/patrickmn/go-cache"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/featuregate"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/internal/azure"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/internal/ec2"
Expand All @@ -31,17 +28,17 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/internal/k8s"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/internal/system"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/provider"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/valid"
)

var HostnamePreviewFeatureGate = featuregate.GlobalRegistry().MustRegister(
var _ = featuregate.GlobalRegistry().MustRegister(
"exporter.datadog.hostname.preview",
featuregate.StageBeta,
featuregate.StageStable,
featuregate.WithRegisterDescription("Use the 'preview' hostname resolution rules, which are consistent with Datadog cloud integration hostname resolution rules, and set 'host_metadata::hostname_source' to 'config_or_system' by default."),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/10424"),
featuregate.WithRegisterRemovalVersion("0.75.0"),
)

func buildPreviewProvider(set component.TelemetrySettings, configHostname string) (source.Provider, error) {
func GetSourceProvider(set component.TelemetrySettings, configHostname string) (source.Provider, error) {
ecs, err := ecs.NewProvider(set)
if err != nil {
return nil, fmt.Errorf("failed to build ECS Fargate provider: %w", err)
Expand Down Expand Up @@ -91,77 +88,3 @@ func buildPreviewProvider(set component.TelemetrySettings, configHostname string

return provider.Once(chain), nil
}

func buildCurrentProvider(set component.TelemetrySettings, configHostname string) (source.Provider, error) {
ec2Provider, err := ec2.NewProvider(set.Logger)
if err != nil {
return nil, fmt.Errorf("failed to build EC2 provider: %w", err)
}

return &currentProvider{
logger: set.Logger,
configHostname: configHostname,
systemProvider: system.NewProvider(set.Logger),
ec2Provider: ec2Provider,
}, nil
}

func GetSourceProvider(set component.TelemetrySettings, configHostname string) (source.Provider, error) {
if HostnamePreviewFeatureGate.IsEnabled() {
return buildPreviewProvider(set, configHostname)
}

return buildCurrentProvider(set, configHostname)
}

var _ source.Provider = (*currentProvider)(nil)

type currentProvider struct {
logger *zap.Logger
configHostname string
systemProvider *system.Provider
ec2Provider *ec2.Provider
}

// Hostname gets the hostname according to configuration.
// It checks in the following order
// 1. Configuration
// 2. Cache
// 3. EC2 instance metadata
// 4. System
func (c *currentProvider) hostname(ctx context.Context) string {
if c.configHostname != "" {
return c.configHostname
}

if cacheVal, ok := hostnameCache.Get(cacheKeyHostname); ok {
return cacheVal.(string)
}

ec2Info := c.ec2Provider.HostInfo()
hostname := ec2Info.GetHostname(c.logger)

if hostname == "" {
// Get system hostname
var err error
src, err := c.systemProvider.Source(ctx)
if err != nil {
c.logger.Debug("system provider is unavailable", zap.Error(err))
} else {
hostname = src.Identifier
}
}

if err := valid.Hostname(hostname); err != nil {
// If invalid log but continue
c.logger.Error("Detected hostname is not valid", zap.Error(err))
}

c.logger.Debug("Canonical hostname automatically set", zap.String("hostname", hostname))
hostnameCache.Set(cacheKeyHostname, hostname, gocache.NoExpiration)
return hostname
}

func (c *currentProvider) Source(ctx context.Context) (source.Source, error) {
return source.Source{Kind: source.HostnameKind, Identifier: c.hostname(ctx)}, nil
}
26 changes: 1 addition & 25 deletions exporter/datadogexporter/internal/hostmetadata/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package hostmetadata

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,32 +24,9 @@ import (
)

func TestHost(t *testing.T) {
// Start with a fresh cache, the following test would fail
// if the cache key is already set.
hostnameCache.Delete(cacheKeyHostname)

p, err := buildCurrentProvider(componenttest.NewNopTelemetrySettings(), "test-host")
p, err := GetSourceProvider(componenttest.NewNopTelemetrySettings(), "test-host")
require.NoError(t, err)
src, err := p.Source(context.Background())
require.NoError(t, err)
assert.Equal(t, src.Identifier, "test-host")

// config.Config.Hostname does not get stored in the cache
p, err = buildCurrentProvider(componenttest.NewNopTelemetrySettings(), "test-host-2")
require.NoError(t, err)
src, err = p.Source(context.Background())
require.NoError(t, err)
assert.Equal(t, src.Identifier, "test-host-2")

// Disable EC2 Metadata service to prevent fetching hostname from there,
// in case the test is running on an EC2 instance
t.Setenv("AWS_EC2_METADATA_DISABLED", "true")

p, err = buildCurrentProvider(componenttest.NewNopTelemetrySettings(), "")
require.NoError(t, err)
src, err = p.Source(context.Background())
require.NoError(t, err)
osHostname, err := os.Hostname()
require.NoError(t, err)
assert.Contains(t, src.Identifier, osHostname)
}
Loading