-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_txn.c
377 lines (291 loc) · 12.1 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
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "tlib_php.h"
#include "php_agent.h"
#include "php_header.h"
#include "php_txn_private.h"
#include "php_newrelic.h"
#include "php_globals.h"
static void test_handle_fpm_error(TSRMLS_D) {
nrobj_t* agent_attributes;
char* sapi_name;
/*
* Test : Bad parameters.
*/
nr_php_txn_handle_fpm_error(NULL TSRMLS_CC);
/*
* Test : Non-FPM. By default, the unit tests report the SAPI as embed, so
* the PHP-FPM behavior won't be triggered.
*/
tlib_php_request_start();
nr_txn_set_path(NULL, NRPRG(txn), "foo", NR_PATH_TYPE_URI,
NR_NOT_OK_TO_OVERWRITE);
nr_php_txn_handle_fpm_error(NRPRG(txn) TSRMLS_CC);
tlib_pass_if_str_equal("transaction path should be unchanged", "foo",
NRTXN(path));
tlib_pass_if_int_equal("transaction path type should be unchanged",
(int)NR_PATH_TYPE_URI, (int)NRTXN(status).path_type);
tlib_php_request_end();
/*
* The next few tests will all pretend to be FPM, so let's set the SAPI name
* accordingly.
*/
sapi_name = sapi_module.name;
sapi_module.name = "fpm-fcgi";
/*
* Test : FPM, but with at least one frame called.
*/
tlib_php_request_start();
nr_txn_set_path(NULL, NRPRG(txn), "foo", NR_PATH_TYPE_URI,
NR_NOT_OK_TO_OVERWRITE);
tlib_php_request_eval("$a = 1 + 1; // create a PHP call frame" TSRMLS_CC);
nr_php_txn_handle_fpm_error(NRPRG(txn) TSRMLS_CC);
tlib_pass_if_str_equal("transaction path should be unchanged", "foo",
NRTXN(path));
tlib_pass_if_int_equal("transaction path type should be unchanged",
(int)NR_PATH_TYPE_URI, (int)NRTXN(status).path_type);
tlib_php_request_end();
/*
* Test : FPM, but with a non-URI path set.
*/
tlib_php_request_start();
nr_txn_set_path(NULL, NRPRG(txn), "foo", NR_PATH_TYPE_ACTION,
NR_NOT_OK_TO_OVERWRITE);
nr_php_txn_handle_fpm_error(NRPRG(txn) TSRMLS_CC);
tlib_pass_if_str_equal("transaction path should be unchanged", "foo",
NRTXN(path));
tlib_pass_if_int_equal("transaction path type should be unchanged",
(int)NR_PATH_TYPE_ACTION,
(int)NRTXN(status).path_type);
tlib_php_request_end();
/*
* Test : FPM, with the specific case that should result in a status code
* based transaction name: a fallback URI path, plus a zero call count
* in PHP (since no user function or file is ever executed in the
* request).
*/
tlib_php_request_start();
nr_txn_set_path(NULL, NRPRG(txn), "foo", NR_PATH_TYPE_URI,
NR_NOT_OK_TO_OVERWRITE);
nr_php_sapi_headers(TSRMLS_C)->http_response_code = 404;
nr_php_txn_handle_fpm_error(NRPRG(txn) TSRMLS_CC);
tlib_pass_if_str_equal("transaction path should be updated", "404",
NRTXN(path));
tlib_pass_if_int_equal("transaction path type should be updated",
(int)NR_PATH_TYPE_STATUS_CODE,
(int)NRTXN(status).path_type);
agent_attributes = nr_attributes_agent_to_obj(NRTXN(attributes),
NR_ATTRIBUTE_DESTINATION_ALL);
tlib_pass_if_not_null("agent attributes must be defined", agent_attributes);
tlib_pass_if_str_equal(
"agent attributes must include a request.uri with the original path",
"foo", nro_get_hash_string(agent_attributes, "request.uri", NULL));
nro_delete(agent_attributes);
tlib_php_request_end();
/*
* Put the SAPI name back to what it should be.
*/
sapi_module.name = sapi_name;
}
static void test_max_segments_config_values(TSRMLS_D) {
nrtxn_t* txn;
/*
* Test : max_segments_cli defaults correctly.
*/
NR_PHP_PROCESS_GLOBALS(cli) = 1;
tlib_php_request_start();
txn = NRPRG(txn);
tlib_pass_if_size_t_equal("max_segments should be the default of 100,000",
100000, txn->options.max_segments);
tlib_php_request_end();
/*
* Test : max_segments_cli gets set correctly.
*/
NRINI(tt_max_segments_cli) = 200;
NR_PHP_PROCESS_GLOBALS(cli) = 1;
tlib_php_request_start();
txn = NRPRG(txn);
tlib_pass_if_size_t_equal("max_segments should be 200", 200,
txn->options.max_segments);
tlib_php_request_end();
/*
* Test : correctly defaults to 0 when web txn.
*/
NR_PHP_PROCESS_GLOBALS(cli) = 0;
tlib_php_request_start();
txn = NRPRG(txn);
tlib_pass_if_size_t_equal("max_segments 0 when it's a web txn", 0,
txn->options.max_segments);
tlib_php_request_end();
/*
* Test : max_segments_cli does not change web txn max segments.
*/
NRINI(tt_max_segments_web) = 400;
NR_PHP_PROCESS_GLOBALS(cli) = 0;
tlib_php_request_start();
txn = NRPRG(txn);
tlib_pass_if_size_t_equal("max_segments should be set by web when a web txn",
400, txn->options.max_segments);
tlib_php_request_end();
}
#define PHP_VERSION_METRIC_BASE "Supportability/PHP/Version"
#define AGENT_VERSION_METRIC_BASE "Supportability/PHP/AgentVersion"
static void test_create_php_version_metric() {
nrtxn_t* txn;
int count;
tlib_php_request_start();
txn = NRPRG(txn);
count = nrm_table_size(txn->unscoped_metrics);
/* Test invalid values are properly handled */
nr_php_txn_create_php_version_metric(NULL, NULL);
tlib_pass_if_int_equal("PHP version metric shouldnt be created 1", count,
nrm_table_size(txn->unscoped_metrics));
nr_php_txn_create_php_version_metric(txn, NULL);
tlib_pass_if_int_equal("PHP version metric shouldnt be created 2", count,
nrm_table_size(txn->unscoped_metrics));
nr_php_txn_create_php_version_metric(NULL, "7.4.0");
tlib_pass_if_int_equal("PHP version metric shouldnt be created 3", count,
nrm_table_size(txn->unscoped_metrics));
nr_php_txn_create_php_version_metric(txn, "");
tlib_pass_if_int_equal("PHP version metric shouldnt be created 4", count,
nrm_table_size(txn->unscoped_metrics));
/* test valid values */
nr_php_txn_create_php_version_metric(txn, "7.4.0");
tlib_pass_if_int_equal("PHP version metric should be create", count + 1,
nrm_table_size(txn->unscoped_metrics));
const nrmetric_t* metric
= nrm_find(txn->unscoped_metrics, PHP_VERSION_METRIC_BASE "/7.4.0");
const char* metric_name = nrm_get_name(txn->unscoped_metrics, metric);
tlib_pass_if_not_null("PHP version metric found", metric);
tlib_pass_if_str_equal("PHP version metric name check", metric_name,
PHP_VERSION_METRIC_BASE "/7.4.0");
tlib_php_request_end();
}
static void test_create_agent_version_metric() {
nrtxn_t* txn;
int count;
tlib_php_request_start();
txn = NRPRG(txn);
count = nrm_table_size(txn->unscoped_metrics);
/* Test invalid values are properly handled */
nr_php_txn_create_agent_version_metric(NULL);
tlib_pass_if_int_equal("Agent version metric shouldnt be created - txn is NULL", count,
nrm_table_size(txn->unscoped_metrics));
/* Test valid values */
nr_php_txn_create_agent_version_metric(txn);
tlib_pass_if_int_equal("Agent version metric should be created - txn is not NULL", count + 1,
nrm_table_size(txn->unscoped_metrics));
const nrmetric_t* metric
= nrm_find(txn->unscoped_metrics, AGENT_VERSION_METRIC_BASE "/" NR_VERSION);
const char* metric_name = nrm_get_name(txn->unscoped_metrics, metric);
tlib_pass_if_not_null("Agent version metric found", metric);
tlib_pass_if_str_equal("Agent version metric name check", metric_name,
AGENT_VERSION_METRIC_BASE "/" NR_VERSION);
tlib_php_request_end();
}
static void test_create_agent_php_version_metrics() {
nrtxn_t* txn;
/*
* Test : Create agent PHP version metrics.
*/
tlib_php_request_start();
txn = NRPRG(txn);
zval* expected_php_zval = tlib_php_request_eval_expr("phpversion();");
char* php_version_name = nr_formatf(PHP_VERSION_METRIC_BASE "/%s",
Z_STRVAL_P(expected_php_zval));
nr_php_zval_free(&expected_php_zval);
char* agent_version_name
= nr_formatf(AGENT_VERSION_METRIC_BASE "/%s", NR_VERSION);
nr_php_txn_create_agent_php_version_metrics(txn);
/* Test the PHP version metric creation */
const nrmetric_t* metric = nrm_find(txn->unscoped_metrics, php_version_name);
const char* metric_name = nrm_get_name(txn->unscoped_metrics, metric);
tlib_pass_if_not_null("happy path: PHP version metric created", metric);
tlib_pass_if_not_null("happy path: PHP version metric name created",
metric_name);
tlib_pass_if_str_equal("happy path: PHP version metric name check",
metric_name, php_version_name);
/* Test the agent version metric creation*/
metric = nrm_find(txn->unscoped_metrics, agent_version_name);
metric_name = nrm_get_name(txn->unscoped_metrics, metric);
tlib_pass_if_not_null("happy path: Agent version metric created", metric);
tlib_pass_if_not_null("happy path: Agent version metric name created",
metric_name);
tlib_pass_if_str_equal("happy path: Agent version metric name check",
metric_name, agent_version_name);
nr_free(agent_version_name);
nr_free(php_version_name);
tlib_php_request_end();
}
#undef PHP_VERSION_METRIC_BASE
#undef AGENT_VERSION_METRIC_BASE
static void test_create_log_forwarding_labels(TSRMLS_D) {
nrobj_t* labels = NULL;
nrobj_t* log_labels = NULL;
char* json = NULL;
/* Test : Create log forwarding labels with valid key/value pairs */
labels = nro_new_hash();
nro_set_hash_string(labels, "key1", "value1");
nro_set_hash_string(labels, "key2", "value2");
nro_set_hash_string(labels, "key3", "value3");
log_labels = nr_php_txn_get_log_forwarding_labels(labels);
json = nro_to_json(labels);
tlib_pass_if_str_equal(
"valid log label creation test",
"{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}", json);
nr_free(json);
nro_delete(labels);
nro_delete(log_labels);
/* Test : Create log forwarding labels with empty key/value pairs */
labels = nro_new_hash();
nro_set_hash_string(labels, "", "");
nro_set_hash_string(labels, "key", "");
nro_set_hash_string(labels, "", "value");
log_labels = nr_php_txn_get_log_forwarding_labels(labels);
json = nro_to_json(labels);
tlib_pass_if_str_equal("empty string log label creation test",
"{\"key\":\"\"}", json);
nr_free(json);
nro_delete(labels);
nro_delete(log_labels);
/* Test : Create log forwarding labels with NULL key/value pairs */
labels = nro_new_hash();
nro_set_hash_string(labels, NULL, NULL);
nro_set_hash_string(labels, "key", NULL);
nro_set_hash_string(labels, NULL, "value");
log_labels = nr_php_txn_get_log_forwarding_labels(labels);
json = nro_to_json(labels);
tlib_pass_if_str_equal("NULL value log label creation test", "{\"key\":\"\"}",
json);
nr_free(json);
nro_delete(labels);
nro_delete(log_labels);
/* Test : Create log forwarding labels NULL labels object */
log_labels = nr_php_txn_get_log_forwarding_labels(NULL);
json = nro_to_json(labels);
tlib_pass_if_str_equal("NULL object log label creation test", "null", json);
nr_free(json);
nro_delete(log_labels);
}
tlib_parallel_info_t parallel_info = {.suggested_nthreads = 1, .state_size = 0};
void test_main(void* p NRUNUSED) {
#if defined(ZTS) && !defined(PHP7)
void*** tsrm_ls = NULL;
#endif /* ZTS && !PHP7 */
/*
* We're setting up our own engine instance because we need to control the
* attribute configuration.
*/
// clang-format off
tlib_php_engine_create("newrelic.transaction_events.attributes.include=request.uri" PTSRMLS_CC);
// clang-format on
test_handle_fpm_error();
test_max_segments_config_values();
test_create_php_version_metric();
test_create_agent_version_metric();
test_create_agent_php_version_metrics();
test_create_log_forwarding_labels();
tlib_php_engine_destroy();
}