-
Notifications
You must be signed in to change notification settings - Fork 229
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
Open
aldy505
wants to merge
33
commits into
getsentry:master
Choose a base branch
from
aldy505:feat/sentrysql
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
e544314
feat: initial sentrysql implementation
aldy505 92e3a6b
chore: resolve a few lint issues
aldy505 aa4acb2
chore: another attempt at resolving lint issues
aldy505 a6b5de8
chore: wrong method name
aldy505 c6ebbda
chore: another attempt of fixing lint issues
aldy505 63413f3
feat: implement missing bits from driver interfaces
aldy505 b77e392
chore: missing a period on comment
aldy505 9b59328
test: replace ramsql with in-memory sqlite
aldy505 7ecd868
test: common queries
aldy505 19c97df
chore: avoid cyclo errors
aldy505 d9073aa
test: no parent span
aldy505 05d699b
test: db.Driver
aldy505 0fc642e
chore: trailing newline
aldy505 7b5fbb5
test: using mysql server for connector
aldy505 93198bd
test: remove mysql server, stay on go1.18
aldy505 ab1d3bf
chore: another go1.18 rollback
aldy505 18d5f66
test: NewSentrySQLConnector
aldy505 3e31bb5
chore(example): sql integration example
aldy505 fbd4dfc
chore: don't lint fakedb
aldy505 1023392
test: backport to go1.18
aldy505 2dc58a5
test: backport to go1.18
aldy505 c5fa49d
chore: lint
aldy505 a6e4fd4
Merge remote-tracking branch 'origin/master' into feat/sentrysql
aldy505 1f38b9f
test(sentrysql): test against legacy driver
aldy505 f173533
chore(sentrysql): remove unvisited context check
aldy505 bcc99fc
chore: lint
aldy505 38ba84c
chore(sentrysql): make sure we implement required interfaces
aldy505 31ef60f
Merge branch 'master' into feat/sentrysql
aldy505 2802546
Merge remote-tracking branch 'origin/master' into feat/sentrysql
aldy505 0020dcc
ref(sentrysql): don't return ErrSkip for conn.QueryContext and conn.E…
aldy505 7cd3b58
ref: move sentrysql to dedicated module
aldy505 3178fa9
ref(sentrysql): set context before accesing non-context methods
aldy505 9b12d0a
test(sentrysql): check for received and want spans before observing t…
aldy505 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
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) | ||
if err != nil { | ||
span.Status = sentry.SpanStatusInternalError | ||
return nil, err | ||
} | ||
|
||
span.Status = sentry.SpanStatusOK | ||
return rows, nil | ||
} | ||
|
||
//nolint:dupl | ||
func (s *sentryConn) Exec(query string, args []driver.Value) (driver.Result, error) { | ||
// should only be executed if the original driver implements Execer | ||
execer, ok := s.originalConn.(driver.Execer) //nolint:staticcheck // We must support legacy clients | ||
if !ok { | ||
return nil, driver.ErrSkip | ||
} | ||
|
||
parentSpan := sentry.SpanFromContext(s.ctx) | ||
if parentSpan == nil { | ||
return execer.Exec(query, args) | ||
} | ||
|
||
span := parentSpan.StartChild("db.sql.exec", sentry.WithDescription(query)) | ||
s.config.SetData(span, query) | ||
defer span.Finish() | ||
|
||
rows, err := execer.Exec(query, args) | ||
if err != nil { | ||
span.Status = sentry.SpanStatusInternalError | ||
return nil, err | ||
} | ||
|
||
span.Status = sentry.SpanStatusOK | ||
return rows, nil | ||
} | ||
|
||
//nolint:dupl | ||
func (s *sentryConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { | ||
// should only be executed if the original driver implements ExecerContext { | ||
execerContext, ok := s.originalConn.(driver.ExecerContext) | ||
if !ok { | ||
// ExecContext may return ErrSkip. | ||
return nil, driver.ErrSkip | ||
} | ||
|
||
parentSpan := sentry.SpanFromContext(ctx) | ||
if parentSpan == nil { | ||
return execerContext.ExecContext(ctx, query, args) | ||
} | ||
|
||
span := parentSpan.StartChild("db.sql.exec", sentry.WithDescription(query)) | ||
s.config.SetData(span, query) | ||
defer span.Finish() | ||
|
||
rows, err := execerContext.ExecContext(ctx, query, args) | ||
if err != nil { | ||
span.Status = sentry.SpanStatusInternalError | ||
return nil, err | ||
} | ||
|
||
span.Status = sentry.SpanStatusOK | ||
return rows, nil | ||
} | ||
|
||
func (s *sentryConn) Ping(ctx context.Context) error { | ||
pinger, ok := s.originalConn.(driver.Pinger) | ||
if !ok { | ||
// We may not return ErrSkip. We should return nil. | ||
return nil | ||
} | ||
|
||
return pinger.Ping(ctx) | ||
} | ||
|
||
func (s *sentryConn) ResetSession(ctx context.Context) error { | ||
sessionResetter, ok := s.originalConn.(driver.SessionResetter) | ||
if !ok { | ||
// We may not return ErrSkip. We should return nil. | ||
return nil | ||
} | ||
|
||
return sessionResetter.ResetSession(ctx) | ||
} | ||
|
||
func (s *sentryConn) CheckNamedValue(namedValue *driver.NamedValue) error { | ||
namedValueChecker, ok := s.originalConn.(driver.NamedValueChecker) | ||
if !ok { | ||
// We may return ErrSkip. | ||
return driver.ErrSkip | ||
} | ||
|
||
return namedValueChecker.CheckNamedValue(namedValue) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.