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

Add new GraphQL tracer complying to span attributes specification #3672

Merged
merged 12 commits into from
Jun 26, 2024

Conversation

vpellan
Copy link
Contributor

@vpellan vpellan commented May 29, 2024

What does this PR do?

This PR adds a new GraphQL tracer that follows Datadog's span attributes specification. This can be activated with the option "with_unified_tracer: true". This new tracer also create a span for every field instead of only the non-scalar ones.

Motivation:

Following the span attributes spec is required to use API Catalog

Additional Notes:

  • Although this now follows the span attributes spec, it seems that the http layer does not entirely (http.route for example). Meaning that we will not be able to use it in API Catalog yet
  • The new tracer is named "unified_trace" as it follows the unified naming convention but feel free to suggest a better name (datadog_trace is already used upstream on the graphql-ruby gem, and while we could use it too, it's better to avoid confusion)

@vpellan vpellan self-assigned this May 29, 2024
@vpellan vpellan requested a review from a team as a code owner May 29, 2024 13:17
@github-actions github-actions bot added integrations Involves tracing integrations tracing labels May 29, 2024
@vpellan vpellan force-pushed the vpellan/new-graphql-tracing branch from 142aec9 to e22728b Compare June 20, 2024 11:00
@vpellan vpellan requested a review from a team as a code owner June 20, 2024 11:00
Copy link
Contributor

@p-datadog p-datadog left a comment

Choose a reason for hiding this comment

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

I read this PR and it looks really good, discussed it with @vpellan and I had two suggestions:

  1. https://github.com/DataDog/dd-trace-rb/blob/master/docs/GettingStarted.md appears to be the source for https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/ruby/, add the new with_unified_tracer option into the options table there
  2. For the graphql methods overridden by the tracer, add catch-all arguments *args, **kwargs to permit graphql library to add additional arguments to the methods which currently (with the PR as it is right now) would not be possible because this PR effectively freezes method signatures.

@vpellan vpellan requested a review from a team as a code owner June 21, 2024 15:39
@hestonhoffman hestonhoffman added the editorial review Waiting for a review from the docs team label Jun 21, 2024
module Tracing
module Contrib
module GraphQL
# These methods will be called by the GraphQL runtime to trace the execution of queries
Copy link
Member

@marcotc marcotc Jun 21, 2024

Choose a reason for hiding this comment

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

Can you add a short blurb here about what exactly the UnifiedTrace represents, specially if compared with the default TracePatcher?

I want to make sure we have enough context in the future to know whether we should switch to use this module by default, in the next major version. (actually, if it is desirable to switch to the UnifiedTrace by default, let's write that down in the comments here. We used the keyword # DEV-3.0: when looking into possible changes for the next major version upgrade, so we can do the same here, if it applies)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed !

span.set_tag('graphql.operation.type', query.selected_operation.operation_type)
span.set_tag('graphql.operation.name', query.selected_operation_name) if query.selected_operation_name
query.provided_variables.each do |key, value|
span.set_tag("graphql.variables.#{key}", value)
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a concern with unbound carnality here? In case query.provided_variables is a very large list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is two possibilities : First option, we use provided_variables, which takes every variable even the ones that are not used in the query. This could indeed create concern for unbound cardinality. Second option: We use variables.storage. This is a map of the variables that will be used in the query, thus reducing concerns about unbound cardinality, but with less information sent in the trace.

end

def execute_field_span(callable, span_key, **kwargs)
platform_key = @platform_key_cache[UnifiedTrace].platform_field_key_cache[kwargs[:field]]
Copy link
Member

Choose a reason for hiding this comment

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

@platform_key_cache doesn't seem to be initialized anywhere. Could you clarify where it comes from? (and probably document it in code, unless it's super trivial and I just personally missed it)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is initialized upstream, in ::GraphQL::Tracing::PlatformTrace. I will add a comment to clarify it.

Comment on lines 124 to 129
# Implement this method in a subclass to apply custom tags to datadog spans
# @param key [String] The event being traced
# @param data [Hash] The runtime data for this event (@see GraphQL::Tracing for keys for each event)
# @param span [Datadog::Tracing::SpanOperation] The datadog span for this event
# def prepare_span(key, data, span)
# end
Copy link
Member

Choose a reason for hiding this comment

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

Is the prepare_span method commented out for a particular reason? Or did we just forget to uncomment it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason is to show how you can add custom tags to your spans. We can delete these comments and add it to the documentation instead.

Copy link
Contributor

@p-datadog p-datadog left a comment

Choose a reason for hiding this comment

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

Approving because the points I brought up are now addressed, but I find Marco's questions and suggestions valuable as well.

Copy link
Member

@marcotc marcotc left a comment

Choose a reason for hiding this comment

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

Awesome work, @vpellan!

@marcotc marcotc enabled auto-merge June 26, 2024 18:50
@marcotc marcotc merged commit 4d44edd into master Jun 26, 2024
164 of 165 checks passed
@marcotc marcotc deleted the vpellan/new-graphql-tracing branch June 26, 2024 19:10
@github-actions github-actions bot added this to the 2.2.0 milestone Jun 26, 2024
@TonyCTHsu TonyCTHsu mentioned this pull request Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
editorial review Waiting for a review from the docs team integrations Involves tracing integrations tracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants