-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
api.go
93 lines (82 loc) · 3.35 KB
/
api.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package clientutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/clientutil"
import (
"context"
"errors"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.uber.org/zap"
zorkian "gopkg.in/zorkian/go-datadog-api.v2"
)
// GZipSubmitMetricsOptionalParameters is used to enable gzip compression for metric payloads submitted by native datadog client
var GZipSubmitMetricsOptionalParameters = datadogV2.NewSubmitMetricsOptionalParameters().WithContentEncoding(datadogV2.METRICCONTENTENCODING_GZIP)
// CreateAPIClient creates a new Datadog API client
func CreateAPIClient(buildInfo component.BuildInfo, endpoint string, hcs confighttp.ClientConfig) *datadog.APIClient {
configuration := datadog.NewConfiguration()
configuration.UserAgent = UserAgent(buildInfo)
configuration.HTTPClient = NewHTTPClient(hcs)
configuration.Compress = true
configuration.Servers = datadog.ServerConfigurations{
{
URL: "{site}",
Description: "No description provided",
Variables: map[string]datadog.ServerVariable{"site": {DefaultValue: endpoint}},
},
}
return datadog.NewAPIClient(configuration)
}
// ValidateAPIKey checks if the API key (not the APP key) is valid
func ValidateAPIKey(ctx context.Context, apiKey string, logger *zap.Logger, apiClient *datadog.APIClient) error {
logger.Info("Validating API key.")
authAPI := datadogV1.NewAuthenticationApi(apiClient)
resp, httpresp, err := authAPI.Validate(GetRequestContext(ctx, apiKey))
if err == nil && resp.Valid != nil && *resp.Valid {
logger.Info("API key validation successful.")
return nil
}
if err != nil {
logger.Warn("Error while validating API key", zap.Error(err))
return nil
}
logger.Warn(ErrInvalidAPI.Error())
return WrapError(ErrInvalidAPI, httpresp)
}
// GetRequestContext creates a new context with API key for DatadogV2 requests
func GetRequestContext(ctx context.Context, apiKey string) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(
ctx,
datadog.ContextAPIKeys,
map[string]datadog.APIKey{"apiKeyAuth": {Key: apiKey}},
)
}
// CreateZorkianClient creates a new Zorkian Datadog client
// Deprecated: CreateZorkianClient returns a Zorkian Datadog client and Zorkian is deprecated. Use CreateAPIClient instead.
func CreateZorkianClient(apiKey string, endpoint string) *zorkian.Client {
client := zorkian.NewClient(apiKey, "")
client.SetBaseUrl(endpoint)
return client
}
var ErrInvalidAPI = errors.New("API Key validation failed")
// ValidateAPIKeyZorkian checks that the provided client was given a correct API key.
// Deprecated: ValidateAPIKeyZorkian uses the deprecated Zorkian client. Use ValidateAPIKey instead.
func ValidateAPIKeyZorkian(logger *zap.Logger, client *zorkian.Client) error {
logger.Info("Validating API key.")
valid, err := client.Validate()
if err == nil && valid {
logger.Info("API key validation successful.")
return nil
}
if err != nil {
logger.Warn("Error while validating API key", zap.Error(err))
return nil
}
logger.Warn(ErrInvalidAPI.Error())
return ErrInvalidAPI
}