Skip to content

Record data-loader execution time within Apollo Tracing results#3691

Merged
Shane32 merged 5 commits intomasterfrom
apollo_tracing_record_dataloader
Nov 5, 2023
Merged

Record data-loader execution time within Apollo Tracing results#3691
Shane32 merged 5 commits intomasterfrom
apollo_tracing_record_dataloader

Conversation

@Shane32
Copy link
Copy Markdown
Member

@Shane32 Shane32 commented Aug 14, 2023

Currently a resolver's duration property starts from the moment the resolver starts executing, and ends when the result is returned, even if the result is an IDataLoaderResult. It does not include the amount of time that the data loader spent executing. So, looking at trace results you can see gaps in the timeline, which is where the execution strategy executes the data loaders.

This PR modifies the per-resolver instrumentation to record the duration to include up to the point in time that the data loader has finished. This means it is recording 'dead' time between the point that the resolver has finished and the point that the data loader starts. (The execution engine tries to run as many resolvers as possible before executing data loaders.) It also means that when batch data loaders are in use (the typical use case), multiple resolvers will be recording time within the duration property for the execution of the same data loader.

Ideally, the data loader execution time would be recorded separately, and only once for each batch data loader in use. This presents two problems: (1) the obsolete Apollo Tracing data format does not specify how to record this information, and so viewers like Playground would not be able to parse it, and (2) the execution engine is not aware of which IDataLoaderResult instances belong to which batch data loaders.

I am hoping for some feedback before merging this PR - thanks!

@Shane32 Shane32 self-assigned this Aug 14, 2023
@github-actions github-actions Bot added the test Pull request that adds new or changes existing tests label Aug 14, 2023
Comment thread src/GraphQL/Instrumentation/InstrumentFieldsMiddleware.cs Fixed
@Shane32 Shane32 added enhancement New feature or request data loader Relates to GraphQL data loaders and removed test Pull request that adds new or changes existing tests labels Aug 23, 2023
@Shane32 Shane32 added this to the 7.6 milestone Aug 23, 2023
@Shane32 Shane32 modified the milestones: 7.6, 7.7 Sep 18, 2023
@Shane32
Copy link
Copy Markdown
Member Author

Shane32 commented Nov 4, 2023

@gao-artur Let me know if you have any thoughts on this PR. I'm not sure if the result is better, or worse!

@gao-artur
Copy link
Copy Markdown
Contributor

I have never used Apollo tracing and have never seen its graphical representation. But it sounds reasonable to include the DL duration in the field resolver because the field is not resolved until the DL completes.

Comment thread src/GraphQL/Instrumentation/InstrumentFieldsMiddleware.cs Outdated

using (context.Metrics.Subject("field", name, metadata))
return await next(context).ConfigureAwait(false);
var marker = context.Metrics.Subject("field", name, metadata);

Check notice

Code scanning / CodeQL

Missed 'using' opportunity

This variable is manually [disposed](1) in a [finally block](2) - consider a C# using statement as a preferable resource management technique.
Comment thread src/GraphQL/Instrumentation/InstrumentFieldsMiddleware.cs Outdated
Comment thread src/GraphQL.DataLoader.Tests/ApolloTracingTests.cs Outdated
Comment thread src/GraphQL.DataLoader.Tests/ApolloTracingTests.cs Outdated
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

Merging #3691 (10e64a4) into master (ea9b758) will increase coverage by 84.35%.
The diff coverage is 100.00%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@             Coverage Diff             @@
##           master    #3691       +/-   ##
===========================================
+ Coverage        0   84.35%   +84.35%     
===========================================
  Files           0      412      +412     
  Lines           0    18885    +18885     
  Branches        0     2964     +2964     
===========================================
+ Hits            0    15930    +15930     
- Misses          0     2261     +2261     
- Partials        0      694      +694     
Files Coverage Δ
...phQL/Instrumentation/InstrumentFieldsMiddleware.cs 100.00% <100.00%> (ø)

... and 411 files with indirect coverage changes

@Shane32 Shane32 merged commit 8360265 into master Nov 5, 2023
@Shane32 Shane32 deleted the apollo_tracing_record_dataloader branch November 5, 2023 19:34
// and the execution engine is not aware of which queued data loaders run as a single batch operation or not,
// making implementation difficult
[Fact]
public async Task DataLoaderTimeRecorded()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test is unstable

[xUnit.net 00:00:04.41]     GraphQL.DataLoader.Tests.ApolloTracingTests.DataLoaderTimeRecorded [FAIL]
  Failed GraphQL.DataLoader.Tests.ApolloTracingTests.DataLoaderTimeRecorded [2 s]
  Error Message:
   Shouldly.ShouldAssertException : resolverData.Duration
    should be greater than or equal to
2000000000L
    but was
1999818500L

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data loader Relates to GraphQL data loaders enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants