-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathphp_api.c
More file actions
1712 lines (1451 loc) · 48.8 KB
/
Copy pathphp_api.c
File metadata and controls
1712 lines (1451 loc) · 48.8 KB
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 <math.h>
#include "php_agent.h"
#include "php_api.h"
#include "php_error.h"
#include "php_hash.h"
#include "php_user_instrument.h"
#include "fw_drupal_common.h"
#include "nr_rum.h"
#include "util_logging.h"
#include "util_memory.h"
#include "util_metrics.h"
#include "util_number_converter.h"
#include "util_strings.h"
void nr_php_api_error(const char* format, ...) {
const char* docref = NULL;
const char* params = "";
va_list args;
/* Avoid using TSRMLS_DC so that we can use NRPRINTFMT */
TSRMLS_FETCH();
va_start(args, format);
nrl_vlog(NRL_WARNING, NRL_API, format, args);
va_end(args);
/*
* Note that if the user has set up a custom error handler and inside it
* calls one of these API functions incorrectly, this could generate an
* infinite loop. This is an acceptable risk, since this is possible even
* without the agent.
*/
va_start(args, format);
php_verror(docref, params, E_WARNING, format, args TSRMLS_CC);
va_end(args);
}
void nr_php_api_add_supportability_metric(const char* name TSRMLS_DC) {
char buf[512];
if (0 == name) {
return;
}
if (0 == NRPRG(txn)) {
return;
}
buf[0] = '\0';
snprintf(buf, sizeof(buf), "Supportability/api/%s", name);
nrm_force_add(NRTXN(unscoped_metrics), buf, 0);
}
/*
* Purpose : (New Relic API) Pretend that there is an error at this exact spot.
* Useful for business logic errors.
* - newrelic_notice_error($errstr)
* - $errstr : string : The error message to record
* - newrelic_notice_error($exception)
* - $exception : object : The exception to use to record the
* exception NOTE: This version is compatible with being a callback for
* set_exception_handler()
* - newrelic_notice_error($errstr,$exception)
* - $errstr : string : The error message to record
* - $exception : object : The exception to use to record the
* exception NOTE: The $errstr value is ignored! Started with agent
* version 4.23
* - newrelic_notice_error($errno,$errstr,$fname,$line_nr)
* - $errno : int : The error number
* - $errstr : string : The error message
* - $fname : string : The filename where the error occurred
* - $line_nr : int : The line number where the error occurred
* NOTE: This version is compatible with being a callback for
* set_error_handler() for PHP 8+
* - newrelic_notice_error($errno,$errstr,$fname,$line_nr,$ctx)
* - $errno : int : The error number
* - $errstr : string : The error message
* - $fname : string : The filename where the error occurred
* - $line_nr : int : The line number where the error occurred
* - $ctx : array : The context of the error
* NOTE: This version is compatible with being a callback for
* set_error_handler() for PHP < 8 The $ctx is ignored!
*/
#ifdef TAGS
void zif_newrelic_notice_error(void); /* ctags landing pad only */
void newrelic_notice_error(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_notice_error) {
const char* errclass = "NoticedError";
char* errormsgstr = NULL;
nr_string_len_t errormsglen = 0;
zend_long ignore1 = 0;
char* ignore2 = 0;
nr_string_len_t ignore3 = 0;
zend_long ignore4 = 0;
zval* exc = NULL;
zval* ignore5 = NULL;
int priority = 0;
zval* ignore = NULL;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_RETURN_VALUE_USED;
NR_UNUSED_THIS_PTR;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_FALSE;
}
nr_php_api_add_supportability_metric("notice_error" TSRMLS_CC);
if (NRINI(prioritize_api_errors)) {
priority = NR_PHP_ERROR_PRIORITY_API_PRIORITIZED;
} else {
priority = nr_php_error_get_priority(E_ERROR);
}
if (NR_SUCCESS != nr_txn_record_error_worthy(NRPRG(txn), priority)) {
nrl_debug(NRL_API,
"newrelic_notice_error: a higher severity error has already been "
"noticed");
RETURN_FALSE;
}
switch (ZEND_NUM_ARGS()) {
case 1:
/*
* Look for an Exception object first: if we look for a string first in
* the one argument case, the Exception will be coerced to a string and
* we won't be able to handle it as an exception without post-processing
* the string.
*/
if (SUCCESS
== zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "o", &exc)) {
break;
}
if (FAILURE
== zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "s",
&errormsgstr, &errormsglen)) {
nrl_debug(
NRL_API,
"newrelic_notice_error: invalid single argument: expected string");
RETURN_NULL();
}
break;
case 2:
if (FAILURE
== zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "zo!", &ignore,
&exc)) {
nrl_debug(NRL_API,
"newrelic_notice_error: invalid two arguments: expected "
"exception as second argument");
RETURN_NULL();
}
break;
case 4:
case 5:
/* PHP 8+ will only pass the first 4 parameters so the 5th parameter is
* declared to be optional. Also this parameter is completely ignored
* so it doesn't matter if it is passed or not.
*/
if (FAILURE
== zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET,
ZEND_NUM_ARGS() TSRMLS_CC, "lssl|z!",
&ignore1, &errormsgstr, &errormsglen,
&ignore2, &ignore3, &ignore4, &ignore5)) {
nrl_debug(NRL_API, "newrelic_notice_error: invalid five arguments");
RETURN_NULL();
}
break;
default:
nrl_debug(NRL_API,
"newrelic_notice_error: invalid number of arguments: %d",
ZEND_NUM_ARGS());
RETURN_NULL();
}
if (exc) {
if (NR_SUCCESS
== nr_php_error_record_exception(NRPRG(txn), exc, priority, true,
"Noticed exception ",
NULL TSRMLS_CC)) {
RETURN_TRUE;
} else {
nrl_debug(NRL_API, "newrelic_notice_error: invalid exception argument");
RETURN_NULL();
}
}
{
char* buf = nr_strndup(errormsgstr, errormsglen);
char* stack_json = nr_php_backtrace_to_json(NULL TSRMLS_CC);
nr_txn_record_error(NRPRG(txn), priority, true, buf, errclass, stack_json);
nr_free(buf);
nr_free(stack_json);
RETURN_TRUE;
}
}
/*
* Purpose : (New Relic API) Completely ignore this current transaction. Useful
* for keeping pinger/uptime urls from polluting the average response
* time.
* - newrelic_ignore_transaction()
*/
#ifdef TAGS
void zif_newrelic_ignore_transaction(void); /* ctags landing pad only */
void newrelic_ignore_transaction(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_ignore_transaction) {
NR_UNUSED_EXECUTE_DATA;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
return;
}
/* No reason to make a supportability metric here! */
nr_txn_ignore(NRPRG(txn));
}
/*
* Purpose : (New Relic API) Don't generate Apdex metrics for the current
* transaction.
* - newrelic_ignore_apdex()
*/
#ifdef TAGS
void zif_newrelic_ignore_apdex(void); /* ctags landing pad only */
void newrelic_ignore_apdex(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_ignore_apdex) {
NR_UNUSED_EXECUTE_DATA;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
return;
}
nr_php_api_add_supportability_metric("ignore_apdex" TSRMLS_CC);
NRTXN(status.ignore_apdex) = 1;
nrl_debug(NRL_API, "not generating Apdex metrics for this transaction");
}
/*
* Purpose : (New Relic API) Consider this point to be the end of
* this transaction. Useful when the page starts streaming video or
* something: the streaming shouldn't count as "slow".
* - newrelic_end_of_transaction()
*/
#ifdef TAGS
void zif_newrelic_end_of_transaction(void); /* ctags landing pad only */
void newrelic_end_of_transaction(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_end_of_transaction) {
NR_UNUSED_EXECUTE_DATA;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
return;
}
nr_php_api_add_supportability_metric("end_of_transaction" TSRMLS_CC);
(void)nr_txn_end(NRPRG(txn));
nrl_debug(NRL_API, "transaction ended prematurely");
}
/*
* Purpose : (New Relic API) End the current transaction, sending its data off
* to the daemon. This differs from the function above considerably,
* which simply marks the end time of a transaction. This call
* actually properly ends the transaction and ships the data off,
* under the assumption that the user code will be starting a new
* transaction.
* - newrelic_end_transaction()
* - newrelic_end_transaction(ignore)
*/
#ifdef TAGS
void zif_newrelic_end_transaction(void); /* ctags landing pad only */
void newrelic_end_transaction(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_end_transaction) {
nr_status_t ret;
zend_bool ignorebool = 0;
zend_long ignore = 0;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (0 == NRPRG(txn)) {
RETURN_FALSE;
}
nr_php_api_add_supportability_metric("end_transaction" TSRMLS_CC);
if (1 == ZEND_NUM_ARGS()) {
if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &ignorebool)) {
ignore = (long)ignorebool;
} else if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
&ignore)) {
RETURN_FALSE;
}
}
ret = nr_php_txn_end((0 != ignore), 0 TSRMLS_CC);
if (NR_SUCCESS == ret) {
nrl_debug(NRL_API, "transaction completed by API");
RETURN_TRUE;
} else {
/* IMPOSSIBLE path through interpreter */
/*
* There is no failure path through nr_php_txn_end, and if there were,
* it would only happen if there weren't a transaction,
* but we've already checked that, above.
*/
nrl_debug(NRL_API, "transaction end API failed");
RETURN_FALSE;
}
}
#ifdef TAGS
void zif_newrelic_start_transaction(void); /* ctags landing pad only */
void newrelic_start_transaction(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_start_transaction) {
nr_status_t ret;
char* appnames = NULL;
char* license = NULL;
char* appstr = NULL;
char* licstr = NULL;
nr_string_len_t applen = 0;
nr_string_len_t liclen = 0;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (0 != NRPRG(txn)) {
RETURN_FALSE;
}
if (1 == ZEND_NUM_ARGS()) {
if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &appstr,
&applen)) {
appnames = (char*)nr_alloca(applen + 1);
nr_strxcpy(appnames, appstr, applen);
} else {
RETURN_FALSE;
}
} else if (2 == ZEND_NUM_ARGS()) {
if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &appstr,
&applen, &licstr, &liclen)) {
appnames = (char*)nr_alloca(applen + 1);
nr_strxcpy(appnames, appstr, applen);
license = (char*)nr_alloca(liclen + 1);
nr_strxcpy(license, licstr, liclen);
} else {
RETURN_FALSE;
}
} else {
RETURN_FALSE;
}
ret = nr_php_txn_begin(appnames, license TSRMLS_CC);
if (NR_SUCCESS == ret) {
nr_php_api_add_supportability_metric("start_transaction" TSRMLS_CC);
nrl_debug(NRL_API, "transaction started by API");
RETURN_TRUE;
} else {
nrl_debug(NRL_API, "transaction start API failed");
RETURN_FALSE;
}
}
/*
* Purpose : (New Relic API) Mark the current transaction as a background job.
* - newrelic_background_job([background])
*/
#ifdef TAGS
void zif_newrelic_background_job(void); /* ctags landing pad only */
void newrelic_background_job(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_background_job) {
zend_long background = 0;
zend_bool backgroundbool = 0;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
return;
}
nr_php_api_add_supportability_metric("background_job" TSRMLS_CC);
if (ZEND_NUM_ARGS() >= 1) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b",
&backgroundbool)) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
&background)) {
background = 1;
} else {
/* background has the value we want */
}
} else {
background = backgroundbool;
}
} else {
background = 1;
}
if (background) {
nr_txn_set_as_background_job(NRPRG(txn),
"newrelic_background_job API call");
} else {
nr_txn_set_as_web_transaction(NRPRG(txn),
"newrelic_background_job API call");
}
}
static void nr_php_api_capture_params_internal(const char* function_name,
INTERNAL_FUNCTION_PARAMETERS) {
zend_long enable = 0;
zend_bool enablebool = 0;
NR_UNUSED_RETURN_VALUE;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
return;
}
nr_php_api_add_supportability_metric(function_name TSRMLS_CC);
if (ZEND_NUM_ARGS() >= 1) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &enablebool)) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &enable)) {
enable = 1;
} else {
/* enable has the value we want */
}
} else {
enable = enablebool;
}
} else {
enable = 1;
}
NRPRG_CTX(deprecated_capture_request_parameters) = enable ? 1 : 0;
nrl_debug(
NRL_API, "capture params enabled='%.10s'",
NRPRG_CTX(deprecated_capture_request_parameters) ? "true" : "false");
}
/*
* Purpose : (New Relic API) Turn the capture params on or off
* - newrelic_enable_params([enable])
*
* Deprecated in favor of newrelic_capture_params.
*/
#ifdef TAGS
void zif_newrelic_enable_params(void); /* ctags landing pad only */
void newrelic_enable_params(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_enable_params) {
nr_php_api_capture_params_internal("enable_params",
INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/*
* Purpose : (New Relic API) Turn the capture params on or off
* - newrelic_capture_params([enable])
*/
#ifdef TAGS
void zif_newrelic_capture_params(void); /* ctags landing pad only */
void newrelic_capture_params(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_capture_params) {
nr_php_api_capture_params_internal("capture_params",
INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
/*
* Purpose : (New Relic API) Add this custom metric
* - newrelic_custom_metric(metric, value)
*/
#ifdef TAGS
void zif_newrelic_custom_metric(void); /* ctags landing pad only */
void newrelic_custom_metric(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_custom_metric) {
char* metricstr = NULL;
nr_string_len_t metriclen = 0;
double value_ms = 0;
char* key = NULL;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_TRUE;
}
nr_php_api_add_supportability_metric("custom_metric" TSRMLS_CC);
if (ZEND_NUM_ARGS() < 2) {
RETURN_FALSE;
}
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sd", &metricstr,
&metriclen, &value_ms)) {
RETURN_FALSE;
}
key = (char*)nr_alloca(metriclen + 1);
nr_strxcpy(key, metricstr, metriclen);
if (NR_SUCCESS == nr_txn_add_custom_metric(NRPRG(txn), key, value_ms)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
#define NR_PHP_API_INVALID_ATTRIBUTE_FMT \
"%s: expects parameter to be scalar, %s given"
static nrobj_t* nr_php_api_zval_to_attribute_obj(const zval* z TSRMLS_DC) {
char* str = NULL;
nrobj_t* obj = NULL;
if (NULL == z) {
return NULL;
}
nr_php_zval_unwrap(z);
switch (Z_TYPE_P(z)) {
case IS_NULL:
return nro_new_none();
case IS_LONG:
return nro_new_long(Z_LVAL_P(z));
case IS_DOUBLE:
return nro_new_double(Z_DVAL_P(z));
case IS_TRUE:
return nro_new_boolean(1);
case IS_FALSE:
return nro_new_boolean(0);
case IS_STRING:
if (!nr_php_is_zval_valid_string(z)) {
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "invalid string");
return NULL;
}
/* Copy the string to ensure that it is NUL-terminated */
str = nr_strndup(Z_STRVAL_P(z), Z_STRLEN_P(z));
obj = nro_new_string(str);
nr_free(str);
return obj;
case IS_ARRAY:
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "array");
return NULL;
case IS_OBJECT:
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "object");
return NULL;
case IS_RESOURCE:
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "resource");
return NULL;
#if ZEND_MODULE_API_NO < ZEND_7_3_X_API_NO
case IS_CONSTANT:
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "constant");
return NULL;
#endif /* PHP < 7.3 */
case IS_CONSTANT_AST:
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "constant AST");
return NULL;
default:
nr_php_api_error(NR_PHP_API_INVALID_ATTRIBUTE_FMT,
get_active_function_name(TSRMLS_C), "unknown");
return NULL;
}
}
/*
* Purpose : (New Relic API) Add this custom parameter to the current
* transaction
* - newrelic_add_custom_parameter(key, value)
*/
#ifdef TAGS
void zif_newrelic_add_custom_parameter(void); /* ctags landing pad only */
void newrelic_add_custom_parameter(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_add_custom_parameter) {
zval* zzkey = NULL;
zval* zzvalue = NULL;
char* key = NULL;
char tmp[64];
nr_status_t rv = NR_SUCCESS;
nrobj_t* obj = NULL;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_TRUE;
}
nr_php_api_add_supportability_metric("add_custom_parameter" TSRMLS_CC);
if (ZEND_NUM_ARGS() < 2) {
RETURN_FALSE;
}
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &zzkey,
&zzvalue)) {
RETURN_FALSE;
}
nr_php_zval_unwrap(zzkey);
switch (Z_TYPE_P(zzkey)) {
case IS_NULL:
key = nr_strdup("(null)");
break;
case IS_LONG:
snprintf(tmp, sizeof(tmp), "%ld", (long)Z_LVAL_P(zzkey));
key = nr_strdup(tmp);
break;
case IS_DOUBLE:
nr_double_to_str(tmp, sizeof(tmp), Z_DVAL_P(zzkey));
key = nr_strdup(tmp);
break;
case IS_TRUE:
key = nr_strdup("True");
break;
case IS_FALSE:
key = nr_strdup("False");
break;
case IS_ARRAY:
key = nr_strdup("(Array)");
break;
case IS_OBJECT:
key = nr_strdup("(Object)");
break;
case IS_STRING:
if (!nr_php_is_zval_valid_string(zzkey)) {
key = nr_strdup("(Invalid String)");
} else {
key = (char*)nr_malloc(Z_STRLEN_P(zzkey) + 1);
nr_strxcpy(key, Z_STRVAL_P(zzkey), Z_STRLEN_P(zzkey));
}
break;
case IS_RESOURCE:
key = nr_strdup("(Resource)");
break;
#if ZEND_MODULE_API_NO < ZEND_7_3_X_API_NO
case IS_CONSTANT:
key = nr_strdup("(Constant)"); /* NOTTESTED */
break;
#endif /* PHP < 7.3 */
case IS_CONSTANT_AST:
key = nr_strdup("(Constant AST)"); /* NOTTESTED */
break;
default:
key = nr_strdup("(?)"); /* NOTTESTED */
break;
}
obj = nr_php_api_zval_to_attribute_obj(zzvalue TSRMLS_CC);
if (obj) {
rv = nr_txn_add_user_custom_parameter(NRPRG(txn), key, obj);
}
nro_delete(obj);
nr_free(key);
if (NR_SUCCESS == rv) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/*
* Purpose : (New Relic API) Specify the name of the current transaction
* - newrelic_name_transaction(string)
*/
#ifdef TAGS
void zif_newrelic_name_transaction(void); /* ctags landing pad only */
void newrelic_name_transaction(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_name_transaction) {
int zend_rv = 0;
nr_status_t rv;
char* namestr = NULL;
nr_string_len_t namestrlen = 0;
char* s = NULL;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_TRUE;
}
nr_php_api_add_supportability_metric("name_transaction" TSRMLS_CC);
if (1 != ZEND_NUM_ARGS()) {
nrl_warning(
NRL_API,
"newrelic_name_transaction failure: improper number of parameters");
RETURN_FALSE;
}
zend_rv = zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &namestr,
&namestrlen);
if ((SUCCESS != zend_rv) || (0 == namestr) || (namestrlen <= 0)) {
nrl_warning(
NRL_API,
"newrelic_name_transaction failure: unable to parse string parameter");
RETURN_FALSE;
}
s = (char*)nr_alloca(namestrlen + 1);
nr_strxcpy(s, namestr, namestrlen);
rv = nr_txn_set_path("API", NRPRG(txn), s, NR_PATH_TYPE_CUSTOM,
NR_OK_TO_OVERWRITE);
if (NR_SUCCESS != rv) {
nrl_warning(
NRL_API,
"newrelic_name_transaction failure: unable to change name to " NRP_FMT,
NRP_TXNNAME(s));
} else {
nrl_debug(NRL_API, "newrelic_name_transaction: API naming is " NRP_FMT,
NRP_TXNNAME(s));
}
RETURN_TRUE;
}
/*
* Purpose : (New Relic API) Add this function to the transaction tracer
* - newrelic_add_custom_tracer(function_name)
* - newrelic_add_custom_tracer(classname::function_name)
*/
#ifdef TAGS
void zif_newrelic_add_custom_tracer(void); /* ctags landing pad only */
void newrelic_add_custom_tracer(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_add_custom_tracer) {
char* namestr = NULL;
nr_string_len_t namestrlen = 0;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_TRUE;
}
nr_php_api_add_supportability_metric("add_custom_tracer" TSRMLS_CC);
if (1 == ZEND_NUM_ARGS()) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &namestr,
&namestrlen)) {
RETURN_FALSE;
}
} else {
RETURN_FALSE;
}
nr_php_add_custom_tracer(namestr, namestrlen TSRMLS_CC);
RETURN_TRUE;
}
/*
* Purpose : (New Relic API) Support Real User Monitoring(tm)
* - newrelic_get_browser_timing_header(bool)
* - newrelic_get_browser_timing_footer(bool)
*
* Optional boolean (defaults to true) tells us whether or not to
* return the enclosing script tags.
*/
#ifdef TAGS
void zif_newrelic_get_browser_timing_header(void); /* ctags landing pad only */
void newrelic_get_browser_timing_header(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_get_browser_timing_header) {
char* timingScript = NULL;
zend_long usetags = 1;
zend_bool tagsbool = 0;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_EMPTY_STRING();
}
nr_php_api_add_supportability_metric("get_browser_timing_header" TSRMLS_CC);
if (ZEND_NUM_ARGS() >= 1) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &tagsbool)) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &usetags)) {
usetags = 1;
} else {
/* usetags has the value we want */
}
} else {
usetags = tagsbool;
}
}
timingScript = nr_rum_produce_header(NRPRG(txn), usetags == 1, 0);
if (0 == timingScript) {
RETURN_EMPTY_STRING();
}
/*
* Required to silence warnings about PHP's prototypes.
*/
RETVAL_STRING(timingScript);
nr_free(timingScript);
}
#ifdef TAGS
void zif_newrelic_get_browser_timing_footer(void); /* ctags landing pad only */
void newrelic_get_browser_timing_footer(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_get_browser_timing_footer) {
zend_long usetags = 1;
zend_bool tagsbool = 0;
char* buf = NULL;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
RETURN_EMPTY_STRING();
}
nr_php_api_add_supportability_metric("get_browser_timing_footer" TSRMLS_CC);
if (ZEND_NUM_ARGS() >= 1) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &tagsbool)) {
if (FAILURE
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &usetags)) {
usetags = 1;
} else {
/* usetags has the value we want */
}
} else {
usetags = tagsbool;
}
}
buf = nr_rum_produce_footer(NRPRG(txn), usetags == 1, 0);
if (0 == buf) {
RETURN_EMPTY_STRING();
}
/*
* Required to silence warnings about PHP's prototypes.
*/
RETVAL_STRING(buf);
nr_free(buf);
}
/*
* Purpose : (New Relic API) If auto-RUM not already sent, disable it.
* - newrelic_disable_autorum()
*/
#ifdef TAGS
void zif_newrelic_disable_autorum(void); /* ctags landing pad only */
void newrelic_disable_autorum(void); /* ctags landing pad only */
#endif
PHP_FUNCTION(newrelic_disable_autorum) {
NR_UNUSED_EXECUTE_DATA;
NR_UNUSED_HT;
NR_UNUSED_RETURN_VALUE_PTR;
NR_UNUSED_THIS_PTR;
NR_UNUSED_RETURN_VALUE_USED;
if (!nr_php_recording(TSRMLS_C)) {
return;
}
nr_php_api_add_supportability_metric("disable_autorum" TSRMLS_CC);
NRTXN(options.autorum_enabled) = 0;
RETURN_TRUE;
}
/* Set up a bitmask to track the state of a call to newrelic_set_appname(). */
typedef uint8_t nr_php_set_appname_state_t;
const nr_php_set_appname_state_t NR_PHP_APPNAME_LICENSE_CHANGED = (1 << 0);
const nr_php_set_appname_state_t NR_PHP_APPNAME_LICENSE_PROVIDED = (1 << 1);
const nr_php_set_appname_state_t NR_PHP_APPNAME_LASP_ENABLED = (1 << 2);
/*
* LASP will prevent an application switch using the newrelic_set_appname() API
* below if LASP is enabled (by setting newrelic.security_policies_token to a
* non-empty string) _and_ a different licence key has been provided. This
* constant encodes what that state looks like in an nr_php_set_appname_state_t
* bitmask, as defined above.
*/