Skip to content

add testctx for instrumenting integration tests with OTel - #7532

Merged
jedevc merged 15 commits into
dagger:mainfrom
vito:testctx
Jun 26, 2024
Merged

jedevc merged 15 commits into
dagger:mainfrom
vito:testctx

Conversation

@vito

@vito vito commented Jun 3, 2024 •

Copy link
Copy Markdown
Contributor

Adds testsctx, a little *testing.T compatible testing library that adds ctx propagation and middleware. Ideally we'd extract it for general use once it's fully baked so Go folks can use it and get pretty OTel integration in Cloud.

Adds the following testctx middleware to our integration test suite:

  • t.Parallel everywhere. Non-parallel tests have been adapted to work in parallel. (I think.)
    • If we need to adjust this, the middleware can be made conditional on t.Name() or something (i.e. put "sync" in the test name), or we can just add a non-parallel suite that doesn't use the middleware and treat it like tracking tech debt.
  • OTel tracing integration: each test and sub-test becomes a span.
  • OTel logging integration: t.Log and friends log to the OTel span.

The net result is a fully instrumented integration test suite, where you don't have to read go test or gotestsum output; you can just see everything visualized in the TUI or Cloud.

TODO (in future PRs):

  • Finish testctx design
    • The FooSuite type is mostly a convenient way to corral around a set of methods, but currently the value itself serves no purpose. Worse, it's tempting to put state there, but t.Run sub-tests will all capture the same var, causing race conditions.
  • Better parallelism internals than t.Parallel?
    • Probably still need use t.Parallel as a primitive for compatibility with go test - there's no way to access the -parallel flag value, so we have to just respect it. Don't want a custom CLI.
    • This mostly comes down to "how to initialize state without races," so maybe related to previous point

Leaving these alone for now because they're hard and I'm tired of rebasing this PR. 😅

@vito
vito force-pushed the testctx branch 5 times, most recently from fce3155 to 2a852b2 Compare June 11, 2024 15:58
@vito
vito force-pushed the testctx branch 5 times, most recently from 75362ad to 9ce94b7 Compare June 13, 2024 15:02
@vito
vito force-pushed the testctx branch 13 times, most recently from e84863e to 67e63b7 Compare June 21, 2024 00:29
@vito
vito force-pushed the testctx branch 5 times, most recently from c1ceeef to 2669559 Compare June 21, 2024 17:11
@vito
vito requested a review from helderco June 24, 2024 19:35
@jedevc

jedevc commented Jun 25, 2024

Copy link
Copy Markdown
Contributor

Ah I hate to be the bearer of bad news - but it looks like the test and testdev jobs in this PR are running at least x2 as slow as currently on main.

Comment thread ci/test.go Outdated

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.

Phew 🎉 Installing deps here was kinda bad.

@vito

vito commented Jun 25, 2024 •

Copy link
Copy Markdown
Contributor Author

@jedevc Yeah I'm not sure what's up with that. I saw the GHA jobs were slower yesterday for some reason, but if you look at the actual traces they complete faster than main. 🤔 (Not that I expect any speedup from this.)

This PR:

main:

There are conflicts now so I'll rebase and see if the weird GHA slowness happens again.

vito added 11 commits June 25, 2024 11:34
* testing.T -> testctx.T
  * parallel by default (currently no way _not_ to)
  * carries around a context.Context
  * supports middleware patterns, e.g. for OTel instrumentation
  * embeds *testing.T, so fully compatible; if you need the concrete
    type, just access the field
* tests now live on a Suite

Signed-off-by: Alex Suraci <[email protected]>
ended up timing out while waiting on t.Parallel

Signed-off-by: Alex Suraci <[email protected]>
apparently the second column is number of hardlinks; we dont care

Signed-off-by: Alex Suraci <[email protected]>
it expected t.Run to run in series

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
something is hiding race detector output. the new instrumentation
replaces the need for prettified output and slowness reports, so let's
just eliminate a variable.

Signed-off-by: Alex Suraci <[email protected]>
failed twice, but looks like our infra is just slow

Signed-off-by: Alex Suraci <[email protected]>
@vito

vito commented Jun 25, 2024

Copy link
Copy Markdown
Contributor Author

Happened again.

wat?

Comparing span count:

  • PR: 145k
  • main: 138k

A bit of a difference there but that's not surprising considering what the PR does. There doesn't seem to be much slowdown on the Cloud side; I was able to view the completed trace there while the GHA job was ongoing.

Comparing the raw logs (PR vs. main, there is a pretty big difference:

❯ wc -l /tmp/*-all.log
   178060 /tmp/main-all.log
   425990 /tmp/pr-all.log

Guess I'll look into that next.

vito added 4 commits June 25, 2024 13:05
just to see if it makes any difference

Signed-off-by: Alex Suraci <[email protected]>
This reverts commit 8951032.

Signed-off-by: Alex Suraci <[email protected]>
Rather than logging to the test span, log to the underlying test logger.

When we log to the test span we're unconditionally sending what is in
total a massive amount of log volume. Previously we only saw these logs
on test failure, so this change brings us back to that by sending them
to the underlying *testing.T logger instead.

Signed-off-by: Alex Suraci <[email protected]>
@vito

vito commented Jun 25, 2024 •

Copy link
Copy Markdown
Contributor Author

@jedevc Figured it out.

Previously we would send test client logs (i.e. dagger.WithLogOutput) to *testing.T, so those logs would only ever get printed in the gotestsum / go test output and only when the test failed.

But this PR changed that to send test logs to *testctx.T which also sends to the test's OpenTelemetry span logs, making all of that log volume unconditional.

The thing is, those logs are completely redundant with the data already being sent over telemetry. I was already tempted to remove it because it made the output pretty verbose, although it was kind of cool to see the plain logs format side-by-side with the TUI.

So, the latest commit changes the client NewTWriter usage to send to t.T instead of t. It's subtle, but the net effect should be what we want: we'll collect logs and show them in go test output on failure (so the testing DX isn't reliant on OTel integration), without sending a ton of redundant data over OTel.

Seems like it did the trick; now the tests finish in approximately the same amount of time as main. 🎉

(Except the latest test run flaked out on ProjectLayout again so I reran it.)

@jedevc
jedevc merged commit f39e388 into dagger:main Jun 26, 2024
@jedevc

jedevc commented Jun 26, 2024

Copy link
Copy Markdown
Contributor

Woo thanks for diving into all this 🎉

Merged this now, just to avoid accidentally merging something before this that would have required more rebase hell for you 😢

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.

2 participants