Skip to content

Commit c5e0d2a

Browse files
committed
feat(agent): Improve performance by delaying special segment creation.
1 parent 3d71d47 commit c5e0d2a

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

agent/lib_aws_sdk_php.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,21 @@
8686
8787
//clang-format on
8888
*/
89-
void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* segment,
89+
void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* auto_segment,
9090
char* command_name_string,
9191
size_t command_name_len,
9292
NR_EXECUTE_PROTO) {
9393
char* command_arg_value = NULL;
94+
nr_segment_t* segment = NULL;
95+
9496
nr_segment_message_params_t message_params = {
9597
.library = SQS_LIBRARY_NAME,
9698
.destination_type = NR_MESSAGE_DESTINATION_TYPE_QUEUE,
9799
.messaging_system = AWS_SQS_MESSAGING_SERVICE,
98100
};
99101
nr_segment_cloud_attrs_t cloud_attrs = {0};
100102

101-
if (NULL == segment) {
103+
if (NULL == auto_segment) {
102104
return;
103105
}
104106

@@ -122,6 +124,16 @@ void nr_lib_aws_sdk_php_sqs_handle(nr_segment_t* segment,
122124
}
123125
#undef AWS_COMMAND_IS
124126

127+
/*
128+
* By this point, it's been determined that this call will be instrumented so
129+
* only create the segment now, grab the parent segment start time, add our
130+
* special segment attributes/metrics then close the newly created segment.
131+
*/
132+
segment = nr_segment_start(NRPRG(txn), NULL, NULL);
133+
if (NULL == segment) {
134+
return;
135+
}
136+
segment->start_time = auto_segment->start_time;
125137
cloud_attrs.aws_operation = command_name_string;
126138

127139
command_arg_value = nr_lib_aws_sdk_php_get_command_arg_value(
@@ -332,20 +344,6 @@ char* nr_lib_aws_sdk_php_get_command_arg_value(char* command_arg_name,
332344
* @throws \Exception
333345
*/
334346

335-
NR_PHP_WRAPPER(nr_aws_client_call_before) {
336-
(void)wraprec;
337-
nr_segment_t* segment = NULL;
338-
/*
339-
* Start a segment in case it needs to become an external, message, or
340-
* datastore segment.
341-
*/
342-
segment = nr_segment_start(NRPRG(txn), NULL, NULL);
343-
if (NULL != segment) {
344-
segment->wraprec = auto_segment->wraprec;
345-
}
346-
}
347-
NR_PHP_WRAPPER_END
348-
349347
NR_PHP_WRAPPER(nr_aws_client_call) {
350348
(void)wraprec;
351349

@@ -388,16 +386,6 @@ NR_PHP_WRAPPER(nr_aws_client_call) {
388386

389387
#undef AWS_CLASS_IS
390388

391-
if (NR_SEGMENT_CUSTOM == auto_segment->type) {
392-
/*
393-
* We need to end the segment that we started in the 'before' wrapper if
394-
* it wasn't handled and ended by the handling function. Handling
395-
* function would have changed the segment type from from default
396-
* (`NR_SEGMENT_CUSTOM`) if it ended it.
397-
*/
398-
nr_segment_discard(&auto_segment);
399-
}
400-
401389
/*
402390
* Since we have klass and command_name, we can give the calling segment
403391
* a more meaningful name than Aws/AwsClient::__call We can decode it to
@@ -406,11 +394,10 @@ NR_PHP_WRAPPER(nr_aws_client_call) {
406394
* EX: Aws\\Sqs\\SqsClient::sendMessage
407395
*/
408396

409-
segment = nr_txn_get_current_segment(NRPRG(txn), NULL);
410-
if (NULL != segment) {
397+
if (NULL != auto_segment) {
411398
real_class_and_command
412399
= nr_formatf("Custom/%s::%s", klass, command_name_string);
413-
nr_segment_set_name(segment, real_class_and_command);
400+
nr_segment_set_name(auto_segment, real_class_and_command);
414401
nr_free(real_class_and_command);
415402
}
416403

@@ -570,7 +557,7 @@ void nr_aws_sdk_php_enable() {
570557
/* We only support instrumentation above PHP 8.1 */
571558
/* Called when a service command is issued from a Client */
572559
nr_php_wrap_user_function_before_after_clean(
573-
NR_PSTR("Aws\\AwsClient::__call"), nr_aws_client_call_before,
574-
nr_aws_client_call, nr_aws_client_call);
560+
NR_PSTR("Aws\\AwsClient::__call"), NULL, nr_aws_client_call,
561+
nr_aws_client_call);
575562
#endif
576563
}

0 commit comments

Comments
 (0)