cmetrics 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/linux.yml +18 -0
- data/.github/workflows/macos.yml +18 -0
- data/.github/workflows/windows.yml +18 -0
- data/.gitignore +18 -0
- data/Gemfile +10 -0
- data/LICENSE +201 -0
- data/README.md +173 -0
- data/Rakefile +37 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/cmetrics-ruby.gemspec +34 -0
- data/ext/cmetrics/cmetrics.c +39 -0
- data/ext/cmetrics/cmetrics_c.h +62 -0
- data/ext/cmetrics/cmetrics_counter.c +664 -0
- data/ext/cmetrics/cmetrics_gauge.c +841 -0
- data/ext/cmetrics/cmetrics_serde.c +253 -0
- data/ext/cmetrics/extconf.rb +68 -0
- data/ext/cmetrics-master.tar.gz +0 -0
- data/lib/cmetrics/version.rb +5 -0
- data/lib/cmetrics.rb +5 -0
- metadata +136 -0
@@ -0,0 +1,841 @@
|
|
1
|
+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2
|
+
|
3
|
+
/* CMetrics-Ruby
|
4
|
+
* =============
|
5
|
+
* Copyright 2021 Hiroshi Hatake <hatake@calyptia.com>
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
*/
|
19
|
+
|
20
|
+
#include "cmetrics_c.h"
|
21
|
+
|
22
|
+
VALUE rb_cGauge;
|
23
|
+
|
24
|
+
static void gauge_free(void* ptr);
|
25
|
+
|
26
|
+
static const rb_data_type_t rb_cmetrics_gauge_type = { "cmetrics/gauge",
|
27
|
+
{
|
28
|
+
0,
|
29
|
+
gauge_free,
|
30
|
+
0,
|
31
|
+
},
|
32
|
+
NULL,
|
33
|
+
NULL,
|
34
|
+
RUBY_TYPED_FREE_IMMEDIATELY };
|
35
|
+
|
36
|
+
|
37
|
+
static void
|
38
|
+
gauge_free(void* ptr)
|
39
|
+
{
|
40
|
+
struct CMetricsGauge* cmetricsGauge = (struct CMetricsGauge*)ptr;
|
41
|
+
|
42
|
+
if (!cmetricsGauge) {
|
43
|
+
if (!cmetricsGauge->instance) {
|
44
|
+
cmt_destroy(cmetricsGauge->instance);
|
45
|
+
}
|
46
|
+
if (!cmetricsGauge->gauge) {
|
47
|
+
cmt_gauge_destroy(cmetricsGauge->gauge);
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
xfree(ptr);
|
52
|
+
}
|
53
|
+
|
54
|
+
static VALUE
|
55
|
+
rb_cmetrics_gauge_alloc(VALUE klass)
|
56
|
+
{
|
57
|
+
VALUE obj;
|
58
|
+
struct CMetricsGauge* cmetricsGauge;
|
59
|
+
obj = TypedData_Make_Struct(
|
60
|
+
klass, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
61
|
+
return obj;
|
62
|
+
}
|
63
|
+
|
64
|
+
/*
|
65
|
+
* Initailize Gauge class.
|
66
|
+
*
|
67
|
+
* @return [Gauge]
|
68
|
+
*
|
69
|
+
*/
|
70
|
+
static VALUE
|
71
|
+
rb_cmetrics_gauge_initialize(VALUE self)
|
72
|
+
{
|
73
|
+
struct CMetricsGauge* cmetricsGauge;
|
74
|
+
|
75
|
+
TypedData_Get_Struct(
|
76
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
77
|
+
|
78
|
+
cmt_initialize();
|
79
|
+
|
80
|
+
cmetricsGauge->instance = cmt_create();
|
81
|
+
cmetricsGauge->gauge = NULL;
|
82
|
+
|
83
|
+
return Qnil;
|
84
|
+
}
|
85
|
+
|
86
|
+
/*
|
87
|
+
* Create gauge.
|
88
|
+
*
|
89
|
+
*/
|
90
|
+
static VALUE
|
91
|
+
rb_cmetrics_gauge_create(int argc, VALUE* argv, VALUE self)
|
92
|
+
{
|
93
|
+
VALUE rb_namespace, rb_subsystem, rb_name, rb_help, rb_labels;
|
94
|
+
struct CMetricsGauge* cmetricsGauge;
|
95
|
+
char **labels = NULL;
|
96
|
+
int labels_count = 0;
|
97
|
+
VALUE tmp_label;
|
98
|
+
long i;
|
99
|
+
|
100
|
+
TypedData_Get_Struct(
|
101
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
102
|
+
|
103
|
+
rb_scan_args(argc, argv, "41", &rb_namespace, &rb_subsystem, &rb_name, &rb_help, &rb_labels);
|
104
|
+
|
105
|
+
Check_Type(rb_namespace, T_STRING);
|
106
|
+
Check_Type(rb_subsystem, T_STRING);
|
107
|
+
Check_Type(rb_name, T_STRING);
|
108
|
+
Check_Type(rb_help, T_STRING);
|
109
|
+
|
110
|
+
if (!NIL_P(rb_labels)) {
|
111
|
+
switch(TYPE(rb_labels)) {
|
112
|
+
case T_STRING:
|
113
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
114
|
+
labels_count = 1;
|
115
|
+
cmetricsGauge->gauge = cmt_gauge_create(cmetricsGauge->instance,
|
116
|
+
StringValuePtr(rb_namespace),
|
117
|
+
StringValuePtr(rb_subsystem),
|
118
|
+
StringValuePtr(rb_name),
|
119
|
+
StringValuePtr(rb_help),
|
120
|
+
labels_count, labels);
|
121
|
+
break;
|
122
|
+
case T_SYMBOL:
|
123
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
124
|
+
labels_count = 1;
|
125
|
+
cmetricsGauge->gauge = cmt_gauge_create(cmetricsGauge->instance,
|
126
|
+
StringValuePtr(rb_namespace),
|
127
|
+
StringValuePtr(rb_subsystem),
|
128
|
+
StringValuePtr(rb_name),
|
129
|
+
StringValuePtr(rb_help),
|
130
|
+
labels_count, labels);
|
131
|
+
break;
|
132
|
+
case T_ARRAY:
|
133
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
134
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
135
|
+
for (i = 0; i < labels_count; i++) {
|
136
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
137
|
+
switch(TYPE(item)) {
|
138
|
+
case T_SYMBOL:
|
139
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
140
|
+
break;
|
141
|
+
case T_STRING:
|
142
|
+
labels[i] = StringValuePtr(item);
|
143
|
+
break;
|
144
|
+
default:
|
145
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
146
|
+
}
|
147
|
+
}
|
148
|
+
cmetricsGauge->gauge = cmt_gauge_create(cmetricsGauge->instance,
|
149
|
+
StringValuePtr(rb_namespace),
|
150
|
+
StringValuePtr(rb_subsystem),
|
151
|
+
StringValuePtr(rb_name),
|
152
|
+
StringValuePtr(rb_help),
|
153
|
+
labels_count, labels);
|
154
|
+
|
155
|
+
ALLOCV_END(tmp_label);
|
156
|
+
break;
|
157
|
+
default:
|
158
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
159
|
+
}
|
160
|
+
} else {
|
161
|
+
cmetricsGauge->gauge = cmt_gauge_create(cmetricsGauge->instance,
|
162
|
+
StringValuePtr(rb_namespace),
|
163
|
+
StringValuePtr(rb_subsystem),
|
164
|
+
StringValuePtr(rb_name),
|
165
|
+
StringValuePtr(rb_help),
|
166
|
+
labels_count, labels);
|
167
|
+
}
|
168
|
+
|
169
|
+
return Qnil;
|
170
|
+
}
|
171
|
+
|
172
|
+
/*
|
173
|
+
* Just increment gauge.
|
174
|
+
*
|
175
|
+
* @return [Boolean]
|
176
|
+
*
|
177
|
+
*/
|
178
|
+
static VALUE
|
179
|
+
rb_cmetrics_gauge_increment(int argc, VALUE* argv, VALUE self)
|
180
|
+
{
|
181
|
+
VALUE rb_labels;
|
182
|
+
struct CMetricsGauge* cmetricsGauge;
|
183
|
+
uint64_t ts;
|
184
|
+
int ret = 0;
|
185
|
+
char **labels = NULL;
|
186
|
+
int labels_count = 0;
|
187
|
+
VALUE tmp_label;
|
188
|
+
long i;
|
189
|
+
|
190
|
+
TypedData_Get_Struct(
|
191
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
192
|
+
|
193
|
+
if (!cmetricsGauge->gauge) {
|
194
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
195
|
+
}
|
196
|
+
|
197
|
+
rb_scan_args(argc, argv, "01", &rb_labels);
|
198
|
+
|
199
|
+
ts = cmt_time_now();
|
200
|
+
if (!NIL_P(rb_labels)) {
|
201
|
+
switch(TYPE(rb_labels)) {
|
202
|
+
case T_STRING:
|
203
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
204
|
+
labels_count = 1;
|
205
|
+
|
206
|
+
ret = cmt_gauge_inc(cmetricsGauge->gauge, ts,
|
207
|
+
labels_count, labels);
|
208
|
+
break;
|
209
|
+
case T_SYMBOL:
|
210
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
211
|
+
labels_count = 1;
|
212
|
+
|
213
|
+
ret = cmt_gauge_inc(cmetricsGauge->gauge, ts,
|
214
|
+
labels_count, labels);
|
215
|
+
break;
|
216
|
+
case T_ARRAY:
|
217
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
218
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
219
|
+
for (i = 0; i < labels_count; i++) {
|
220
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
221
|
+
switch(TYPE(item)) {
|
222
|
+
case T_SYMBOL:
|
223
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
224
|
+
break;
|
225
|
+
case T_STRING:
|
226
|
+
labels[i] = StringValuePtr(item);
|
227
|
+
break;
|
228
|
+
default:
|
229
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
230
|
+
}
|
231
|
+
}
|
232
|
+
ret = cmt_gauge_inc(cmetricsGauge->gauge, ts,
|
233
|
+
labels_count, labels);
|
234
|
+
|
235
|
+
ALLOCV_END(tmp_label);
|
236
|
+
|
237
|
+
break;
|
238
|
+
default:
|
239
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
240
|
+
}
|
241
|
+
} else {
|
242
|
+
ret = cmt_gauge_inc(cmetricsGauge->gauge, ts,
|
243
|
+
labels_count, labels);
|
244
|
+
}
|
245
|
+
|
246
|
+
if (ret == 0) {
|
247
|
+
return Qtrue;
|
248
|
+
} else {
|
249
|
+
return Qfalse;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
|
253
|
+
/*
|
254
|
+
* Just decrement gauge.
|
255
|
+
*
|
256
|
+
* @return [Boolean]
|
257
|
+
*
|
258
|
+
*/
|
259
|
+
static VALUE
|
260
|
+
rb_cmetrics_gauge_decrement(int argc, VALUE* argv, VALUE self)
|
261
|
+
{
|
262
|
+
VALUE rb_labels;
|
263
|
+
struct CMetricsGauge* cmetricsGauge;
|
264
|
+
uint64_t ts;
|
265
|
+
int ret = 0;
|
266
|
+
char **labels = NULL;
|
267
|
+
int labels_count = 0;
|
268
|
+
VALUE tmp_label;
|
269
|
+
long i;
|
270
|
+
|
271
|
+
TypedData_Get_Struct(
|
272
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
273
|
+
|
274
|
+
if (!cmetricsGauge->gauge) {
|
275
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
276
|
+
}
|
277
|
+
|
278
|
+
rb_scan_args(argc, argv, "01", &rb_labels);
|
279
|
+
|
280
|
+
ts = cmt_time_now();
|
281
|
+
if (!NIL_P(rb_labels)) {
|
282
|
+
switch(TYPE(rb_labels)) {
|
283
|
+
case T_STRING:
|
284
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
285
|
+
labels_count = 1;
|
286
|
+
|
287
|
+
ret = cmt_gauge_dec(cmetricsGauge->gauge, ts,
|
288
|
+
labels_count, labels);
|
289
|
+
break;
|
290
|
+
case T_SYMBOL:
|
291
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
292
|
+
labels_count = 1;
|
293
|
+
|
294
|
+
ret = cmt_gauge_dec(cmetricsGauge->gauge, ts,
|
295
|
+
labels_count, labels);
|
296
|
+
break;
|
297
|
+
case T_ARRAY:
|
298
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
299
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
300
|
+
for (i = 0; i < labels_count; i++) {
|
301
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
302
|
+
switch(TYPE(item)) {
|
303
|
+
case T_SYMBOL:
|
304
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
305
|
+
break;
|
306
|
+
case T_STRING:
|
307
|
+
labels[i] = StringValuePtr(item);
|
308
|
+
break;
|
309
|
+
default:
|
310
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
311
|
+
}
|
312
|
+
}
|
313
|
+
ret = cmt_gauge_dec(cmetricsGauge->gauge, ts,
|
314
|
+
labels_count, labels);
|
315
|
+
|
316
|
+
ALLOCV_END(tmp_label);
|
317
|
+
|
318
|
+
break;
|
319
|
+
default:
|
320
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
321
|
+
}
|
322
|
+
} else {
|
323
|
+
ret = cmt_gauge_dec(cmetricsGauge->gauge, ts,
|
324
|
+
labels_count, labels);
|
325
|
+
}
|
326
|
+
|
327
|
+
if (ret == 0) {
|
328
|
+
return Qtrue;
|
329
|
+
} else {
|
330
|
+
return Qfalse;
|
331
|
+
}
|
332
|
+
}
|
333
|
+
|
334
|
+
static VALUE
|
335
|
+
rb_cmetrics_gauge_get_value(int argc, VALUE* argv, VALUE self)
|
336
|
+
{
|
337
|
+
VALUE rb_labels;
|
338
|
+
struct CMetricsGauge* cmetricsGauge;
|
339
|
+
int ret = 0;
|
340
|
+
double value = 0;
|
341
|
+
char **labels = NULL;
|
342
|
+
int labels_count = 0;
|
343
|
+
VALUE tmp_label;
|
344
|
+
long i;
|
345
|
+
|
346
|
+
TypedData_Get_Struct(
|
347
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
348
|
+
|
349
|
+
if (!cmetricsGauge->gauge) {
|
350
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
351
|
+
}
|
352
|
+
|
353
|
+
rb_scan_args(argc, argv, "01", &rb_labels);
|
354
|
+
|
355
|
+
if (!NIL_P(rb_labels)) {
|
356
|
+
switch(TYPE(rb_labels)) {
|
357
|
+
case T_STRING:
|
358
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
359
|
+
labels_count = 1;
|
360
|
+
|
361
|
+
ret = cmt_gauge_get_val(cmetricsGauge->gauge,
|
362
|
+
labels_count, labels, &value);
|
363
|
+
break;
|
364
|
+
case T_SYMBOL:
|
365
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
366
|
+
labels_count = 1;
|
367
|
+
|
368
|
+
ret = cmt_gauge_get_val(cmetricsGauge->gauge,
|
369
|
+
labels_count, labels, &value);
|
370
|
+
break;
|
371
|
+
case T_ARRAY:
|
372
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
373
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
374
|
+
for (i = 0; i < labels_count; i++) {
|
375
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
376
|
+
switch(TYPE(item)) {
|
377
|
+
case T_SYMBOL:
|
378
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
379
|
+
break;
|
380
|
+
case T_STRING:
|
381
|
+
labels[i] = StringValuePtr(item);
|
382
|
+
break;
|
383
|
+
default:
|
384
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
385
|
+
}
|
386
|
+
}
|
387
|
+
|
388
|
+
ret = cmt_gauge_get_val(cmetricsGauge->gauge,
|
389
|
+
labels_count, labels, &value);
|
390
|
+
|
391
|
+
ALLOCV_END(tmp_label);
|
392
|
+
|
393
|
+
break;
|
394
|
+
default:
|
395
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
396
|
+
}
|
397
|
+
} else {
|
398
|
+
ret = cmt_gauge_get_val(cmetricsGauge->gauge,
|
399
|
+
labels_count, labels, &value);
|
400
|
+
}
|
401
|
+
|
402
|
+
if (ret == 0) {
|
403
|
+
return DBL2NUM(value);
|
404
|
+
} else {
|
405
|
+
return Qnil;
|
406
|
+
}
|
407
|
+
}
|
408
|
+
|
409
|
+
/*
|
410
|
+
* Add value into counter.
|
411
|
+
*
|
412
|
+
* @return [Boolean]
|
413
|
+
*
|
414
|
+
*/
|
415
|
+
static VALUE
|
416
|
+
rb_cmetrics_gauge_add(int argc, VALUE* argv, VALUE self)
|
417
|
+
{
|
418
|
+
VALUE rb_labels, rb_num;
|
419
|
+
struct CMetricsGauge* cmetricsGauge;
|
420
|
+
uint64_t ts;
|
421
|
+
int ret = 0;
|
422
|
+
double value = 0;
|
423
|
+
char **labels = NULL;
|
424
|
+
int labels_count = 0;
|
425
|
+
VALUE tmp_label;
|
426
|
+
long i;
|
427
|
+
|
428
|
+
TypedData_Get_Struct(
|
429
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
430
|
+
|
431
|
+
if (!cmetricsGauge->gauge) {
|
432
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
433
|
+
}
|
434
|
+
|
435
|
+
rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);
|
436
|
+
|
437
|
+
switch(TYPE(rb_num)) {
|
438
|
+
case T_FLOAT:
|
439
|
+
case T_FIXNUM:
|
440
|
+
case T_BIGNUM:
|
441
|
+
case T_RATIONAL:
|
442
|
+
value = NUM2DBL(rb_num);
|
443
|
+
break;
|
444
|
+
default:
|
445
|
+
rb_raise(rb_eArgError, "CMetrics::Gauge#add can handle numerics values only.");
|
446
|
+
}
|
447
|
+
|
448
|
+
|
449
|
+
ts = cmt_time_now();
|
450
|
+
if (!NIL_P(rb_labels)) {
|
451
|
+
switch(TYPE(rb_labels)) {
|
452
|
+
case T_STRING:
|
453
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
454
|
+
labels_count = 1;
|
455
|
+
ret = cmt_gauge_add(cmetricsGauge->gauge, ts, value,
|
456
|
+
labels_count, labels);
|
457
|
+
break;
|
458
|
+
case T_SYMBOL:
|
459
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
460
|
+
labels_count = 1;
|
461
|
+
ret = cmt_gauge_add(cmetricsGauge->gauge, ts, value,
|
462
|
+
labels_count, labels);
|
463
|
+
break;
|
464
|
+
case T_ARRAY:
|
465
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
466
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
467
|
+
for (i = 0; i < labels_count; i++) {
|
468
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
469
|
+
switch(TYPE(item)) {
|
470
|
+
case T_SYMBOL:
|
471
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
472
|
+
break;
|
473
|
+
case T_STRING:
|
474
|
+
labels[i] = StringValuePtr(item);
|
475
|
+
break;
|
476
|
+
default:
|
477
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
478
|
+
}
|
479
|
+
}
|
480
|
+
ret = cmt_gauge_add(cmetricsGauge->gauge, ts, value,
|
481
|
+
labels_count, labels);
|
482
|
+
|
483
|
+
ALLOCV_END(tmp_label);
|
484
|
+
|
485
|
+
break;
|
486
|
+
default:
|
487
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
488
|
+
}
|
489
|
+
} else {
|
490
|
+
ret = cmt_gauge_add(cmetricsGauge->gauge, ts, value,
|
491
|
+
labels_count, labels);
|
492
|
+
}
|
493
|
+
|
494
|
+
if (ret == 0) {
|
495
|
+
return Qtrue;
|
496
|
+
} else {
|
497
|
+
return Qfalse;
|
498
|
+
}
|
499
|
+
}
|
500
|
+
|
501
|
+
/*
|
502
|
+
* Subtract value into counter.
|
503
|
+
*
|
504
|
+
* @return [Boolean]
|
505
|
+
*
|
506
|
+
*/
|
507
|
+
static VALUE
|
508
|
+
rb_cmetrics_gauge_sub(int argc, VALUE* argv, VALUE self)
|
509
|
+
{
|
510
|
+
VALUE rb_labels, rb_num;
|
511
|
+
struct CMetricsGauge* cmetricsGauge;
|
512
|
+
uint64_t ts;
|
513
|
+
int ret = 0;
|
514
|
+
double value = 0;
|
515
|
+
char **labels = NULL;
|
516
|
+
int labels_count = 0;
|
517
|
+
VALUE tmp_label;
|
518
|
+
long i;
|
519
|
+
|
520
|
+
TypedData_Get_Struct(
|
521
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
522
|
+
|
523
|
+
if (!cmetricsGauge->gauge) {
|
524
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
525
|
+
}
|
526
|
+
|
527
|
+
rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);
|
528
|
+
|
529
|
+
switch(TYPE(rb_num)) {
|
530
|
+
case T_FLOAT:
|
531
|
+
case T_FIXNUM:
|
532
|
+
case T_BIGNUM:
|
533
|
+
case T_RATIONAL:
|
534
|
+
value = NUM2DBL(rb_num);
|
535
|
+
break;
|
536
|
+
default:
|
537
|
+
rb_raise(rb_eArgError, "CMetrics::Gauge#add can handle numerics values only.");
|
538
|
+
}
|
539
|
+
|
540
|
+
|
541
|
+
ts = cmt_time_now();
|
542
|
+
if (!NIL_P(rb_labels)) {
|
543
|
+
switch(TYPE(rb_labels)) {
|
544
|
+
case T_STRING:
|
545
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
546
|
+
labels_count = 1;
|
547
|
+
ret = cmt_gauge_sub(cmetricsGauge->gauge, ts, value,
|
548
|
+
labels_count, labels);
|
549
|
+
break;
|
550
|
+
case T_SYMBOL:
|
551
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
552
|
+
labels_count = 1;
|
553
|
+
ret = cmt_gauge_sub(cmetricsGauge->gauge, ts, value,
|
554
|
+
labels_count, labels);
|
555
|
+
break;
|
556
|
+
case T_ARRAY:
|
557
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
558
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
559
|
+
for (i = 0; i < labels_count; i++) {
|
560
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
561
|
+
switch(TYPE(item)) {
|
562
|
+
case T_SYMBOL:
|
563
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
564
|
+
break;
|
565
|
+
case T_STRING:
|
566
|
+
labels[i] = StringValuePtr(item);
|
567
|
+
break;
|
568
|
+
default:
|
569
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
570
|
+
}
|
571
|
+
}
|
572
|
+
ret = cmt_gauge_sub(cmetricsGauge->gauge, ts, value,
|
573
|
+
labels_count, labels);
|
574
|
+
|
575
|
+
ALLOCV_END(tmp_label);
|
576
|
+
|
577
|
+
break;
|
578
|
+
default:
|
579
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
580
|
+
}
|
581
|
+
} else {
|
582
|
+
ret = cmt_gauge_sub(cmetricsGauge->gauge, ts, value,
|
583
|
+
labels_count, labels);
|
584
|
+
}
|
585
|
+
|
586
|
+
if (ret == 0) {
|
587
|
+
return Qtrue;
|
588
|
+
} else {
|
589
|
+
return Qfalse;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
|
593
|
+
/*
|
594
|
+
* Set value into gauge.
|
595
|
+
*
|
596
|
+
* @return [Boolean]
|
597
|
+
*
|
598
|
+
*/
|
599
|
+
static VALUE
|
600
|
+
rb_cmetrics_gauge_set(int argc, VALUE* argv, VALUE self)
|
601
|
+
{
|
602
|
+
VALUE rb_labels, rb_num;
|
603
|
+
struct CMetricsGauge* cmetricsGauge;
|
604
|
+
uint64_t ts;
|
605
|
+
int ret = 0;
|
606
|
+
double value = 0;
|
607
|
+
char **labels = NULL;
|
608
|
+
int labels_count = 0;
|
609
|
+
VALUE tmp_label;
|
610
|
+
long i;
|
611
|
+
|
612
|
+
TypedData_Get_Struct(
|
613
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
614
|
+
|
615
|
+
if (!cmetricsGauge->gauge) {
|
616
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
617
|
+
}
|
618
|
+
|
619
|
+
rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);
|
620
|
+
|
621
|
+
switch(TYPE(rb_num)) {
|
622
|
+
case T_FLOAT:
|
623
|
+
case T_FIXNUM:
|
624
|
+
case T_BIGNUM:
|
625
|
+
case T_RATIONAL:
|
626
|
+
value = NUM2DBL(rb_num);
|
627
|
+
break;
|
628
|
+
default:
|
629
|
+
rb_raise(rb_eArgError, "CMetrics::Gauge#set can handle numerics values only.");
|
630
|
+
}
|
631
|
+
|
632
|
+
ts = cmt_time_now();
|
633
|
+
if (!NIL_P(rb_labels)) {
|
634
|
+
switch(TYPE(rb_labels)) {
|
635
|
+
case T_STRING:
|
636
|
+
labels = (char *[]) { StringValuePtr(rb_labels) };
|
637
|
+
labels_count = 1;
|
638
|
+
ret = cmt_gauge_set(cmetricsGauge->gauge, ts, value,
|
639
|
+
labels_count, labels);
|
640
|
+
break;
|
641
|
+
case T_SYMBOL:
|
642
|
+
labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
|
643
|
+
labels_count = 1;
|
644
|
+
ret = cmt_gauge_set(cmetricsGauge->gauge, ts, value,
|
645
|
+
labels_count, labels);
|
646
|
+
break;
|
647
|
+
case T_ARRAY:
|
648
|
+
labels_count = (int)RARRAY_LEN(rb_labels);
|
649
|
+
labels = ALLOCV_N(char *, tmp_label, labels_count);
|
650
|
+
for (i = 0; i < labels_count; i++) {
|
651
|
+
VALUE item = RARRAY_AREF(rb_labels, i);
|
652
|
+
switch(TYPE(item)) {
|
653
|
+
case T_SYMBOL:
|
654
|
+
labels[i] = RSTRING_PTR(rb_sym2str(item));
|
655
|
+
break;
|
656
|
+
case T_STRING:
|
657
|
+
labels[i] = StringValuePtr(item);
|
658
|
+
break;
|
659
|
+
default:
|
660
|
+
rb_raise(rb_eArgError, "labels must be Symbol/String");
|
661
|
+
}
|
662
|
+
}
|
663
|
+
|
664
|
+
ret = cmt_gauge_set(cmetricsGauge->gauge, ts, value,
|
665
|
+
labels_count, labels);
|
666
|
+
|
667
|
+
ALLOCV_END(tmp_label);
|
668
|
+
|
669
|
+
break;
|
670
|
+
default:
|
671
|
+
rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
|
672
|
+
}
|
673
|
+
} else {
|
674
|
+
ret = cmt_gauge_set(cmetricsGauge->gauge, ts, value,
|
675
|
+
labels_count, labels);
|
676
|
+
}
|
677
|
+
|
678
|
+
if (ret == 0) {
|
679
|
+
return Qtrue;
|
680
|
+
} else {
|
681
|
+
return Qfalse;
|
682
|
+
}
|
683
|
+
}
|
684
|
+
|
685
|
+
/*
|
686
|
+
* Set label into gauge.
|
687
|
+
*
|
688
|
+
* @return [Boolean]
|
689
|
+
*/
|
690
|
+
|
691
|
+
static VALUE
|
692
|
+
rb_cmetrics_gauge_add_label(VALUE self, VALUE rb_key, VALUE rb_value)
|
693
|
+
{
|
694
|
+
struct CMetricsGauge* cmetricsGauge;
|
695
|
+
char *key, *value;
|
696
|
+
int ret = 0;
|
697
|
+
|
698
|
+
TypedData_Get_Struct(
|
699
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
700
|
+
|
701
|
+
if (!cmetricsGauge->gauge) {
|
702
|
+
rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
|
703
|
+
}
|
704
|
+
|
705
|
+
switch(TYPE(rb_key)) {
|
706
|
+
case T_STRING:
|
707
|
+
key = StringValuePtr(rb_key);
|
708
|
+
break;
|
709
|
+
case T_SYMBOL:
|
710
|
+
key = RSTRING_PTR(rb_sym2str(rb_key));
|
711
|
+
break;
|
712
|
+
default:
|
713
|
+
rb_raise(rb_eArgError, "key should be String or Symbol class instance.");
|
714
|
+
}
|
715
|
+
|
716
|
+
switch(TYPE(rb_value)) {
|
717
|
+
case T_STRING:
|
718
|
+
value = StringValuePtr(rb_value);
|
719
|
+
break;
|
720
|
+
case T_SYMBOL:
|
721
|
+
value = RSTRING_PTR(rb_sym2str(rb_value));
|
722
|
+
break;
|
723
|
+
default:
|
724
|
+
rb_raise(rb_eArgError, "key should be String or Symbol class instance.");
|
725
|
+
}
|
726
|
+
|
727
|
+
ret = cmt_label_add(cmetricsGauge->instance, key, value);
|
728
|
+
|
729
|
+
if (ret == 0) {
|
730
|
+
return Qtrue;
|
731
|
+
} else {
|
732
|
+
return Qfalse;
|
733
|
+
}
|
734
|
+
}
|
735
|
+
|
736
|
+
static VALUE
|
737
|
+
rb_cmetrics_gauge_to_influx(VALUE self)
|
738
|
+
{
|
739
|
+
struct CMetricsGauge* cmetricsGauge;
|
740
|
+
cmt_sds_t prom;
|
741
|
+
VALUE str;
|
742
|
+
|
743
|
+
TypedData_Get_Struct(
|
744
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
745
|
+
|
746
|
+
prom = cmt_encode_influx_create(cmetricsGauge->instance);
|
747
|
+
|
748
|
+
str = rb_str_new2(prom);
|
749
|
+
|
750
|
+
cmt_encode_influx_destroy(prom);
|
751
|
+
|
752
|
+
return str;
|
753
|
+
}
|
754
|
+
|
755
|
+
static VALUE
|
756
|
+
rb_cmetrics_gauge_to_prometheus(VALUE self)
|
757
|
+
{
|
758
|
+
struct CMetricsGauge* cmetricsGauge;
|
759
|
+
cmt_sds_t prom;
|
760
|
+
VALUE str;
|
761
|
+
|
762
|
+
TypedData_Get_Struct(
|
763
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
764
|
+
|
765
|
+
prom = cmt_encode_prometheus_create(cmetricsGauge->instance, CMT_TRUE);
|
766
|
+
|
767
|
+
str = rb_str_new2(prom);
|
768
|
+
|
769
|
+
cmt_encode_prometheus_destroy(prom);
|
770
|
+
|
771
|
+
return str;
|
772
|
+
}
|
773
|
+
|
774
|
+
static VALUE
|
775
|
+
rb_cmetrics_gauge_to_msgpack(VALUE self)
|
776
|
+
{
|
777
|
+
struct CMetricsGauge* cmetricsGauge;
|
778
|
+
char *buffer;
|
779
|
+
size_t buffer_size;
|
780
|
+
int ret = 0;
|
781
|
+
|
782
|
+
TypedData_Get_Struct(
|
783
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
784
|
+
|
785
|
+
ret = cmt_encode_msgpack_create(cmetricsGauge->instance, &buffer, &buffer_size);
|
786
|
+
|
787
|
+
if (ret == 0) {
|
788
|
+
return rb_str_new(buffer, buffer_size);
|
789
|
+
} else {
|
790
|
+
return Qnil;
|
791
|
+
}
|
792
|
+
}
|
793
|
+
|
794
|
+
static VALUE
|
795
|
+
rb_cmetrics_gauge_to_text(VALUE self)
|
796
|
+
{
|
797
|
+
struct CMetricsGauge* cmetricsGauge;
|
798
|
+
cmt_sds_t buffer;
|
799
|
+
VALUE text;
|
800
|
+
|
801
|
+
TypedData_Get_Struct(
|
802
|
+
self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
|
803
|
+
|
804
|
+
buffer = cmt_encode_text_create(cmetricsGauge->instance);
|
805
|
+
if (buffer == NULL) {
|
806
|
+
return Qnil;
|
807
|
+
}
|
808
|
+
|
809
|
+
text = rb_str_new2(buffer);
|
810
|
+
|
811
|
+
cmt_sds_destroy(buffer);
|
812
|
+
|
813
|
+
return text;
|
814
|
+
}
|
815
|
+
|
816
|
+
void Init_cmetrics_gauge(VALUE rb_mCMetrics)
|
817
|
+
{
|
818
|
+
rb_cGauge = rb_define_class_under(rb_mCMetrics, "Gauge", rb_cObject);
|
819
|
+
|
820
|
+
rb_define_alloc_func(rb_cGauge, rb_cmetrics_gauge_alloc);
|
821
|
+
|
822
|
+
rb_define_method(rb_cGauge, "initialize", rb_cmetrics_gauge_initialize, 0);
|
823
|
+
rb_define_method(rb_cGauge, "create", rb_cmetrics_gauge_create, -1);
|
824
|
+
rb_define_method(rb_cGauge, "inc", rb_cmetrics_gauge_increment, -1);
|
825
|
+
rb_define_method(rb_cGauge, "increment", rb_cmetrics_gauge_increment, -1);
|
826
|
+
rb_define_method(rb_cGauge, "dec", rb_cmetrics_gauge_decrement, -1);
|
827
|
+
rb_define_method(rb_cGauge, "decrement", rb_cmetrics_gauge_decrement, -1);
|
828
|
+
rb_define_method(rb_cGauge, "get_value", rb_cmetrics_gauge_get_value, -1);
|
829
|
+
rb_define_method(rb_cGauge, "value", rb_cmetrics_gauge_get_value, -1);
|
830
|
+
rb_define_method(rb_cGauge, "val", rb_cmetrics_gauge_get_value, -1);
|
831
|
+
rb_define_method(rb_cGauge, "add", rb_cmetrics_gauge_add, -1);
|
832
|
+
rb_define_method(rb_cGauge, "sub", rb_cmetrics_gauge_sub, -1);
|
833
|
+
rb_define_method(rb_cGauge, "set", rb_cmetrics_gauge_set, -1);
|
834
|
+
rb_define_method(rb_cGauge, "val=", rb_cmetrics_gauge_set, -1);
|
835
|
+
rb_define_method(rb_cGauge, "value=", rb_cmetrics_gauge_set, -1);
|
836
|
+
rb_define_method(rb_cGauge, "add_label", rb_cmetrics_gauge_add_label, 2);
|
837
|
+
rb_define_method(rb_cGauge, "to_influx", rb_cmetrics_gauge_to_influx, 0);
|
838
|
+
rb_define_method(rb_cGauge, "to_prometheus", rb_cmetrics_gauge_to_prometheus, 0);
|
839
|
+
rb_define_method(rb_cGauge, "to_msgpack", rb_cmetrics_gauge_to_msgpack, 0);
|
840
|
+
rb_define_method(rb_cGauge, "to_s", rb_cmetrics_gauge_to_text, 0);
|
841
|
+
}
|