-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheck_bundle_metrics.go
102 lines (82 loc) · 2.97 KB
/
check_bundle_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
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
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// CheckBundleMetrics API support - Fetch, Create*, Update, and Delete**
// See: https://login.circonus.com/resources/api/calls/check_bundle_metrics
// * : create metrics by adding to array with a status of 'active'
// ** : delete (distable collection of) metrics by changing status from 'active' to 'available'
package apiclient
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/circonus-labs/go-apiclient/config"
"github.com/pkg/errors"
)
// CheckBundleMetrics defines metrics for a specific check bundle. See https://login.circonus.com/resources/api/calls/check_bundle_metrics for more information.
type CheckBundleMetrics struct {
CID string `json:"_cid,omitempty"` // string
Metrics []CheckBundleMetric `json:"metrics"` // See check_bundle.go for CheckBundleMetric definition
}
// FetchCheckBundleMetrics retrieves metrics for the check bundle with passed cid.
func (a *API) FetchCheckBundleMetrics(cid CIDType) (*CheckBundleMetrics, error) {
if cid == nil || *cid == "" {
return nil, errors.New("invalid check bundle metrics CID (none)")
}
var metricsCID string
if !strings.HasPrefix(*cid, config.CheckBundleMetricsPrefix) {
metricsCID = fmt.Sprintf("%s/%s", config.CheckBundleMetricsPrefix, *cid)
} else {
metricsCID = *cid
}
matched, err := regexp.MatchString(config.CheckBundleMetricsCIDRegex, metricsCID)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf("invalid check bundle metrics CID (%s)", metricsCID)
}
result, err := a.Get(metricsCID)
if err != nil {
return nil, errors.Wrap(err, "fetching check bundle metrics")
}
if a.Debug {
a.Log.Printf("fetch check bundle metrics, received JSON: %s", string(result))
}
metrics := &CheckBundleMetrics{}
if err := json.Unmarshal(result, metrics); err != nil {
return nil, errors.Wrap(err, "parsing check bundle metrics")
}
return metrics, nil
}
// UpdateCheckBundleMetrics updates passed metrics.
func (a *API) UpdateCheckBundleMetrics(cfg *CheckBundleMetrics) (*CheckBundleMetrics, error) {
if cfg == nil {
return nil, errors.New("invalid check bundle metrics config (nil)")
}
metricsCID := cfg.CID
matched, err := regexp.MatchString(config.CheckBundleMetricsCIDRegex, metricsCID)
if err != nil {
return nil, err
}
if !matched {
return nil, errors.Errorf("invalid check bundle metrics CID (%s)", metricsCID)
}
jsonCfg, err := json.Marshal(cfg)
if err != nil {
return nil, err
}
if a.Debug {
a.Log.Printf("update check bundle metrics, sending JSON: %s", string(jsonCfg))
}
result, err := a.Put(metricsCID, jsonCfg)
if err != nil {
return nil, errors.Wrap(err, "updating check bundle metrics")
}
metrics := &CheckBundleMetrics{}
if err := json.Unmarshal(result, metrics); err != nil {
return nil, errors.Wrap(err, "parsing check bundle metrics")
}
return metrics, nil
}