-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
62 lines (48 loc) · 1.45 KB
/
metrics.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
package cloudwatch
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/YaleSpinup/apierror"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
log "github.com/sirupsen/logrus"
)
type Metric []string
type MetricsRequest map[string]interface{}
// GetMetricWidget gets a metric widget image for an instance id
func (c *Cloudwatch) GetMetricWidget(ctx context.Context, req MetricsRequest) ([]byte, error) {
if req == nil {
return nil, apierror.New(apierror.ErrBadRequest, "invalid input", nil)
}
log.Infof("getting metric widget with input request %+v", req)
j, err := json.Marshal(req)
if err != nil {
msg := fmt.Sprintf("failed to build widget request input for metrics from '%+v': %s", req, err)
return nil, ErrCode(msg, err)
}
log.Debugf("getting metric widget with input json %s", string(j))
out, err := c.Service.GetMetricWidgetImageWithContext(ctx, &cloudwatch.GetMetricWidgetImageInput{MetricWidget: aws.String(string(j))})
if err != nil {
msg := fmt.Sprintf("failed to get metric widget from request '%+s': %s", string(j), err)
return nil, ErrCode(msg, err)
}
return out.MetricWidgetImage, nil
}
func (m *MetricsRequest) String() string {
var s string
if m == nil {
return s
}
req := map[string]interface{}(*m)
keys := make([]string, 0, len(req))
for k := range req {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
s += fmt.Sprintf("/%s:%v", k, req[k])
}
return s
}