This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
driver.go
110 lines (98 loc) · 3.07 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
// 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 elasticsearch
import (
"net/url"
"sync"
"time"
"github.com/golang/glog"
"gopkg.in/olivere/elastic.v3"
esCommon "k8s.io/heapster/common/elasticsearch"
"k8s.io/heapster/metrics/core"
)
const (
typeName = "k8s-heapster"
)
// SaveDataFunc is a pluggable function to enforce limits on the object
type SaveDataFunc func(esClient *elastic.Client, indexName string, typeName string, sinkData interface{}) error
type elasticSearchSink struct {
saveDataFunc SaveDataFunc
esConfig esCommon.ElasticSearchConfig
sync.RWMutex
}
type EsSinkPoint struct {
MetricsName string
MetricsValue interface{}
MetricsTimestamp time.Time
MetricsTags map[string]string
}
func (sink *elasticSearchSink) ExportData(dataBatch *core.DataBatch) {
sink.Lock()
defer sink.Unlock()
for _, metricSet := range dataBatch.MetricSets {
for metricName, metricValue := range metricSet.MetricValues {
point := EsSinkPoint{
MetricsName: metricName,
MetricsTags: metricSet.Labels,
MetricsValue: map[string]interface{}{
"value": metricValue.GetValue(),
},
MetricsTimestamp: dataBatch.Timestamp.UTC(),
}
err := sink.saveDataFunc(sink.esConfig.EsClient, sink.esConfig.Index, typeName, point)
if err != nil {
glog.V(3).Info("Failed to export data to ElasticSearch sink: ", err)
}
}
for _, metric := range metricSet.LabeledMetrics {
labels := make(map[string]string)
for k, v := range metricSet.Labels {
labels[k] = v
}
for k, v := range metric.Labels {
labels[k] = v
}
point := EsSinkPoint{
MetricsName: metric.Name,
MetricsTags: labels,
MetricsValue: map[string]interface{}{
"value": metric.GetValue(),
},
MetricsTimestamp: dataBatch.Timestamp.UTC(),
}
err := sink.saveDataFunc(sink.esConfig.EsClient, sink.esConfig.Index, typeName, point)
if err != nil {
glog.V(3).Info("Failed to export data to ElasticSearch sink: ", err)
}
}
}
}
func (sink *elasticSearchSink) Name() string {
return "ElasticSearch Sink"
}
func (sink *elasticSearchSink) Stop() {
// nothing needs to be done.
}
func NewElasticSearchSink(uri *url.URL) (core.DataSink, error) {
var esSink elasticSearchSink
elasticsearchConfig, err := esCommon.CreateElasticSearchConfig(uri)
if err != nil {
glog.V(2).Infof("Failed to config ElasticSearch")
return nil, err
}
esSink.esConfig = *elasticsearchConfig
esSink.saveDataFunc = esCommon.SaveDataIntoES
glog.V(2).Infof("ElasticSearch sink setup successfully")
return &esSink, nil
}