cmetrics 0.1.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '039922256e09204e10d0e707fee425ecc22f835c9cf373ac6383666bb2a27517'
4
- data.tar.gz: 62909440c1a9c8e347195fe4f11f7738b08e159dd22478ac49d794049f6bd275
3
+ metadata.gz: 7718f2df26fa25f74be30a4ac65f555b53576641df5ceb5fcb4db8e67ed8b1d0
4
+ data.tar.gz: 7e154efbf9beaff565af8f812bd63932ebf0594faf800fda4b82790d11911fe1
5
5
  SHA512:
6
- metadata.gz: 2519712adf54ab9dd3ad43e658c6fc504bb24224f21d7acf36ca4cc8bddfce06ac93412f24c963c7739a1d8de7e21d27610776e9f0149104971dfea676ad8e13
7
- data.tar.gz: b00ed69d225f816d549b2b3078ed919d779be994e5fe25b955e2c2147c976f69d772574657bf1ff3c3c7f475c402fc2faae1da47e345653b87467cfd4e674ce4
6
+ metadata.gz: 807d890634944b9bc738fd518af39a28cfea55a2daad19c0435786ece2535bd94c5fe34c8ab9ebae4c3408180e8005b75ee97377ace9fb6c67c802baad39da1d
7
+ data.tar.gz: '0698fef3a0195bd4499cedff46f3146aff5a49831e23ef3bc6ce6789df77cb36ef2b956cbcb99d254fabbd44709f964c44612c03c64a9f49b67b9fb4819f2a6c'
@@ -18,6 +18,8 @@
18
18
  */
19
19
 
20
20
  #include "cmetrics_c.h"
21
+ #include <cmetrics/cmt_map.h>
22
+ #include <cmetrics/cmt_metric.h>
21
23
 
22
24
  VALUE rb_cSerde;
23
25
 
@@ -111,7 +113,11 @@ rb_cmetrics_serde_from_msgpack(int argc, VALUE *argv, VALUE self)
111
113
  rb_raise(rb_eRuntimeError, "offset should be smaller than msgpack buffer size.");
112
114
  }
113
115
 
114
- ret = cmt_decode_msgpack_create(&cmt, StringValuePtr(rb_msgpack_buffer), msgpack_length, &offset);
116
+ if (!NIL_P(rb_msgpack_buffer)) {
117
+ ret = cmt_decode_msgpack_create(&cmt, StringValuePtr(rb_msgpack_buffer), msgpack_length, &offset);
118
+ } else {
119
+ rb_raise(rb_eArgError, "nil is not valid value for buffer");
120
+ }
115
121
 
116
122
  if (ret == 0) {
117
123
  cmetricsSerde->instance = cmt;
@@ -154,8 +160,11 @@ static VALUE
154
160
  rb_cmetrics_serde_from_msgpack_feed_each(VALUE self, VALUE rb_data)
155
161
  {
156
162
  RETURN_ENUMERATOR(self, 0, 0);
157
-
158
- return rb_cmetrics_serde_from_msgpack_feed_each_impl(self, rb_data, RSTRING_LEN(rb_data));
163
+ if (!NIL_P(rb_data)) {
164
+ return rb_cmetrics_serde_from_msgpack_feed_each_impl(self, rb_data, RSTRING_LEN(rb_data));
165
+ } else {
166
+ rb_raise(rb_eArgError, "nil is not valid value for buffer");
167
+ }
159
168
  }
160
169
 
161
170
  static VALUE
@@ -237,6 +246,151 @@ rb_cmetrics_serde_to_text(VALUE self)
237
246
  return text;
238
247
  }
239
248
 
249
+ static VALUE
250
+ append_metric_value(struct cmt_map *map,
251
+ VALUE rbHash, struct cmt_metric *metric)
252
+ {
253
+ uint64_t ts;
254
+ double val;
255
+ struct cmt_opts *opts;
256
+
257
+ opts = map->opts;
258
+
259
+ /* Retrieve metric value */
260
+ val = cmt_metric_get_value(metric);
261
+
262
+ ts = cmt_metric_get_timestamp(metric);
263
+
264
+ rb_hash_aset(rbHash, rb_str_new2("name"), rb_str_new2(opts->name));
265
+ rb_hash_aset(rbHash, rb_str_new2("description"), rb_str_new2(opts->description));
266
+ rb_hash_aset(rbHash, rb_str_new2("value"), DBL2NUM(val));
267
+ rb_hash_aset(rbHash, rb_str_new2("timestamp"), DBL2NUM(ts/1000000000.0));
268
+
269
+ return rbHash;
270
+ }
271
+
272
+ static VALUE
273
+ format_metric(struct cmt *cmt, struct cmt_map *map,
274
+ struct cmt_metric *metric)
275
+ {
276
+ int n;
277
+ int static_labels = 0;
278
+ struct cmt_map_label *label_k;
279
+ struct cmt_map_label *label_v;
280
+ struct mk_list *head;
281
+ struct cmt_opts *opts;
282
+ struct cmt_label *slabel;
283
+ VALUE rb_hash = rb_hash_new();
284
+ VALUE shash = rb_hash_new();
285
+ VALUE lhash = rb_hash_new();
286
+
287
+ opts = map->opts;
288
+
289
+ /* Measurement */
290
+ rb_hash_aset(rb_hash, rb_str_new2("namespace"), rb_str_new2(opts->ns));
291
+ rb_hash_aset(rb_hash, rb_str_new2("subsystem"), rb_str_new2(opts->subsystem));
292
+
293
+ /* Static labels (tags) */
294
+ static_labels = cmt_labels_count(cmt->static_labels);
295
+ if (static_labels > 0) {
296
+ mk_list_foreach(head, &cmt->static_labels->list) {
297
+ slabel = mk_list_entry(head, struct cmt_label, _head);
298
+ rb_hash_aset(shash, rb_str_new2(slabel->key), rb_str_new2(slabel->val));
299
+ }
300
+ rb_hash_aset(rb_hash, rb_str_new2("static_labels"), shash);
301
+ }
302
+
303
+ /* Labels / Tags */
304
+ n = mk_list_size(&metric->labels);
305
+ if (n > 0) {
306
+ label_k = mk_list_entry_first(&map->label_keys, struct cmt_map_label, _head);
307
+
308
+ mk_list_foreach(head, &metric->labels) {
309
+ label_v = mk_list_entry(head, struct cmt_map_label, _head);
310
+
311
+ rb_hash_aset(lhash, rb_str_new2(label_k->name), rb_str_new2(label_v->name));
312
+
313
+ label_k = mk_list_entry_next(&label_k->_head, struct cmt_map_label,
314
+ _head, &map->label_keys);
315
+ }
316
+ rb_hash_aset(rb_hash, rb_str_new2("labels"), lhash);
317
+ }
318
+
319
+ rb_hash = append_metric_value(map, rb_hash, metric);
320
+
321
+ return rb_hash;
322
+ }
323
+
324
+ static VALUE
325
+ format_metrics(struct cmt *cmt,
326
+ struct cmt_map *map, int add_timestamp)
327
+ {
328
+ VALUE rbMetrics = rb_ary_new();
329
+ VALUE rbMetric;
330
+ struct mk_list *head;
331
+ struct cmt_metric *metric;
332
+
333
+ /* Simple metric, no labels */
334
+ if (map->metric_static_set == 1) {
335
+ rbMetric = format_metric(cmt, map, &map->metric);
336
+ rb_ary_push(rbMetrics, rbMetric);
337
+ }
338
+
339
+ mk_list_foreach(head, &map->metrics) {
340
+ metric = mk_list_entry(head, struct cmt_metric, _head);
341
+ rbMetric = format_metric(cmt, map, metric);
342
+ rb_ary_push(rbMetrics, rbMetric);
343
+ }
344
+
345
+ return rbMetrics;
346
+ }
347
+
348
+ static VALUE
349
+ rb_cmetrics_serde_get_metrics(VALUE self)
350
+ {
351
+ VALUE rbMetrics = rb_ary_new();
352
+ VALUE rbMetricsInner = rb_ary_new();
353
+ struct CMetricsSerde* cmetricsSerde;
354
+ struct mk_list *head;
355
+ struct cmt_gauge *gauge;
356
+ struct cmt_counter *counter;
357
+ struct cmt_untyped *untyped;
358
+ struct cmt *cmt;
359
+ int add_timestamp = CMT_TRUE;
360
+
361
+ TypedData_Get_Struct(
362
+ self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
363
+
364
+ cmt = cmetricsSerde->instance;
365
+
366
+ if (cmt == NULL) {
367
+ rb_raise(rb_eRuntimeError, "Invalid cmt context");
368
+ }
369
+
370
+ /* Counters */
371
+ mk_list_foreach(head, &cmt->counters) {
372
+ counter = mk_list_entry(head, struct cmt_counter, _head);
373
+ rbMetricsInner = format_metrics(cmt, counter->map, add_timestamp);
374
+ rb_ary_push(rbMetrics, rbMetricsInner);
375
+ }
376
+
377
+ /* Gauges */
378
+ mk_list_foreach(head, &cmt->gauges) {
379
+ gauge = mk_list_entry(head, struct cmt_gauge, _head);
380
+ rbMetricsInner = format_metrics(cmt, gauge->map, add_timestamp);
381
+ rb_ary_push(rbMetrics, rbMetricsInner);
382
+ }
383
+
384
+ /* Untyped */
385
+ mk_list_foreach(head, &cmt->untypeds) {
386
+ untyped = mk_list_entry(head, struct cmt_untyped, _head);
387
+ rbMetricsInner = format_metrics(cmt, untyped->map, add_timestamp);
388
+ rb_ary_push(rbMetrics, rbMetricsInner);
389
+ }
390
+
391
+ return rbMetrics;
392
+ }
393
+
240
394
  void Init_cmetrics_serde(VALUE rb_mCMetrics)
241
395
  {
242
396
  rb_cSerde = rb_define_class_under(rb_mCMetrics, "Serde", rb_cObject);
@@ -250,4 +404,6 @@ void Init_cmetrics_serde(VALUE rb_mCMetrics)
250
404
  rb_define_method(rb_cSerde, "to_msgpack", rb_cmetrics_serde_to_msgpack, 0);
251
405
  rb_define_method(rb_cSerde, "feed_each", rb_cmetrics_serde_from_msgpack_feed_each, 1);
252
406
  rb_define_method(rb_cSerde, "to_s", rb_cmetrics_serde_to_text, 0);
407
+ rb_define_method(rb_cSerde, "get_metrics", rb_cmetrics_serde_get_metrics, 0);
408
+ rb_define_method(rb_cSerde, "metrics", rb_cmetrics_serde_get_metrics, 0);
253
409
  }
@@ -28,7 +28,7 @@ class BuildCMetrics
28
28
  @recipe.target = File.join(ROOT, "ports")
29
29
  @recipe.files << {
30
30
  url: "https://codeload.github.com/calyptia/cmetrics/tar.gz/v#{version}",
31
- sha256sum: "50f74147f8247dba434099e1075755097099c6bae77456da7322bb5ffb1f3f54",
31
+ sha256sum: "f0c79707ad4d18980bf0d1d64ed8cb73a40c586c36a4caf38233ae8d9a4c6cc7",
32
32
  }
33
33
  end
34
34
 
@@ -52,7 +52,7 @@ class BuildCMetrics
52
52
  end
53
53
  end
54
54
 
55
- cmetrics = BuildCMetrics.new("0.1.4")
55
+ cmetrics = BuildCMetrics.new("0.2.1")
56
56
  cmetrics.build
57
57
 
58
58
  libdir = RbConfig::CONFIG["libdir"]
@@ -63,8 +63,6 @@ find_library("xxhash", nil, __dir__)
63
63
  find_library("mpack", nil, __dir__)
64
64
  find_library("cmetrics", nil, __dir__)
65
65
 
66
- $CFLAGS << " -std=c99 "
67
-
68
66
  have_func("gmtime_s", "time.h")
69
67
 
70
68
  create_makefile("cmetrics/cmetrics")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CMetrics
4
- VERSION = "0.1.9"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cmetrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Hatake
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-14 00:00:00.000000000 Z
11
+ date: 2021-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  - !ruby/object:Gem::Version
130
130
  version: '0'
131
131
  requirements: []
132
- rubygems_version: 3.0.3
132
+ rubygems_version: 3.2.22
133
133
  signing_key:
134
134
  specification_version: 4
135
135
  summary: C binding for cmetric library.