-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmetrics.go
314 lines (275 loc) · 8.07 KB
/
cmetrics.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//go:build linux
package cmetrics
/*
#cgo LDFLAGS: -L/usr/local/lib -lcmetrics -lcfl -lmpack -lxxhash
#cgo CFLAGS: -I/usr/local/include/ -w
#include <cmetrics/cmetrics.h>
#include <cmetrics/cmt_gauge.h>
#include <cmetrics/cmt_encode_prometheus.h>
#include <cmetrics/cmt_encode_msgpack.h>
#include <cmetrics/cmt_decode_msgpack.h>
#include <cmetrics/cmt_encode_text.h>
#include <cmetrics/cmt_encode_influx.h>
#include <cmetrics/cmt_counter.h>
*/
import "C"
import (
"errors"
"time"
"unsafe"
)
type Context struct {
context *C.struct_cmt
}
type Gauge struct {
gauge *C.struct_cmt_gauge
}
type Counter struct {
counter *C.struct_cmt_counter
}
func GoStringArrayToCptr(arr []string) **C.char {
size := C.size_t(unsafe.Sizeof((*C.char)(nil)))
length := C.size_t(len(arr))
ptr := C.malloc(length * size)
for i := 0; i < len(arr); i++ {
element := (**C.char)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*unsafe.Sizeof((*C.char)(nil))))
*element = C.CString(arr[i])
}
return (**C.char)(ptr)
}
func (g *Gauge) Add(ts time.Time, value float64, labels []string) error {
ret := C.cmt_gauge_add(g.gauge, C.ulong(ts.UnixNano()), C.double(value), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot substract gauge value")
}
return nil
}
func (g *Gauge) Inc(ts time.Time, labels []string) error {
ret := C.cmt_gauge_inc(g.gauge, C.ulong(ts.UnixNano()), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot increment gauge value")
}
return nil
}
func (g *Gauge) Dec(ts time.Time, labels []string) error {
ret := C.cmt_gauge_dec(g.gauge, C.ulong(ts.UnixNano()), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot decrement gauge value")
}
return nil
}
func (g *Gauge) Sub(ts time.Time, value float64, labels []string) error {
ret := C.cmt_gauge_sub(g.gauge, C.ulong(ts.UnixNano()), C.double(value), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot subtract gauge value")
}
return nil
}
func (g *Gauge) GetVal(labels []string) (float64, error) {
var value C.double
ret := C.cmt_gauge_get_val(
g.gauge,
C.int(len(labels)),
GoStringArrayToCptr(labels),
&value)
if ret != 0 {
return -1, errors.New("cannot get value for gauge")
}
return float64(value), nil
}
func (g *Gauge) Set(ts time.Time, value float64, labels []string) error {
ret := C.cmt_gauge_set(g.gauge, C.ulong(ts.UnixNano()), C.double(value), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot set gauge value")
}
return nil
}
func (ctx *Context) LabelAdd(key, val string) error {
ret := C.cmt_labels_add_kv(ctx.context.static_labels, C.CString(key), C.CString(val))
if ret != 0 {
return errors.New("cannot set label to context")
}
return nil
}
func (ctx *Context) EncodePrometheus() (string, error) {
ret := C.cmt_encode_prometheus_create(ctx.context, 1)
if ret == nil {
return "", errors.New("error encoding to prometheus format")
}
return C.GoString(ret), nil
}
func (ctx *Context) EncodeText() (string, error) {
buffer := C.cmt_encode_text_create(ctx.context)
if buffer == nil {
return "", errors.New("error encoding to text format")
}
var text string = C.GoString(buffer)
C.cmt_encode_text_destroy(buffer)
return text, nil
}
func (ctx *Context) EncodeInflux() (string, error) {
buffer := C.cmt_encode_influx_create(ctx.context)
if buffer == nil {
return "", errors.New("error encoding to text format")
}
var text string = C.GoString(buffer)
C.cmt_encode_influx_destroy(buffer)
return text, nil
}
func NewContextSetFromMsgPack(msgPackBuffer []byte, offset int) ([]*Context, error) {
var cBuffer = (*C.char)(unsafe.Pointer(&msgPackBuffer[0]))
var ctxSet []*Context
var ret C.int
for ret == C.CMT_DECODE_MSGPACK_SUCCESS {
ct, err := NewContext()
if err != nil {
return nil, err
}
ret = C.cmt_decode_msgpack_create(&ct.context, cBuffer, C.ulong(len(msgPackBuffer)), (*C.ulong)(unsafe.Pointer(&offset)))
if ret == C.CMT_DECODE_MSGPACK_INSUFFICIENT_DATA {
break
}
ctxSet = append(ctxSet, ct)
}
if len(ctxSet) == 0 {
return nil, errors.New("error decoding metric context(s)")
}
return ctxSet, nil
}
func NewContextFromMsgPack(msgPackBuffer []byte, offset int64) (*Context, error) {
var cBuffer *C.char
var cOffset *C.ulong
ct, err := NewContext()
if err != nil {
return nil, err
}
cBuffer = (*C.char)(unsafe.Pointer(&msgPackBuffer[0]))
cOffset = (*C.ulong)(unsafe.Pointer(&offset))
ret := C.cmt_decode_msgpack_create(&ct.context, cBuffer, C.ulong(len(msgPackBuffer)), cOffset)
if ret != 0 {
return nil, errors.New("error decoding msgpack")
}
return ct, nil
}
type EncoderType string
const (
MsgPackEncoder EncoderType = "MsgPackEncoder"
PrometheusEncoder EncoderType = "PrometheusEncoder"
InfluxEncoder EncoderType = "InfluxEncoder"
TextEncoder EncoderType = "TextEncoder"
)
func (ctx *Context) Encode(t EncoderType) (interface{}, error) {
switch t {
case MsgPackEncoder:
{
return ctx.EncodeMsgPack()
}
case PrometheusEncoder:
{
return ctx.EncodePrometheus()
}
case InfluxEncoder:
{
return ctx.EncodeInflux()
}
case TextEncoder:
{
return ctx.EncodeText()
}
}
return nil, errors.New(string("not found encoder suiteable for type " + t))
}
func (ctx *Context) EncodeMsgPack() ([]byte, error) {
var buffer *C.char
var bufferSize C.size_t
ret := C.cmt_encode_msgpack_create(ctx.context, &buffer, &bufferSize)
if ret != 0 {
return nil, errors.New("error encoding to msgpack format")
}
return C.GoBytes(unsafe.Pointer(buffer), C.int(bufferSize)), nil
}
func (ctx *Context) GaugeCreate(namespace, subsystem, name, help string, labelKeys []string) (*Gauge, error) {
gauge := C.cmt_gauge_create(ctx.context,
C.CString(namespace),
C.CString(subsystem),
C.CString(name),
C.CString(help),
C.int(len(labelKeys)),
GoStringArrayToCptr(labelKeys),
)
if gauge == nil {
return nil, errors.New("cannot create gauge")
}
return &Gauge{gauge}, nil
}
func (g *Counter) Add(ts time.Time, value float64, labels []string) error {
ret := C.cmt_counter_add(g.counter, C.ulong(ts.UnixNano()), C.double(value), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot add counter value")
}
return nil
}
func (g *Counter) Inc(ts time.Time, labels []string) error {
ret := C.cmt_counter_inc(g.counter, C.ulong(ts.UnixNano()), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot Inc counter value")
}
return nil
}
func (g *Counter) GetVal(labels []string) (float64, error) {
var value C.double
ret := C.cmt_counter_get_val(
g.counter,
C.int(len(labels)),
GoStringArrayToCptr(labels),
&value)
if ret != 0 {
return -1, errors.New("cannot get value for counter")
}
return float64(value), nil
}
func (g *Counter) Set(ts time.Time, value float64, labels []string) error {
ret := C.cmt_counter_set(g.counter, C.ulong(ts.UnixNano()), C.double(value), C.int(len(labels)), GoStringArrayToCptr(labels))
if ret != 0 {
return errors.New("cannot set counter value")
}
return nil
}
func (ctx *Context) CounterCreate(namespace, subsystem, name, help string, labelKeys []string) (*Counter, error) {
counter := C.cmt_counter_create(ctx.context,
C.CString(namespace),
C.CString(subsystem),
C.CString(name),
C.CString(help),
C.int(len(labelKeys)),
GoStringArrayToCptr(labelKeys),
)
if counter == nil {
return nil, errors.New("cannot create counter")
}
return &Counter{counter}, nil
}
func (ctx *Context) Destroy() {
C.cmt_destroy(ctx.context)
}
func NewContext() (*Context, error) {
cmt := C.cmt_create()
if cmt == nil {
return nil, errors.New("cannot create cmt context")
}
return &Context{context: cmt}, nil
}
func NewRawCMTPointer() (unsafe.Pointer, error) {
cmt := C.cmt_create()
if cmt == nil {
return nil, errors.New("cannot create cmt context")
}
return unsafe.Pointer(cmt), nil
}
func NewContextFromCMTPointer(cmt unsafe.Pointer) (*Context, error) {
if cmt == nil {
return nil, errors.New("cannot create Context from nil")
}
ctx := (*C.struct_cmt)(cmt)
return &Context{context: ctx}, nil
}