-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
hostname.go
50 lines (40 loc) · 1.3 KB
/
hostname.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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 2016-present Datadog, Inc.
// Package utils holds utils related files
package utils
import (
"context"
"time"
"github.com/avast/retry-go/v4"
pbgo "github.com/DataDog/datadog-agent/pkg/proto/pbgo/core"
"github.com/DataDog/datadog-agent/pkg/util/grpc"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
// maxAttempts is the maximum number of times we try to get the hostname
// from the core-agent before bailing out.
maxAttempts = 6
)
// GetHostname attempts to acquire a hostname by connecting to the core agent's
// gRPC endpoints
func GetHostname() (string, error) {
var hostname string
err := retry.Do(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
client, err := grpc.GetDDAgentClient(ctx)
if err != nil {
return err
}
reply, err := client.GetHostname(ctx, &pbgo.HostnameRequest{})
if err != nil {
return err
}
log.Debugf("Acquired hostname from gRPC: %s", reply.Hostname)
hostname = reply.Hostname
return nil
}, retry.LastErrorOnly(true), retry.Attempts(maxAttempts))
return hostname, err
}