This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 8
/
driver.go
213 lines (189 loc) · 5.11 KB
/
driver.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
// Copyright 2014 Google Inc. All Rights Reserved.
//
// 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.
package riemann
import (
"net/url"
"reflect"
"runtime"
"strconv"
"sync"
"time"
riemann_api "github.com/bigdatadev/goryman"
"github.com/golang/glog"
"k8s.io/heapster/metrics/core"
)
// Abstracted for testing: this package works against any client that obeys the
// interface contract exposed by the goryman Riemann client
type riemannClient interface {
Connect() error
Close() error
SendEvent(e *riemann_api.Event) error
}
type riemannSink struct {
client riemannClient
config riemannConfig
sync.RWMutex
}
type riemannConfig struct {
host string
ttl float32
state string
tags []string
}
const (
// Maximum number of riemann Events to be sent in one batch.
maxSendBatchSize = 10000
max_retries = 2
)
func CreateRiemannSink(uri *url.URL) (core.DataSink, error) {
c := riemannConfig{
host: "riemann-heapster:5555",
ttl: 60.0,
state: "",
tags: make([]string, 0),
}
if len(uri.Host) > 0 {
c.host = uri.Host
}
options := uri.Query()
if len(options["ttl"]) > 0 {
var ttl, err = strconv.ParseFloat(options["ttl"][0], 32)
if err != nil {
return nil, err
}
c.ttl = float32(ttl)
}
if len(options["state"]) > 0 {
c.state = options["state"][0]
}
if len(options["tags"]) > 0 {
c.tags = options["tags"]
}
glog.Infof("Riemann sink URI: '%+v', host: '%+v', options: '%+v', ", uri, c.host, options)
rs := &riemannSink{
client: nil,
config: c,
}
err := rs.setupRiemannClient()
if err != nil {
glog.Warningf("Riemann sink not connected: %v", err)
// Warn but return the sink.
}
return rs, nil
}
func (rs *riemannSink) setupRiemannClient() error {
client := riemann_api.NewGorymanClient(rs.config.host)
runtime.SetFinalizer(client, func(c riemannClient) { c.Close() })
err := client.Connect()
if err != nil {
return err
}
rs.client = client
return nil
}
// Return a user-friendly string describing the sink
func (sink *riemannSink) Name() string {
return "Riemann Sink"
}
func (sink *riemannSink) Stop() {
// nothing needs to be done.
}
// ExportData Send a collection of Timeseries to Riemann
func (sink *riemannSink) ExportData(dataBatch *core.DataBatch) {
sink.Lock()
defer sink.Unlock()
if sink.client == nil {
if err := sink.setupRiemannClient(); err != nil {
glog.Warningf("Riemann sink not connected: %v", err)
return
}
}
var dataEvents []riemann_api.Event
appendMetric := func(host, name string, value interface{}, labels map[string]string) {
event := riemann_api.Event{
Time: dataBatch.Timestamp.Unix(),
Service: name,
Host: host,
Description: "", //no description - waste of bandwidth.
Attributes: labels,
Metric: value,
Ttl: sink.config.ttl,
State: sink.config.state,
Tags: sink.config.tags,
}
dataEvents = append(dataEvents, event)
if len(dataEvents) >= maxSendBatchSize {
sink.sendData(dataEvents)
dataEvents = nil
}
}
riemannValue := func(value interface{}) interface{} {
// Workaround for error from goryman: "Metric of invalid type (type int64)"
if reflect.TypeOf(value).Kind() == reflect.Int64 {
return int(value.(int64))
}
return value
}
for _, metricSet := range dataBatch.MetricSets {
host := metricSet.Labels[core.LabelHostname.Key]
for metricName, metricValue := range metricSet.MetricValues {
if value := metricValue.GetValue(); value != nil {
appendMetric(host, metricName, riemannValue(value), metricSet.Labels)
}
}
for _, metric := range metricSet.LabeledMetrics {
if value := metric.GetValue(); value != nil {
labels := make(map[string]string)
for k, v := range metricSet.Labels {
labels[k] = v
}
for k, v := range metric.Labels {
labels[k] = v
}
appendMetric(host, metric.Name, riemannValue(value), labels)
}
}
}
if len(dataEvents) > 0 {
sink.sendData(dataEvents)
}
}
func (sink *riemannSink) sendData(dataEvents []riemann_api.Event) {
if sink.client == nil {
return
}
start := time.Now()
errors := 0
for _, event := range dataEvents {
glog.V(8).Infof("Sending event to Riemann: %+v", event)
var err error
for try := 0; try < max_retries; try++ {
err = sink.client.SendEvent(&event)
if err == nil {
break
}
}
if err != nil {
errors++
glog.V(4).Infof("Failed to send event to Riemann: %+v: %+v", event, err)
}
}
end := time.Now()
if errors > 0 {
glog.V(2).Info("There were errors sending events to Riemman, forcing reconnection")
sink.client.Close()
sink.client = nil
}
glog.V(4).Infof("Exported %d events to riemann in %s", len(dataEvents)-errors, end.Sub(start))
}