forked from DataDog/datadog-agent
/
tencent.go
94 lines (79 loc) · 2.89 KB
/
tencent.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
87
88
89
90
91
92
93
94
// 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 tencent
import (
"context"
"fmt"
"time"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/util/cachedfetch"
httputils "github.com/StackVista/stackstate-agent/pkg/util/http"
"github.com/StackVista/stackstate-agent/pkg/util/log"
)
// declare these as vars not const to ease testing
var (
metadataURL = "http://169.254.0.23"
timeout = 300 * time.Millisecond
// CloudProviderName contains the inventory name of for CVM
CloudProviderName = "Tencent"
)
// IsRunningOn returns true if the agent is running on Tencent Cloud
func IsRunningOn(ctx context.Context) bool {
if _, err := GetInstanceID(ctx); err == nil {
return true
}
return false
}
// GetHostAlias returns the VM ID from the Tencent Metadata api
func GetHostAlias(ctx context.Context) (string, error) {
return GetInstanceID(ctx)
}
var instanceIDFetcher = cachedfetch.Fetcher{
Name: "Tencent InstanceID",
Attempt: func(ctx context.Context) (interface{}, error) {
res, err := getMetadataItemWithMaxLength(ctx, metadataURL+"/meta-data/instance-id", config.Datadog.GetInt("metadata_endpoints_max_hostname_size"))
if err != nil {
return "", fmt.Errorf("unable to get TencentCloud CVM instanceID: %s", err)
}
return res, err
},
}
// GetInstanceID fetches the instance id for current host from the Tencent metadata API
func GetInstanceID(ctx context.Context) (string, error) {
return instanceIDFetcher.FetchString(ctx)
}
// HostnameProvider gets the hostname
func HostnameProvider(ctx context.Context, options map[string]interface{}) (string, error) {
log.Debug("GetHostname trying Tencent metadata...")
return GetInstanceID(ctx)
}
// GetNTPHosts returns the NTP hosts for Tencent if it is detected as the cloud provider, otherwise an empty array.
// Demo: https://intl.cloud.tencent.com/document/product/213/32379
func GetNTPHosts(ctx context.Context) []string {
if IsRunningOn(ctx) {
return []string{"ntpupdate.tencentyun.com"}
}
return nil
}
func getMetadataItemWithMaxLength(ctx context.Context, endpoint string, maxLength int) (string, error) {
result, err := getMetadataItem(ctx, endpoint)
if err != nil {
return result, err
}
if len(result) > maxLength {
return "", fmt.Errorf("%v gave a response with length > to %v", endpoint, maxLength)
}
return result, err
}
func getMetadataItem(ctx context.Context, endpoint string) (string, error) {
if !config.IsCloudProviderEnabled(CloudProviderName) {
return "", fmt.Errorf("cloud provider is disabled by configuration")
}
res, err := httputils.Get(ctx, endpoint, nil, timeout)
if err != nil {
return "", fmt.Errorf("unable to fetch Tencent Metadata API, %s", err)
}
return res, nil
}