forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_txn.c
8126 lines (7036 loc) · 299 KB
/
test_txn.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
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include "nr_attributes.h"
#include "nr_attributes_private.h"
#include "nr_analytics_events.h"
#include "nr_distributed_trace.h"
#include "nr_distributed_trace_private.h"
#include "nr_errors.h"
#include "nr_guid.h"
#include "nr_header.h"
#include "nr_header_private.h"
#include "nr_rules.h"
#include "nr_segment.h"
#include "nr_segment_traces.h"
#include "nr_segment_tree.h"
#include "nr_slowsqls.h"
#include "nr_txn.h"
#include "nr_txn_private.h"
#include "util_base64.h"
#include "util_flatbuffers.h"
#include "util_memory.h"
#include "util_metrics.h"
#include "util_metrics_private.h"
#include "util_random.h"
#include "util_strings.h"
#include "util_text.h"
#include "util_url.h"
#include "nr_commands_private.h"
#include "tlib_main.h"
#include "test_app_helpers.h"
#include "test_segment_helpers.h"
typedef struct _test_txn_state_t {
nrapp_t* txns_app;
} test_txn_state_t;
/*
* hash_is_subset_of is a callback that can be passed to nro_iteratehash. It
* asserts that one hashmap is a subset of another hashmap:
*
* nro_iteratehash(subset, hash_is_subset_of, fullset);
*
* The composite hash_is_subset_of_data_t is used to let the tlib
* assertion print a valid test name. This is especially useful for
* cross agent tests read from JSON definitions.
*/
typedef struct {
const char* testname;
nrobj_t* set;
const char* file;
int line;
} hash_is_subset_of_data_t;
static nr_status_t hash_is_subset_of(const char* key,
const nrobj_t* val,
void* ptr) {
hash_is_subset_of_data_t* data = (hash_is_subset_of_data_t*)ptr;
/*
* Comparing the JSON representation allows us to compare values of arbitrary
* types.
*/
char* expected = nro_to_json(val);
char* found = nro_to_json(nro_get_hash_value(data->set, key, NULL));
test_pass_if_true_file_line(
data->testname, 0 == nr_strcmp(expected, found), data->file, data->line,
"key='%s' expected='%s' found='%s'", NRSAFESTR(key), NRSAFESTR(expected),
NRSAFESTR(found));
nr_free(expected);
nr_free(found);
return NR_SUCCESS;
}
#define TEST_DAEMON_ID 1357
nrapp_t* nr_app_verify_id(nrapplist_t* applist NRUNUSED,
const char* agent_run_id NRUNUSED) {
nr_status_t rv;
test_txn_state_t* p = (test_txn_state_t*)tlib_getspecific();
if (0 == p->txns_app) {
return 0;
}
rv = nrt_mutex_lock(&p->txns_app->app_lock);
tlib_pass_if_true("app locked", NR_SUCCESS == rv, "rv=%d", (int)rv);
return p->txns_app;
}
#define test_freeze_name(...) \
test_freeze_name_fn(__VA_ARGS__, __FILE__, __LINE__)
static void test_freeze_name_fn(const char* testname,
nr_path_type_t path_type,
int background,
const char* path,
const char* rules,
const char* segment_terms,
const char* expected_name,
const char* file,
int line) {
nr_status_t rv;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
nrapp_t appv = {.info = {0}};
nrapp_t* app = &appv;
test_txn_state_t* p = (test_txn_state_t*)tlib_getspecific();
nrt_mutex_init(&app->app_lock, 0);
txn->app_connect_reply = 0;
p->txns_app = app;
txn->status.ignore = 0;
txn->name = 0;
txn->options.apdex_t = 0;
txn->options.tt_is_apdex_f = 0;
txn->options.tt_threshold = 0;
txn->status.path_is_frozen = 0;
txn->status.path_type = path_type;
txn->status.background = background;
txn->path = nr_strdup(path);
if (rules) {
nrobj_t* ob = nro_create_from_json(rules);
app->url_rules
= nr_rules_create_from_obj(nro_get_hash_array(ob, "url_rules", 0));
app->txn_rules
= nr_rules_create_from_obj(nro_get_hash_array(ob, "txn_rules", 0));
nro_delete(ob);
} else {
app->url_rules = 0;
app->txn_rules = 0;
}
if (segment_terms) {
nrobj_t* st_obj = nro_create_from_json(segment_terms);
app->segment_terms = nr_segment_terms_create_from_obj(st_obj);
nro_delete(st_obj);
} else {
app->segment_terms = 0;
}
rv = nr_txn_freeze_name_update_apdex(txn);
/* Txn path should be frozen no matter the return value. */
test_pass_if_true(testname, (0 != txn->status.path_is_frozen),
"txn->status.path_is_frozen=%d",
txn->status.path_is_frozen);
/*
* Since there are no key transactions (0 == txn->app_connect_reply), apdex
* and threshold should be unchanged.
*/
test_pass_if_true(
testname, (0 == txn->options.tt_threshold) && (0 == txn->options.apdex_t),
"txn->options.tt_threshold=" NR_TIME_FMT
" txn->options.apdex_t=" NR_TIME_FMT,
txn->options.tt_threshold, txn->options.apdex_t);
if (0 == expected_name) {
test_pass_if_true(testname, NR_FAILURE == rv, "rv=%d", (int)rv);
} else {
test_pass_if_true(testname, NR_SUCCESS == rv, "rv=%d", (int)rv);
test_pass_if_true(testname, 0 == nr_strcmp(expected_name, txn->name),
"expected_name=%s actual_name=%s", expected_name,
NRSAFESTR(txn->name));
}
nr_free(txn->path);
nr_free(txn->name);
nr_rules_destroy(&app->url_rules);
nr_rules_destroy(&app->txn_rules);
nr_segment_terms_destroy(&app->segment_terms);
nrt_mutex_destroy(&app->app_lock);
}
#define test_key_txns(...) test_key_txns_fn(__VA_ARGS__, __FILE__, __LINE__)
static void test_key_txns_fn(const char* testname,
const char* path,
int is_apdex_f,
nrtime_t expected_apdex_t,
nrtime_t expected_tt_threshold,
const char* rules,
const char* segment_terms,
nrobj_t* key_txns,
const char* file,
int line) {
nr_status_t rv;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
nrapp_t appv = {.info = {0}};
nrapp_t* app = &appv;
test_txn_state_t* p = (test_txn_state_t*)tlib_getspecific();
nrt_mutex_init(&app->app_lock, 0);
txn->app_connect_reply = nro_new_hash();
nro_set_hash(txn->app_connect_reply, "web_transactions_apdex", key_txns);
p->txns_app = app;
txn->status.ignore = 0;
txn->name = 0;
txn->options.apdex_t = 0;
txn->options.tt_threshold = 0;
txn->options.tt_is_apdex_f = is_apdex_f;
txn->status.path_is_frozen = 0;
txn->status.path_type = NR_PATH_TYPE_URI;
txn->status.background = 0;
txn->path = nr_strdup(path);
if (rules) {
nrobj_t* ob = nro_create_from_json(rules);
app->url_rules
= nr_rules_create_from_obj(nro_get_hash_array(ob, "url_rules", 0));
app->txn_rules
= nr_rules_create_from_obj(nro_get_hash_array(ob, "txn_rules", 0));
nro_delete(ob);
} else {
app->url_rules = 0;
app->txn_rules = 0;
}
if (segment_terms) {
nrobj_t* st_obj = nro_create_from_json(segment_terms);
app->segment_terms = nr_segment_terms_create_from_obj(st_obj);
nro_delete(st_obj);
} else {
app->segment_terms = 0;
}
rv = nr_txn_freeze_name_update_apdex(txn);
test_pass_if_true(testname, NR_SUCCESS == rv, "rv=%d", (int)rv);
test_pass_if_true(testname, expected_apdex_t == txn->options.apdex_t,
"expected_apdex_t=" NR_TIME_FMT
" txn->options.apdex_t=" NR_TIME_FMT,
expected_apdex_t, txn->options.apdex_t);
test_pass_if_true(testname,
expected_tt_threshold == txn->options.tt_threshold,
"expected_tt_threshold=" NR_TIME_FMT
" txn->options.tt_threshold=" NR_TIME_FMT,
expected_tt_threshold, txn->options.tt_threshold);
nro_delete(txn->app_connect_reply);
nr_free(txn->name);
nr_free(txn->path);
nr_rules_destroy(&app->url_rules);
nr_rules_destroy(&app->txn_rules);
nr_segment_terms_destroy(&app->segment_terms);
nrt_mutex_destroy(&app->app_lock);
}
static void test_txn_cmp_options(void) {
nrtxnopt_t o1 = {.custom_events_enabled = 1};
nrtxnopt_t o2 = {.custom_events_enabled = 1};
bool rv = false;
rv = nr_txn_cmp_options(NULL, NULL);
tlib_pass_if_true("NULL pointers are equal", rv, "rv=%d", (int)rv);
rv = nr_txn_cmp_options(&o1, &o1);
tlib_pass_if_true("Equal pointers are equal", rv, "rv=%d", (int)rv);
rv = nr_txn_cmp_options(&o1, &o2);
tlib_pass_if_true("Equal fields are equal", rv, "rv=%d", (int)rv);
o2.custom_events_enabled = 0;
rv = nr_txn_cmp_options(NULL, &o1);
tlib_pass_if_false("NULL and other are not equal", rv, "rv=%d", (int)rv);
rv = nr_txn_cmp_options(&o1, NULL);
tlib_pass_if_false("Other and null are not equal", rv, "rv=%d", (int)rv);
rv = nr_txn_cmp_options(&o1, &o2);
tlib_pass_if_false("Inequal fields are not equal", rv, "rv=%d", (int)rv);
}
const char test_rules[]
= "{\"url_rules\":[{\"match_expression\":\"what\", "
"\"replacement\":\"txn\"},"
"{\"match_expression\":\"ignore_path\", \"ignore\":true}],"
"\"txn_rules\":[{\"match_expression\":\"ignore_txn\", \"ignore\":true},"
"{\"match_expression\":\"rename_txn\", \"replacement\":\"ok\"}]}";
const char test_segment_terms[]
= "["
"{\"prefix\":\"WebTransaction/Custom\",\"terms\":[\"white\",\"list\"]}"
"]";
static void test_freeze_name_update_apdex(void) {
test_txn_state_t* p = (test_txn_state_t*)tlib_getspecific();
/*
* Test : Bad input to nr_txn_freeze_name_update_apdex
*/
{
nr_status_t rv;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
nrapp_t appv = {.info = {0}};
nrapp_t* app = &appv;
nrt_mutex_init(&app->app_lock, 0);
txn->path = 0;
txn->status.ignore = 0;
txn->name = 0;
txn->status.background = 0;
txn->status.path_is_frozen = 0;
txn->status.path_type = NR_PATH_TYPE_URI;
txn->app_connect_reply = 0;
app->url_rules = 0;
app->txn_rules = 0;
app->segment_terms = 0;
p->txns_app = app;
rv = nr_txn_freeze_name_update_apdex(0);
tlib_pass_if_true("no txn", NR_FAILURE == rv, "rv=%d", (int)rv);
p->txns_app = 0;
rv = nr_txn_freeze_name_update_apdex(txn);
tlib_pass_if_true("no app", NR_FAILURE == rv, "rv=%d", (int)rv);
p->txns_app = app;
txn->status.ignore = 1;
rv = nr_txn_freeze_name_update_apdex(txn);
tlib_pass_if_true("ignore txn", NR_FAILURE == rv, "rv=%d", (int)rv);
txn->status.ignore = 0;
txn->status.path_is_frozen = 1;
txn->status.path_type = NR_PATH_TYPE_URI;
rv = nr_txn_freeze_name_update_apdex(txn);
tlib_pass_if_true("already frozen", (NR_SUCCESS == rv) && (0 == txn->name),
"rv=%d txn->name=%p", (int)rv, txn->name);
txn->status.path_is_frozen = 0;
txn->status.path_type = NR_PATH_TYPE_URI;
rv = nr_txn_freeze_name_update_apdex(txn);
tlib_pass_if_true(
"no path",
(NR_SUCCESS == rv)
&& (0 == nr_strcmp(txn->name, "WebTransaction/Uri/unknown")),
"rv=%d txn->name=%s", (int)rv, NRSAFESTR(txn->name));
nr_free(txn->name);
nrt_mutex_destroy(&app->app_lock);
}
/*
* Transaction Naming Tests
*
* url_rules should only be applied to URI non-background txns and CUSTOM
* non-background txns.
*/
/*
* Test : URI Web Transaction Naming
*/
test_freeze_name("URI WT", NR_PATH_TYPE_URI, 0, "/zap.php", 0, 0,
"WebTransaction/Uri/zap.php");
test_freeze_name("URI WT no slash", NR_PATH_TYPE_URI, 0, "zap.php", 0, 0,
"WebTransaction/Uri/zap.php");
test_freeze_name("URI WT url_rule change", NR_PATH_TYPE_URI, 0, "/what.php",
test_rules, 0, "WebTransaction/Uri/txn.php");
test_freeze_name("URI WT url_rule ignore", NR_PATH_TYPE_URI, 0,
"/ignore_path.php", test_rules, 0, 0);
test_freeze_name("URI WT url_rule and txn_rule change", NR_PATH_TYPE_URI, 0,
"/rename_what.php", test_rules, 0,
"WebTransaction/Uri/ok.php");
test_freeze_name("URI WT url_rule change txn_rule ignore", NR_PATH_TYPE_URI,
0, "/ignore_what.php", test_rules, 0, 0);
/*
* Test : URI Background Naming
*/
test_freeze_name("URI BG", NR_PATH_TYPE_URI, 1, "/zap.php", 0, 0,
"OtherTransaction/php/zap.php");
test_freeze_name("URI BG no slash", NR_PATH_TYPE_URI, 1, "zap.php", 0, 0,
"OtherTransaction/php/zap.php");
test_freeze_name("URI BG url_rule no change", NR_PATH_TYPE_URI, 1,
"/what.php", test_rules, 0, "OtherTransaction/php/what.php");
test_freeze_name("URI BG url_rule no ignore", NR_PATH_TYPE_URI, 1,
"/ignore_path.php", test_rules, 0,
"OtherTransaction/php/ignore_path.php");
test_freeze_name("URI BG txn_rule change", NR_PATH_TYPE_URI, 1,
"/rename_txn.php", test_rules, 0,
"OtherTransaction/php/ok.php");
test_freeze_name("URI BG txn_rule ignore", NR_PATH_TYPE_URI, 1,
"/ignore_txn.php", test_rules, 0, 0);
/*
* Test : Status code web transaction naming.
*/
test_freeze_name("STATUS WT", NR_PATH_TYPE_STATUS_CODE, 0, "/404", 0, 0,
"WebTransaction/StatusCode/404");
test_freeze_name("STATUS WT url_rule no change", NR_PATH_TYPE_STATUS_CODE, 0,
"/404", test_rules, 0, "WebTransaction/StatusCode/404");
test_freeze_name("STATUS WT url_rule no ignore", NR_PATH_TYPE_STATUS_CODE, 0,
"/ignore_path", test_rules, 0,
"WebTransaction/StatusCode/ignore_path");
test_freeze_name("STATUS WT txn_rule change", NR_PATH_TYPE_STATUS_CODE, 0,
"/rename_txn", test_rules, 0,
"WebTransaction/StatusCode/ok");
test_freeze_name("STATUS WT txn_rule ignore", NR_PATH_TYPE_STATUS_CODE, 0,
"/ignore_txn", test_rules, 0, 0);
/*
* Test : Status code background transaction naming.
*/
test_freeze_name("STATUS WT", NR_PATH_TYPE_STATUS_CODE, 1, "/404", 0, 0,
"OtherTransaction/StatusCode/404");
test_freeze_name("STATUS WT url_rule no change", NR_PATH_TYPE_STATUS_CODE, 1,
"/404", test_rules, 0, "OtherTransaction/StatusCode/404");
test_freeze_name("STATUS WT url_rule no ignore", NR_PATH_TYPE_STATUS_CODE, 1,
"/ignore_path", test_rules, 0,
"OtherTransaction/StatusCode/ignore_path");
test_freeze_name("STATUS WT txn_rule change", NR_PATH_TYPE_STATUS_CODE, 1,
"/rename_txn", test_rules, 0,
"OtherTransaction/StatusCode/ok");
test_freeze_name("STATUS WT txn_rule ignore", NR_PATH_TYPE_STATUS_CODE, 1,
"/ignore_txn", test_rules, 0, 0);
/*
* Test : ACTION Web Transaction Naming
*/
test_freeze_name("ACTION WT", NR_PATH_TYPE_ACTION, 0, "/zap.php", 0, 0,
"WebTransaction/Action/zap.php");
test_freeze_name("ACTION WT no slash", NR_PATH_TYPE_ACTION, 0, "zap.php", 0,
0, "WebTransaction/Action/zap.php");
test_freeze_name("ACTION WT url_rule no change", NR_PATH_TYPE_ACTION, 0,
"/what.php", test_rules, 0,
"WebTransaction/Action/what.php");
test_freeze_name("ACTION WT url_rule no ignore", NR_PATH_TYPE_ACTION, 0,
"/ignore_path.php", test_rules, 0,
"WebTransaction/Action/ignore_path.php");
test_freeze_name("ACTION WT txn_rule change", NR_PATH_TYPE_ACTION, 0,
"/rename_txn.php", test_rules, 0,
"WebTransaction/Action/ok.php");
test_freeze_name("ACTION WT txn_rule ignore", NR_PATH_TYPE_ACTION, 0,
"/ignore_txn.php", test_rules, 0, 0);
/*
* Test : ACTION Background Naming
*/
test_freeze_name("ACTION BG", NR_PATH_TYPE_ACTION, 1, "/zap.php", 0, 0,
"OtherTransaction/Action/zap.php");
test_freeze_name("ACTION BG no slash", NR_PATH_TYPE_ACTION, 1, "zap.php", 0,
0, "OtherTransaction/Action/zap.php");
test_freeze_name("ACTION BG url_rule no change", NR_PATH_TYPE_ACTION, 1,
"/what.php", test_rules, 0,
"OtherTransaction/Action/what.php");
test_freeze_name("ACTION BG url_rule no ignore", NR_PATH_TYPE_ACTION, 1,
"/ignore_path.php", test_rules, 0,
"OtherTransaction/Action/ignore_path.php");
test_freeze_name("ACTION BG txn_rule change", NR_PATH_TYPE_ACTION, 1,
"/rename_txn.php", test_rules, 0,
"OtherTransaction/Action/ok.php");
test_freeze_name("ACTION BG txn_rule ignore", NR_PATH_TYPE_ACTION, 1,
"/ignore_txn.php", test_rules, 0, 0);
/*
* Test : FUNCTION Web Transaction Naming
*/
test_freeze_name("FUNCTION WT", NR_PATH_TYPE_FUNCTION, 0, "/zap.php", 0, 0,
"WebTransaction/Function/zap.php");
test_freeze_name("FUNCTION WT no slash", NR_PATH_TYPE_FUNCTION, 0, "zap.php",
0, 0, "WebTransaction/Function/zap.php");
test_freeze_name("FUNCTION WT url_rule no change", NR_PATH_TYPE_FUNCTION, 0,
"/what.php", test_rules, 0,
"WebTransaction/Function/what.php");
test_freeze_name("FUNCTION WT url_rule no ignore", NR_PATH_TYPE_FUNCTION, 0,
"/ignore_path.php", test_rules, 0,
"WebTransaction/Function/ignore_path.php");
test_freeze_name("FUNCTION WT txn_rule change", NR_PATH_TYPE_FUNCTION, 0,
"/rename_txn.php", test_rules, 0,
"WebTransaction/Function/ok.php");
test_freeze_name("FUNCTION WT txn_rule ignore", NR_PATH_TYPE_FUNCTION, 0,
"/ignore_txn.php", test_rules, 0, 0);
/*
* Test : FUNCTION Background Naming
*/
test_freeze_name("FUNCTION BG", NR_PATH_TYPE_FUNCTION, 1, "/zap.php", 0, 0,
"OtherTransaction/Function/zap.php");
test_freeze_name("FUNCTION BG no slash", NR_PATH_TYPE_FUNCTION, 1, "zap.php",
0, 0, "OtherTransaction/Function/zap.php");
test_freeze_name("FUNCTION BG url_rule no change", NR_PATH_TYPE_FUNCTION, 1,
"/what.php", test_rules, 0,
"OtherTransaction/Function/what.php");
test_freeze_name("FUNCTION BG url_rule no ignore", NR_PATH_TYPE_FUNCTION, 1,
"/ignore_path.php", test_rules, 0,
"OtherTransaction/Function/ignore_path.php");
test_freeze_name("FUNCTION BG txn_rule change", NR_PATH_TYPE_FUNCTION, 1,
"/rename_txn.php", test_rules, 0,
"OtherTransaction/Function/ok.php");
test_freeze_name("FUNCTION BG txn_rule ignore", NR_PATH_TYPE_FUNCTION, 1,
"/ignore_txn.php", test_rules, 0, 0);
/*
* Test : CUSTOM Web Transaction Naming
*/
test_freeze_name("CUSTOM WT", NR_PATH_TYPE_CUSTOM, 0, "/zap.php", 0, 0,
"WebTransaction/Custom/zap.php");
test_freeze_name("CUSTOM WT no slash", NR_PATH_TYPE_CUSTOM, 0, "zap.php", 0,
0, "WebTransaction/Custom/zap.php");
test_freeze_name("CUSTOM WT url_rule change", NR_PATH_TYPE_CUSTOM, 0,
"/what.php", test_rules, 0, "WebTransaction/Custom/txn.php");
test_freeze_name("CUSTOM WT url_rule ignore", NR_PATH_TYPE_CUSTOM, 0,
"/ignore_path.php", test_rules, 0, 0);
test_freeze_name("CUSTOM WT url_rule and txn_rule change",
NR_PATH_TYPE_CUSTOM, 0, "/rename_what.php", test_rules, 0,
"WebTransaction/Custom/ok.php");
test_freeze_name("CUSTOM WT url_rule change txn_rule ignore",
NR_PATH_TYPE_CUSTOM, 0, "/ignore_what.php", test_rules, 0,
0);
/*
* Test : CUSTOM Background Naming
*/
test_freeze_name("CUSTOM BG", NR_PATH_TYPE_CUSTOM, 1, "/zap.php", 0, 0,
"OtherTransaction/Custom/zap.php");
test_freeze_name("CUSTOM BG no slash", NR_PATH_TYPE_CUSTOM, 1, "zap.php", 0,
0, "OtherTransaction/Custom/zap.php");
test_freeze_name("CUSTOM BG url_rule no change", NR_PATH_TYPE_CUSTOM, 1,
"/what.php", test_rules, 0,
"OtherTransaction/Custom/what.php");
test_freeze_name("CUSTOM BG url_rule no ignore", NR_PATH_TYPE_CUSTOM, 1,
"/ignore_path.php", test_rules, 0,
"OtherTransaction/Custom/ignore_path.php");
test_freeze_name("CUSTOM BG txn_rule change", NR_PATH_TYPE_CUSTOM, 1,
"/rename_txn.php", test_rules, 0,
"OtherTransaction/Custom/ok.php");
test_freeze_name("CUSTOM BG txn_rule ignore", NR_PATH_TYPE_CUSTOM, 1,
"/ignore_txn.php", test_rules, 0, 0);
/*
* Test : UNKNOWN Web Transaction Naming
*/
test_freeze_name("UNKNOWN WT", NR_PATH_TYPE_UNKNOWN, 0, "/zap.php", 0, 0,
"WebTransaction/Uri/<unknown>");
test_freeze_name("UNKNOWN WT no slash", NR_PATH_TYPE_UNKNOWN, 0, "zap.php", 0,
0, "WebTransaction/Uri/<unknown>");
test_freeze_name("UNKNOWN WT url_rule no change", NR_PATH_TYPE_UNKNOWN, 0,
"/what.php", test_rules, 0, "WebTransaction/Uri/<unknown>");
test_freeze_name("UNKNOWN WT url_rule no ignore", NR_PATH_TYPE_UNKNOWN, 0,
"/ignore_path.php", test_rules, 0,
"WebTransaction/Uri/<unknown>");
test_freeze_name("UNKNOWN WT txn_rule no change", NR_PATH_TYPE_UNKNOWN, 0,
"/rename_txn.php", test_rules, 0,
"WebTransaction/Uri/<unknown>");
test_freeze_name("UNKNOWN WT txn_rule no ignore", NR_PATH_TYPE_UNKNOWN, 0,
"/ignore_txn.php", test_rules, 0,
"WebTransaction/Uri/<unknown>");
/*
* Test : UNKNOWN Background Naming
*/
test_freeze_name("UNKNOWN BG", NR_PATH_TYPE_UNKNOWN, 1, "/zap.php", 0, 0,
"OtherTransaction/php/<unknown>");
test_freeze_name("UNKNOWN BG no slash", NR_PATH_TYPE_UNKNOWN, 1, "zap.php", 0,
0, "OtherTransaction/php/<unknown>");
test_freeze_name("UNKNOWN BG url_rule no change", NR_PATH_TYPE_UNKNOWN, 1,
"/what.php", test_rules, 0,
"OtherTransaction/php/<unknown>");
test_freeze_name("UNKNOWN BG url_rule no ignore", NR_PATH_TYPE_UNKNOWN, 1,
"/ignore_path.php", test_rules, 0,
"OtherTransaction/php/<unknown>");
test_freeze_name("UNKNOWN BG txn_rule no change", NR_PATH_TYPE_UNKNOWN, 1,
"/rename_txn.php", test_rules, 0,
"OtherTransaction/php/<unknown>");
test_freeze_name("UNKNOWN BG txn_rule no ignore", NR_PATH_TYPE_UNKNOWN, 1,
"/ignore_txn.php", test_rules, 0,
"OtherTransaction/php/<unknown>");
/*
* Test : Segment term application
*/
test_freeze_name("Prefix does not match", NR_PATH_TYPE_ACTION, 0, "/zap.php",
0, test_segment_terms, "WebTransaction/Action/zap.php");
test_freeze_name("Prefix matches; all whitelisted", NR_PATH_TYPE_CUSTOM, 0,
"/white/list", 0, test_segment_terms,
"WebTransaction/Custom/white/list");
test_freeze_name("Prefix matches; none whitelisted", NR_PATH_TYPE_CUSTOM, 0,
"/black/foo", 0, test_segment_terms,
"WebTransaction/Custom/*");
test_freeze_name("Prefix matches; some whitelisted", NR_PATH_TYPE_CUSTOM, 0,
"/black/list", 0, test_segment_terms,
"WebTransaction/Custom/*/list");
/*
* Test : Key Transactions
*/
{
nrobj_t* key_txns = nro_create_from_json(
"{\"WebTransaction\\/Uri\\/key\":0.1,"
"\"WebTransaction\\/Uri\\/ok\":0.1,"
"\"WebTransaction\\/Uri\\/key_int\":2,"
"\"WebTransaction\\/Uri\\/key_negative\":-0.1}");
test_key_txns("not key txn", "/not", 1, 0, 0, test_rules, 0, key_txns);
test_key_txns("key txn", "/key", 0, 100000, 0, test_rules, 0, key_txns);
test_key_txns("key txn is_apdex_f", "/key", 1, 100000, 400000, test_rules,
0, key_txns);
test_key_txns("key txn after rules", "/rename_what", 0, 100000, 0,
test_rules, 0, key_txns);
test_key_txns("key txn after rules is_apdex_f", "/rename_what", 1, 100000,
400000, test_rules, 0, key_txns);
test_key_txns("key txn apdex int", "/key_int", 0, 2000000, 0, test_rules, 0,
key_txns);
test_key_txns("key txn apdex negative", "/key_negative", 0, 0, 0,
test_rules, 0, key_txns);
nro_delete(key_txns);
}
}
#define test_apdex_metric_created(...) \
test_apdex_metric_created_fn(__VA_ARGS__, __FILE__, __LINE__)
static void test_apdex_metric_created_fn(const char* testname,
nrmtable_t* table,
uint32_t flags,
const char* name,
nrtime_t satisfying,
nrtime_t tolerating,
nrtime_t failing,
nrtime_t min,
nrtime_t max,
const char* file,
int line) {
const nrmetric_t* m = nrm_find(table, name);
const char* nm = nrm_get_name(table, m);
test_pass_if_true_file_line(testname, 0 != m, file, line, "m=%p", m);
test_pass_if_true_file_line(testname, 0 == nr_strcmp(nm, name), file, line,
"nm=%s name=%s", nm, name);
test_metric_values_are_fn(testname, m, flags | MET_IS_APDEX, satisfying,
tolerating, failing, min, max, 0, file, line);
}
#define test_apdex_metrics(...) \
test_apdex_metrics_fn(__VA_ARGS__, __FILE__, __LINE__)
static void test_apdex_metrics_fn(const char* txn_name,
int has_error,
nrtime_t duration,
nrtime_t apdex_t,
const char* mname,
nrtime_t satisfying,
nrtime_t tolerating,
nrtime_t failing,
const char* file,
int line) {
int table_size;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
txn->unscoped_metrics = nrm_table_create(0);
txn->name = nr_strdup(txn_name);
txn->options.apdex_t = apdex_t;
txn->error = NULL;
if (has_error) {
int priority = 5;
txn->error = nr_error_create(priority, "my/msg", "my/class", "my/span_id",
"[\"my\\/stacktrace\"]", nr_get_time());
}
nr_txn_create_apdex_metrics(txn, duration);
/*
* Test : 'Apdex' metric created and is correct.
*/
test_apdex_metric_created_fn(txn_name, txn->unscoped_metrics, MET_FORCED,
"Apdex", satisfying, tolerating, failing,
apdex_t, apdex_t, file, line);
/*
* Test : Specific apdex metric created and correct, and table size.
*/
table_size = nrm_table_size(txn->unscoped_metrics);
if (mname) {
test_apdex_metric_created_fn(txn_name, txn->unscoped_metrics, 0, mname,
satisfying, tolerating, failing, apdex_t,
apdex_t, file, line);
test_pass_if_true(txn_name, 2 == table_size, "table_size=%d", table_size);
} else {
test_pass_if_true(txn_name, 1 == table_size, "table_size=%d", table_size);
}
nr_free(txn->name);
nrm_table_destroy(&txn->unscoped_metrics);
nr_error_destroy(&txn->error);
}
static void test_create_apdex_metrics(void) {
/* Should not blow up on NULL input */
nr_txn_create_apdex_metrics(0, 0);
/*
* Test : Apdex value is properly calculated.
*/
test_apdex_metrics(NULL, 0, 2, 4, NULL, 1, 0, 0);
test_apdex_metrics("nope", 0, 2, 4, NULL, 1, 0, 0);
test_apdex_metrics("OtherTransaction/php/path.php", 0, 2, 4,
"Apdex/php/path.php", 1, 0, 0);
test_apdex_metrics("WebTransaction/Uri/path.php", 0, 2, 4,
"Apdex/Uri/path.php", 1, 0, 0);
test_apdex_metrics("OtherTransaction/Action/path.php", 0, 5, 4,
"Apdex/Action/path.php", 0, 1, 0);
test_apdex_metrics("WebTransaction/Action/path.php", 0, 17, 4,
"Apdex/Action/path.php", 0, 0, 1);
test_apdex_metrics("OtherTransaction/Function/path.php", 1, 1, 4,
"Apdex/Function/path.php", 0, 0, 1);
test_apdex_metrics("WebTransaction/Function/path.php", 0, 2, 4,
"Apdex/Function/path.php", 1, 0, 0);
test_apdex_metrics("OtherTransaction/Custom/path.php", 0, 2, 4,
"Apdex/Custom/path.php", 1, 0, 0);
test_apdex_metrics("OtherTransaction/php/<unknown>", 0, 2, 4,
"Apdex/php/<unknown>", 1, 0, 0);
test_apdex_metrics("WebTransaction/Uri/<unknown>", 0, 2, 4,
"Apdex/Uri/<unknown>", 1, 0, 0);
}
static void test_create_error_metrics(void) {
int table_size;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
txn->status.background = 0;
txn->trace_strings = 0;
txn->unscoped_metrics = 0;
txn->options.distributed_tracing_enabled = false;
/*
* Test : Bad Params. Should not blow up.
*/
nr_txn_create_error_metrics(0, 0);
nr_txn_create_error_metrics(0, "WebTransaction/Action/not_words");
nr_txn_create_error_metrics(txn, 0);
nr_txn_create_error_metrics(txn, "");
nr_txn_create_error_metrics(
txn, "WebTransaction/Action/not_words"); /* No metric table */
/*
* Test : Web Transaction
*/
txn->trace_strings = nr_string_pool_create();
txn->unscoped_metrics = nrm_table_create(2);
nr_txn_create_error_metrics(txn, "WebTransaction/Action/not_words");
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_true("three error metrics created", 3 == table_size,
"table_size=%d", table_size);
test_txn_metric_is("rollup", txn->unscoped_metrics, MET_FORCED, "Errors/all",
1, 0, 0, 0, 0, 0);
test_txn_metric_is("web rollup", txn->unscoped_metrics, MET_FORCED,
"Errors/allWeb", 1, 0, 0, 0, 0, 0);
test_txn_metric_is("specific", txn->unscoped_metrics, MET_FORCED,
"Errors/WebTransaction/Action/not_words", 1, 0, 0, 0, 0,
0);
/*
* Test : Background Task
*/
nr_string_pool_destroy(&txn->trace_strings);
nrm_table_destroy(&txn->unscoped_metrics);
txn->trace_strings = nr_string_pool_create();
txn->unscoped_metrics = nrm_table_create(2);
txn->status.background = 1;
nr_txn_create_error_metrics(txn, "OtherTransaction/Custom/zap");
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_true("three error metrics created", 3 == table_size,
"table_size=%d", table_size);
test_txn_metric_is("rollup", txn->unscoped_metrics, MET_FORCED, "Errors/all",
1, 0, 0, 0, 0, 0);
test_txn_metric_is("background rollup", txn->unscoped_metrics, MET_FORCED,
"Errors/allOther", 1, 0, 0, 0, 0, 0);
test_txn_metric_is("specific", txn->unscoped_metrics, MET_FORCED,
"Errors/OtherTransaction/Custom/zap", 1, 0, 0, 0, 0, 0);
nr_string_pool_destroy(&txn->trace_strings);
nrm_table_destroy(&txn->unscoped_metrics);
}
static void test_create_duration_metrics(void) {
int table_size;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
const nrtime_t duration = 999;
const nrtime_t total_time = 1999;
nr_memset((void*)txn, 0, sizeof(txnv));
txn->status.background = 0;
txn->unscoped_metrics = 0;
txn->status.recording = 1;
txn->segment_slab = nr_slab_create(sizeof(nr_segment_t), 0);
txn->segment_root = nr_segment_start(txn, NULL, NULL);
txn->segment_root->start_time = 0;
txn->segment_root->stop_time = duration;
txn->segment_root->exclusive_time = nr_exclusive_time_create(16, 0, duration);
/*
* Test : Bad Params. Should not blow up.
*/
nr_txn_create_duration_metrics(NULL, duration, total_time);
nr_txn_create_duration_metrics(txn, duration, total_time); // No metric table
/*
* Test : Web Transaction
*/
nr_exclusive_time_add_child(txn->segment_root->exclusive_time, 0, 111);
txn->unscoped_metrics = nrm_table_create(2);
txn->name = "WebTransaction/Action/not_words";
nr_txn_create_duration_metrics(txn, duration, total_time);
test_txn_metric_is("web txn", txn->unscoped_metrics, MET_FORCED,
"WebTransaction", 1, 999, 888, 999, 999, 998001);
test_txn_metric_is("web txn", txn->unscoped_metrics, MET_FORCED,
"HttpDispatcher", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("web txn", txn->unscoped_metrics, MET_FORCED,
"WebTransaction/Action/not_words", 1, 999, 888, 999, 999,
998001);
test_txn_metric_is("web txn", txn->unscoped_metrics, MET_FORCED,
"WebTransactionTotalTime", 1, 1999, 1999, 1999, 1999,
3996001);
test_txn_metric_is("web txn", txn->unscoped_metrics, MET_FORCED,
"WebTransactionTotalTime/Action/not_words", 1, 1999, 1999,
1999, 1999, 3996001);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_int_equal("number of metrics created", 5, table_size);
nrm_table_destroy(&txn->unscoped_metrics);
/*
* Test : Web Transaction No Exclusive
*/
nr_exclusive_time_add_child(txn->segment_root->exclusive_time, 0, 1000);
txn->unscoped_metrics = nrm_table_create(2);
nr_txn_create_duration_metrics(txn, duration, total_time);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"WebTransaction", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"HttpDispatcher", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"WebTransaction/Action/not_words", 1, 999, 0, 999, 999,
998001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"WebTransactionTotalTime", 1, 1999, 1999, 1999, 1999,
3996001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"WebTransactionTotalTime/Action/not_words", 1, 1999, 1999,
1999, 1999, 3996001);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_int_equal("number of metrics created", 5, table_size);
nrm_table_destroy(&txn->unscoped_metrics);
/*
* Test : Web Transaction (no slash)
*/
txn->unscoped_metrics = nrm_table_create(2);
txn->name = "NoSlash";
nr_txn_create_duration_metrics(txn, duration, total_time);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"WebTransaction", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"HttpDispatcher", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"NoSlash", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"WebTransactionTotalTime", 1, 1999, 1999, 1999, 1999,
3996001);
test_txn_metric_is("web txn no exclusive", txn->unscoped_metrics, MET_FORCED,
"NoSlashTotalTime", 1, 1999, 1999, 1999, 1999, 3996001);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_int_equal("number of metrics created", 5, table_size);
nrm_table_destroy(&txn->unscoped_metrics);
/*
* Background Task
*/
nr_exclusive_time_destroy(&txn->segment_root->exclusive_time);
txn->segment_root->exclusive_time = nr_exclusive_time_create(16, 0, duration);
nr_exclusive_time_add_child(txn->segment_root->exclusive_time, 0, 111);
txn->status.background = 1;
txn->name = "WebTransaction/Action/not_words";
txn->unscoped_metrics = nrm_table_create(2);
nr_txn_create_duration_metrics(txn, duration, total_time);
test_txn_metric_is("background", txn->unscoped_metrics, MET_FORCED,
"OtherTransaction/all", 1, 999, 888, 999, 999, 998001);
test_txn_metric_is("background", txn->unscoped_metrics, MET_FORCED,
"WebTransaction/Action/not_words", 1, 999, 888, 999, 999,
998001);
test_txn_metric_is("background", txn->unscoped_metrics, MET_FORCED,
"OtherTransactionTotalTime", 1, 1999, 1999, 1999, 1999,
3996001);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_int_equal("number of metrics created", 4, table_size);
nrm_table_destroy(&txn->unscoped_metrics);
/*
* Background Task No Exclusive
*/
nr_exclusive_time_add_child(txn->segment_root->exclusive_time, 0, 1111);
txn->status.background = 1;
txn->unscoped_metrics = nrm_table_create(2);
nr_txn_create_duration_metrics(txn, duration, total_time);
test_txn_metric_is("background no exclusive", txn->unscoped_metrics,
MET_FORCED, "OtherTransaction/all", 1, 999, 0, 999, 999,
998001);
test_txn_metric_is("background no exclusive", txn->unscoped_metrics,
MET_FORCED, "WebTransaction/Action/not_words", 1, 999, 0,
999, 999, 998001);
test_txn_metric_is("background no exclusive", txn->unscoped_metrics,
MET_FORCED, "OtherTransactionTotalTime", 1, 1999, 1999,
1999, 1999, 3996001);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_int_equal("number of metrics created", 4, table_size);
nrm_table_destroy(&txn->unscoped_metrics);
/*
* Background Task (no slash)
*/
txn->unscoped_metrics = nrm_table_create(2);
txn->name = "NoSlash";
nr_txn_create_duration_metrics(txn, duration, total_time);
test_txn_metric_is("background no slash", txn->unscoped_metrics, MET_FORCED,
"OtherTransaction/all", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("background no slash", txn->unscoped_metrics, MET_FORCED,
"NoSlash", 1, 999, 0, 999, 999, 998001);
test_txn_metric_is("background no slash", txn->unscoped_metrics, MET_FORCED,
"NoSlashTotalTime", 1, 1999, 1999, 1999, 1999, 3996001);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_int_equal("four duration metrics created", 4, table_size);
nrm_table_destroy(&txn->unscoped_metrics);
nr_segment_destroy_tree(txn->segment_root);
nr_hashmap_destroy(&txn->parent_stacks);
nr_stack_destroy_fields(&txn->default_parent_stack);
nr_slab_destroy(&txn->segment_slab);
}
static void test_create_queue_metric(void) {
int table_size;
nrtxn_t txnv;
nrtxn_t* txn = &txnv;
txn->unscoped_metrics = 0;
txn->abs_start_time = 444;
txn->status.http_x_start = 333;
txn->status.background = 0;
/*
* Test : Bad Params. Should not blow up.
*/
nr_txn_create_queue_metric(0);
nr_txn_create_queue_metric(txn); /* No metric table */
/*
* Test : Non-Zero Queue Time
*/
txn->unscoped_metrics = nrm_table_create(2);
nr_txn_create_queue_metric(txn);
test_txn_metric_is("non-zero queue time", txn->unscoped_metrics, MET_FORCED,
"WebFrontend/QueueTime", 1, 111, 111, 111, 111, 12321);
table_size = nrm_table_size(txn->unscoped_metrics);
tlib_pass_if_true("non-zero queue time", 1 == table_size, "table_size=%d",
table_size);
nrm_table_destroy(&txn->unscoped_metrics);