Class: CMetrics::Serde

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

Instance Method Summary collapse

Constructor Details

#initializeSerde

Initailize Serde class.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/cmetrics/cmetrics_serde.c', line 75

static VALUE
rb_cmetrics_serde_initialize(VALUE self)
{
    struct CMetricsSerde* cmetricsSerde;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    cmt_initialize();

    cmetricsSerde->instance = NULL;
    cmetricsSerde->unpack_msgpack_offset = 0;

    return Qnil;
}

Instance Method Details

#concat(rb_data) ⇒ Object



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

static VALUE
rb_cmetrics_serde_concat_metric(VALUE self, VALUE rb_data)
{
    struct CMetricsSerde* cmetricsSerde = NULL;
    struct CMetricsCounter* cmetricsCounter = NULL;
    struct CMetricsGauge* cmetricsGauge = NULL;
    struct CMetricsUntyped* cmetricsUntyped = NULL;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    if (!NIL_P(rb_data)) {
        if (cmetricsSerde->instance == NULL) {
            cmetricsSerde->instance = cmt_create();
        }

        if (rb_obj_is_kind_of(rb_data, rb_cCounter)) {
            cmetricsCounter = (struct CMetricsCounter *)cmetrics_counter_get_ptr(rb_data);
            cmt_cat(cmetricsSerde->instance, cmetricsCounter->instance);
        } else if (rb_obj_is_kind_of(rb_data, rb_cGauge)) {
            cmetricsGauge = (struct CMetricsGauge *)cmetrics_gauge_get_ptr(rb_data);
            cmt_cat(cmetricsSerde->instance, cmetricsGauge->instance);
        } else if (rb_obj_is_kind_of(rb_data, rb_cUntyped)) {
            cmetricsUntyped = (struct CMetricsUntyped *)cmetrics_untyped_get_ptr(rb_data);
            cmt_cat(cmetricsSerde->instance, cmetricsUntyped->instance);
        } else {
            rb_raise(rb_eArgError, "specified type of instance is not supported.");
        }
    } else {
        rb_raise(rb_eArgError, "nil is not valid value for concatenating");
    }

    return Qnil;
}

#feed_each(rb_data) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'ext/cmetrics/cmetrics_serde.c', line 166

static VALUE
rb_cmetrics_serde_from_msgpack_feed_each(VALUE self, VALUE rb_data)
{
    RETURN_ENUMERATOR(self, 0, 0);
    if (!NIL_P(rb_data)) {
        return rb_cmetrics_serde_from_msgpack_feed_each_impl(self, rb_data, RSTRING_LEN(rb_data));
    } else {
        rb_raise(rb_eArgError, "nil is not valid value for buffer");
    }
}

#from_msgpack(*args) ⇒ Object



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

static VALUE
rb_cmetrics_serde_from_msgpack(int argc, VALUE *argv, VALUE self)
{
    VALUE rb_msgpack_buffer, rb_msgpack_length, rb_offset;
    struct CMetricsSerde* cmetricsSerde;
    int ret = 0;
    struct cmt *cmt = NULL;
    size_t offset = 0;
    size_t msgpack_length = 0;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    rb_scan_args(argc, argv, "12", &rb_msgpack_buffer, &rb_msgpack_length, &rb_offset);
    if (!NIL_P(rb_msgpack_length)) {
        Check_Type(rb_msgpack_length, T_FIXNUM);
        msgpack_length = ULONG2NUM(rb_msgpack_length);
    } else {
        msgpack_length = RSTRING_LEN(rb_msgpack_buffer);
    }
    if (!NIL_P(rb_offset)) {
        Check_Type(rb_offset, T_FIXNUM);
        offset = NUM2INT(rb_offset);
    } else {
        offset = cmetricsSerde->unpack_msgpack_offset;
    }

    if (offset >= msgpack_length) {
        rb_raise(rb_eRuntimeError, "offset should be smaller than msgpack buffer size.");
    }

    if (!NIL_P(rb_msgpack_buffer)) {
        ret = cmt_decode_msgpack_create(&cmt, StringValuePtr(rb_msgpack_buffer), msgpack_length, &offset);
    } else {
        rb_raise(rb_eArgError, "nil is not valid value for buffer");
    }

    if (ret == 0) {
        cmetricsSerde->instance = cmt;
        cmetricsSerde->unpack_msgpack_offset = offset;

        return Qtrue;
    } else {
        return Qnil;
    }
}

#get_metricsObject



429
430
431
432
433
434
435
436
437
438
439
440
441
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
# File 'ext/cmetrics/cmetrics_serde.c', line 429

