-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
165 lines (149 loc) · 4.38 KB
/
main.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
157
158
159
160
161
162
163
164
165
package main
import (
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/gregscott94/z-table-golang"
"github.com/iancoleman/strcase"
)
const (
DefaultPollInterval = "1m"
)
// To store and analyze data
type AccountData struct {
AccountId string
MetricName string
NewMetricName string
MetricWhere string
MetricFacet string
Attributes []string
Metrics []Metric
Timestamp int64
Threshold float64
ZTable *ztable.ZTable
LicenseKey string
UserKey string
Client *http.Client
GraphQlHeaders []string
MetricHeaders []string
Details Details
SampleTime int64
PollInterval time.Duration
Since int64
}
type Details struct {
EntityGuid string `json:"entityGuid"`
CurrentTime int64 `json:"-"`
StartTime int64 `json:"startTimeMs"`
Duration int `json:"durationMs"`
}
func (data *AccountData) makeClient() {
data.Client = &http.Client{}
data.GraphQlHeaders = []string{"Content-Type:application/json", "API-Key:" + data.UserKey}
data.MetricHeaders = []string{"Content-Type:application/json", "Api-Key:" + data.LicenseKey}
// make list of attributes for each metric
attributes := strings.Split(data.MetricFacet, ",")
for _, attributeRaw := range attributes {
attribute := strings.TrimSpace(attributeRaw)
if attribute == "entity.guid" || attribute == "entityGuid" {
continue
}
data.Attributes = append(data.Attributes, attribute)
}
data.Attributes = append(data.Attributes, "entity.guid")
data.ZTable = ztable.NewZTable(nil)
}
func main() {
// Get poll interval
pollInterval := os.Getenv("POLL_INTERVAL")
if len(pollInterval) == 0 {
pollInterval = DefaultPollInterval
}
pollIntervalDuration, err := time.ParseDuration(pollInterval)
if err != nil {
log.Fatalf("Error: could not parse env var POLL_INTERVAL: %s, must be a duration (ex: 1h)", err)
}
if pollIntervalDuration < time.Minute {
log.Fatalf("Error: POLL_INTERVAL %v, must be at least 1 minute", pollIntervalDuration)
}
pollIntervalDuration = pollIntervalDuration.Round(time.Minute)
// Get required settings
data := AccountData{
AccountId: strings.TrimSpace(os.Getenv("NEW_RELIC_ACCOUNT")),
MetricName: strings.TrimSpace(os.Getenv("METRIC_NAME")),
MetricWhere: strings.TrimSpace(os.Getenv("METRIC_WHERE")),
MetricFacet: strings.TrimSpace(os.Getenv("METRIC_FACET")),
LicenseKey: strings.TrimSpace(os.Getenv("NEW_RELIC_LICENSE_KEY")),
UserKey: strings.TrimSpace(os.Getenv("NEW_RELIC_USER_KEY")),
PollInterval: pollIntervalDuration,
Since: int64((pollIntervalDuration + time.Minute).Minutes()),
}
if len(data.AccountId) == 0 {
log.Printf("Please set env var NEW_RELIC_ACCOUNT")
os.Exit(0)
}
if len(data.MetricName) == 0 {
log.Printf("Please set env var METRIC_NAME")
os.Exit(0)
}
data.NewMetricName = strcase.ToLowerCamel(data.MetricName) + "Threshold"
if len(data.MetricWhere) == 0 {
log.Printf("Please set env var METRIC_WHERE")
os.Exit(0)
}
if len(data.MetricFacet) == 0 {
log.Printf("Please set env var METRIC_FACET")
os.Exit(0)
}
threshold := os.Getenv("THRESHOLD")
if len(threshold) == 0 {
log.Printf("Please set env var THRESHOLD")
os.Exit(0)
}
data.Threshold, err = strconv.ParseFloat(threshold, 64)
if err != nil {
log.Printf("Invalid number for env var THRESHOLD: %v", err)
os.Exit(0)
}
if len(data.LicenseKey) == 0 {
log.Printf("Please set env var NEW_RELIC_LICENSE_KEY")
os.Exit(0)
}
if len(data.UserKey) == 0 {
log.Printf("Please set env var NEW_RELIC_USER_KEY")
os.Exit(0)
}
log.Printf("Using account %s, queryting %s to generate metric %s", data.AccountId, data.MetricName, data.NewMetricName)
log.Printf("Poll interval is %s", data.PollInterval)
// Create GraphQl client
data.makeClient()
// Graceful shutdown
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sigs := <-sigs
log.Printf("Process %v - Shutting down\n", sigs)
os.Exit(0)
}()
// Start poll loop
log.Println("Starting polling loop")
for {
startTime := time.Now()
data.SampleTime = startTime.Unix()
// Query timeslice metrics
data.queryGraphQl()
// Make results into metrics
data.makeMetrics()
remainder := data.PollInterval - time.Now().Sub(startTime)
if remainder > 0 {
log.Printf("Sleeping %v", remainder)
// Wait remainder of poll interval
time.Sleep(remainder)
}
}
}