Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set _dd.base_service when overriding service name for a span #2275

Merged
merged 57 commits into from
Nov 13, 2023

Conversation

pablomartinezbernardo
Copy link
Contributor

@pablomartinezbernardo pablomartinezbernardo commented Sep 21, 2023

Description

Add _dd.base_service to child spans when child service name is updated manually. AIT-8051

Integration tests have been changed to by default have DD_TRACE_GENERATE_ROOT_SPAN=0, to minimize the amount of tests that would have needed to be modified to add by adding the _dd.base_service tag

Readiness checklist

  • (only for Members) Changelog has been added to the release document.
  • Tests added for this feature/bug.

Reviewer checklist

  • Appropriate labels assigned.
  • Milestone is set.
  • Changelog has been added to the release document. For community contributors the reviewer is in charge of this task.

@bwoebi
Copy link
Collaborator

bwoebi commented Sep 21, 2023

Just for the record, the API you are targeting is mostly the "legacy API". A common way to change the service name would be directly editing the DDTrace\SpanData object (i.e. a simple span object defined directly in C).

E.g. (snippet from CurlIntegration):

        \DDTrace\trace_function('curl_exec', [
            'posthook' => function (SpanData $span, $args, $retval) use ($integration) {
                $span->name = $span->resource = 'curl_exec';
                $span->type = Type::HTTP_CLIENT;
                $span->service = 'curl';
// ...
             },
        ]);

What you want to do is most likely checking the service of the root span during serialization (in ext/serializer.c) and then dynamically add _dd.base_service = , if the service is not equal to the root spans service.

@pablomartinezbernardo pablomartinezbernardo changed the title [WIP] Set _dd.base_service when overriding service name for a span Set _dd.base_service when overriding service name for a span Oct 4, 2023
@pablomartinezbernardo pablomartinezbernardo marked this pull request as ready for review October 4, 2023 15:46
@pablomartinezbernardo pablomartinezbernardo requested a review from a team as a code owner October 4, 2023 15:46
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

@pablomartinezbernardo pablomartinezbernardo marked this pull request as ready for review November 7, 2023 16:29
ext/serializer.c Outdated
ZVAL_COPY(&prop_root_service_as_string, new_root_name);
}

if (strcasecmp(Z_STRVAL(prop_service_as_string), Z_STRVAL(prop_root_service_as_string)) != 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (strcasecmp(Z_STRVAL(prop_service_as_string), Z_STRVAL(prop_root_service_as_string)) != 0) {
if (zend_string_equals(Z_STR(prop_service_as_string), Z_STR(prop_root_service_as_string))) {

zend_string_equals is aware of the lengths directly and also does a pointer equality comparison (which likely will match if both are equal).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 modified the suggestion a bit by using zend_string_equals_ci instead of zend_string_equals to keep the behavior consistent with strcasecmp

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, missed that it wasn't strcmp :-)

ext/serializer.c Outdated

if (strcasecmp(Z_STRVAL(prop_service_as_string), Z_STRVAL(prop_root_service_as_string)) != 0) {
zend_array *meta = ddtrace_property_array(&span->property_meta);
zend_hash_str_add_new(meta, ZEND_STRL("_dd.base_service"), &prop_root_service_as_string);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zend_hash_str_add_new(meta, ZEND_STRL("_dd.base_service"), &prop_root_service_as_string);
zend_hash_str_update(meta, ZEND_STRL("_dd.base_service"), &prop_root_service_as_string);

Don't use _add_new if you cannot be sure that there's no user-input you could overwrite. If there's already a manually added meta key from PHP code named _dd.base_service, it'll leak. And just _add needs to be checked for success or failure to potentially free the value.

ext/serializer.c Outdated
Comment on lines 1261 to 1264
zval *prop_root_service = &span->root->property_service;
ZVAL_DEREF(prop_root_service);
zval prop_root_service_as_string;
ddtrace_convert_to_string(&prop_root_service_as_string, prop_root_service);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
zval *prop_root_service = &span->root->property_service;
ZVAL_DEREF(prop_root_service);
zval prop_root_service_as_string;
ddtrace_convert_to_string(&prop_root_service_as_string, prop_root_service);
zval prop_root_service_as_string;
ddtrace_convert_to_string(&prop_root_service_as_string, &span->root->property_service);

ddtrace_convert_to_string is able to handle refs directly

@bwoebi
Copy link
Collaborator

bwoebi commented Nov 7, 2023

Generally such code just inserting into meta should be probably moved into _serialize_meta directly and avoid altering the meta of the original span itself.

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

Copy link
Collaborator

@bwoebi bwoebi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now :-)

}

if (!zend_string_equals_ci(Z_STR(prop_service_as_string), Z_STR(prop_root_service_as_string))) {
add_assoc_str(meta, "_dd.base_service", Z_STR(prop_root_service_as_string));
Copy link
Collaborator

@bwoebi bwoebi Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this needs a refcount increase, or the release below needs to be moved into an else.

This comment has been minimized.

This comment has been minimized.

Copy link

Snapshots difference summary

The following differences have been observed in committed snapshots. It is meant to help the reviewer.
The diff is simplistic, so please check some files anyway while we improve it.

@pablomartinezbernardo pablomartinezbernardo merged commit e990779 into master Nov 13, 2023
477 checks passed
@pablomartinezbernardo pablomartinezbernardo deleted the base-service branch November 13, 2023 14:38
@PROFeNoM PROFeNoM added this to the 0.94.0 milestone Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants