Class: CMetrics::Counter

Inherits:
Object
  • Object
show all
Defined in:
ext/cmetrics/cmetrics_counter.c

Instance Method Summary collapse

Constructor Details

#initializeCounter

Initailize Counter class.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'ext/cmetrics/cmetrics_counter.c', line 86

static VALUE
rb_cmetrics_counter_initialize(VALUE self)
{
    struct CMetricsCounter* cmetricsCounter;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    cmt_initialize();

    cmetricsCounter->instance = cmt_create();
    cmetricsCounter->counter = NULL;

    return Qnil;
}

Instance Method Details

#add(*args) ⇒ Boolean

Add value into counter.

Returns:

  • (Boolean)


350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'ext/cmetrics/cmetrics_counter.c', line 350

static VALUE
rb_cmetrics_counter_add(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels, rb_num;
    struct CMetricsCounter* cmetricsCounter;
    uint64_t ts;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);

    switch(TYPE(rb_num)) {
    case T_FLOAT:
    case T_FIXNUM:
    case T_BIGNUM:
    case T_RATIONAL:
        value = NUM2DBL(rb_num);
        break;
    default:
        rb_raise(rb_eArgError, "CMetrics::Counter#add can handle numerics values only.");
    }


    ts = cmt_time_now();
    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;
            ret = cmt_counter_add(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;
            ret = cmt_counter_add(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }
            ret = cmt_counter_add(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_add(cmetricsCounter->counter, ts, value,
                              labels_count, labels);
    }

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#add_label(rb_key, rb_value) ⇒ Boolean

Set label into counter.

Returns:

  • (Boolean)


534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'ext/cmetrics/cmetrics_counter.c', line 534

static VALUE
rb_cmetrics_counter_add_label(VALUE self, VALUE rb_key, VALUE rb_value)
{
    struct CMetricsCounter* cmetricsCounter;
    char *key, *value;
    int ret = 0;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    switch(TYPE(rb_key)) {
    case T_STRING:
        key = StringValuePtr(rb_key);
        break;
    case T_SYMBOL:
        key = RSTRING_PTR(rb_sym2str(rb_key));
        break;
    default:
        rb_raise(rb_eArgError, "key should be String or Symbol class instance.");
    }

    switch(TYPE(rb_value)) {
    case T_STRING:
        value = StringValuePtr(rb_value);
        break;
    case T_SYMBOL:
        value = RSTRING_PTR(rb_sym2str(rb_value));
        break;
    default:
        rb_raise(rb_eArgError, "value should be String or Symbol class instance.");
    }
    ret = cmt_label_add(cmetricsCounter->instance, key, value);

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#create(*args) ⇒ Object

Create counter.



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
# File 'ext/cmetrics/cmetrics_counter.c', line 106

static VALUE
rb_cmetrics_counter_create(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_namespace, rb_subsystem, rb_name, rb_help, rb_labels;
    struct CMetricsCounter* cmetricsCounter;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    rb_scan_args(argc, argv, "41", &rb_namespace, &rb_subsystem, &rb_name, &rb_help, &rb_labels);

    Check_Type(rb_namespace, T_STRING);
    Check_Type(rb_subsystem, T_STRING);
    Check_Type(rb_name, T_STRING);
    Check_Type(rb_help, T_STRING);

    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;
            cmetricsCounter->counter = cmt_counter_create(cmetricsCounter->instance,
                                                          StringValuePtr(rb_namespace),
                                                          StringValuePtr(rb_subsystem),
                                                          StringValuePtr(rb_name),
                                                          StringValuePtr(rb_help),
                                                          labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;
            cmetricsCounter->counter = cmt_counter_create(cmetricsCounter->instance,
                                                          StringValuePtr(rb_namespace),
                                                          StringValuePtr(rb_subsystem),
                                                          StringValuePtr(rb_name),
                                                          StringValuePtr(rb_help),
                                                          labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }
            cmetricsCounter->counter = cmt_counter_create(cmetricsCounter->instance,
                                                          StringValuePtr(rb_namespace),
                                                          StringValuePtr(rb_subsystem),
                                                          StringValuePtr(rb_name),
                                                          StringValuePtr(rb_help),
                                                          labels_count, labels);

            ALLOCV_END(tmp_label);
            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        cmetricsCounter->counter = cmt_counter_create(cmetricsCounter->instance,
                                                      StringValuePtr(rb_namespace),
                                                      StringValuePtr(rb_subsystem),
                                                      StringValuePtr(rb_name),
                                                      StringValuePtr(rb_help),
                                                      labels_count, labels);
    }

    return Qnil;
}

#get_value(*args) ⇒ Object



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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/cmetrics/cmetrics_counter.c', line 269

static VALUE
rb_cmetrics_counter_get_value(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels;
    struct CMetricsCounter* cmetricsCounter;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "01", &rb_labels);

    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_get_val(cmetricsCounter->counter,
                                  labels_count, labels, &value);
    }

    if (ret == 0) {
        return DBL2NUM(value);
    } else {
        return Qnil;
    }
}

