Skip to content

Commit

Permalink
[process-agent] Use the same hostname lookup call when building the h…
Browse files Browse the repository at this point in the history
…ostInfo in the core agent (#22941)

[process-agent] Use the same hostname lookup call when building the hostInfo in the core agent
  • Loading branch information
wiyu authored Feb 17, 2024
1 parent 6cbb594 commit 4bf26c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/process/checks/host_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"github.com/DataDog/datadog-agent/pkg/config"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
"github.com/DataDog/datadog-agent/pkg/util/fargate"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
ddgrpc "github.com/DataDog/datadog-agent/pkg/util/grpc"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/hostname/validate"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
Expand Down Expand Up @@ -63,6 +65,15 @@ func resolveHostName(config config.Reader) (string, error) {
agentBin := config.GetString("process_config.dd_agent_bin")
connectionTimeout := config.GetDuration("process_config.grpc_connection_timeout_secs") * time.Second
var err error
// TODO: We should migrate to the common hostname component
if flavor.GetFlavor() == flavor.DefaultAgent {
hostName, err = hostname.Get(context.TODO())
if err != nil {
return "", fmt.Errorf("error while getting hostname: %v", err)
}
return hostName, nil
}

hostName, err = getHostname(context.Background(), agentBin, connectionTimeout)
if err != nil {
return "", log.Errorf("cannot get hostname: %v", err)
Expand Down
6 changes: 6 additions & 0 deletions pkg/process/checks/host_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/DataDog/datadog-agent/pkg/config"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
pbmocks "github.com/DataDog/datadog-agent/pkg/proto/pbgo/mocks/core"
"github.com/DataDog/datadog-agent/pkg/util/flavor"
)

func TestGetHostname(t *testing.T) {
Expand Down Expand Up @@ -81,6 +82,11 @@ func TestGetHostnameFromCmd(t *testing.T) {
}

func TestInvalidHostname(t *testing.T) {
oldFlavor := flavor.GetFlavor()
defer flavor.SetFlavor(oldFlavor)

flavor.SetFlavor(flavor.ProcessAgent)

cfg := config.Mock(t)

// Lower the GRPC timeout, otherwise the test will time out in CI
Expand Down

0 comments on commit 4bf26c2

Please sign in to comment.