Skip to content

Commit

Permalink
[exporter/datadog] Remove reference to Config on GetHost
Browse files Browse the repository at this point in the history
  • Loading branch information
mx-psi committed Mar 10, 2022
1 parent 32e14e6 commit 695fdff
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 15 deletions.
7 changes: 3 additions & 4 deletions exporter/datadogexporter/internal/metadata/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package metadata // import "github.com/open-telemetry/opentelemetry-collector-co
import (
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/config"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/ec2"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/system"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/valid"
Expand All @@ -30,9 +29,9 @@ import (
// 2. Cache
// 3. EC2 instance metadata
// 4. System
func GetHost(logger *zap.Logger, cfg *config.Config) string {
if cfg.Hostname != "" {
return cfg.Hostname
func GetHost(logger *zap.Logger, configHostname string) string {
if configHostname != "" {
return configHostname
}

if cacheVal, ok := cache.Cache.Get(cache.CanonicalHostnameKey); ok {
Expand Down
11 changes: 3 additions & 8 deletions exporter/datadogexporter/internal/metadata/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/config"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/utils/cache"
)

Expand All @@ -34,15 +33,11 @@ func TestHost(t *testing.T) {
// if the cache key is already set.
cache.Cache.Delete(cache.CanonicalHostnameKey)

host := GetHost(logger, &config.Config{
TagsConfig: config.TagsConfig{Hostname: "test-host"},
})
host := GetHost(logger, "test-host")
assert.Equal(t, host, "test-host")

// config.Config.Hostname does not get stored in the cache
host = GetHost(logger, &config.Config{
TagsConfig: config.TagsConfig{Hostname: "test-host-2"},
})
host = GetHost(logger, "test-host-2")
assert.Equal(t, host, "test-host-2")

// Disable EC2 Metadata service to prevent fetching hostname from there,
Expand All @@ -53,7 +48,7 @@ func TestHost(t *testing.T) {
require.NoError(t, err)
defer os.Setenv(awsEc2MetadataDisabled, curr)

host = GetHost(logger, &config.Config{})
host = GetHost(logger, "")
osHostname, err := os.Hostname()
require.NoError(t, err)
// TODO: Investigate why the returned host contains more data on github actions.
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/internal/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func metadataFromAttributes(attrs pdata.AttributeMap) *HostMetadata {
func fillHostMetadata(params component.ExporterCreateSettings, cfg *config.Config, hm *HostMetadata) {
// Could not get hostname from attributes
if hm.InternalHostname == "" {
hostname := GetHost(params.Logger, cfg)
hostname := GetHost(params.Logger, cfg.Hostname)
hm.InternalHostname = hostname
hm.Meta.Hostname = hostname
}
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type hostProvider struct {
}

func (p *hostProvider) Hostname(context.Context) (string, error) {
return metadata.GetHost(p.logger, p.cfg), nil
return metadata.GetHost(p.logger, p.cfg.Hostname), nil
}

// translatorFromConfig creates a new metrics translator from the exporter config.
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/traces_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (exp *traceExporter) pushTraceData(
// convert traces to datadog traces and group trace payloads by env
// we largely apply the same logic as the serverless implementation, simplified a bit
// https://github.com/DataDog/datadog-serverless-functions/blob/f5c3aedfec5ba223b11b76a4239fcbf35ec7d045/aws/logs_monitoring/trace_forwarder/cmd/trace/main.go#L61-L83
fallbackHost := metadata.GetHost(exp.params.Logger, exp.cfg)
fallbackHost := metadata.GetHost(exp.params.Logger, exp.cfg.Hostname)
ddTraces, ms := convertToDatadogTd(td, fallbackHost, exp.cfg, exp.denylister, exp.params.BuildInfo)

// group the traces by env to reduce the number of flushes
Expand Down

0 comments on commit 695fdff

Please sign in to comment.