cmetrics 0.1.6 → 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: 6ab76eb9253339b05455e7424e0f440bec6a9dd612ef61224066650151755683
4
- data.tar.gz: '056618e48e0bb0d6c50a325797096824d8c970b48f26d24c9f224608f48f513d'
3
+ metadata.gz: 7718f2df26fa25f74be30a4ac65f555b53576641df5ceb5fcb4db8e67ed8b1d0
4
+ data.tar.gz: 7e154efbf9beaff565af8f812bd63932ebf0594faf800fda4b82790d11911fe1
5
5
  SHA512:
6
- metadata.gz: 157c179195aa7273939807fa10ac06b2b660cd52ef459ffe733dafe11109eccc8b9c48d4f2cb384ecd250909fe4327321ab3382b83ff77e4432ca76641178059
7
- data.tar.gz: 6937cbb0e2ab74b51683d3d3ad5d440b9bd571663a4cae06dd0385b7b3f74bb223c6bf3e4fb9a04e29e13ed480daa928e059323d28eecefe4cdb8af9d086ee70
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
  }
@@ -27,19 +27,19 @@ class BuildCMetrics
27
27
  @checkpoint = ".#{@recipe.name}-#{@recipe.version}.installed"
28
28
  @recipe.target = File.join(ROOT, "ports")
29
29
  @recipe.files << {
30
- url: "file://#{ROOT}/ext/#{@recipe.name}-#{@recipe.version}.tar.gz",
31
- sha256sum: "5ba909df989bf7f3404000d9e04702eaa47cbfd51bb8ec161ca19011f95bc360",
30
+ url: "https://codeload.github.com/calyptia/cmetrics/tar.gz/v#{version}",
31
+ sha256sum: "f0c79707ad4d18980bf0d1d64ed8cb73a40c586c36a4caf38233ae8d9a4c6cc7",
32
32
  }
33
33
  end
34
34
 
35
35
  def build
36
36
  unless File.exist?(@checkpoint)
37
37
  @recipe.cook
38
- libcmetrics_path = File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/lib/libcmetrics.a")
38
+ libcmetrics_path = Dir.glob(File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/lib*/libcmetrics.a")).first
39
39
  FileUtils.cp(libcmetrics_path, File.join(ROOT, "ext", "cmetrics", "libcmetrics.a"))
40
- libmpack_path = File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/lib/libmpack.a")
40
+ libmpack_path = Dir.glob(File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/lib*/libmpack.a")).first
41
41
  FileUtils.cp(libmpack_path, File.join(ROOT, "ext", "cmetrics", "libmpack.a"))
42
- libxxhash_path = File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/lib/libxxhash.a")
42
+ libxxhash_path = Dir.glob(File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/lib*/libxxhash.a")).first
43
43
  FileUtils.cp(libxxhash_path, File.join(ROOT, "ext", "cmetrics", "libxxhash.a"))
44
44
  include_path = File.join(ROOT, "ports/#{@recipe.host}/cmetrics/#{@version}/include/")
45
45
  FileUtils.cp_r(Dir.glob(File.join(include_path, "*")), File.join(ROOT, "ext", "cmetrics"))
@@ -52,7 +52,7 @@ class BuildCMetrics
52
52
  end
53
53
  end
54
54
 
55
- cmetrics = BuildCMetrics.new
55
+ cmetrics = BuildCMetrics.new("0.2.1")
56
56
  cmetrics.build
57
57
 
58
58
  libdir = RbConfig::CONFIG["libdir"]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CMetrics
4
- VERSION = "0.1.6"
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.6
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-12 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
@@ -99,7 +99,6 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - cmetrics-ruby.gemspec
102
- - ext/cmetrics-master.tar.gz
103
102
  - ext/cmetrics/cmetrics.c
104
103
  - ext/cmetrics/cmetrics_c.h
105
104
  - ext/cmetrics/cmetrics_counter.c
@@ -130,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
129
  - !ruby/object:Gem::Version
131
130
  version: '0'
132
131
  requirements: []
133
- rubygems_version: 3.0.3
132
+ rubygems_version: 3.2.22
134
133
  signing_key:
135
134
  specification_version: 4
136
135
  summary: C binding for cmetric library.
Binary file