forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_txn.c
182 lines (147 loc) · 5.56 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
/*
* 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();
}
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.
*/
tlib_php_engine_create(
"newrelic.transaction_events.attributes.include=request.uri" PTSRMLS_CC);
test_handle_fpm_error(TSRMLS_C);
test_max_segments_config_values(TSRMLS_C);
tlib_php_engine_destroy(TSRMLS_C);
}