cmetrics 0.1.7 → 0.2.0.1

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: c0c8e72735e2b8d4a23cff25f45d22b8532bb9d5264011292e07a624304bbbf1
4
- data.tar.gz: 55fac2f85a6729a18897abcfed5fdde9098269be899038a7b2bf1c4313988d04
3
+ metadata.gz: a1d2203114cf90a7d63a5fc3654f2feb4dca6f06e6a5f7bef307ac3d22b7b890
4
+ data.tar.gz: d06d5b84c4b6614df1e0e9a1a301ab450cc871192b89fb1cb556d54d4040d338
5
5
  SHA512:
6
- metadata.gz: b6372b5e06fbba2d4c53c5e1df6250e02192a7ca645093b5682fe49ed985e50ce4fda080d0b2fe18b00d0abc8fd101bba2d33f5e5de91163f429937966d920f3
7
- data.tar.gz: 4933e44b64f8e2cebf0f5534238e22cc0053cf4959070cd3f84acda7561e5cf8020e42bc510f13f468865905d90a9d8e8ed8e6ad3dca6367f085a398a64722f5
6
+ metadata.gz: 1808639928823b48c0e52f333a898a3816bbf3e4396a922b30a22eb062552ee28cf5c7a7fdaac91392dc23ff2747ee5d0c2e183e74591b43577b4240397c1384
7
+ data.tar.gz: 347c9a59d332714286996dcc0450a20f8fd7de511677e9a1768fb059f2fec73578bf2dd18d2184094c498bc6adc64d35cdb25c559c278b9e43fc3395a0812adc
@@ -0,0 +1,39 @@
1
+ name: Apt based Linux
2
+ on:
3
+ push:
4
+ pull_request:
5
+ jobs:
6
+ build:
7
+ name: Build
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ label:
12
+ - Debian GNU/Linux Bullseye amd64
13
+ - Debian GNU/Linux Buster amd64
14
+ - Ubuntu Bionic amd64
15
+ - Ubuntu Focal amd64
16
+ include:
17
+ - label: Debian GNU/Linux Bullseye amd64
18
+ test-docker-image: debian:bullseye
19
+ test-script: ci/apt-test.sh
20
+ - label: Debian GNU/Linux Buster amd64
21
+ test-docker-image: debian:buster
22
+ test-script: ci/apt-test.sh
23
+ - label: Ubuntu Bionic amd64
24
+ test-docker-image: ubuntu:bionic
25
+ test-script: ci/apt-test.sh
26
+ - label: Ubuntu Focal amd64
27
+ test-docker-image: ubuntu:focal
28
+ test-script: ci/apt-test.sh
29
+ runs-on: ubuntu-latest
30
+ steps:
31
+ - uses: actions/checkout@master
32
+ - name: rake compile & rake test
33
+ run: |
34
+ docker run \
35
+ --rm \
36
+ --tty \
37
+ --volume ${PWD}:/cmetrics-ruby \
38
+ ${{ matrix.test-docker-image }} \
39
+ /cmetrics-ruby/${{ matrix.test-script }}
@@ -10,9 +10,9 @@ jobs:
10
10
  - name: Set up Ruby
11
11
  uses: ruby/setup-ruby@v1
12
12
  with:
13
- ruby-version: 3.0.1
13
+ ruby-version: '3.0'
14
14
  - name: Run the default task
15
15
  run: |
16
- gem install bundler -v 2.2.15
16
+ gem install bundler -v 2.2.26
17
17
  bundle install
18
18
  bundle exec rake
@@ -10,9 +10,9 @@ jobs:
10
10
  - name: Set up Ruby
11
11
  uses: ruby/setup-ruby@v1
12
12
  with:
13
- ruby-version: 3.0.1
13
+ ruby-version: '3.0'
14
14
  - name: Run the default task
15
15
  run: |
16
- gem install bundler -v 2.2.15
16
+ gem install bundler -v 2.2.26
17
17
  bundle install
18
18
  bundle exec rake
@@ -10,9 +10,9 @@ jobs:
10
10
  - name: Set up Ruby
11
11
  uses: ruby/setup-ruby@v1
12
12
  with:
13
- ruby-version: 3.0.1
13
+ ruby-version: '3.0'
14
14
  - name: Run the default task
15
15
  run: |
16
- gem install bundler -v 2.2.15
16
+ gem install bundler -v 2.2.26
17
17
  ridk exec bundle install
