Skip to content

Commit

Permalink
add remote host
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshg13 committed Jun 23, 2024
1 parent 6953c09 commit 5d2073f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 47 deletions.
46 changes: 4 additions & 42 deletions cmd/otel-agent/subcommands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/DataDog/datadog-agent/comp/api/authtoken/fetchonlyimpl"
coreconfig "github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface"
"github.com/DataDog/datadog-agent/comp/core/hostname/remotehostnameimpl"
corelog "github.com/DataDog/datadog-agent/comp/core/log"
corelogimpl "github.com/DataDog/datadog-agent/comp/core/log/logimpl"
"github.com/DataDog/datadog-agent/comp/core/log/tracelogimpl"
Expand Down Expand Up @@ -44,7 +45,6 @@ import (
traceconfig "github.com/DataDog/datadog-agent/comp/trace/config"
pkgconfigenv "github.com/DataDog/datadog-agent/pkg/config/env"
pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup"
"github.com/DataDog/datadog-agent/pkg/security/utils"
"github.com/DataDog/datadog-agent/pkg/serializer"
"github.com/DataDog/datadog-agent/pkg/trace/telemetry"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
Expand Down Expand Up @@ -87,42 +87,7 @@ func (o *orchestratorinterfaceimpl) Reset() {
o.f = nil
}

// TODO create a alternative implementation for hostname.Component that uses the remote host
// The logic is used by other agents, and so having it exposed as Component would be useful
type remotehostimpl struct {
hostname string
}

func (r *remotehostimpl) Get(ctx context.Context) (string, error) {
if r.hostname != "" {
return r.hostname, nil
}
hostname, err := utils.GetHostnameWithContextAndFallback(ctx)
if err != nil {
return "", err
}
r.hostname = hostname
return hostname, nil
}

func (r *remotehostimpl) GetSafe(ctx context.Context) string {
h, _ := r.Get(ctx)
return h
}

func (r *remotehostimpl) GetWithProvider(ctx context.Context) (hostnameinterface.Data, error) {
h, err := r.Get(ctx)
if err != nil {
return hostnameinterface.Data{}, err
}
return hostnameinterface.Data{
Hostname: h,
Provider: "remote",
}, nil
}

func runOTelAgentCommand(ctx context.Context, params *subcommands.GlobalParams, opts ...fx.Option) error {
rh := &remotehostimpl{}
err := fxutil.Run(
forwarder.Bundle(),
tracelogimpl.Module(), // cannot have corelogimpl and tracelogimpl at the same time
Expand Down Expand Up @@ -151,13 +116,10 @@ func runOTelAgentCommand(ctx context.Context, params *subcommands.GlobalParams,
fx.Provide(func() []string {
return append(params.ConfPaths, params.Sets...)
}),
fx.Provide(func() (serializerexporter.SourceProviderFunc, error) {
return rh.Get, nil
}),
fx.Provide(func() hostnameinterface.Component {
return rh
fx.Provide(func(h hostnameinterface.Component) (serializerexporter.SourceProviderFunc, error) {
return h.Get, nil
}),

fx.Provide(remotehostnameimpl.Module()),
fx.Supply(optional.NewNoneOption[secrets.Component]()),

fx.Provide(func(c coreconfig.Component) corelogimpl.Params {
Expand Down
44 changes: 39 additions & 5 deletions comp/core/hostname/remotehostnameimpl/hostname.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-present Datadog, Inc.

// Package remotehostnameimpl provides a function to get the hostname from core agent.
package remotehostnameimpl

Expand All @@ -9,30 +14,59 @@ import (

"github.com/DataDog/datadog-agent/pkg/config"
pbgo "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
"github.com/DataDog/datadog-agent/pkg/util/grpc"
"github.com/DataDog/datadog-agent/pkg/util/hostname"
"github.com/DataDog/datadog-agent/pkg/util/log"

"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface"
cache "github.com/patrickmn/go-cache"
"go.uber.org/fx"
)

const (
defaultExpire = 15 * time.Minute
defaultPurge = 30 * time.Second
// AgentCachePrefix is the common root to use to prefix all the cache
// keys for any value regarding the Agent
AgentCachePrefix = "agent"

// encapsulate the cache module for easy refactoring

// NoExpiration maps to go-cache corresponding value
NoExpiration = cache.NoExpiration
// maxAttempts is the maximum number of times we try to get the hostname
// from the core-agent before bailing out.
maxAttempts = 6
)

// Module defines the fx options for this component.
func Module() fxutil.Module {
return fxutil.Component(
fx.Provide(newRemoteHostImpl))
}

var cachKey = "hostname"

type remotehostimpl struct {
hostname string
cache *cache.Cache
}

func newRemoteHostImpl() hostnameinterface.Component {
return &remotehostimpl{
cache: cache.New(defaultExpire, defaultPurge),
}
}

func (r *remotehostimpl) Get(ctx context.Context) (string, error) {
if r.hostname != "" {
return r.hostname, nil
if hostname, found := r.cache.Get(cachKey); found {
return hostname.(string), nil
}
hostname, err := utils.GetHostnameWithContextAndFallback(ctx)
hostname, err := getHostnameWithContextAndFallback(ctx)
if err != nil {
return "", err
}
r.hostname = hostname
r.cache.Set(cachKey, hostname, NoExpiration)
return hostname, nil
}

Expand Down

0 comments on commit 5d2073f

Please sign in to comment.