-
Notifications
You must be signed in to change notification settings - Fork 243
feat: database/sql integration #893
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
base: master
Are you sure you want to change the base?
Changes from 16 commits
e544314
92e3a6b
aa4acb2
a6b5de8
c6ebbda
63413f3
b77e392
9b59328
7ecd868
19c97df
d9073aa
05d699b
0fc642e
7b5fbb5
93198bd
ab1d3bf
18d5f66
3e31bb5
fbd4dfc
1023392
2dc58a5
c5fa49d
a6e4fd4
1f38b9f
f173533
bcc99fc
38ba84c
31ef60f
2802546
0020dcc
7cd3b58
3178fa9
9b12d0a
b399672
6e6e5f8
b5458bc
e7ac573
ed45c51
f610dc9
1a78d47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,231 @@ | ||||||
| package sentrysql | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "database/sql/driver" | ||||||
|
|
||||||
| "github.com/getsentry/sentry-go" | ||||||
| ) | ||||||
|
|
||||||
| // sentryConn wraps the original driver.Conn. | ||||||
| // As per the driver's documentation: | ||||||
| // - All Conn implementations should implement the following interfaces: | ||||||
| // Pinger, SessionResetter, and Validator. | ||||||
| // - If named parameters or context are supported, the driver's Conn should | ||||||
| // implement: ExecerContext, QueryerContext, ConnPrepareContext, | ||||||
| // and ConnBeginTx. | ||||||
| // | ||||||
| // On this specific Sentry wrapper, we are not going to implement the Validator | ||||||
| // interface because it does not support ErrSkip, since returning ErrSkip | ||||||
| // is only possible when it's explicitly stated on the driver documentation. | ||||||
| type sentryConn struct { | ||||||
| originalConn driver.Conn | ||||||
| ctx context.Context | ||||||
| config *sentrySQLConfig | ||||||
| } | ||||||
|
|
||||||
| // Make sure that sentryConn implements the driver.Conn interface. | ||||||
| var _ driver.Conn = (*sentryConn)(nil) | ||||||
|
|
||||||
| func (s *sentryConn) Prepare(query string) (driver.Stmt, error) { | ||||||
| stmt, err := s.originalConn.Prepare(query) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| return &sentryStmt{ | ||||||
| originalStmt: stmt, | ||||||
| query: query, | ||||||
| ctx: s.ctx, | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Race condition on shared context fieldThe Additional Locations (2) |
||||||
| config: s.config, | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
| func (s *sentryConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { | ||||||
| // should only be executed if the original driver implements ConnPrepareContext | ||||||
| connPrepareContext, ok := s.originalConn.(driver.ConnPrepareContext) | ||||||
| if !ok { | ||||||
| // We can't return driver.ErrSkip here. We should fall back to Prepare without context. | ||||||
| return s.Prepare(query) | ||||||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| stmt, err := connPrepareContext.PrepareContext(ctx, query) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| return &sentryStmt{ | ||||||
| originalStmt: stmt, | ||||||
| query: query, | ||||||
| ctx: ctx, | ||||||
| config: s.config, | ||||||
| }, nil | ||||||
| } | ||||||
|
|
||||||
| func (s *sentryConn) Close() error { | ||||||
| return s.originalConn.Close() | ||||||
| } | ||||||
|
|
||||||
| func (s *sentryConn) Begin() (driver.Tx, error) { | ||||||
| tx, err := s.originalConn.Begin() //nolint:staticcheck // We must support legacy clients | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| return &sentryTx{originalTx: tx, ctx: s.ctx, config: s.config}, nil | ||||||
| } | ||||||
|
|
||||||
| func (s *sentryConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { | ||||||
| // should only be executed if the original driver implements ConnBeginTx | ||||||
| connBeginTx, ok := s.originalConn.(driver.ConnBeginTx) | ||||||
| if !ok { | ||||||
| // We can't return driver.ErrSkip here. We should fall back to Begin without context. | ||||||
| return s.Begin() | ||||||
| } | ||||||
giortzisg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| tx, err := connBeginTx.BeginTx(ctx, opts) | ||||||
| if err != nil { | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| return &sentryTx{originalTx: tx, ctx: s.ctx, config: s.config}, nil | ||||||
|
||||||
| } | ||||||
|
|
||||||
| //nolint:dupl | ||||||
| func (s *sentryConn) Query(query string, args []driver.Value) (driver.Rows, error) { | ||||||
| // should only be executed if the original driver implements Queryer | ||||||
| queryer, ok := s.originalConn.(driver.Queryer) //nolint:staticcheck // We must support legacy clients | ||||||
| if !ok { | ||||||
| return nil, driver.ErrSkip | ||||||
| } | ||||||
|
|
||||||
| parentSpan := sentry.SpanFromContext(s.ctx) | ||||||
| if parentSpan == nil { | ||||||
| return queryer.Query(query, args) | ||||||
| } | ||||||
|
|
||||||
| span := parentSpan.StartChild("db.sql.query", sentry.WithDescription(query)) | ||||||
| s.config.SetData(span, query) | ||||||
| defer span.Finish() | ||||||
|
|
||||||
| rows, err := queryer.Query(query, args) | ||||||
| if err != nil { | ||||||
| span.Status = sentry.SpanStatusInternalError | ||||||
| return nil, err | ||||||
| } | ||||||
|
|
||||||
| span.Status = sentry.SpanStatusOK | ||||||
| return rows, nil | ||||||
| } | ||||||
|
|
||||||
| //nolint:dupl | ||||||
| func (s *sentryConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { | ||||||
| // should only be executed if the original driver implements QueryerContext | ||||||
| queryerContext, ok := s.originalConn.(driver.QueryerContext) | ||||||
| if !ok { | ||||||
| return nil, driver.ErrSkip | ||||||
| } | ||||||
|
|
||||||
| parentSpan := sentry.SpanFromContext(ctx) | ||||||
| if parentSpan == nil { | ||||||
| return queryerContext.QueryContext(ctx, query, args) | ||||||
| } | ||||||
|
|
||||||
| span := parentSpan.StartChild("db.sql.query", sentry.WithDescription(query)) | ||||||
| s.config.SetData(span, query) | ||||||
| defer span.Finish() | ||||||
|
|
||||||
| rows, err := queryerContext.QueryContext(ctx, query, args) | ||||||
|
||||||
| rows, err := queryerContext.QueryContext(ctx, query, args) | |
| rows, err := queryerContext.QueryContext(span.Context(), query, args) |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| rows, err := execerContext.ExecContext(ctx, query, args) | |
| rows, err := execerContext.ExecContext(span.Context(), query, args) |
Uh oh!
There was an error while loading. Please reload this page.