forked from skytap/heapster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external.go
164 lines (145 loc) · 5.15 KB
/
external.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
// Copyright 2015 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 sinks
import (
"flag"
"fmt"
sink_api "github.com/skytap/heapster/sinks/api"
"github.com/skytap/heapster/sinks/gcm"
"github.com/skytap/heapster/sinks/influxdb"
"github.com/skytap/heapster/sinks/rrdcached"
source_api "github.com/skytap/heapster/sources/api"
)
var (
argSink = flag.String("sink", "memory", "Backend storage. Options are [memory | influxdb | rrdcached | bigquery | gcm ]")
// TODO: Take in auth via some other secure mechanism.
// influxdb
argDbUsername = flag.String("sink_influxdb_username", "root", "InfluxDB username")
argDbPassword = flag.String("sink_influxdb_password", "root", "InfluxDB password")
argDbHost = flag.String("sink_influxdb_host", "localhost:8086", "InfluxDB host:port")
argDbName = flag.String("sink_influxdb_name", "k8s", "Influxdb database name")
argAvoidColumns = flag.Bool("sink_influxdb_no_columns", false, "When true, prefixes metric series names with metadata instead of storing metadata in additional columns. Metadata includes hostname, container name, etc. ")
// rrdcached
argRrdHost = flag.String("sink_rrdcached_host", "localhost:50060", "RRDCacheD host:port")
argRemapKeys = flag.String("sink_rrdcached_remap_keys", "", "Comma-separated key remappings. Remap to empty to omit a metric. eg 'network/rx=network_rx,network/rx_errors='.")
)
type externalSinkManager struct {
decoder sink_api.Decoder
externalSinks []sink_api.ExternalSink
}
func newExternalSinkManager(externalSinks []sink_api.ExternalSink) (ExternalSinkManager, error) {
// Get supported metrics.
supportedMetrics := sink_api.SupportedStatMetrics()
for i := range supportedMetrics {
supportedMetrics[i].Labels = sink_api.SupportedLabels()
}
// Create the metrics.
descriptors := make([]sink_api.MetricDescriptor, 0, len(supportedMetrics))
for _, supported := range supportedMetrics {
descriptors = append(descriptors, supported.MetricDescriptor)
}
for _, externalSink := range externalSinks {
err := externalSink.Register(descriptors)
if err != nil {
return nil, err
}
}
decoder := sink_api.NewDecoder()
return &externalSinkManager{
externalSinks: externalSinks,
decoder: decoder,
}, nil
}
// TODO(vmarmol): Paralellize this.
func (self *externalSinkManager) Store(input interface{}) error {
data, ok := input.(source_api.AggregateData)
if !ok {
return fmt.Errorf("unknown input type %T", input)
}
timeseries, err := self.decoder.Timeseries(data)
if err != nil {
return err
}
// Format metrics and push them.
var errors []error
for _, externalSink := range self.externalSinks {
if err := externalSink.StoreTimeseries(timeseries); err != nil {
errors = append(errors, err)
}
}
err = nil
if len(errors) > 0 {
errStr := ""
for _, err := range errors {
errStr = fmt.Sprintf("%v ", err)
}
err = fmt.Errorf("encountered the following errors: %s", errStr)
}
return err
}
func (self *externalSinkManager) DebugInfo() string {
desc := "External Sinks\n"
// Add metrics being exported.
desc += "\tExported metrics:"
for _, supported := range sink_api.SupportedStatMetrics() {
desc += fmt.Sprintf("\t\t%s: %s", supported.Name, supported.Description)
}
// Add labels being used.
desc += "\tExported labels:"
for _, label := range sink_api.SupportedLabels() {
desc += fmt.Sprintf("\t\t%s: %s", label.Key, label.Description)
}
desc += "\n\tExternal Sinks:"
for _, externalSink := range self.externalSinks {
desc += fmt.Sprintf("\n\t\t%s", externalSink.DebugInfo())
}
return desc
}
func NewSink() (ExternalSinkManager, error) {
switch *argSink {
case "memory":
return NewMemorySink(), nil
case "influxdb":
if *argDbHost == "" {
return nil, fmt.Errorf("flag '-sink_influxdb_host' invalid")
}
if *argDbName == "" {
return nil, fmt.Errorf("flag '-sink_influxdb_name' invalid")
}
externalSink, err := influxdb.NewSink(*argDbHost, *argDbUsername, *argDbPassword, *argDbName, *argAvoidColumns)
if err != nil {
return nil, err
}
return newExternalSinkManager([]sink_api.ExternalSink{externalSink})
case "rrdcached":
if *argRrdHost == "" {
return nil, fmt.Errorf("flag '-sink_rrdcached_host' invalid")
}
externalSink, err := rrdcached.NewSink(*argRrdHost, *argRemapKeys)
if err != nil {
return nil, err
}
return newExternalSinkManager([]sink_api.ExternalSink{externalSink})
case "gcm":
externalSink, err := gcm.NewSink()
if err != nil {
return nil, err
}
return newExternalSinkManager([]sink_api.ExternalSink{externalSink})
case "bigquery":
return NewBigQuerySink()
default:
return nil, fmt.Errorf("invalid sink specified - %s", *argSink)
}
}