-
Notifications
You must be signed in to change notification settings - Fork 116
/
appsignal_extension.c
949 lines (722 loc) · 23.8 KB
/
appsignal_extension.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
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
187
188
189
190
191
192
193
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
268
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
343
344
345
346
347
348
349
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
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
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
527
528
529
530
531
532
533
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
#include "ruby/ruby.h"
#include "ruby/encoding.h"
#include "appsignal.h"
static inline appsignal_string_t make_appsignal_string(VALUE str) {
return (appsignal_string_t) {
.len = RSTRING_LEN(str),
.buf = RSTRING_PTR(str)
};
}
static inline VALUE make_ruby_string(appsignal_string_t string) {
VALUE str = rb_str_new(string.buf, string.len);
rb_enc_associate(str, rb_utf8_encoding());
return str;
}
VALUE Appsignal;
VALUE Extension;
VALUE Transaction;
VALUE Data;
VALUE Span;
static VALUE start(VALUE self) {
appsignal_start();
return Qnil;
}
static VALUE stop(VALUE self) {
appsignal_stop();
return Qnil;
}
static VALUE diagnose(VALUE self) {
return make_ruby_string(appsignal_diagnose());
}
static VALUE get_server_state(VALUE self, VALUE key) {
appsignal_string_t string;
Check_Type(key, T_STRING);
string = appsignal_get_server_state(make_appsignal_string(key));
if (string.len > 0) {
return make_ruby_string(string);
} else {
return Qnil;
}
}
static VALUE start_transaction(VALUE self, VALUE transaction_id, VALUE namespace, VALUE gc_duration_ms) {
appsignal_transaction_t* transaction;
Check_Type(transaction_id, T_STRING);
Check_Type(namespace, T_STRING);
Check_Type(gc_duration_ms, T_FIXNUM);
transaction = appsignal_start_transaction(
make_appsignal_string(transaction_id),
make_appsignal_string(namespace),
NUM2LONG(gc_duration_ms)
);
if (transaction) {
return Data_Wrap_Struct(Transaction, NULL, appsignal_free_transaction, transaction);
} else {
return Qnil;
}
}
static VALUE start_event(VALUE self, VALUE gc_duration_ms) {
appsignal_transaction_t* transaction;
Check_Type(gc_duration_ms, T_FIXNUM);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
appsignal_start_event(transaction, NUM2LONG(gc_duration_ms));
return Qnil;
}
static VALUE finish_event(VALUE self, VALUE name, VALUE title, VALUE body, VALUE body_format, VALUE gc_duration_ms) {
appsignal_transaction_t* transaction;
appsignal_data_t* body_data;
int body_type;
Check_Type(name, T_STRING);
Check_Type(title, T_STRING);
Check_Type(body_format, T_FIXNUM);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
body_type = TYPE(body);
if (body_type == T_STRING) {
appsignal_finish_event(
transaction,
make_appsignal_string(name),
make_appsignal_string(title),
make_appsignal_string(body),
FIX2INT(body_format),
FIX2LONG(gc_duration_ms)
);
} else if (body_type == RUBY_T_DATA) {
Data_Get_Struct(body, appsignal_data_t, body_data);
appsignal_finish_event_data(
transaction,
make_appsignal_string(name),
make_appsignal_string(title),
body_data,
FIX2INT(body_format),
FIX2LONG(gc_duration_ms)
);
} else {
rb_raise(rb_eTypeError, "body should be a String or Appsignal::Extension::Data");
}
return Qnil;
}
static VALUE record_event(VALUE self, VALUE name, VALUE title, VALUE body, VALUE body_format, VALUE duration, VALUE gc_duration_ms) {
appsignal_transaction_t* transaction;
appsignal_data_t* body_data;
int body_type;
int duration_type;
Check_Type(name, T_STRING);
Check_Type(title, T_STRING);
duration_type = TYPE(duration);
if (duration_type != T_FIXNUM && duration_type != T_BIGNUM) {
rb_raise(rb_eTypeError, "duration should be an Integer");
}
Check_Type(body_format, T_FIXNUM);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
body_type = TYPE(body);
if (body_type == T_STRING) {
appsignal_record_event(
transaction,
make_appsignal_string(name),
make_appsignal_string(title),
make_appsignal_string(body),
FIX2INT(body_format),
NUM2LONG(duration),
NUM2LONG(gc_duration_ms)
);
} else if (body_type == RUBY_T_DATA) {
Data_Get_Struct(body, appsignal_data_t, body_data);
appsignal_record_event_data(
transaction,
make_appsignal_string(name),
make_appsignal_string(title),
body_data,
FIX2INT(body_format),
NUM2LONG(duration),
NUM2LONG(gc_duration_ms)
);
} else {
rb_raise(rb_eTypeError, "body should be a String or Appsignal::Extension::Data");
}
return Qnil;
}
static VALUE set_transaction_error(VALUE self, VALUE name, VALUE message, VALUE backtrace) {
appsignal_transaction_t* transaction;
appsignal_data_t* backtrace_data;
Check_Type(name, T_STRING);
Check_Type(message, T_STRING);
Check_Type(backtrace, RUBY_T_DATA);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
Data_Get_Struct(backtrace, appsignal_data_t, backtrace_data);
appsignal_set_transaction_error(
transaction,
make_appsignal_string(name),
make_appsignal_string(message),
backtrace_data
);
return Qnil;
}
static VALUE set_transaction_sample_data(VALUE self, VALUE key, VALUE payload) {
appsignal_transaction_t* transaction;
appsignal_data_t* payload_data;
Check_Type(key, T_STRING);
Check_Type(payload, RUBY_T_DATA);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
Data_Get_Struct(payload, appsignal_data_t, payload_data);
appsignal_set_transaction_sample_data(
transaction,
make_appsignal_string(key),
payload_data
);
return Qnil;
}
static VALUE set_transaction_action(VALUE self, VALUE action) {
appsignal_transaction_t* transaction;
Check_Type(action, T_STRING);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
appsignal_set_transaction_action(
transaction,
make_appsignal_string(action)
);
return Qnil;
}
static VALUE set_transaction_namespace(VALUE self, VALUE namespace) {
appsignal_transaction_t* transaction;
Check_Type(namespace, T_STRING);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
appsignal_set_transaction_namespace(
transaction,
make_appsignal_string(namespace)
);
return Qnil;
}
static VALUE set_transaction_queue_start(VALUE self, VALUE queue_start) {
appsignal_transaction_t* transaction;
int queue_start_type;
queue_start_type = TYPE(queue_start);
if (queue_start_type != T_FIXNUM && queue_start_type != T_BIGNUM) {
rb_raise(rb_eTypeError, "queue_start should be an Integer");
}
Data_Get_Struct(self, appsignal_transaction_t, transaction);
appsignal_set_transaction_queue_start(
transaction,
NUM2LONG(queue_start)
);
return Qnil;
}
static VALUE set_transaction_metadata(VALUE self, VALUE key, VALUE value) {
appsignal_transaction_t* transaction;
Check_Type(key, T_STRING);
Check_Type(value, T_STRING);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
appsignal_set_transaction_metadata(
transaction,
make_appsignal_string(key),
make_appsignal_string(value)
);
return Qnil;
}
static VALUE finish_transaction(VALUE self, VALUE gc_duration_ms) {
appsignal_transaction_t* transaction;
int sample;
Check_Type(gc_duration_ms, T_FIXNUM);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
sample = appsignal_finish_transaction(transaction, NUM2LONG(gc_duration_ms));
return sample == 1 ? Qtrue : Qfalse;
}
static VALUE duplicate_transaction(VALUE self, VALUE new_transaction_id) {
appsignal_transaction_t* transaction;
appsignal_transaction_t* duplicate_transaction;
Check_Type(new_transaction_id, T_STRING);
Data_Get_Struct(self, appsignal_transaction_t, transaction);
duplicate_transaction = appsignal_duplicate_transaction(
transaction,
make_appsignal_string(new_transaction_id)
);
if (duplicate_transaction) {
return Data_Wrap_Struct(
Transaction,
NULL,
appsignal_free_transaction,
duplicate_transaction
);
} else {
return Qnil;
}
}
static VALUE complete_transaction(VALUE self) {
appsignal_transaction_t* transaction;
Data_Get_Struct(self, appsignal_transaction_t, transaction);
appsignal_complete_transaction(transaction);
return Qnil;
}
static VALUE transaction_to_json(VALUE self) {
appsignal_transaction_t* transaction;
appsignal_string_t json;
Data_Get_Struct(self, appsignal_transaction_t, transaction);
json = appsignal_transaction_to_json(transaction);
if (json.len == 0) {
return Qnil;
} else {
return make_ruby_string(json);
}
}
static VALUE data_map_new(VALUE self) {
appsignal_data_t* data;
data = appsignal_data_map_new();
if (data) {
return Data_Wrap_Struct(Data, NULL, appsignal_free_data, data);
} else {
return Qnil;
}
}
static VALUE data_array_new(VALUE self) {
appsignal_data_t* data;
data = appsignal_data_array_new();
if (data) {
return Data_Wrap_Struct(Data, NULL, appsignal_free_data, data);
} else {
return Qnil;
}
}
static VALUE data_set_string(VALUE self, VALUE key, VALUE value) {
appsignal_data_t* data;
Check_Type(key, T_STRING);
Check_Type(value, T_STRING);
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_map_set_string(
data,
make_appsignal_string(key),
make_appsignal_string(value)
);
return Qnil;
}
static VALUE data_set_integer(VALUE self, VALUE key, VALUE value) {
appsignal_data_t* data;
VALUE value_type = TYPE(value);
Check_Type(key, T_STRING);
if (value_type != T_FIXNUM && value_type != T_BIGNUM) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(value));
}
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_map_set_integer(
data,
make_appsignal_string(key),
NUM2LONG(value)
);
return Qnil;
}
static VALUE data_set_float(VALUE self, VALUE key, VALUE value) {
appsignal_data_t* data;
Check_Type(key, T_STRING);
Check_Type(value, T_FLOAT);
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_map_set_float(
data,
make_appsignal_string(key),
NUM2DBL(value)
);
return Qnil;
}
static VALUE data_set_boolean(VALUE self, VALUE key, VALUE value) {
appsignal_data_t* data;
Check_Type(key, T_STRING);
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_map_set_boolean(
data,
make_appsignal_string(key),
RTEST(value)
);
return Qnil;
}
static VALUE data_set_nil(VALUE self, VALUE key) {
appsignal_data_t* data;
Check_Type(key, T_STRING);
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_map_set_null(
data,
make_appsignal_string(key)
);
return Qnil;
}
static VALUE data_set_data(VALUE self, VALUE key, VALUE value) {
appsignal_data_t* data;
appsignal_data_t* value_data;
Check_Type(key, T_STRING);
Check_Type(value, RUBY_T_DATA);
Data_Get_Struct(self, appsignal_data_t, data);
Data_Get_Struct(value, appsignal_data_t, value_data);
appsignal_data_map_set_data(
data,
make_appsignal_string(key),
value_data
);
return Qnil;
}
static VALUE data_append_string(VALUE self, VALUE value) {
appsignal_data_t* data;
Check_Type(value, T_STRING);
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_array_append_string(
data,
make_appsignal_string(value)
);
return Qnil;
}
static VALUE data_append_integer(VALUE self, VALUE value) {
appsignal_data_t* data;
VALUE value_type = TYPE(value);
if (value_type != T_FIXNUM && value_type != T_BIGNUM) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(value));
}
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_array_append_integer(
data,
NUM2LONG(value)
);
return Qnil;
}
static VALUE data_append_float(VALUE self, VALUE value) {
appsignal_data_t* data;
Check_Type(value, T_FLOAT);
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_array_append_float(
data,
NUM2DBL(value)
);
return Qnil;
}
static VALUE data_append_boolean(VALUE self, VALUE value) {
appsignal_data_t* data;
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_array_append_boolean(
data,
RTEST(value)
);
return Qnil;
}
static VALUE data_append_nil(VALUE self) {
appsignal_data_t* data;
Data_Get_Struct(self, appsignal_data_t, data);
appsignal_data_array_append_null(data);
return Qnil;
}
static VALUE data_append_data(VALUE self, VALUE value) {
appsignal_data_t* data;
appsignal_data_t* value_data;
Check_Type(value, RUBY_T_DATA);
Data_Get_Struct(self, appsignal_data_t, data);
Data_Get_Struct(value, appsignal_data_t, value_data);
appsignal_data_array_append_data(
data,
value_data
);
return Qnil;
}
static VALUE data_equal(VALUE self, VALUE other) {
appsignal_data_t* data;
appsignal_data_t* other_data;
if (TYPE(other) != RUBY_T_DATA) {
return Qfalse;
}
Data_Get_Struct(self, appsignal_data_t, data);
Data_Get_Struct(other, appsignal_data_t, other_data);
if (appsignal_data_equal(data, other_data) == 1) {
return Qtrue;
} else {
return Qfalse;
}
}
static VALUE data_to_s(VALUE self) {
appsignal_data_t* data;
appsignal_string_t json;
Data_Get_Struct(self, appsignal_data_t, data);
json = appsignal_data_to_json(data);
if (json.len == 0) {
return Qnil;
} else {
return make_ruby_string(json);
}
}
static VALUE root_span_new(VALUE self, VALUE namespace) {
appsignal_span_t* span;
Check_Type(namespace, T_STRING);
span = appsignal_create_root_span(make_appsignal_string(namespace));
if (span) {
return Data_Wrap_Struct(Span, NULL, appsignal_free_span, span);
} else {
return Qnil;
}
}
static VALUE child_span_new(VALUE self) {
appsignal_span_t* parent;
appsignal_span_t* span;
Data_Get_Struct(self, appsignal_span_t, parent);
span = appsignal_create_child_span(parent);
if (span) {
return Data_Wrap_Struct(Span, NULL, appsignal_free_span, span);
} else {
return Qnil;
}
}
static VALUE set_span_name(VALUE self, VALUE name) {
appsignal_span_t* span;
Check_Type(name, T_STRING);
Data_Get_Struct(self, appsignal_span_t, span);
appsignal_set_span_name(span, make_appsignal_string(name));
return Qnil;
}
static VALUE add_span_error(VALUE self, VALUE name, VALUE message, VALUE backtrace) {
appsignal_span_t* span;
appsignal_data_t* backtrace_data;
Check_Type(name, T_STRING);
Check_Type(message, T_STRING);
Check_Type(backtrace, RUBY_T_DATA);
Data_Get_Struct(self, appsignal_span_t, span);
Data_Get_Struct(backtrace, appsignal_data_t, backtrace_data);
appsignal_add_span_error(
span,
make_appsignal_string(name),
make_appsignal_string(message),
backtrace_data
);
return Qnil;
}
static VALUE set_span_sample_data(VALUE self, VALUE key, VALUE payload) {
appsignal_span_t* span;
appsignal_data_t* payload_data;
Check_Type(key, T_STRING);
Check_Type(payload, RUBY_T_DATA);
Data_Get_Struct(self, appsignal_span_t, span);
Data_Get_Struct(payload, appsignal_data_t, payload_data);
appsignal_set_span_sample_data(
span,
make_appsignal_string(key),
payload_data
);
return Qnil;
}
static VALUE set_span_attribute_string(VALUE self, VALUE key, VALUE value) {
appsignal_span_t* span;
Check_Type(key, T_STRING);
Check_Type(value, T_STRING);
Data_Get_Struct(self, appsignal_span_t, span);
appsignal_set_span_attribute_string(
span,
make_appsignal_string(key),
make_appsignal_string(value)
);
return Qnil;
}
static VALUE set_span_attribute_int(VALUE self, VALUE key, VALUE value) {
appsignal_span_t* span;
VALUE value_type = TYPE(value);
Check_Type(key, T_STRING);
if (value_type != T_FIXNUM && value_type != T_BIGNUM) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(value));
}
Data_Get_Struct(self, appsignal_span_t, span);
appsignal_set_span_attribute_int(
span,
make_appsignal_string(key),
NUM2LONG(value)
);
return Qnil;
}
static VALUE set_span_attribute_bool(VALUE self, VALUE key, VALUE value) {
appsignal_span_t* span;
Check_Type(key, T_STRING);
Data_Get_Struct(self, appsignal_span_t, span);
appsignal_set_span_attribute_bool(
span,
make_appsignal_string(key),
RTEST(value)
);
return Qnil;
}
static VALUE set_span_attribute_double(VALUE self, VALUE key, VALUE value) {
appsignal_span_t* span;
Check_Type(key, T_STRING);
Check_Type(value, T_FLOAT);
Data_Get_Struct(self, appsignal_span_t, span);
appsignal_set_span_attribute_double(
span,
make_appsignal_string(key),
NUM2DBL(value)
);
return Qnil;
}
static VALUE span_to_json(VALUE self) {
appsignal_span_t* span;
appsignal_string_t json;
Data_Get_Struct(self, appsignal_span_t, span);
json = appsignal_span_to_json(span);
if (json.len == 0) {
return Qnil;
} else {
return make_ruby_string(json);
}
}
static VALUE close_span(VALUE self) {
appsignal_span_t* span;
Data_Get_Struct(self, appsignal_span_t, span);
appsignal_close_span(span);
return Qnil;
}
static VALUE a_log(VALUE self, VALUE group, VALUE severity, VALUE format, VALUE message, VALUE attributes) {
appsignal_data_t* attributes_data;
Check_Type(group, T_STRING);
Check_Type(severity, T_FIXNUM);
Check_Type(format, T_FIXNUM);
Check_Type(message, T_STRING);
Check_Type(attributes, RUBY_T_DATA);
Data_Get_Struct(attributes, appsignal_data_t, attributes_data);
appsignal_log(
make_appsignal_string(group),
FIX2INT(severity),
FIX2INT(format),
make_appsignal_string(message),
attributes_data
);
return Qnil;
}
static VALUE set_gauge(VALUE self, VALUE key, VALUE value, VALUE tags) {
appsignal_data_t* tags_data;
Check_Type(key, T_STRING);
Check_Type(value, T_FLOAT);
Check_Type(tags, RUBY_T_DATA);
Data_Get_Struct(tags, appsignal_data_t, tags_data);
appsignal_set_gauge(
make_appsignal_string(key),
NUM2DBL(value),
tags_data
);
return Qnil;
}
static VALUE increment_counter(VALUE self, VALUE key, VALUE count, VALUE tags) {
appsignal_data_t* tags_data;
Check_Type(key, T_STRING);
Check_Type(count, T_FLOAT);
Check_Type(tags, RUBY_T_DATA);
Data_Get_Struct(tags, appsignal_data_t, tags_data);
appsignal_increment_counter(
make_appsignal_string(key),
NUM2DBL(count),
tags_data
);
return Qnil;
}
static VALUE add_distribution_value(VALUE self, VALUE key, VALUE value, VALUE tags) {
appsignal_data_t* tags_data;
Check_Type(key, T_STRING);
Check_Type(value, T_FLOAT);
Check_Type(tags, RUBY_T_DATA);
Data_Get_Struct(tags, appsignal_data_t, tags_data);
appsignal_add_distribution_value(
make_appsignal_string(key),
NUM2DBL(value),
tags_data
);
return Qnil;
}
static void track_allocation(rb_event_flag_t flag, VALUE arg1, VALUE arg2, ID arg3, VALUE arg4) {
appsignal_track_allocation();
}
static VALUE install_allocation_event_hook() {
// This event hook is only available on Ruby 2.1 and 2.2
#if defined(RUBY_INTERNAL_EVENT_NEWOBJ)
rb_add_event_hook(
track_allocation,
RUBY_INTERNAL_EVENT_NEWOBJ,
Qnil
);
#endif
return Qnil;
}
static VALUE running_in_container() {
return appsignal_running_in_container() == 1 ? Qtrue : Qfalse;
}
static VALUE set_environment_metadata(VALUE self, VALUE key, VALUE value) {
appsignal_set_environment_metadata(
make_appsignal_string(key),
make_appsignal_string(value)
);
return Qnil;
}
void Init_appsignal_extension(void) {
Appsignal = rb_define_module("Appsignal");
Extension = rb_define_class_under(Appsignal, "Extension", rb_cObject);
rb_undef_alloc_func(Extension);
Transaction = rb_define_class_under(Extension, "Transaction", rb_cObject);
rb_undef_alloc_func(Transaction);
Data = rb_define_class_under(Extension, "Data", rb_cObject);
rb_undef_alloc_func(Data);
Span = rb_define_class_under(Extension, "Span", rb_cObject);
rb_undef_alloc_func(Span);
// Starting and stopping
rb_define_singleton_method(Extension, "start", start, 0);
rb_define_singleton_method(Extension, "stop", stop, 0);
// Diagnostics
rb_define_singleton_method(Extension, "diagnose", diagnose, 0);
// Logging
rb_define_singleton_method(Extension, "log", a_log, 5);
// Server state
rb_define_singleton_method(Extension, "get_server_state", get_server_state, 1);
// Start transaction
rb_define_singleton_method(Extension, "start_transaction", start_transaction, 3);
// Transaction instance methods
rb_define_method(Transaction, "start_event", start_event, 1);
rb_define_method(Transaction, "finish_event", finish_event, 5);
rb_define_method(Transaction, "record_event", record_event, 6);
rb_define_method(Transaction, "set_error", set_transaction_error, 3);
rb_define_method(Transaction, "set_sample_data", set_transaction_sample_data, 2);
rb_define_method(Transaction, "set_action", set_transaction_action, 1);
rb_define_method(Transaction, "set_namespace", set_transaction_namespace, 1);
rb_define_method(Transaction, "set_queue_start", set_transaction_queue_start, 1);
rb_define_method(Transaction, "set_metadata", set_transaction_metadata, 2);
rb_define_method(Transaction, "finish", finish_transaction, 1);
rb_define_method(Transaction, "duplicate", duplicate_transaction, 1);
rb_define_method(Transaction, "complete", complete_transaction, 0);
rb_define_method(Transaction, "to_json", transaction_to_json, 0);
// Create a data map or array
rb_define_singleton_method(Extension, "data_map_new", data_map_new, 0);
rb_define_singleton_method(Extension, "data_array_new", data_array_new, 0);
// Add content to a data map
rb_define_method(Data, "set_string", data_set_string, 2);
rb_define_method(Data, "set_integer", data_set_integer, 2);
rb_define_method(Data, "set_float", data_set_float, 2);
rb_define_method(Data, "set_boolean", data_set_boolean, 2);
rb_define_method(Data, "set_nil", data_set_nil, 1);
rb_define_method(Data, "set_data", data_set_data, 2);
// Add content to a data array
rb_define_method(Data, "append_string", data_append_string, 1);
rb_define_method(Data, "append_integer", data_append_integer, 1);
rb_define_method(Data, "append_float", data_append_float, 1);
rb_define_method(Data, "append_boolean", data_append_boolean, 1);
rb_define_method(Data, "append_nil", data_append_nil, 0);
rb_define_method(Data, "append_data", data_append_data, 1);
// Data equality
rb_define_method(Data, "==", data_equal, 1);
// Get JSON content of a data
rb_define_method(Data, "to_s", data_to_s, 0);
// Create a span
rb_define_singleton_method(Span, "root", root_span_new, 1);
rb_define_method(Span, "child", child_span_new, 0);
// Set span error
rb_define_method(Span, "add_error", add_span_error, 3);
// Set span sample data
rb_define_method(Span, "set_sample_data", set_span_sample_data, 2);
// Span name and namespace
rb_define_method(Span, "set_name", set_span_name, 1);
// Set attributes on a span
rb_define_method(Span, "set_attribute_string", set_span_attribute_string, 2);
rb_define_method(Span, "set_attribute_int", set_span_attribute_int, 2);
rb_define_method(Span, "set_attribute_bool", set_span_attribute_bool, 2);
rb_define_method(Span, "set_attribute_double", set_span_attribute_double, 2);
// Span to json
rb_define_method(Span, "to_json", span_to_json, 0);
// Close span
rb_define_method(Span, "close", close_span, 0);
// Other helper methods
rb_define_singleton_method(Extension, "install_allocation_event_hook", install_allocation_event_hook, 0);
rb_define_singleton_method(Extension, "running_in_container?", running_in_container, 0);
rb_define_singleton_method(Extension, "set_environment_metadata", set_environment_metadata, 2);
// Metrics
rb_define_singleton_method(Extension, "set_gauge", set_gauge, 3);
rb_define_singleton_method(Extension, "increment_counter", increment_counter, 3);
rb_define_singleton_method(Extension, "add_distribution_value", add_distribution_value, 3);
}