18
18
  ridk exec bundle exec rake
@@ -0,0 +1,39 @@
1
+ name: Yum based Linux
2
+ on:
3
+ push:
4
+ pull_request:
5
+ jobs:
6
+ build:
7
+ name: Build
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ label:
12
+ - CentOS 7 x86_64
13
+ - CentOS 8 x86_64
14
+ - Fedora 34 x86_64
15
+ - AmazonLinux 2 x86_64
16
+ include:
17
+ - label: CentOS 7 x86_64
18
+ test-docker-image: centos:7
19
+ test-script: ci/yum-test.sh
20
+ - label: CentOS 8 x86_64
21
+ test-docker-image: centos:8
22
+ test-script: ci/yum-test.sh
23
+ - label: Fedora 34 x86_64
24
+ test-docker-image: fedora:34
25
+ test-script: ci/yum-test.sh
26
+ - label: AmazonLinux 2 x86_64
27
+ test-docker-image: amazonlinux:2
28
+ test-script: ci/yum-test.sh
29
+ runs-on: ubuntu-latest
30
+ steps:
31
+ - uses: actions/checkout@master
32
+ - name: rake compile & rake test
33
+ run: |
34
+ docker run \
35
+ --rm \
36
+ --tty \
37
+ --volume ${PWD}:/cmetrics-ruby \
38
+ ${{ matrix.test-docker-image }} \
39
+ /cmetrics-ruby/${{ matrix.test-script }}
data/Gemfile CHANGED
@@ -7,4 +7,10 @@ gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
9
 
10
- gem "test-unit", "~> 3.0"
10
+ gem "test-unit", "~> 3.4"
11
+
12
+ local_gemfile = File.join(File.dirname(__FILE__), "Gemfile.local")
13
+ if File.exist?(local_gemfile)
14
+ puts "Loading Gemfile.local ..." if $DEBUG # `ruby -d` or `bundle -v`
15
+ instance_eval File.read(local_gemfile)
16
+ end
data/ci/apt-test.sh ADDED
@@ -0,0 +1,40 @@
1
+ #!/bin/bash
2
+
3
+ set -exu
4
+
5
+ export DEBIAN_FRONTEND=noninteractive
6
+
7
+ if [ -f /etc/lsb-release ]; then
8
+ . /etc/lsb-release
9
+ distribution=$DISTRIB_ID
10
+ version=${DISTRIB_RELEASE%%.*}
11
+ codename=${DISTRIB_CODENAME%%.*}
12
+ else
13
+ distribution="Debian"
14
+ version=$(cat /etc/debian_version | cut -d'.' -f1)
15
+ fi
16
+
17
+ apt update
18
+
19
+ case "$distribution" in
20
+ "Ubuntu")
21
+ case "$codename" in
22
+ "bionic")
23
+ apt install -V -y wget gpg
24
+ wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null && \
25
+ echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ bionic main' | tee /etc/apt/sources.list.d/kitware.list >/dev/null && \
26
+ apt update
27
+ ;;
28
+ *)
29
+ ;;
30
+ esac
31
+ ;;
32
+ esac
33
+
34
+ apt install -V -y lsb-release
35
+
36
+ apt install -V -y ruby-dev git build-essential pkg-config cmake
37
+ cd /cmetrics-ruby && \
38
+ gem install bundler --no-document && \
39
+ bundle install && \
40
+ bundle exec rake
data/ci/yum-test.sh ADDED
@@ -0,0 +1,77 @@
1
+ #!/bin/bash
2
+
3
+ set -exu
4
+
5
+ USE_SCL=0
6
+ USE_AMZN_EXT=0
7
+
8
+ distribution=$(cat /etc/system-release-cpe | awk '{print substr($0, index($1, "o"))}' | cut -d: -f2)
9
+ version=$(cat /etc/system-release-cpe | awk '{print substr($0, index($1, "o"))}' | cut -d: -f4)
10
+ USE_SCL=0
11
+ USE_AMZN_EXT=0
12
+
13
+ case ${distribution} in
14
+ amazon)
15
+ case ${version} in
16
+ 2)
17
+ DNF=yum
18
+ USE_AMZN_EXT=1
19
+ ;;
20
+ esac
21
+ ;;
22
+ centos)
23
+ case ${version} in
24
+ 7)
25
+ DNF=yum
26
+ USE_SCL=1
27
+ ;;
28
+ *)
29
+ DNF="dnf --enablerepo=powertools"
30
+ ;;
31
+ esac
32
+ ;;
33
+ fedoraproject)
34
+ case ${version} in
35
+ 33|34)
36
+ DNF=yum
37
+ ;;
38
+ esac
39
+ ;;
40
+ esac
41
+
42
+ ${DNF} groupinstall -y "Development Tools"
43
+
44
+ if [ $USE_SCL -eq 1 ]; then
45
+ ${DNF} install -y centos-release-scl && \
46
+ ${DNF} install -y epel-release && \
47
+ ${DNF} install -y \
48
+ rh-ruby26-ruby-devel \
49
+ rh-ruby26-rubygems \
50
+ rh-ruby26-rubygem-rake \
51
+ rpm-build \
52
+ cmake3
53
+ elif [ $USE_AMZN_EXT -eq 1 ]; then
54
+ yum update -y && \
55
+ yum install -y yum-utils && \
56
+ yum-config-manager --enable epel && \
57
+ amazon-linux-extras install -y ruby2.6 && \
58
+ ${DNF} install -y ruby-devel \
59
+ cmake3
60
+ else
61
+ ${DNF} install -y ruby-devel \
62
+ rubygems \
63
+ rpm-build \
64
+ cmake \
65
+ libarchive
66
+ fi
67
+
68
+ if [ $USE_SCL -eq 1 ]; then
69
+ # For unbound variable error
70
+ export MANPATH=
71
+ cd /cmetrics-ruby && source /opt/rh/rh-ruby26/enable && gem install bundler --no-document && bundle install && bundle exec rake
72
+ else
73
+ if [ $USE_AMZN_EXT -eq 1 ]; then
74
+ echo 'gem "io-console"' > /cmetrics-ruby/Gemfile.local
75
+ fi
76
+ cd /cmetrics-ruby && gem install bundler --no-document && bundle install && bundle exec rake
77
+ fi
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency "rake", ">= 0"
30
30
  spec.add_development_dependency "rake-compiler", "~> 1.0"
31
31
  spec.add_development_dependency "rake-compiler-dock", "~> 1.0"
32
+ spec.add_development_dependency "msgpack", "~> 1.4.2"
32
33
 
33
- spec.add_dependency 'mini_portile2', '~> 2.1'
34
+ spec.add_dependency 'mini_portile2', '~> 2.6'
34
35
  end
@@ -65,5 +65,8 @@ void Init_cmetrics_counter(VALUE rb_mCMetrics);
65
65
  void Init_cmetrics_gauge(VALUE rb_mCMetrics);
66
66
  void Init_cmetrics_serde(VALUE rb_mCMetrics);
67
67
  void Init_cmetrics_untyped(VALUE rb_mCMetrics);
68
+ const struct CMetricsCounter *cmetrics_counter_get_ptr(VALUE rb_mCMetrics);
69
+ const struct CMetricsGauge *cmetrics_gauge_get_ptr(VALUE rb_mCMetrics);
70
+ const struct CMetricsUntyped *cmetrics_untyped_get_ptr(VALUE rb_mCMetrics);
68
71
 
69
72
  #endif // _CMETRICS_C_H
@@ -34,16 +34,32 @@ static const rb_data_type_t rb_cmetrics_counter_type = { "cmetrics/counter",
34
34
  RUBY_TYPED_FREE_IMMEDIATELY };
35
35
 
36
36
 
37
+ const struct CMetricsCounter *cmetrics_counter_get_ptr(VALUE self)
38
+ {
39
+ struct CMetricsCounter *cmetricsCounter = NULL;
40
+
41
+ TypedData_Get_Struct(
42
+ self, struct CMetricsCounter, &rb_cmetrics_counter_type, cmetricsCounter);
43
+
44
+ if (NIL_P(self)) {
45
+ rb_raise(rb_eRuntimeError, "Given CMetrics argument must not be nil");
46
+ }
47
+ if (!cmetricsCounter->counter) {
48
+ rb_raise(rb_eRuntimeError, "Create counter with CMetrics::Counter#create first.");
49
+ }
50
+ return cmetricsCounter;
51
+ }
52
+
37
53
  static void
38
54
  counter_free(void* ptr)
39
55
  {
40
56
  struct CMetricsCounter* cmetricsCounter = (struct CMetricsCounter*)ptr;
41
57
 
42
- if (!cmetricsCounter) {
43
- if (!cmetricsCounter->counter) {
58
+ if (cmetricsCounter) {
59
+ if (cmetricsCounter->counter) {
44
60
  cmt_counter_destroy(cmetricsCounter->counter);
45
61
  }
46
- if (!cmetricsCounter->instance) {
62
+ if (cmetricsCounter->instance) {
47
63
  cmt_destroy(cmetricsCounter->instance);
48
64
  }
49
65
  }
@@ -34,16 +34,32 @@ static const rb_data_type_t rb_cmetrics_gauge_type = { "cmetrics/gauge",
34
34
  RUBY_TYPED_FREE_IMMEDIATELY };
35
35
 
36
36
 
37
+ const struct CMetricsGauge *cmetrics_gauge_get_ptr(VALUE self)
38
+ {
39
+ struct CMetricsGauge *cmetricsGauge = NULL;
40
+
41
+ TypedData_Get_Struct(
42
+ self, struct CMetricsGauge, &rb_cmetrics_gauge_type, cmetricsGauge);
43
+
44
+ if (NIL_P(self)) {
45
+ rb_raise(rb_eRuntimeError, "Given CMetrics argument must not be nil");
46
+ }
47
+ if (!cmetricsGauge->gauge) {
48
+ rb_raise(rb_eRuntimeError, "Create gauge with CMetrics::Gauge#create first.");
49
+ }
50
+ return cmetricsGauge;
51
+ }
52
+
37
53
  static void
38
54
  gauge_free(void* ptr)
39
55
  {
40
56
  struct CMetricsGauge* cmetricsGauge = (struct CMetricsGauge*)ptr;
41
57
 
42
- if (!cmetricsGauge) {
43
- if (!cmetricsGauge->gauge) {
58
+ if (cmetricsGauge) {
59
+ if (cmetricsGauge->gauge) {
44
60
  cmt_gauge_destroy(cmetricsGauge->gauge);
45
61
  }
46
- if (!cmetricsGauge->instance) {
62
+ if (cmetricsGauge->instance) {
47
63
  cmt_destroy(cmetricsGauge->instance);
48
64
  }
49
65
  }
@@ -18,9 +18,16 @@
18
18
  */
19
19
 
20
20
  #include "cmetrics_c.h"
21
+ #include <cmetrics/cmt_map.h>
22
+ #include <cmetrics/cmt_metric.h>
23
+ #include <cmetrics/cmt_cat.h>
21
24
 
22
25
  VALUE rb_cSerde;
23
26
 
27
+ extern VALUE rb_cCounter;
28
+ extern VALUE rb_cGauge;
29
+ extern VALUE rb_cUntyped;
30
+
24
31
  static void serde_free(void* ptr);
25
32
 
26
33
  static const rb_data_type_t rb_cmetrics_serde_type = { "cmetrics/serde",
@@ -39,8 +46,8 @@ serde_free(void* ptr)
39
46
  {
40
47
  struct CMetricsSerde* cmetricsSerde = (struct CMetricsSerde*)ptr;
41
48
 
42
- if (!cmetricsSerde) {
43
- if (!cmetricsSerde->instance) {
49
+ if (cmetricsSerde) {
50
+ if (cmetricsSerde->instance) {
44
51
  cmt_destroy(cmetricsSerde->instance);
45
52
  }
46
53
  }
@@ -111,7 +118,11 @@ rb_cmetrics_serde_from_msgpack(int argc, VALUE *argv, VALUE self)
111
118
  rb_raise(rb_eRuntimeError, "offset should be smaller than msgpack buffer size.");
112
119
  }
113
120
 
114
- ret = cmt_decode_msgpack_create(&cmt, StringValuePtr(rb_msgpack_buffer), msgpack_length, &offset);
121
+ if (!NIL_P(rb_msgpack_buffer)) {
122
+ ret = cmt_decode_msgpack_create(&cmt, StringValuePtr(rb_msgpack_buffer), msgpack_length, &offset);
123
+ } else {
124
+ rb_raise(rb_eArgError, "nil is not valid value for buffer");
125
+ }
115
126
 
116
127
  if (ret == 0) {
117
128
  cmetricsSerde->instance = cmt;
@@ -129,13 +140,14 @@ rb_cmetrics_serde_from_msgpack_feed_each_impl(VALUE self, VALUE rb_msgpack_buffe
129
140
  struct CMetricsSerde* cmetricsSerde;
130
141
  struct cmt *cmt = NULL;
131
142
  int ret = 0;
143
+ size_t offset = 0;
132
144
 
133
145
  RETURN_ENUMERATOR(self, 0, 0);
134
146
 
135
147
  TypedData_Get_Struct(
136
148
  self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
137
149
 
138
- for (size_t offset = 0; offset <= msgpack_length; ) {
150
+ for (offset = 0; offset <= msgpack_length; ) {
139
151
  ret = cmt_decode_msgpack_create(&cmt, StringValuePtr(rb_msgpack_buffer), msgpack_length, &offset);
140
152
  if (ret == 0) {
141
153
  cmetricsSerde->instance = cmt;
@@ -154,8 +166,46 @@ static VALUE
154
166
  rb_cmetrics_serde_from_msgpack_feed_each(VALUE self, VALUE rb_data)
155
167
  {
156
168
  RETURN_ENUMERATOR(self, 0, 0);
169
+ if (!NIL_P(rb_data)) {
170
+ return rb_cmetrics_serde_from_msgpack_feed_each_impl(self, rb_data, RSTRING_LEN(rb_data));
171
+ } else {
172
+ rb_raise(rb_eArgError, "nil is not valid value for buffer");
173
+ }
174
+ }
175
+
176
+ static VALUE
177
+ rb_cmetrics_serde_concat_metric(VALUE self, VALUE rb_data)
178
+ {
179
+ struct CMetricsSerde* cmetricsSerde = NULL;
180
+ struct CMetricsCounter* cmetricsCounter = NULL;
181
+ struct CMetricsGauge* cmetricsGauge = NULL;
182
+ struct CMetricsUntyped* cmetricsUntyped = NULL;
183
+
184
+ TypedData_Get_Struct(
185
+ self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
186
+
187
+ if (!NIL_P(rb_data)) {
188
+ if (cmetricsSerde->instance == NULL) {
189
+ cmetricsSerde->instance = cmt_create();
190
+ }
191
+
192
+ if (rb_obj_is_kind_of(rb_data, rb_cCounter)) {
193
+ cmetricsCounter = (struct CMetricsCounter *)cmetrics_counter_get_ptr(rb_data);
194
+ cmt_cat(cmetricsSerde->instance, cmetricsCounter->instance);
195
+ } else if (rb_obj_is_kind_of(rb_data, rb_cGauge)) {
196
+ cmetricsGauge = (struct CMetricsGauge *)cmetrics_gauge_get_ptr(rb_data);
197
+ cmt_cat(cmetricsSerde->instance, cmetricsGauge->instance);
198
+ } else if (rb_obj_is_kind_of(rb_data, rb_cUntyped)) {
199
+ cmetricsUntyped = (struct CMetricsUntyped *)cmetrics_untyped_get_ptr(rb_data);
200
+ cmt_cat(cmetricsSerde->instance, cmetricsUntyped->instance);
201
+ } else {
202
+ rb_raise(rb_eArgError, "specified type of instance is not supported.");
203
+ }
204
+ } else {
205
+ rb_raise(rb_eArgError, "nil is not valid value for concatenating");
206
+ }
157
207
 
158
- return rb_cmetrics_serde_from_msgpack_feed_each_impl(self, rb_data, RSTRING_LEN(rb_data));
208
+ return Qnil;
159
209
  }
160
210
 
161
211
  static VALUE
@@ -168,6 +218,10 @@ rb_cmetrics_serde_to_prometheus(VALUE self)
168
218
  TypedData_Get_Struct(
169
219
  self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
170
220
 
221
+ if (cmetricsSerde->instance == NULL) {
222
+ rb_raise(rb_eRuntimeError, "Invalid cmt context");
223
+ }
224
+
171
225
  prom = cmt_encode_prometheus_create(cmetricsSerde->instance, CMT_TRUE);
172
226
 
173
227
  str = rb_str_new2(prom);
@@ -187,6 +241,10 @@ rb_cmetrics_serde_to_influx(VALUE self)
187
241
  TypedData_Get_Struct(
188
242
  self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
189
243
 
244
+ if (cmetricsSerde->instance == NULL) {
245
+ rb_raise(rb_eRuntimeError, "Invalid cmt context");
246
+ }
247
+
190
248
  prom = cmt_encode_influx_create(cmetricsSerde->instance);
191
249
 
192
250
  str = rb_str_new2(prom);
@@ -206,6 +264,10 @@ rb_cmetrics_serde_to_msgpack(VALUE self)
206
264
  TypedData_Get_Struct(
207
265
  self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
208
266
 
267
+ if (cmetricsSerde->instance == NULL) {
268
+ rb_raise(rb_eRuntimeError, "Invalid cmt context");
269
+ }
270
+
209
271
  ret = cmt_encode_msgpack_create(cmetricsSerde->instance, &buffer, &buffer_size);
210
272
 
211
273
  if (ret == 0) {
@@ -225,6 +287,10 @@ rb_cmetrics_serde_to_text(VALUE self)
225
287
  TypedData_Get_Struct(
226
288
  self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
227
289
 
290
+ if (cmetricsSerde->instance == NULL) {
291
+ rb_raise(rb_eRuntimeError, "Invalid cmt context");
292
+ }
293
+
228
294
  buffer = cmt_encode_text_create(cmetricsSerde->instance);
229
295
  if (buffer == NULL) {
230
296
  return Qnil;
@@ -237,6 +303,151 @@ rb_cmetrics_serde_to_text(VALUE self)
237
303
  return text;
238
304
  }
239
305
 
306
+ static VALUE
307
+ append_metric_value(struct cmt_map *map,
308
+ VALUE rbHash, struct cmt_metric *metric)
309
+ {
310
+ uint64_t ts;
311
+ double val;
312
+ struct cmt_opts *opts;
313
+
314
+ opts = map->opts;
315
+
316
+ /* Retrieve metric value */
317
+ val = cmt_metric_get_value(metric);
318
+
319
+ ts = cmt_metric_get_timestamp(metric);
320
+
321
+ rb_hash_aset(rbHash, rb_str_new2("name"), rb_str_new2(opts->name));
322
+ rb_hash_aset(rbHash, rb_str_new2("description"), rb_str_new2(opts->description));
323
+ rb_hash_aset(rbHash, rb_str_new2("value"), DBL2NUM(val));
324
+ rb_hash_aset(rbHash, rb_str_new2("timestamp"), DBL2NUM(ts/1000000000.0));
325
+
326
+ return rbHash;
327
+ }
328
+
329
+ static VALUE
330
+ format_metric(struct cmt *cmt, struct cmt_map *map,
331
+ struct cmt_metric *metric)
332
+ {
333
+ int n;
334
+ int static_labels = 0;
335
+ struct cmt_map_label *label_k;
336
+ struct cmt_map_label *label_v;
337
+ struct mk_list *head;
338
+ struct cmt_opts *opts;
339
+ struct cmt_label *slabel;
340
+ VALUE rb_hash = rb_hash_new();
341
+ VALUE shash = rb_hash_new();
342
+ VALUE lhash = rb_hash_new();
343
+
344
+ opts = map->opts;
345
+
346
+ /* Measurement */
347
+ rb_hash_aset(rb_hash, rb_str_new2("namespace"), rb_str_new2(opts->ns));
348
+ rb_hash_aset(rb_hash, rb_str_new2("subsystem"), rb_str_new2(opts->subsystem));
349
+
350
+ /* Static labels (tags) */
351
+ static_labels = cmt_labels_count(cmt->static_labels);
352
+ if (static_labels > 0) {
353
+ mk_list_foreach(head, &cmt->static_labels->list) {
354
+ slabel = mk_list_entry(head, struct cmt_label, _head);
355
+ rb_hash_aset(shash, rb_str_new2(slabel->key), rb_str_new2(slabel->val));
356
+ }
357
+ rb_hash_aset(rb_hash, rb_str_new2("static_labels"), shash);
358
+ }
359
+
360
+ /* Labels / Tags */
361
+ n = mk_list_size(&metric->labels);
362
+ if (n > 0) {
363
+ label_k = mk_list_entry_first(&map->label_keys, struct cmt_map_label, _head);
364
+
365
+ mk_list_foreach(head, &metric->labels) {
366
+ label_v = mk_list_entry(head, struct cmt_map_label, _head);
367
+
368
+ rb_hash_aset(lhash, rb_str_new2(label_k->name), rb_str_new2(label_v->name));
369
+
370
+ label_k = mk_list_entry_next(&label_k->_head, struct cmt_map_label,
371
+ _head, &map->label_keys);
372
+ }
373
+ rb_hash_aset(rb_hash, rb_str_new2("labels"), lhash);
374
+ }
375
+
376
+ rb_hash = append_metric_value(map, rb_hash, metric);
377
+
378
+ return rb_hash;
379
+ }
380
+
381
+ static VALUE
382
+ format_metrics(struct cmt *cmt,
383
+ struct cmt_map *map, int add_timestamp)
384
+ {
385
+ VALUE rbMetrics = rb_ary_new();
386
+ VALUE rbMetric;
387
+ struct mk_list *head;
388
+ struct cmt_metric *metric;
389
+
390
+ /* Simple metric, no labels */
391
+ if (map->metric_static_set == 1) {
392
+ rbMetric = format_metric(cmt, map, &map->metric);
393
+ rb_ary_push(rbMetrics, rbMetric);
394
+ }
395
+
396
+ mk_list_foreach(head, &map->metrics) {
397
+ metric = mk_list_entry(head, struct cmt_metric, _head);
398
+ rbMetric = format_metric(cmt, map, metric);
399
+ rb_ary_push(rbMetrics, rbMetric);
400
+ }
401
+
402
+ return rbMetrics;
403
+ }
404
+
405
+ static VALUE
406
+ rb_cmetrics_serde_get_metrics(VALUE self)
407
+ {
408
+ VALUE rbMetrics = rb_ary_new();
409
+ VALUE rbMetricsInner = rb_ary_new();
410
+ struct CMetricsSerde* cmetricsSerde;
411
+ struct mk_list *head;
412
+ struct cmt_gauge *gauge;
413
+ struct cmt_counter *counter;
414
+ struct cmt_untyped *untyped;
415
+ struct cmt *cmt;
416
+ int add_timestamp = CMT_TRUE;
417
+
418
+ TypedData_Get_Struct(
419
+ self, struct CMetricsSerde, &rb_cmetrics_serde_type, cmetricsSerde);
420
+
421
+ cmt = cmetricsSerde->instance;
422
+
423
+ if (cmt == NULL) {
424
+ rb_raise(rb_eRuntimeError, "Invalid cmt context");
425
+ }
426
+
427
+ /* Counters */
428
+ mk_list_foreach(head, &cmt->counters) {
429
+ counter = mk_list_entry(head, struct cmt_counter, _head);
430
+ rbMetricsInner = format_metrics(cmt, counter->map, add_timestamp);
431
+ rb_ary_push(rbMetrics, rbMetricsInner);
432
+ }
433
+
434
+ /* Gauges */
435
+ mk_list_foreach(head, &cmt->gauges) {
436
+ gauge = mk_list_entry(head, struct cmt_gauge, _head);
437
+ rbMetricsInner = format_metrics(cmt, gauge->map, add_timestamp);
438
+ rb_ary_push(rbMetrics, rbMetricsInner);
439
+ }
440
+
441
+ /* Untyped */
442
+ mk_list_foreach(head, &cmt->untypeds) {
443
+ untyped = mk_list_entry(head, struct cmt_untyped, _head);
444
+ rbMetricsInner = format_metrics(cmt, untyped->map, add_timestamp);
445
+ rb_ary_push(rbMetrics, rbMetricsInner);
446
+ }
447
+
448
+ return rbMetrics;
449
+ }
450
+
240
451
  void Init_cmetrics_serde(VALUE rb_mCMetrics)
241
452
  {
242
453
  rb_cSerde = rb_define_class_under(rb_mCMetrics, "Serde", rb_cObject);
@@ -244,10 +455,13 @@ void Init_cmetrics_serde(VALUE rb_mCMetrics)
244
455
  rb_define_alloc_func(rb_cSerde, rb_cmetrics_serde_alloc);
245
456
 
246
457
  rb_define_method(rb_cSerde, "initialize", rb_cmetrics_serde_initialize, 0);
458
+ rb_define_method(rb_cSerde, "concat", rb_cmetrics_serde_concat_metric, 1);
247
459
  rb_define_method(rb_cSerde, "from_msgpack", rb_cmetrics_serde_from_msgpack, -1);
248
460
  rb_define_method(rb_cSerde, "to_prometheus", rb_cmetrics_serde_to_prometheus, 0);
249
461
  rb_define_method(rb_cSerde, "to_influx", rb_cmetrics_serde_to_influx, 0);
250
462
  rb_define_method(rb_cSerde, "to_msgpack", rb_cmetrics_serde_to_msgpack, 0);
251
463
  rb_define_method(rb_cSerde, "feed_each", rb_cmetrics_serde_from_msgpack_feed_each, 1);
252
464
  rb_define_method(rb_cSerde, "to_s", rb_cmetrics_serde_to_text, 0);
465
+ rb_define_method(rb_cSerde, "get_metrics", rb_cmetrics_serde_get_metrics, 0);
466
+ rb_define_method(rb_cSerde, "metrics", rb_cmetrics_serde_get_metrics, 0);
253
467
  }
@@ -34,16 +34,32 @@ static const rb_data_type_t rb_cmetrics_untyped_type = { "cmetrics/untyped",
34
34
  RUBY_TYPED_FREE_IMMEDIATELY };
35
35
 
36
36
 
37
+ const struct CMetricsUntyped *cmetrics_untyped_get_ptr(VALUE self)
38
+ {
39
+ struct CMetricsUntyped *cmetricsUntyped = NULL;
40
+
41
+ TypedData_Get_Struct(
42
+ self, struct CMetricsUntyped, &rb_cmetrics_untyped_type, cmetricsUntyped);
43
+
44
+ if (NIL_P(self)) {
45
+ rb_raise(rb_eRuntimeError, "Given CMetrics argument must not be nil");
46
+ }
47
+ if (!cmetricsUntyped->untyped) {
48
+ rb_raise(rb_eRuntimeError, "Create untyped with CMetrics::Untyped#create first.");
49
+ }
50
+ return cmetricsUntyped;
51
+ }
52
+
37
53
  static void
38
54
  untyped_free(void* ptr)
39
55
  {
40
56
  struct CMetricsUntyped* cmetricsUntyped = (struct CMetricsUntyped*)ptr;
41
57
 
42
- if (!cmetricsUntyped) {
43
- if (!cmetricsUntyped->untyped) {
58
+ if (cmetricsUntyped) {
59
+ if (cmetricsUntyped->untyped) {
44
60
  cmt_untyped_destroy(cmetricsUntyped->untyped);
45
61
  }
46
- if (!cmetricsUntyped->instance) {
62
+ if (cmetricsUntyped->instance) {
47
63
  cmt_destroy(cmetricsUntyped->instance);
48
64
  }
49
65
  }
@@ -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: "3375ac1073b0bbea44ee9986601977ef04b119a74a7c663e2486a3ed31953203",
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.7"
4
+ VERSION = "0.2.0.1"
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.7
4
+ version: 0.2.0.1
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-13 00:00:00.000000000 Z
11
+ date: 2021-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,20 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: msgpack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.2
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: mini_portile2
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: '2.1'
89
+ version: '2.6'
76
90
  type: :runtime
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: '2.1'
96
+ version: '2.6'
83
97
  description: C binding for cmetric library.
84
98
  email:
85
99
  - cosmo0920.oucc@gmail.com
@@ -88,9 +102,11 @@ extensions:
88
102
  - ext/cmetrics/extconf.rb
89
103
  extra_rdoc_files: []
90
104
  files:
105
+ - ".github/workflows/apt.yml"
91
106
  - ".github/workflows/linux.yml"
92
107
  - ".github/workflows/macos.yml"
93
108
  - ".github/workflows/windows.yml"
109
+ - ".github/workflows/yum.yml"
94
110
  - ".gitignore"
95
111
  - Gemfile
96
112
  - LICENSE
@@ -98,8 +114,9 @@ files:
98
114
  - Rakefile
99
115
  - bin/console
100
116
  - bin/setup
117
+ - ci/apt-test.sh
118
+ - ci/yum-test.sh
101
119
  - cmetrics-ruby.gemspec
102
- - ext/cmetrics-master.tar.gz
103
120
  - ext/cmetrics/cmetrics.c
104
121
  - ext/cmetrics/cmetrics_c.h
105
122
  - ext/cmetrics/cmetrics_counter.c
@@ -130,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
147
  - !ruby/object:Gem::Version
131
148
  version: '0'
132
149
  requirements: []
133
- rubygems_version: 3.0.3
150
+ rubygems_version: 3.2.22
134
151
  signing_key:
135
152
  specification_version: 4
136
153
  summary: C binding for cmetric library.
Binary file