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

contrib/database/sql: trace when new connection is established #794

Closed

Conversation

stroem
Copy link
Contributor

@stroem stroem commented Dec 18, 2020

This will create a new span when database establishes a new connection

Issue: #760

@gbbr
Copy link
Contributor

gbbr commented Dec 18, 2020

@CAFxX perhaps you can share some thoughts

@CAFxX
Copy link
Contributor

CAFxX commented Dec 21, 2020

LGTM, would only suggest to add a test to ensure that the span is emitted even if the context is cancelled.

@knusbaum knusbaum added this to the 1.29.0 milestone Dec 23, 2020
@knusbaum
Copy link
Contributor

@stroem would you be able to add a test for cancelled context?

Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

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

Let's add a test for cancelled context.

adw1n
adw1n previously approved these changes Jan 14, 2021
@@ -60,6 +62,24 @@ func RunAll(t *testing.T, cfg *Config) {
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

@CAFxX @knusbaum the test that you're asking for would look more or less like this:

Suggested change
func testConnectCancelledCtx(cfg *Config) func(*testing.T) {
return func(t *testing.T) {
cfg.mockTracer.Reset()
assert := assert.New(t)
ctx, cancel := context.WithCancel(context.Background())
cancel()
rs := reflect.ValueOf(cfg.DB).Elem()
rf := rs.FieldByName("connector")
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
_, err := rf.Interface().(driver.Connector).Connect(ctx)
assert.ErrorIs(err, context.Canceled)
spans := cfg.mockTracer.FinishedSpans()
_ = spans
// assert.Len(spans, 2)
// ...
}
}

You can't use cfg.DB.PingContext(ctx) because sql.DB.conn checks the context at the start (https://github.com/golang/go/blob/7eb31d999cf2769deb0e7bdcafc30e18f52ceb48/src/database/sql/sql.go#L1205) and tracedConnector::Connect isn't even called.

This test won't pass because

func (t dsnConnector) Connect(_ context.Context) (driver.Conn, error) {
ignores the context.

@CAFxX @knusbaum Taking the complexity of the test in mind and the fact that fixing dsnConnector.Connect isn't the goal of this PR I'd suggest to allow to merge this PR.

To run the integration test deploy postgres like docker run -it --rm --name dd-tests-postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -p 127.0.0.1:5432:5432/tcp postgres and set env variable INTEGRATION=true.

Copy link
Contributor

@CAFxX CAFxX Jan 14, 2021

Choose a reason for hiding this comment

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

What I had in mind was more along the lines of:

  • write hangingConnector, a driver.Connector that, when e.g. PingContext is called, hangs until the context is cancelled
  • construct a tracedConnector wrapping hangingConnector
  • call PingContext on the tracedConnector in a goroutine
  • wait until we know we are blocked in hangingConnector
  • cancel the context
  • wait until the PingContext call returns
  • check that the span was emitted

@gbbr gbbr modified the milestones: 1.29.0, 1.30.0 Feb 26, 2021
@gbbr gbbr modified the milestones: 1.30.0, Triage Apr 22, 2021
@knusbaum knusbaum added the stale Stuck for more than 1 month label May 26, 2021
@knusbaum
Copy link
Contributor

knusbaum commented Aug 9, 2021

We want this and the PR is close. It would be great if someone could finish this, else I'll get to it when I can.

@moonsub-kim
Copy link
Contributor

I wish the PR is merged! I'm waiting for 10 months (ㅠ_ㅠ)

@CAFxX
Copy link
Contributor

CAFxX commented Feb 1, 2022

FWIW, LGTM

@gbbr
Copy link
Contributor

gbbr commented Feb 1, 2022

Let's close in favour of #1154 which is actively being worked on.

@gbbr gbbr closed this Feb 1, 2022
@knusbaum knusbaum removed this from the Triage milestone Jan 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Stuck for more than 1 month
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants