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 tests ensuring the tracing module sets and cleans up metadata properly #2894

Merged
merged 3 commits into from
Apr 13, 2023

Conversation

oleiade
Copy link
Member

@oleiade oleiade commented Feb 2, 2023

This Pull Request addresses #2889, and adds tests ensuring that the tracing module does add a trace_id metadata entry when performing a request and cleans it up afterward.

To make it possible, I had to wrap a bunch of existing behaviors throughout the module in interfaces. That way I could easily swap them with mocked/controllable behaviors: trace id generation and randomness generation.

Let me know what you think 🤝

closes #2889

@oleiade oleiade added this to the v0.43.0 milestone Feb 2, 2023
@oleiade oleiade requested review from mstoykov and codebien February 2, 2023 08:27
@oleiade oleiade self-assigned this Feb 2, 2023
@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch 2 times, most recently from 8999740 to 920bb59 Compare February 2, 2023 08:46
@codecov-commenter
Copy link

codecov-commenter commented Feb 2, 2023

Codecov Report

Merging #2894 (167224e) into master (124fc79) will decrease coverage by 0.06%.
The diff coverage is 63.88%.

❗ Current head 167224e differs from pull request most recent head 8083ef7. Consider uploading reports for the commit 8083ef7 to get more accurate results

@@            Coverage Diff             @@
##           master    #2894      +/-   ##
==========================================
- Coverage   77.03%   76.98%   -0.06%     
==========================================
  Files         228      228              
  Lines       17059    17068       +9     
==========================================
- Hits        13141    13139       -2     
- Misses       3076     3086      +10     
- Partials      842      843       +1     
Flag Coverage Δ
ubuntu 76.98% <63.88%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
js/modules/k6/experimental/tracing/trace_id.go 62.50% <16.66%> (-16.45%) ⬇️
js/modules/k6/experimental/tracing/client.go 70.79% <73.33%> (+1.07%) ⬆️

... and 4 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

mstoykov
mstoykov previously approved these changes Feb 2, 2023
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

LGTM!

I was thinking more of js code test that has

callToTestMetadata()
http.post(...)
callToTestMetadata() 

maybe something like what I do in the async group code but with a bertter name, The script that is run is in test case sabove the line I linked.

Goja's author also does something similar in https://github.com/dop251/goja/blob/8a0df7296ebdd6b66ab2bb8230f9744dc8824825/func_test.go#L212

@oleiade
Copy link
Member Author

oleiade commented Feb 3, 2023

@mstoykov that's another clever way of doing it indeed 🤝 When looking at how to access the metadata before the callback is called, I didn't think of doing it from the JS indeed.

In this case, I find the unit test ends up being "lower-context", forcing me to make the code a bit more flexible by using interfaces. If you have a strong opinion towards making it in JS, I could give it a shot too. Otherwise, I would keep it as is as unit tests and the JS approach in mind for the next time. Let me know 😸

codebien
codebien previously approved these changes Feb 6, 2023
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

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

LGTM

@codebien
Copy link
Contributor

codebien commented Feb 6, 2023

I have re-run the test-tip check to verify it is just one of the flaky.

@oleiade oleiade requested a review from mstoykov February 6, 2023 12:29
@na-- na-- modified the milestones: v0.43.0, v0.44.0 Feb 8, 2023
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

Having now gone through the code one more time and through #2886 it seems like the new Producer only purpose is to make the current test possible.

There is also no "pruducer" abstraction or terminology within the tracing vocabulary as well.

Given just how much code this adds and that I feel like it makes everything harder and the fact this all can be tested with just a single defintion of a helper js function as I said before - I would prefer we go that route and this PR to only add tests instead of also refactor.

Additionally this will hopefully make this PR have less conflicts with the sampling one

@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch from 9362103 to 520289d Compare March 7, 2023 11:00
@oleiade
Copy link
Member Author

oleiade commented Mar 7, 2023

@mstoykov Although I disagree with the perceived complexity introduced by the approach in this PR, I see your point, and I'm indeed open to doing it in JS.

After experimenting with your examples and approach, I want to clarify one of my findings. Unless I missed something, as opposed to my current approach, testing in JS won't let me assert that an instrumented function follows the underlying steps of:

  1. set the trace_id metadata
  2. perform the instrumented operation
  3. unset trace_id metadata
    because I don't have access to what happens under the hood (which I can do with the current relatively standard interfaces/mocks testing strategy). Yet, I understood that those steps were exactly what we were interested to verify they happened when calling instrumented functions.

Unless I missed or misunderstood something, what I can test from JS is only that the trace_id metadata is removed after calling an instrumented function. I would still need to read the resulting output of the test run to verify that each instrumented function emitted a trace_id. Let me know if that's what you had in mind, or if you meant something else 👍🏻 🙇🏻

@mstoykov
Copy link
Contributor

mstoykov commented Mar 7, 2023

tl;dr I checked out the branch to test something and got carried away :(

perceived complexity introduced

The problem for me is that all the new changes exist to make it possible to test this (slightly) better.

As you can see from my try at this the only thing I am not checking is the concrete value of the trace_id.

I could be testing its format, but decided to not spend more time on it.

While I also worked on this I realized I also have an issue with the fact you are basically having a unit test, while I would argue that we need more of an integration test. One in which you do what the user will do and check that we do set the metadata correctly and then unset it after the operation. But doing this with actual JS code as the user will use it instead of what seems like a reimplementation of the actual code the user will call.

Both have their place, but arguably we care more about the user experience instead of the underlying implementation details.

The "integration" test here did take more fiddling in some parts due to the existence of two functions generating test setups, but mostly due to the requirements of the k6/http package so that it is actually functional.

But even with that both commits are on par with just the code that is to add the Producers and add the mock ones.

This also now lends itself to be expanded so we can add more tests directly in the package instead of adding it to actual integration tests https://github.com/grafana/k6/tree/master/cmd/tests

@oleiade
Copy link
Member Author

oleiade commented Mar 9, 2023

@mstoykov Thanks for the thorough examples, I see what you meant much better now indeed. I'm grateful for it 🙇🏻

I hadn't consider the instrumentCall route to build an integration test, that's a good idea 👀 I'll integrate your approach in an integration test then indeed, and drop most of the interfaces/mocks I've added. 👍🏻

@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch 3 times, most recently from 7b6265c to a85227e Compare March 10, 2023 17:37
@oleiade
Copy link
Member Author

oleiade commented Mar 10, 2023

I've updated the PR according to your recommendations. I've integrated the tests you kindly put together Mihail, thanks again for that 🤝 I've also dropped the Producer abstraction.

@oleiade oleiade requested a review from mstoykov March 10, 2023 17:37
@oleiade oleiade requested a review from codebien March 10, 2023 17:38
@codebien
Copy link
Contributor

@oleiade the CI reported a panic

@oleiade
Copy link
Member Author

oleiade commented Mar 13, 2023

👍🏻 @codebien looking into it

@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch from a85227e to 505915b Compare March 14, 2023 10:54
codebien
codebien previously approved these changes Mar 15, 2023
Copy link
Contributor

@codebien codebien left a comment

Choose a reason for hiding this comment

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

LGTM 👏 . It seems you moved some functions, the next time would be helpful not to do it in the same commit you are doing the changes so it will be easier to review/skip.

Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

I do like htat we now do not use the global rand source - that was definitel a problem I didn't notice before, but it is a concurrency concern as I mentioned in another PR.

But you then go and do not actually check the value :(.

@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch from 8756d59 to 2532a35 Compare April 13, 2023 11:28
In order to be able to test how the client updates VUs metadata with
a trace_id, we need to be able to predictably control the generated
trace ids.

Because the traceID encodes itself, we set a private an `io.Reader`
randomness source attribute on it that will be used during encoding.
That way we can for instance pass a reader that always produces the
same value in the context of tests.
@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch from 2532a35 to 65727a4 Compare April 13, 2023 11:28
@oleiade oleiade requested review from mstoykov and codebien April 13, 2023 11:28
@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch 2 times, most recently from e79f122 to 0f9f0b5 Compare April 13, 2023 13:02
@mstoykov
Copy link
Contributor

Can you please squash/fixup 0f9f0b5 into f3388db unless you will be squashign the whole PR?

@oleiade oleiade force-pushed the experimental/tracing-test-metadata branch from 0f9f0b5 to 8083ef7 Compare April 13, 2023 14:27
@oleiade
Copy link
Member Author

oleiade commented Apr 13, 2023

@mstoykov done 👍🏻

@oleiade oleiade requested a review from codebien April 13, 2023 14:28
Comment on lines +197 to +204
func (c *Client) instrumentedCall(call func(args ...goja.Value) error, args ...goja.Value) error {
if len(args) == 0 {
args = []goja.Value{goja.Null()}
}

traceContextHeader, encodedTraceID, err := c.generateTraceContext()
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this Pr would've been easier to code review if you didn't move this methods around in the codebase

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough 🙇‍♂️Sorry for that. I'm trying to get better at isolating these kind of changes, but still having a hard time.

Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

LGTM!

@oleiade oleiade merged commit d946fdb into master Apr 13, 2023
@oleiade oleiade deleted the experimental/tracing-test-metadata branch April 13, 2023 15:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write a test that the metadata gets reset with the tracing API
5 participants