#inc(*args) ⇒ Boolean

Just increment counter.

Returns:

  • (Boolean)


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
# File 'ext/cmetrics/cmetrics_counter.c', line 194

static VALUE
rb_cmetrics_counter_increment(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels;
    struct CMetricsCounter* cmetricsCounter;
    uint64_t ts;
    int ret = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "01", &rb_labels);

    ts = cmt_time_now();
    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;

            ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                                  labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;

            ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                                  labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }
            ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                                  labels_count, labels);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                              labels_count, labels);
    }

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#increment(*args) ⇒ Boolean

Just increment counter.

Returns:

  • (Boolean)


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
# File 'ext/cmetrics/cmetrics_counter.c', line 194

static VALUE
rb_cmetrics_counter_increment(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels;
    struct CMetricsCounter* cmetricsCounter;
    uint64_t ts;
    int ret = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "01", &rb_labels);

    ts = cmt_time_now();
    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;

            ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                                  labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;

            ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                                  labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }
            ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                                  labels_count, labels);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_inc(cmetricsCounter->counter, ts,
                              labels_count, labels);
    }

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#set(*args) ⇒ Boolean

Set value into counter.

Returns:

  • (Boolean)


442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/cmetrics/cmetrics_counter.c', line 442

static VALUE
rb_cmetrics_counter_set(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels, rb_num;
    struct CMetricsCounter* cmetricsCounter;
    uint64_t ts;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);

    switch(TYPE(rb_num)) {
    case T_FLOAT:
    case T_FIXNUM:
    case T_BIGNUM:
    case T_RATIONAL:
        value = NUM2DBL(rb_num);
        break;
    default:
        rb_raise(rb_eArgError, "CMetrics::Counter#set can handle numerics values only.");
    }

    ts = cmt_time_now();
    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;
            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;
            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }

            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                              labels_count, labels);
    }

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#to_influxObject



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'ext/cmetrics/cmetrics_counter.c', line 597

static VALUE
rb_cmetrics_counter_to_influx(VALUE self)
{
    struct CMetricsCounter* cmetricsCounter;
    cmt_sds_t prom;
    VALUE str;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    prom = cmt_encode_influx_create(cmetricsCounter->instance);

    str = rb_str_new2(prom);

    cmt_encode_influx_destroy(prom);

    return str;
}

#to_msgpackObject



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'ext/cmetrics/cmetrics_counter.c', line 616

static VALUE
rb_cmetrics_counter_to_msgpack(VALUE self)
{
    struct CMetricsCounter* cmetricsCounter;
    char *buffer;
    size_t buffer_size;
    int ret = 0;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    ret = cmt_encode_msgpack_create(cmetricsCounter->instance, &buffer, &buffer_size);

    if (ret == 0) {
        return rb_str_new(buffer, buffer_size);
    } else {
        return Qnil;
    }
}

#to_prometheusObject



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'ext/cmetrics/cmetrics_counter.c', line 578

static VALUE
rb_cmetrics_counter_to_prometheus(VALUE self)
{
    struct CMetricsCounter* cmetricsCounter;
    cmt_sds_t prom;
    VALUE str;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    prom = cmt_encode_prometheus_create(cmetricsCounter->instance, CMT_TRUE);

    str = rb_str_new2(prom);

    cmt_encode_prometheus_destroy(prom);

    return str;
}

#to_sObject



636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'ext/cmetrics/cmetrics_counter.c', line 636

static VALUE
rb_cmetrics_counter_to_text(VALUE self)
{
    struct CMetricsCounter* cmetricsCounter;
    cmt_sds_t buffer;
    VALUE text;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    buffer = cmt_encode_text_create(cmetricsCounter->instance);
    if (buffer == NULL) {
        return Qnil;
    }

    text = rb_str_new2(buffer);

    cmt_sds_destroy(buffer);

    return text;
}

