-
Notifications
You must be signed in to change notification settings - Fork 48
/
structured_metric.go
249 lines (227 loc) · 8.07 KB
/
structured_metric.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package cloudwatch
// So a metric is the top level fields that map to the Metric
// info in the serialization layer. So we need a map of names to their
// info. And we can map the rest in the log/publish statement...
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"
)
var envMap map[string]string
func init() {
// Get them all and turn it into a map...
// Ref: https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
envMap = make(map[string]string)
envVars := os.Environ()
for _, eachValue := range envVars {
parts := strings.Split(eachValue, "=")
if len(parts) == 2 {
envMap[parts[0]] = parts[1]
}
}
}
// MetricDirective represents an element in the array
// MetricUnit Represents a MetricUnit type
type MetricUnit string
const (
// UnitSeconds Seconds
UnitSeconds MetricUnit = "Seconds"
// UnitMicroseconds Microseconds
UnitMicroseconds MetricUnit = "Microseconds"
// UnitMilliseconds Milliseconds
UnitMilliseconds MetricUnit = "Milliseconds"
// UnitBytes Bytes
UnitBytes MetricUnit = "Bytes"
//UnitKilobytes Kilobytes
UnitKilobytes MetricUnit = "Kilobytes"
//UnitMegabytes Megabytes
UnitMegabytes MetricUnit = "Megabytes"
//UnitGigabytes Gigabytes
UnitGigabytes MetricUnit = "Gigabytes"
//UnitTerabytes Terabytes
UnitTerabytes MetricUnit = "Terabytes"
//UnitBits Bits
UnitBits MetricUnit = "Bits"
//UnitKilobits Kilobits
UnitKilobits MetricUnit = "Kilobits"
//UnitMegabits Megabits
UnitMegabits MetricUnit = "Megabits"
//UnitGigabits Gigabits
UnitGigabits MetricUnit = "Gigabits"
//UnitTerabits Terabits
UnitTerabits MetricUnit = "Terabits"
//UnitPercent Percent
UnitPercent MetricUnit = "Percent"
//UnitCount Count
UnitCount MetricUnit = "Count"
//UnitBytesPerSecond BytesPerSecond
UnitBytesPerSecond MetricUnit = "Bytes/Second"
//UnitKilobytesPerSecond KilobytesPerSecond
UnitKilobytesPerSecond MetricUnit = "Kilobytes/Second"
//UnitMegabytesPerSecond MegabytesPerSecond
UnitMegabytesPerSecond MetricUnit = "Megabytes/Second"
//UnitGigabytesPerSecond GigabytesPerSecond
UnitGigabytesPerSecond MetricUnit = "Gigabytes/Second"
//UnitTerabytesPerSecond TerabytesPerSecond
UnitTerabytesPerSecond MetricUnit = "Terabytes/Second"
//UnitBitsPerSecond BitsPerSecond
UnitBitsPerSecond MetricUnit = "Bits/Second"
//UnitKilobitsPerSecond KilobitsPerSecond
UnitKilobitsPerSecond MetricUnit = "Kilobits/Second"
//UnitMegabitsPerSecond MegabitsPerSecond
UnitMegabitsPerSecond MetricUnit = "Megabits/Second"
//UnitGigabitsPerSecond GigabitsPerSecond
UnitGigabitsPerSecond MetricUnit = "Gigabits/Second"
//UnitTerabitsPerSecond TerabitsPerSecond
UnitTerabitsPerSecond MetricUnit = "Terabits/Second"
//UnitCountPerSecond CountPerSecond
UnitCountPerSecond MetricUnit = "Count/Second"
// UnitNone No units
UnitNone MetricUnit = "None"
)
// MetricValue represents a metric value
type MetricValue struct {
Value interface{}
Unit MetricUnit
}
// MetricDirective is the directive that encapsulates a metric
type MetricDirective struct {
// Dimensions corresponds to the JSON schema field "Dimensions".
Dimensions map[string]string
// Metrics corresponds to the JSON schema field "Metrics".
Metrics map[string]MetricValue
// namespace corresponds to the JSON schema field "Namespace".
namespace string
}
// EmbeddedMetric represents an embedded metric that should be published
type EmbeddedMetric struct {
metrics []*MetricDirective
properties map[string]interface{}
}
// WithProperty is a fluent builder to add property to the EmbeddedMetric state.
// Properties should be used for high cardintality values that need to be
// searchable, but not treated as independent metrics
func (em *EmbeddedMetric) WithProperty(key string, value interface{}) *EmbeddedMetric {
if em.properties == nil {
em.properties = make(map[string]interface{})
}
em.properties[key] = value
return em
}
// NewMetricDirective returns an initialized MetricDirective
// that's included in the EmbeddedMetric instance
func (em *EmbeddedMetric) NewMetricDirective(namespace string,
dimensions map[string]string) *MetricDirective {
md := &MetricDirective{
namespace: namespace,
Dimensions: dimensions,
Metrics: make(map[string]MetricValue),
}
if md.Dimensions == nil {
md.Dimensions = make(map[string]string)
}
em.metrics = append(em.metrics, md)
return md
}
// PublishToSink writes the EmbeddedMetric info to the provided writer
func (em *EmbeddedMetric) PublishToSink(additionalProperties map[string]interface{},
sink io.Writer) {
// BEGIN - Preconditions
for _, eachDirective := range em.metrics {
// Precondition...
if len(eachDirective.Dimensions) > 9 {
fmt.Printf("DimensionSet for structured metric must not have more than 9 elements. Count: %d",
len(eachDirective.Dimensions))
}
}
// END - Preconditions
for eachKey, eachValue := range additionalProperties {
em = em.WithProperty(eachKey, eachValue)
}
rawJSON, rawJSONErr := json.Marshal(em)
var writtenErr error
if rawJSONErr == nil {
_, writtenErr = io.WriteString(sink, (string)(rawJSON))
} else {
_, writtenErr = io.WriteString(sink, fmt.Sprintf("Error publishing metric: %v", rawJSONErr))
}
if writtenErr != nil {
fmt.Printf("ERROR: %#v", writtenErr)
}
}
// Publish the metric to the logfile
func (em *EmbeddedMetric) Publish(additionalProperties map[string]interface{}) {
em.PublishToSink(additionalProperties, os.Stdout)
}
// MarshalJSON is a custom marshaller to ensure that the marshalled
// headers are always lowercase
func (em *EmbeddedMetric) MarshalJSON() ([]byte, error) {
/* From: https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Generation_CloudWatch_Agent.html
The logs must contain a log_group_name key that tells the agent which log group to use.
Each log event must be on a single line. In other words, a log event cannot contain the newline (\n) character.
*/
jsonMap := map[string]interface{}{
"log_group_name": envMap["AWS_LAMBDA_LOG_GROUP_NAME"],
"log_steam_name": envMap["AWS_LAMBDA_LOG_STREAM_NAME"],
}
for eachKey, eachValue := range em.properties {
jsonMap[eachKey] = eachValue
}
// Walk everything and create the references...
cwMetrics := &emfAWS{
Timestamp: int((time.Now().UnixNano() / int64(time.Millisecond))),
CloudWatchMetrics: []emfAWSCloudWatchMetricsElem{},
}
for _, eachDirective := range em.metrics {
metricsElem := emfAWSCloudWatchMetricsElem{
Dimensions: [][]string{},
Namespace: eachDirective.namespace,
Metrics: []emfAWSCloudWatchMetricsElemMetricsElem{},
}
// Create the references and update the metrics...
for eachKey, eachMetric := range eachDirective.Metrics {
jsonMap[eachKey] = eachMetric.Value
metricsElem.Metrics = append(metricsElem.Metrics,
emfAWSCloudWatchMetricsElemMetricsElem{
Name: eachKey,
Unit: string(eachMetric.Unit),
})
}
for eachKey, eachValue := range eachDirective.Dimensions {
jsonMap[eachKey] = eachValue
metricsElem.Dimensions = append(metricsElem.Dimensions,
[]string{eachKey})
}
cwMetrics.CloudWatchMetrics = append(cwMetrics.CloudWatchMetrics,
metricsElem)
}
jsonMap["_aws"] = cwMetrics
return json.Marshal(jsonMap)
}
// JSON encoding the fields gives us the top level keys, which we need
// to map to the Metrics...
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html#CloudWatch_Embedded_Metric_Format_Specification_structure_target
// NewEmbeddedMetric returns a new fully initialized embedded metric. Callers
// should populate the Fields
func NewEmbeddedMetric() (*EmbeddedMetric, error) {
embeddedMetric := &EmbeddedMetric{
metrics: []*MetricDirective{},
properties: make(map[string]interface{}),
}
return embeddedMetric, nil
}
// NewEmbeddedMetricWithProperties returns an EmbeddedMetric with the
// user supplied properties
func NewEmbeddedMetricWithProperties(props map[string]interface{}) (*EmbeddedMetric, error) {
embeddedMetric := &EmbeddedMetric{
metrics: []*MetricDirective{},
properties: props,
}
if embeddedMetric.properties == nil {
embeddedMetric.properties = make(map[string]interface{})
}
return embeddedMetric, nil
}