diff --git a/exporter/datadogexporter/config.go b/exporter/datadogexporter/config.go index 4e5ba14905ffc..39b131dfde077 100644 --- a/exporter/datadogexporter/config.go +++ b/exporter/datadogexporter/config.go @@ -10,6 +10,7 @@ import ( "regexp" "strings" + "github.com/DataDog/datadog-agent/pkg/util/hostname/validate" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/confighttp" "go.opentelemetry.io/collector/config/confignet" @@ -18,8 +19,6 @@ import ( "go.opentelemetry.io/collector/confmap" "go.opentelemetry.io/collector/exporter/exporterhelper" "go.uber.org/zap" - - "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/valid" ) var ( @@ -469,7 +468,7 @@ func (c *Config) Validate() error { return errNoMetadata } - if err := valid.Hostname(c.Hostname); c.Hostname != "" && err != nil { + if err := validate.ValidHostname(c.Hostname); c.Hostname != "" && err != nil { return fmt.Errorf("hostname field is invalid: %w", err) } diff --git a/exporter/datadogexporter/config_test.go b/exporter/datadogexporter/config_test.go index 2bff5d17d220b..7caaa318eb75c 100644 --- a/exporter/datadogexporter/config_test.go +++ b/exporter/datadogexporter/config_test.go @@ -41,7 +41,7 @@ func TestValidate(t *testing.T) { API: APIConfig{Key: "notnull"}, TagsConfig: TagsConfig{Hostname: "invalid_host"}, }, - err: "hostname field is invalid: 'invalid_host' is not RFC1123 compliant", + err: "hostname field is invalid: invalid_host is not RFC1123 compliant", }, { name: "no metadata", diff --git a/exporter/datadogexporter/go.mod b/exporter/datadogexporter/go.mod index 3f3fc7356123e..c0effa83877c9 100644 --- a/exporter/datadogexporter/go.mod +++ b/exporter/datadogexporter/go.mod @@ -12,6 +12,7 @@ require ( github.com/DataDog/datadog-agent/comp/otelcol/logsagentpipeline/logsagentpipelineimpl v0.56.0-rc.1 github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/exporter/logsagentexporter v0.56.0-rc.1 github.com/DataDog/datadog-agent/comp/otelcol/otlp/components/metricsclient v0.56.0-rc.1 + github.com/DataDog/datadog-agent/comp/trace/compression/impl-gzip v0.56.0-rc.1 github.com/DataDog/datadog-agent/pkg/config/model v0.56.0-rc.1 github.com/DataDog/datadog-agent/pkg/config/setup v0.56.0-rc.1 github.com/DataDog/datadog-agent/pkg/logs/auditor v0.56.0-rc.1 // indirect @@ -23,6 +24,7 @@ require ( github.com/DataDog/datadog-agent/pkg/proto v0.56.0-rc.1 github.com/DataDog/datadog-agent/pkg/status/health v0.56.0-rc.1 // indirect github.com/DataDog/datadog-agent/pkg/trace v0.56.0-rc.1.0.20240711082232-dc70454ece9f + github.com/DataDog/datadog-agent/pkg/util/hostname/validate v0.56.0-rc.1 github.com/DataDog/datadog-agent/pkg/util/startstop v0.56.0-rc.1 // indirect github.com/DataDog/datadog-api-client-go/v2 v2.27.0 github.com/DataDog/datadog-go/v5 v5.5.0 @@ -86,8 +88,6 @@ require ( k8s.io/client-go v0.29.3 ) -require github.com/DataDog/datadog-agent/comp/trace/compression/impl-gzip v0.56.0-rc.1 - require ( cloud.google.com/go/auth v0.5.1 // indirect cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect @@ -123,7 +123,6 @@ require ( github.com/DataDog/datadog-agent/pkg/util/executable v0.56.0-rc.1 // indirect github.com/DataDog/datadog-agent/pkg/util/filesystem v0.56.0-rc.1 // indirect github.com/DataDog/datadog-agent/pkg/util/fxutil v0.56.0-rc.1 // indirect - github.com/DataDog/datadog-agent/pkg/util/hostname/validate v0.56.0-rc.1 // indirect github.com/DataDog/datadog-agent/pkg/util/http v0.56.0-rc.1 // indirect github.com/DataDog/datadog-agent/pkg/util/log v0.56.0-rc.1 // indirect github.com/DataDog/datadog-agent/pkg/util/optional v0.56.0-rc.1 // indirect diff --git a/exporter/datadogexporter/internal/hostmetadata/internal/system/host.go b/exporter/datadogexporter/internal/hostmetadata/internal/system/host.go index faed83a74864e..7dbbaf649fa56 100644 --- a/exporter/datadogexporter/internal/hostmetadata/internal/system/host.go +++ b/exporter/datadogexporter/internal/hostmetadata/internal/system/host.go @@ -9,10 +9,9 @@ import ( "os" "sync" + "github.com/DataDog/datadog-agent/pkg/util/hostname/validate" "github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source" "go.uber.org/zap" - - "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/valid" ) type HostInfo struct { @@ -44,7 +43,7 @@ func (hi *HostInfo) GetHostname(logger *zap.Logger) string { if hi.FQDN == "" { // Don't report failure since FQDN was just not available return hi.OS - } else if err := valid.Hostname(hi.FQDN); err != nil { + } else if err := validate.ValidHostname(hi.FQDN); err != nil { logger.Info("FQDN is not valid", zap.Error(err)) return hi.OS } diff --git a/exporter/datadogexporter/internal/hostmetadata/internal/system/package_test.go b/exporter/datadogexporter/internal/hostmetadata/internal/system/package_test.go index 641255b7bf5e3..35c0ab8ee91a7 100644 --- a/exporter/datadogexporter/internal/hostmetadata/internal/system/package_test.go +++ b/exporter/datadogexporter/internal/hostmetadata/internal/system/package_test.go @@ -10,5 +10,9 @@ import ( ) func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) + // We have to ignore seelog because of upstream issue + // https://github.com/cihub/seelog/issues/182 + goleak.VerifyTestMain(m, + goleak.IgnoreAnyFunction("github.com/cihub/seelog.(*asyncLoopLogger).processQueue"), + ) } diff --git a/exporter/datadogexporter/internal/hostmetadata/valid/package_test.go b/exporter/datadogexporter/internal/hostmetadata/valid/package_test.go deleted file mode 100644 index af34af695cd4e..0000000000000 --- a/exporter/datadogexporter/internal/hostmetadata/valid/package_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package valid - -import ( - "testing" - - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - goleak.VerifyTestMain(m) -} diff --git a/exporter/datadogexporter/internal/hostmetadata/valid/valid.go b/exporter/datadogexporter/internal/hostmetadata/valid/valid.go deleted file mode 100644 index 46dffff5d2466..0000000000000 --- a/exporter/datadogexporter/internal/hostmetadata/valid/valid.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -// This file includes software developed at Datadog (https://www.datadoghq.com/) -// for the Datadog Agent (https://github.com/DataDog/datadog-agent) - -// Package valid contains functions that validate hostnames -package valid // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/hostmetadata/valid" - -import ( - "fmt" - "regexp" - "strings" -) - -var ( - validHostnameRfc1123 = regexp.MustCompile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`) - localhostIdentifiers = [...]string{ - "localhost", - "localhost.localdomain", - "localhost6.localdomain6", - "ip6-localhost", - } -) - -// Hostname determines whether the passed string is a valid hostname. -// In case it's not, the returned error contains the details of the failure. -func Hostname(hostname string) error { - const maxLength = 255 - - switch { - case hostname == "": - return fmt.Errorf("hostname is empty") - case isLocal(hostname): - return fmt.Errorf("'%s' is a local hostname", hostname) - case len(hostname) > maxLength: - return fmt.Errorf("name exceeded the maximum length of %d characters", maxLength) - case !validHostnameRfc1123.MatchString(hostname): - return fmt.Errorf("'%s' is not RFC1123 compliant", hostname) - } - return nil -} - -// check whether the name is in the list of local hostnames -func isLocal(name string) bool { - name = strings.ToLower(name) - for _, val := range localhostIdentifiers { - if val == name { - return true - } - } - return false -} diff --git a/exporter/datadogexporter/internal/hostmetadata/valid/valid_test.go b/exporter/datadogexporter/internal/hostmetadata/valid/valid_test.go deleted file mode 100644 index e313e570b8371..0000000000000 --- a/exporter/datadogexporter/internal/hostmetadata/valid/valid_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright The OpenTelemetry Authors -// SPDX-License-Identifier: Apache-2.0 - -package valid - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestHostname(t *testing.T) { - // Empty - require.Error(t, Hostname("")) - - // Local - require.Error(t, Hostname("localhost")) - - // Max length exceeded - require.Error(t, Hostname(strings.Repeat("a", 256))) - - // non RFC1123 compliant - require.Error(t, Hostname("***")) - require.Error(t, Hostname("invalid_hostname")) - - require.NoError(t, Hostname("valid-hostname")) -}