forked from DataDog/datadog-agent
-
Notifications
You must be signed in to change notification settings - Fork 2
/
agent_client.go
86 lines (70 loc) · 2.62 KB
/
agent_client.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package grpc
import (
"context"
"crypto/tls"
"errors"
"net"
"time"
"github.com/StackVista/stackstate-agent/pkg/config"
pb "github.com/StackVista/stackstate-agent/pkg/proto/pbgo"
"github.com/StackVista/stackstate-agent/pkg/util/log"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
)
var defaultBackoffConfig = backoff.Config{
BaseDelay: 1.0 * time.Second,
Multiplier: 1.1,
Jitter: 0.2,
MaxDelay: 2 * time.Second,
}
// defaultAgentDialOpts default dial options to the main agent which blocks and retries based on the backoffConfig
var defaultAgentDialOpts = []grpc.DialOption{
grpc.WithConnectParams(grpc.ConnectParams{Backoff: defaultBackoffConfig}),
grpc.WithBlock(),
}
func getGRPCClientConn(ctx context.Context, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
if config.Datadog.GetString("cmd_port") == "-1" {
return nil, errors.New("grpc client disabled via cmd_port: -1")
}
// This is needed as the server hangs when using "grpc.WithInsecure()"
tlsConf := tls.Config{InsecureSkipVerify: true}
if len(opts) == 0 {
opts = defaultAgentDialOpts
}
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tlsConf)))
target, err := getIPCAddressPort()
if err != nil {
return nil, err
}
log.Debugf("attempting to create grpc agent client connection to: %s", target)
return grpc.DialContext(ctx, target, opts...)
}
// GetDDAgentClient creates a pb.AgentClient for IPC with the main agent via gRPC. This call is blocking by default, so
// it is up to the caller to supply a context with appropriate timeout/cancel options
func GetDDAgentClient(ctx context.Context, opts ...grpc.DialOption) (pb.AgentClient, error) {
conn, err := getGRPCClientConn(ctx, opts...)
if err != nil {
return nil, err
}
log.Debug("grpc agent client created")
return pb.NewAgentClient(conn), nil
}
// GetDDAgentSecureClient creates a pb.AgentSecureClient for IPC with the main agent via gRPC. This call is blocking by default, so
// it is up to the caller to supply a context with appropriate timeout/cancel options
func GetDDAgentSecureClient(ctx context.Context, opts ...grpc.DialOption) (pb.AgentSecureClient, error) {
conn, err := getGRPCClientConn(ctx, opts...)
if err != nil {
return nil, err
}
log.Debug("grpc agent secure client created")
return pb.NewAgentSecureClient(conn), nil
}
// getIPCAddressPort returns the host and port for connecting to the main agent
func getIPCAddressPort() (string, error) {
ipcAddress, err := config.GetIPCAddress()
if err != nil {
return "", err
}
return net.JoinHostPort(ipcAddress, config.Datadog.GetString("cmd_port")), nil
}