-
Notifications
You must be signed in to change notification settings - Fork 141
/
datadog.go
156 lines (138 loc) · 3.99 KB
/
datadog.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2023 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package datadog
import (
"context"
"fmt"
"net/http"
"time"
"github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/app/piped/analysisprovider/metrics"
)
const (
ProviderType = "Datadog"
defaultAddress = "datadoghq.com"
defaultTimeout = 30 * time.Second
)
// Provider works as an HTTP client for datadog.
type Provider struct {
client *datadog.APIClient
runQuery func(request datadog.ApiQueryMetricsRequest) (datadog.MetricsQueryResponse, *http.Response, error)
address string
apiKey string
applicationKey string
timeout time.Duration
logger *zap.Logger
}
func NewProvider(apiKey, applicationKey string, opts ...Option) (*Provider, error) {
if apiKey == "" {
return nil, fmt.Errorf("api-key is required")
}
if applicationKey == "" {
return nil, fmt.Errorf("application-key is required")
}
p := &Provider{
client: datadog.NewAPIClient(datadog.NewConfiguration()),
runQuery: func(request datadog.ApiQueryMetricsRequest) (datadog.MetricsQueryResponse, *http.Response, error) {
return request.Execute()
},
address: defaultAddress,
apiKey: apiKey,
applicationKey: applicationKey,
timeout: defaultTimeout,
logger: zap.NewNop(),
}
for _, opt := range opts {
opt(p)
}
return p, nil
}
type Option func(*Provider)
func WithAddress(address string) Option {
return func(p *Provider) {
p.address = address
}
}
func WithLogger(logger *zap.Logger) Option {
return func(p *Provider) {
p.logger = logger.Named("datadog-provider")
}
}
func WithTimeout(timeout time.Duration) Option {
return func(p *Provider) {
p.timeout = timeout
}
}
func (p *Provider) Type() string {
return ProviderType
}
func (p *Provider) QueryPoints(ctx context.Context, query string, queryRange metrics.QueryRange) ([]metrics.DataPoint, error) {
ctx, cancel := context.WithTimeout(ctx, p.timeout)
defer cancel()
if err := queryRange.Validate(); err != nil {
return nil, err
}
ctx = context.WithValue(
ctx,
datadog.ContextServerVariables,
map[string]string{"site": p.address},
)
ctx = context.WithValue(
ctx,
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: p.apiKey,
},
"appKeyAuth": {
Key: p.applicationKey,
},
},
)
req := p.client.MetricsApi.QueryMetrics(ctx).
From(queryRange.From.Unix()).
To(queryRange.To.Unix()).
Query(query)
resp, httpResp, err := p.runQuery(req)
if err != nil {
return nil, fmt.Errorf("failed to call \"MetricsApi.QueryMetrics\": %w", err)
}
if httpResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected HTTP status code from %s: %d", httpResp.Request.URL, httpResp.StatusCode)
}
// Collect data points given by the provider.
var size int
for _, s := range *resp.Series {
size += int(*s.Length)
}
out := make([]metrics.DataPoint, 0, size)
for _, s := range *resp.Series {
points := s.Pointlist
if points == nil || len(*points) == 0 {
return nil, fmt.Errorf("invalid response: no data points found within the queried range: %w", metrics.ErrNoDataFound)
}
for _, point := range *points {
if len(point) < 2 {
return nil, fmt.Errorf("invalid response: invalid data point found")
}
// NOTE: A data point is assumed to be kind of like [unix-time, value].
out = append(out, metrics.DataPoint{
Timestamp: int64(point[0]),
Value: point[1],
})
}
}
return out, nil
}