-
Notifications
You must be signed in to change notification settings - Fork 242
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 5 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
b7c348f
d864e1b
1618821
3695056
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,247 @@ | ||||||
| package sentrysql | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "database/sql/driver" | ||||||
|
|
||||||
| "github.com/getsentry/sentry-go" | ||||||
| ) | ||||||
|
|
||||||
| type sentryConn struct { | ||||||
| originalConn driver.Conn | ||||||
| ctx context.Context | ||||||
| config *sentrySQLConfig | ||||||
| } | ||||||
|
|
||||||
| 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 { | ||||||
| return nil, driver.ErrSkip | ||||||
| } | ||||||
|
|
||||||
| 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 { | ||||||
| // fallback to the so-called deprecated "Begin" method | ||||||
| return s.Begin() | ||||||
| } | ||||||
|
|
||||||
| 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)) | ||||||
| if s.config.databaseSystem != "" { | ||||||
ribice marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| span.SetData("db.system", s.config.databaseSystem) | ||||||
| } | ||||||
| if s.config.databaseName != "" { | ||||||
| span.SetData("db.name", s.config.databaseName) | ||||||
| } | ||||||
| if s.config.serverAddress != "" { | ||||||
| span.SetData("server.address", s.config.serverAddress) | ||||||
| } | ||||||
| if s.config.serverPort != "" { | ||||||
| span.SetData("server.port", s.config.serverPort) | ||||||
| } | ||||||
| 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)) | ||||||
| if s.config.databaseSystem != "" { | ||||||
| span.SetData("db.system", s.config.databaseSystem) | ||||||
| } | ||||||
| if s.config.databaseName != "" { | ||||||
| span.SetData("db.name", s.config.databaseName) | ||||||
| } | ||||||
| if s.config.serverAddress != "" { | ||||||
| span.SetData("server.address", s.config.serverAddress) | ||||||
| } | ||||||
| if s.config.serverPort != "" { | ||||||
| span.SetData("server.port", s.config.serverPort) | ||||||
| } | ||||||
| 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package sentrysql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql/driver" | ||
| ) | ||
|
|
||
| type sentrySQLDriver struct { | ||
| originalDriver driver.Driver | ||
| config *sentrySQLConfig | ||
| } | ||
|
|
||
| func (s *sentrySQLDriver) OpenConnector(name string) (driver.Connector, error) { | ||
| driverContext, ok := s.originalDriver.(driver.DriverContext) | ||
| if !ok { | ||
| return nil, driver.ErrSkip | ||
| } | ||
|
|
||
| connector, err := driverContext.OpenConnector(name) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &sentrySQLConnector{originalConnector: connector, config: s.config}, nil | ||
| } | ||
|
|
||
| func (s *sentrySQLDriver) Open(name string) (driver.Conn, error) { | ||
| conn, err := s.originalDriver.Open(name) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &sentryConn{originalConn: conn, config: s.config}, nil | ||
| } | ||
|
|
||
| type sentrySQLConnector struct { | ||
| originalConnector driver.Connector | ||
| config *sentrySQLConfig | ||
| } | ||
|
|
||
| func (s *sentrySQLConnector) Connect(ctx context.Context) (driver.Conn, error) { | ||
| conn, err := s.originalConnector.Connect(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &sentryConn{originalConn: conn, ctx: ctx, config: s.config}, nil | ||
| } | ||
|
|
||
| func (s *sentrySQLConnector) Driver() driver.Driver { | ||
| return s.originalConnector.Driver() | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.