This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
opentsdb.go
170 lines (142 loc) · 4.5 KB
/
opentsdb.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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 opentsdb
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/control/plugin/cpolicy"
"github.com/intelsdi-x/snap/core"
"github.com/intelsdi-x/snap/core/ctypes"
)
const (
name = "opentsdb"
version = 9
pluginType = plugin.PublisherPluginType
timeout = 5
host = "host"
source = "source"
)
// Meta returns a plugin meta data
func Meta() *plugin.PluginMeta {
return plugin.NewPluginMeta(name, version, pluginType, []string{plugin.SnapGOBContentType}, []string{plugin.SnapGOBContentType})
}
//NewOpentsdbPublisher returns an instance of the OpenTSDB publisher
func NewOpentsdbPublisher() *opentsdbPublisher {
return &opentsdbPublisher{}
}
type opentsdbPublisher struct {
}
func (p *opentsdbPublisher) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
cp := cpolicy.New()
config := cpolicy.NewPolicyNode()
r1, err := cpolicy.NewStringRule("host", true)
handleErr(err)
r1.Description = "Opentsdb host"
config.Add(r1)
r2, err := cpolicy.NewIntegerRule("port", true)
handleErr(err)
r2.Description = "Opentsdb port"
config.Add(r2)
cp.Add([]string{""}, config)
return cp, nil
}
// Publish publishes metric data to opentsdb.
func (p *opentsdbPublisher) Publish(contentType string, content []byte, config map[string]ctypes.ConfigValue) error {
logger := log.New()
var metrics []plugin.MetricType
switch contentType {
case plugin.SnapGOBContentType:
dec := gob.NewDecoder(bytes.NewBuffer(content))
if err := dec.Decode(&metrics); err != nil {
logger.Printf("Error decoding GOB: error=%v content=%v", err, content)
return err
}
case plugin.SnapJSONContentType:
err := json.Unmarshal(content, &metrics)
if err != nil {
logger.Printf("Error decoding JSON: error=%v content=%v", err, content)
return err
}
default:
logger.Printf("Error unknown content type '%v'", contentType)
return fmt.Errorf("Unknown content type '%s'", contentType)
}
u, err := url.Parse(fmt.Sprintf("%s:%d", config["host"].(ctypes.ConfigValueStr).Value, config["port"].(ctypes.ConfigValueInt).Value))
if err != nil {
handleErr(err)
}
var pts []DataPoint
var temp DataPoint
var i = 0
for _, m := range metrics {
tempTags := make(map[string]StringValue)
isDynamic, indexes := m.Namespace().IsDynamic()
ns := m.Namespace().Strings()
if isDynamic {
for i, j := range indexes {
// The second return value from IsDynamic(), in this case `indexes`, is the index of
// the dynamic element in the unmodified namespace. However, here we're deleting
// elements, which is problematic when the number of dynamic elements in a namespace is
// greater than 1. Therefore, we subtract i (the loop iteration) from j
// (the original index) to compensate.
//
// Remove "data" from the namespace and create a tag for it
ns = append(ns[:j-i], ns[j-i+1:]...)
tempTags[m.Namespace()[j].Name] = StringValue(m.Namespace()[j].Value)
}
}
tags := m.Tags()
for k, v := range tags {
tempTags[k] = StringValue(v)
}
tempTags[host] = StringValue(tags[core.STD_TAG_PLUGIN_RUNNING_ON])
temp = DataPoint{
Metric: StringValue(strings.Join(ns, ".")),
Timestamp: m.Timestamp().Unix(),
Value: m.Data(),
Tags: tempTags,
}
// Omits invalid data points
if temp.Valid() {
pts = append(pts, temp)
i++
} else {
logger.Printf("Omitted invalid data point %s (non-numeric values not allowed in OpenTSDB)", temp.Metric)
}
}
if len(pts) == 0 {
logger.Printf("Info: '%s' posting metrics: %+v", "no valid data", metrics)
return nil
}
td := time.Duration(timeout * time.Second)
con := NewClient(u.String(), td)
err = con.Save(pts)
if err != nil {
logger.Printf("Error: '%s' posting metrics: %+v", err.Error(), metrics)
return err
}
return nil
}
func handleErr(e error) {
if e != nil {
panic(e)
}
}