forked from bellycard/heka-stackdriver-output
/
stackdriver_output.go
143 lines (122 loc) · 4.23 KB
/
stackdriver_output.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
// Copyright 2014, Belly, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code based on Stackdriver custom metrics API v1.
package stackdriver
import (
"fmt"
sd "github.com/bellycard/stackdriver"
"github.com/mozilla-services/heka/message"
. "github.com/mozilla-services/heka/pipeline"
"strconv"
"strings"
)
type StackdriverCustomMetric struct {
Name string
Value string
InstanceId string
}
type StackdriverCustomMetricOutput struct {
conf *StackdriverCustomMetricOutputConfig
gwm sd.GatewayMessage
metrics map[string]StackdriverCustomMetric
or OutputRunner
}
// Stackdriver Output config struct
type StackdriverCustomMetricOutputConfig struct {
// Stackdriver API key.
ApiKey string `toml:"api_key"`
// Interval to send metrics to Stackdriver customer metrics API.
// Defaults to one minute intervals to send messages as that is the current limit from Stackdriver.
TickerInterval uint `toml:"ticker_interval"`
// Set of metric templates this output should use, keyed by field name.
Metric map[string]StackdriverCustomMetric
}
func (so *StackdriverCustomMetricOutput) ConfigStruct() interface{} {
return &StackdriverCustomMetricOutputConfig{
TickerInterval: uint(60),
}
}
func (so *StackdriverCustomMetricOutput) Init(config interface{}) (err error) {
so.conf = config.(*StackdriverCustomMetricOutputConfig)
// Ensure Stackdriver API key value is set from TOML configuration.
if so.conf.ApiKey == "" {
return fmt.Errorf("api_key must contain a Stackdriver API key.")
}
// Populate StackdriverCustomMetricOutput config with values from TOML configuration.
so.metrics = so.conf.Metric
so.gwm = sd.NewGatewayMessage()
return
}
// FomatUnixNano converts a Unix nanosecond-precision timestamp to a Unix seconds-precision timestamp.
func FormatUnixNano(t int64) int64 {
return int64(t / 1000000000)
}
func (so *StackdriverCustomMetricOutput) Run(or OutputRunner, h PluginHelper) (err error) {
inChan := or.InChan()
ticker := or.Ticker()
var (
pack *PipelinePack
values = make(map[string]string)
tmpMetric sd.Metric
ok = true
)
so.or = or
for ok {
select {
case pack, ok = <-inChan:
if !ok {
break
}
for _, field := range pack.Message.Fields {
if field.GetValueType() == message.Field_STRING && len(field.ValueString) > 0 {
values[field.GetName()] = field.ValueString[0]
}
if field.GetValueType() == message.Field_INTEGER {
values[field.GetName()] = strconv.FormatInt(field.ValueInteger[0], 10)
}
if field.GetValueType() == message.Field_DOUBLE {
values[field.GetName()] = strconv.FormatFloat(field.ValueDouble[0], 'f', -1, 64)
}
}
for _, met := range so.metrics {
tmpMetric.Name = InterpolateString(met.Name, values)
if met.InstanceId != "" {
tmpMetric.InstanceId = InterpolateString(met.InstanceId, values)
}
tmpMetric.Value = InterpolateString(met.Value, values)
if strings.Contains(tmpMetric.Value.(string), ".") {
val, _ := strconv.ParseFloat(tmpMetric.Value.(string), 64)
so.gwm.CustomMetric(tmpMetric.Name, tmpMetric.InstanceId, FormatUnixNano(pack.Message.GetTimestamp()), val)
} else {
val, _ := strconv.ParseInt(tmpMetric.Value.(string), 10, 64)
so.gwm.CustomMetric(tmpMetric.Name, tmpMetric.InstanceId, FormatUnixNano(pack.Message.GetTimestamp()), val)
}
}
pack.Recycle()
case <-ticker:
client := sd.NewStackdriverClient(so.conf.ApiKey)
err = client.Send(so.gwm)
if err != nil {
so.or.LogMessage(fmt.Sprintf("[StackdriverCustomMetricOutput] API submission fail: %s\n", err))
}
so.gwm = sd.NewGatewayMessage()
}
}
return
}
func init() {
RegisterPlugin("StackdriverCustomMetricOutput", func() interface{} {
return new(StackdriverCustomMetricOutput)
})
}