forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_segment_external.c
311 lines (258 loc) · 12.1 KB
/
test_segment_external.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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include "nr_header.h"
#include "nr_segment_external.h"
#include "test_segment_helpers.h"
static nr_segment_t* mock_txn_segment(void) {
nrtxn_t* txn = new_txn(0);
return nr_segment_start(txn, NULL, NULL);
}
#define TEST_EXTERNAL_SEGMENT(M_segment, M_name, M_uri, M_library, \
M_procedure, M_guid) \
test_segment_metric_created("rollup segment metric exists", \
(M_segment)->metrics, (M_name), true); \
tlib_pass_if_str_equal( \
"segment name", (M_name), \
nr_string_get((M_segment)->txn->trace_strings, (M_segment)->name)); \
tlib_pass_if_true("segment type", NR_SEGMENT_EXTERNAL == (M_segment)->type, \
"NR_SEGMENT_EXTERNAL"); \
tlib_pass_if_str_equal( \
"segment uri", (M_segment)->typed_attributes->external.uri, (M_uri)); \
tlib_pass_if_str_equal("segment library", \
(M_segment)->typed_attributes->external.library, \
(M_library)); \
tlib_pass_if_str_equal("segment procedure", \
(M_segment)->typed_attributes->external.procedure, \
(M_procedure)); \
tlib_pass_if_str_equal( \
"transaction guid", \
(M_segment)->typed_attributes->external.transaction_guid, (M_guid));
void nr_header_outbound_response(nrtxn_t* txn,
const char* encoded_response,
char** external_id_ptr,
char** external_txnname_ptr,
char** external_guid_ptr) {
nrobj_t* obj;
const char* external_id;
const char* external_txnname;
const char* external_guid;
tlib_pass_if_not_null("txn present", txn);
tlib_pass_if_not_null("encoded_response present", encoded_response);
tlib_pass_if_not_null("external_id_ptr present", external_id_ptr);
tlib_pass_if_not_null("external_txnname_ptr present", external_txnname_ptr);
tlib_pass_if_not_null("external_guid_ptr present", external_guid_ptr);
obj = nro_create_from_json(encoded_response);
external_id = nro_get_hash_string(obj, "id", 0);
external_txnname = nro_get_hash_string(obj, "txnname", 0);
external_guid = nro_get_hash_string(obj, "guid", 0);
if (external_id && external_id_ptr) {
*external_id_ptr = nr_strdup(external_id);
}
if (external_txnname && external_txnname_ptr) {
*external_txnname_ptr = nr_strdup(external_txnname);
}
if (external_guid && external_guid_ptr) {
*external_guid_ptr = nr_strdup(external_guid);
}
nro_delete(obj);
}
static void test_bad_parameters(void) {
nr_segment_t seg_null = {0};
nr_segment_t* seg_null_ptr;
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
nr_segment_external_params_t params;
tlib_pass_if_false("bad parameters", nr_segment_external_end(NULL, ¶ms),
"expected false");
seg_null_ptr = NULL;
tlib_pass_if_false("bad parameters",
nr_segment_external_end(&seg_null_ptr, ¶ms),
"expected false");
seg_null_ptr = &seg_null;
tlib_pass_if_false("bad parameters",
nr_segment_external_end(&seg_null_ptr, ¶ms),
"expected false");
tlib_pass_if_false("bad parameters", nr_segment_external_end(&seg, NULL),
"expected false");
test_metric_vector_size(seg->metrics, 0);
nr_txn_destroy(&txn);
}
static void test_web_transaction(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){.uri = "newrelic.com"});
TEST_EXTERNAL_SEGMENT(seg, "External/newrelic.com/all", "newrelic.com", NULL,
NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created("web transaction creates a segment metric",
seg->metrics, "External/newrelic.com/all", true);
test_txn_metric_created("web transaction creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_null_url(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){.uri = NULL});
TEST_EXTERNAL_SEGMENT(seg, "External/<unknown>/all", NULL, NULL, NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created("NULL url creates a segment metric", seg->metrics,
"External/<unknown>/all", true);
test_txn_metric_created("NULL url creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_empty_url(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){.uri = ""});
TEST_EXTERNAL_SEGMENT(seg, "External/<unknown>/all", NULL, NULL, NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created("empty URL creates a segment metric",
seg->metrics, "External/<unknown>/all", true);
test_txn_metric_created("empty URL creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_domain_parsing_fails(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){.uri = "@@@@@"});
TEST_EXTERNAL_SEGMENT(seg, "External/<unknown>/all", "", NULL, NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created("failed domain parsing creates a segment metric",
seg->metrics, "External/<unknown>/all", true);
test_txn_metric_created("failed domain parsing creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_url_saving_strips_parameters(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){
.uri = "http://newrelic.com?secret=hhhhhhh"});
TEST_EXTERNAL_SEGMENT(seg, "External/newrelic.com/all", "http://newrelic.com",
NULL, NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created("a stripped URL creates a segment metric",
seg->metrics, "External/newrelic.com/all", true);
test_txn_metric_created("a stripped URL creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_only_external_id(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){
.uri = "newrelic.com",
.encoded_response_header = "{\"id\":\"12345#6789\"}"});
TEST_EXTERNAL_SEGMENT(seg, "External/newrelic.com/all", "newrelic.com", NULL,
NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created(
"only having an external ID creates a segment metric", seg->metrics,
"External/newrelic.com/all", true);
test_txn_metric_created("only having an external ID creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_only_external_txnname(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){
.uri = "newrelic.com",
.encoded_response_header = "{\"txnname\":\"my_txn\"}"});
TEST_EXTERNAL_SEGMENT(seg, "External/newrelic.com/all", "newrelic.com", NULL,
NULL, NULL);
test_metric_vector_size(seg->metrics, 1);
test_segment_metric_created(
"only having an external transaction name creates a segment metric",
seg->metrics, "External/newrelic.com/all", true);
test_txn_metric_created(
"only having an external transaction name creates a rollup metric",
txn->unscoped_metrics, "External/all");
nr_txn_destroy(&txn);
}
static void test_external_id_and_txnname(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg, &(nr_segment_external_params_t){
.uri = "newrelic.com",
.encoded_response_header
= "{\"id\":\"12345#6789\",\"txnname\":\"my_txn\"}"});
TEST_EXTERNAL_SEGMENT(seg,
"ExternalTransaction/newrelic.com/12345#6789/my_txn",
"newrelic.com", NULL, NULL, NULL);
test_metric_vector_size(seg->metrics, 3);
test_txn_metric_created(
"having both an external ID and transaction name creates a rollup metric",
txn->unscoped_metrics, "External/all");
test_segment_metric_created(
"having both an external ID and transaction name creates a segment "
"metric in the External namespace",
seg->metrics, "External/newrelic.com/all", false);
test_segment_metric_created(
"having both an external ID and transaction name creates a segment "
"metric in the ExternalApp namespace",
seg->metrics, "ExternalApp/newrelic.com/12345#6789/all", false);
test_segment_metric_created(
"having both an external ID and transaction name creates a segment "
"metric in the ExternalTransaction namespace",
seg->metrics, "ExternalTransaction/newrelic.com/12345#6789/my_txn", true);
nr_txn_destroy(&txn);
}
static void test_external_id_and_txnname_and_guid(void) {
nr_segment_t* seg = mock_txn_segment();
nrtxn_t* txn = seg->txn;
test_segment_external_end_and_keep(
&seg,
&(nr_segment_external_params_t){
.uri = "newrelic.com",
.encoded_response_header = "{\"id\":\"12345#6789\",\"txnname\":\"my_"
"txn\",\"guid\":\"0123456789ABCDEF\"}"});
TEST_EXTERNAL_SEGMENT(seg,
"ExternalTransaction/newrelic.com/12345#6789/my_txn",
"newrelic.com", NULL, NULL, "0123456789ABCDEF");
test_metric_vector_size(seg->metrics, 3);
test_txn_metric_created(
"having an external ID, transaction name, and GUID creates a rollup "
"metric",
txn->unscoped_metrics, "External/all");
test_segment_metric_created(
"having an external ID, transaction name, and GUID creates a segment "
"metric in the External namespace",
seg->metrics, "External/newrelic.com/all", false);
test_segment_metric_created(
"having an external ID, transaction name, and GUID creates a segment "
"metric in the ExternalApp namespace",
seg->metrics, "ExternalApp/newrelic.com/12345#6789/all", false);
test_segment_metric_created(
"having an external ID, transaction name, and GUID creates a segment "
"metric in the ExternalTransaction namespace",
seg->metrics, "ExternalTransaction/newrelic.com/12345#6789/my_txn", true);
nr_txn_destroy(&txn);
}
tlib_parallel_info_t parallel_info = {.suggested_nthreads = 4, .state_size = 0};
void test_main(void* p NRUNUSED) {
test_bad_parameters();
test_web_transaction();
test_null_url();
test_empty_url();
test_domain_parsing_fails();
test_url_saving_strips_parameters();
test_only_external_id();
test_only_external_txnname();
test_external_id_and_txnname();
test_external_id_and_txnname_and_guid();
}