#val(*args) ⇒ Object



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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/cmetrics/cmetrics_counter.c', line 269

static VALUE
rb_cmetrics_counter_get_value(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels;
    struct CMetricsCounter* cmetricsCounter;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "01", &rb_labels);

    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_get_val(cmetricsCounter->counter,
                                  labels_count, labels, &value);
    }

    if (ret == 0) {
        return DBL2NUM(value);
    } else {
        return Qnil;
    }
}

#val=(*args) ⇒ Boolean

Set value into counter.

Returns:

  • (Boolean)


442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/cmetrics/cmetrics_counter.c', line 442

static VALUE
rb_cmetrics_counter_set(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels, rb_num;
    struct CMetricsCounter* cmetricsCounter;
    uint64_t ts;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);

    switch(TYPE(rb_num)) {
    case T_FLOAT:
    case T_FIXNUM:
    case T_BIGNUM:
    case T_RATIONAL:
        value = NUM2DBL(rb_num);
        break;
    default:
        rb_raise(rb_eArgError, "CMetrics::Counter#set can handle numerics values only.");
    }

    ts = cmt_time_now();
    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;
            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;
            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }

            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                              labels_count, labels);
    }

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}

#value(*args) ⇒ Object



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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'ext/cmetrics/cmetrics_counter.c', line 269

static VALUE
rb_cmetrics_counter_get_value(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels;
    struct CMetricsCounter* cmetricsCounter;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "01", &rb_labels);

    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }

            ret = cmt_counter_get_val(cmetricsCounter->counter,
                                      labels_count, labels, &value);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_get_val(cmetricsCounter->counter,
                                  labels_count, labels, &value);
    }

    if (ret == 0) {
        return DBL2NUM(value);
    } else {
        return Qnil;
    }
}

#value=(*args) ⇒ Boolean

Set value into counter.

Returns:

  • (Boolean)


442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'ext/cmetrics/cmetrics_counter.c', line 442

static VALUE
rb_cmetrics_counter_set(int argc, VALUE* argv, VALUE self)
{
    VALUE rb_labels, rb_num;
    struct CMetricsCounter* cmetricsCounter;
    uint64_t ts;
    int ret = 0;
    double value = 0;
    char **labels = NULL;
    int labels_count = 0;
    VALUE tmp_label;
    long i;

    TypedData_Get_Struct(
            self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);

    if (!cmetricsCounter->counter) {
        rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
    }

    rb_scan_args(argc, argv, "11", &rb_num, &rb_labels);

    switch(TYPE(rb_num)) {
    case T_FLOAT:
    case T_FIXNUM:
    case T_BIGNUM:
    case T_RATIONAL:
        value = NUM2DBL(rb_num);
        break;
    default:
        rb_raise(rb_eArgError, "CMetrics::Counter#set can handle numerics values only.");
    }

    ts = cmt_time_now();
    if (!NIL_P(rb_labels)) {
        switch(TYPE(rb_labels)) {
        case T_STRING:
            labels = (char *[]) { StringValuePtr(rb_labels) };
            labels_count = 1;
            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_SYMBOL:
            labels = (char *[]) { RSTRING_PTR(rb_sym2str(rb_labels)) };
            labels_count = 1;
            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);
            break;
        case T_ARRAY:
            labels_count = (int)RARRAY_LEN(rb_labels);
            labels = ALLOCV_N(char *, tmp_label, labels_count);
            for (i = 0; i < labels_count; i++) {
                VALUE item = RARRAY_AREF(rb_labels, i);
                switch(TYPE(item)) {
                case T_SYMBOL:
                    labels[i] = RSTRING_PTR(rb_sym2str(item));
                    break;
                case T_STRING:
                    labels[i] = StringValuePtr(item);
                    break;
                default:
                    rb_raise(rb_eArgError, "labels must be Symbol/String");
                }
            }

            ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                                  labels_count, labels);

            ALLOCV_END(tmp_label);

            break;
        default:
            rb_raise(rb_eArgError, "labels should be String, Symbol or Array class instance.");
        }
    } else {
        ret = cmt_counter_set(cmetricsCounter->counter, ts, value,
                              labels_count, labels);
    }

    if (ret == 0) {
        return Qtrue;
    } else {
        return Qfalse;
    }
}