static VALUE
rb_cmetrics_serde_get_metrics(VALUE self)
{
    VALUE rbMetrics = rb_ary_new();
    VALUE rbMetricsInner = rb_ary_new();
    struct CMetricsSerde* cmetricsSerde;
    struct mk_list *head;
    struct cmt_gauge *gauge;
    struct cmt_counter *counter;
    struct cmt_untyped *untyped;
    struct cmt *cmt;
    int add_timestamp = CMT_TRUE;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    cmt = cmetricsSerde->instance;

    if (cmt == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

    /* Counters */
    mk_list_foreach(head, &cmt->counters) {
        counter = mk_list_entry(head, struct cmt_counter, _head);
        rbMetricsInner = format_metrics(cmt, counter->map, add_timestamp);
        rb_ary_push(rbMetrics, rbMetricsInner);
    }

    /* Gauges */
    mk_list_foreach(head, &cmt->gauges) {
        gauge = mk_list_entry(head, struct cmt_gauge, _head);
        rbMetricsInner = format_metrics(cmt, gauge->map, add_timestamp);
        rb_ary_push(rbMetrics, rbMetricsInner);
    }

    /* Untyped */
    mk_list_foreach(head, &cmt->untypeds) {
        untyped = mk_list_entry(head, struct cmt_untyped, _head);
        rbMetricsInner = format_metrics(cmt, untyped->map, add_timestamp);
        rb_ary_push(rbMetrics, rbMetricsInner);
    }

    return rbMetrics;
}

#metricsObject



429
430
431
432
433
434
435
436
437
438
439
440
441
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
# File 'ext/cmetrics/cmetrics_serde.c', line 429

static VALUE
rb_cmetrics_serde_get_metrics(VALUE self)
{
    VALUE rbMetrics = rb_ary_new();
    VALUE rbMetricsInner = rb_ary_new();
    struct CMetricsSerde* cmetricsSerde;
    struct mk_list *head;
    struct cmt_gauge *gauge;
    struct cmt_counter *counter;
    struct cmt_untyped *untyped;
    struct cmt *cmt;
    int add_timestamp = CMT_TRUE;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    cmt = cmetricsSerde->instance;

    if (cmt == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

    /* Counters */
    mk_list_foreach(head, &cmt->counters) {
        counter = mk_list_entry(head, struct cmt_counter, _head);
        rbMetricsInner = format_metrics(cmt, counter->map, add_timestamp);
        rb_ary_push(rbMetrics, rbMetricsInner);
    }

    /* Gauges */
    mk_list_foreach(head, &cmt->gauges) {
        gauge = mk_list_entry(head, struct cmt_gauge, _head);
        rbMetricsInner = format_metrics(cmt, gauge->map, add_timestamp);
        rb_ary_push(rbMetrics, rbMetricsInner);
    }

    /* Untyped */
    mk_list_foreach(head, &cmt->untypeds) {
        untyped = mk_list_entry(head, struct cmt_untyped, _head);
        rbMetricsInner = format_metrics(cmt, untyped->map, add_timestamp);
        rb_ary_push(rbMetrics, rbMetricsInner);
    }

    return rbMetrics;
}

#prometheus_remote_writeObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/cmetrics/cmetrics_serde.c', line 212

static VALUE
rb_metrics_serde_prometheus_remote_write(VALUE self)
{
    struct CMetricsSerde* cmetricsSerde;
    cmt_sds_t prom;
    VALUE str;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    if (cmetricsSerde->instance == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

    prom = cmt_encode_prometheus_remote_write_create(cmetricsSerde->instance);

    str = rb_str_new2(prom);

    cmt_encode_prometheus_remote_write_destroy(prom);

    return str;
}

#to_influxObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'ext/cmetrics/cmetrics_serde.c', line 258

static VALUE
rb_cmetrics_serde_to_influx(VALUE self)
{
    struct CMetricsSerde* cmetricsSerde;
    cmt_sds_t prom;
    VALUE str;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    if (cmetricsSerde->instance == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

    prom = cmt_encode_influx_create(cmetricsSerde->instance);

    str = rb_str_new2(prom);

    cmt_encode_influx_destroy(prom);

    return str;
}

#to_msgpackObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'ext/cmetrics/cmetrics_serde.c', line 280

static VALUE
rb_cmetrics_serde_to_msgpack(VALUE self)
{
    struct CMetricsSerde* cmetricsSerde;
    char *buffer;
    size_t buffer_size;
    int ret = 0;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    if (cmetricsSerde->instance == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

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

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

#to_prometheusObject



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/cmetrics/cmetrics_serde.c', line 235

static VALUE
rb_cmetrics_serde_to_prometheus(VALUE self)
{
    struct CMetricsSerde* cmetricsSerde;
    cmt_sds_t prom;
    VALUE str;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    if (cmetricsSerde->instance == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

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

    str = rb_str_new2(prom);

    cmt_encode_prometheus_destroy(prom);

    return str;
}

#to_sObject



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

static VALUE
rb_cmetrics_serde_to_text(VALUE self)
{
    struct CMetricsSerde* cmetricsSerde;
    cmt_sds_t buffer;
    VALUE text;

    TypedData_Get_Struct(
            self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);

    if (cmetricsSerde->instance == NULL) {
        rb_raise(rb_eRuntimeError, "Invalid cmt context");
    }

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

    text = rb_str_new2(buffer);

    cmt_sds_destroy(buffer);

    return text;
}