forked from circonus-labs/circonus-gometrics
/
check_bundle_metrics.go
95 lines (75 loc) · 2.62 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
// 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 api
import (
"encoding/json"
"fmt"
"regexp"
"github.com/circonus-labs/circonus-gometrics/api/config"
)
// 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, fmt.Errorf("Invalid check bundle metrics CID [none]")
}
metricsCID := string(*cid)
matched, err := regexp.MatchString(config.CheckBundleMetricsCIDRegex, metricsCID)
if err != nil {
return nil, err
}
if !matched {
return nil, fmt.Errorf("Invalid check bundle metrics CID [%s]", metricsCID)
}
result, err := a.Get(metricsCID)
if err != nil {
return nil, err
}
if a.Debug {
a.Log.Printf("[DEBUG] fetch check bundle metrics, received JSON: %s", string(result))
}
metrics := &CheckBundleMetrics{}
if err := json.Unmarshal(result, metrics); err != nil {
return nil, err
}
return metrics, nil
}
// UpdateCheckBundleMetrics updates passed metrics.
func (a *API) UpdateCheckBundleMetrics(cfg *CheckBundleMetrics) (*CheckBundleMetrics, error) {
if cfg == nil {
return nil, fmt.Errorf("Invalid check bundle metrics config [nil]")
}
metricsCID := string(cfg.CID)
matched, err := regexp.MatchString(config.CheckBundleMetricsCIDRegex, metricsCID)
if err != nil {
return nil, err
}
if !matched {
return nil, fmt.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("[DEBUG] update check bundle metrics, sending JSON: %s", string(jsonCfg))
}
result, err := a.Put(metricsCID, jsonCfg)
if err != nil {
return nil, err
}
metrics := &CheckBundleMetrics{}
if err := json.Unmarshal(result, metrics); err != nil {
return nil, err
}
return metrics, nil
}