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

feat: database/sql integration #893

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e544314
feat: initial sentrysql implementation
aldy505 Oct 18, 2024
92e3a6b
chore: resolve a few lint issues
aldy505 Oct 18, 2024
aa4acb2
chore: another attempt at resolving lint issues
aldy505 Oct 18, 2024
a6b5de8
chore: wrong method name
aldy505 Oct 18, 2024
c6ebbda
chore: another attempt of fixing lint issues
aldy505 Oct 18, 2024
63413f3
feat: implement missing bits from driver interfaces
aldy505 Oct 19, 2024
b77e392
chore: missing a period on comment
aldy505 Oct 19, 2024
9b59328
test: replace ramsql with in-memory sqlite
aldy505 Oct 19, 2024
7ecd868
test: common queries
aldy505 Oct 19, 2024
19c97df
chore: avoid cyclo errors
aldy505 Oct 19, 2024
d9073aa
test: no parent span
aldy505 Oct 19, 2024
05d699b
test: db.Driver
aldy505 Oct 19, 2024
0fc642e
chore: trailing newline
aldy505 Oct 19, 2024
7b5fbb5
test: using mysql server for connector
aldy505 Oct 19, 2024
93198bd
test: remove mysql server, stay on go1.18
aldy505 Oct 19, 2024
ab1d3bf
chore: another go1.18 rollback
aldy505 Oct 19, 2024
18d5f66
test: NewSentrySQLConnector
aldy505 Oct 26, 2024
3e31bb5
chore(example): sql integration example
aldy505 Oct 26, 2024
fbd4dfc
chore: don't lint fakedb
aldy505 Oct 26, 2024
1023392
test: backport to go1.18
aldy505 Oct 26, 2024
2dc58a5
test: backport to go1.18
aldy505 Oct 26, 2024
c5fa49d
chore: lint
aldy505 Oct 26, 2024
a6e4fd4
Merge remote-tracking branch 'origin/master' into feat/sentrysql
aldy505 Nov 2, 2024
1f38b9f
test(sentrysql): test against legacy driver
aldy505 Nov 2, 2024
f173533
chore(sentrysql): remove unvisited context check
aldy505 Nov 2, 2024
bcc99fc
chore: lint
aldy505 Nov 2, 2024
38ba84c
chore(sentrysql): make sure we implement required interfaces
aldy505 Nov 6, 2024
31ef60f
Merge branch 'master' into feat/sentrysql
aldy505 Nov 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ require (
github.com/google/go-cmp v0.5.9
github.com/kataras/iris/v12 v12.2.0
github.com/labstack/echo/v4 v4.10.0
github.com/lib/pq v1.10.9
github.com/pingcap/errors v0.11.4
github.com/pkg/errors v0.9.1
github.com/proullon/ramsql v0.1.4
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.2
github.com/urfave/negroni/v3 v3.1.1
Expand Down Expand Up @@ -88,6 +90,7 @@ require (
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-gorp/gorp v2.2.0+incompatible h1:xAUh4QgEeqPPhK3vxZN+bzrim1z5Av6q837gtjUlshc=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
Expand Down Expand Up @@ -71,6 +72,11 @@ github.com/iris-contrib/httpexpect/v2 v2.12.1 h1:3cTZSyBBen/kfjCtgNFoUKi1u0FVXNa
github.com/iris-contrib/httpexpect/v2 v2.12.1/go.mod h1:7+RB6W5oNClX7PTwJgJnsQP3ZuUUYB3u61KCqeSgZ88=
github.com/iris-contrib/schema v0.0.6 h1:CPSBLyx2e91H2yJzPuhGuifVRnZBBJ3pCOMbOvPZaTw=
github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down Expand Up @@ -103,6 +109,8 @@ github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailgun/raymond/v2 v2.0.48 h1:5dmlB680ZkFG2RN/0lvTAghrSxIESeu9/2aeDqACtjw=
github.com/mailgun/raymond/v2 v2.0.48/go.mod h1:lsgvL50kgt1ylcFJYZiULi5fjPBkkhNfj4KA0W54Z18=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down Expand Up @@ -142,6 +150,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/proullon/ramsql v0.1.4 h1:yTFRTn46gFH/kPbzCx+mGjuFlyTBUeDr3h2ldwxddl0=
github.com/proullon/ramsql v0.1.4/go.mod h1:CFGqeQHQpdRfWqYmWD3yXqPTEaHkF4zgXy1C6qDWc9E=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down Expand Up @@ -220,6 +230,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
Expand Down Expand Up @@ -289,5 +301,7 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0=
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho=
moul.io/http2curl/v2 v2.3.0 h1:9r3JfDzWPcbIklMOs2TnIFzDYvfAZvjeavG6EzP7jYs=
moul.io/http2curl/v2 v2.3.0/go.mod h1:RW4hyBjTWSYDOxapodpNEtX0g5Eb16sxklBqmd2RHcE=
247 changes: 247 additions & 0 deletions sentrysql/conn.go
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
}

Check warning on line 20 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L16-L20

Added lines #L16 - L20 were not covered by tests

return &sentryStmt{
originalStmt: stmt,
query: query,
ctx: s.ctx,
config: s.config,
}, nil

Check warning on line 27 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L22-L27

Added lines #L22 - L27 were not covered by tests
}

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
}

Check warning on line 35 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L30-L35

Added lines #L30 - L35 were not covered by tests

stmt, err := connPrepareContext.PrepareContext(ctx, query)
if err != nil {
return nil, err
}

Check warning on line 40 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L37-L40

Added lines #L37 - L40 were not covered by tests

return &sentryStmt{
originalStmt: stmt,
query: query,
ctx: ctx,
config: s.config,
}, nil

Check warning on line 47 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L42-L47

Added lines #L42 - L47 were not covered by tests
}

func (s *sentryConn) Close() error {
return s.originalConn.Close()

Check warning on line 51 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

func (s *sentryConn) Begin() (driver.Tx, error) {
tx, err := s.originalConn.Begin() //nolint:staticcheck We must support legacy clients

Check failure on line 55 in sentrysql/conn.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: s.originalConn.Begin has been deprecated since Go 1.8: Drivers should implement ConnBeginTx instead (or additionally). (staticcheck)
if err != nil {
return nil, err
}

Check warning on line 58 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L54-L58

Added lines #L54 - L58 were not covered by tests

return &sentryTx{originalTx: tx, ctx: s.ctx, config: s.config}, nil

Check warning on line 60 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L60

Added line #L60 was not covered by tests
}

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()
}

Check warning on line 69 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L63-L69

Added lines #L63 - L69 were not covered by tests

tx, err := connBeginTx.BeginTx(ctx, opts)
if err != nil {
return nil, err
}

Check warning on line 74 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L71-L74

Added lines #L71 - L74 were not covered by tests

return &sentryTx{originalTx: tx, ctx: s.ctx, config: s.config}, nil

Check warning on line 76 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L76

Added line #L76 was not covered by tests
}

//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

Check failure on line 82 in sentrysql/conn.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: driver.Queryer has been deprecated since Go 1.8: Drivers should implement [QueryerContext] instead. (staticcheck)
if !ok {
return nil, driver.ErrSkip
}

Check warning on line 85 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L80-L85

Added lines #L80 - L85 were not covered by tests

parentSpan := sentry.SpanFromContext(s.ctx)
if parentSpan == nil {
return queryer.Query(query, args)
}

Check warning on line 90 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L87-L90

Added lines #L87 - L90 were not covered by tests

span := parentSpan.StartChild("db.sql.query", sentry.WithDescription(query))
if s.config.databaseSystem != "" {
ribice marked this conversation as resolved.
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
}

Check warning on line 111 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L92-L111

Added lines #L92 - L111 were not covered by tests

span.Status = sentry.SpanStatusOK
return rows, nil

Check warning on line 114 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L113-L114

Added lines #L113 - L114 were not covered by tests
}

//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
}

Check warning on line 123 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L118-L123

Added lines #L118 - L123 were not covered by tests

parentSpan := sentry.SpanFromContext(ctx)
if parentSpan == nil {
return queryerContext.QueryContext(ctx, query, args)
}

Check warning on line 128 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L125-L128

Added lines #L125 - L128 were not covered by tests

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)
if err != nil {
span.Status = sentry.SpanStatusInternalError
return nil, err
}

Check warning on line 149 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L130-L149

Added lines #L130 - L149 were not covered by tests

span.Status = sentry.SpanStatusOK
return rows, nil

Check warning on line 152 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L151-L152

Added lines #L151 - L152 were not covered by tests
}

//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

Check failure on line 158 in sentrysql/conn.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: driver.Execer has been deprecated since Go 1.8: Drivers should implement [ExecerContext] instead. (staticcheck)
if !ok {
return nil, driver.ErrSkip
}

Check warning on line 161 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L156-L161

Added lines #L156 - L161 were not covered by tests

parentSpan := sentry.SpanFromContext(s.ctx)
if parentSpan == nil {
return execer.Exec(query, args)
}

Check warning on line 166 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L163-L166

Added lines #L163 - L166 were not covered by tests

span := parentSpan.StartChild("db.sql.exec", 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 := execer.Exec(query, args)
if err != nil {
span.Status = sentry.SpanStatusInternalError
return nil, err
}

Check warning on line 187 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L168-L187

Added lines #L168 - L187 were not covered by tests

span.Status = sentry.SpanStatusOK
return rows, nil

Check warning on line 190 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L189-L190

Added lines #L189 - L190 were not covered by tests
}

//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 {
return nil, driver.ErrSkip
}

Check warning on line 199 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L194-L199

Added lines #L194 - L199 were not covered by tests

parentSpan := sentry.SpanFromContext(ctx)
if parentSpan == nil {
return execerContext.ExecContext(ctx, query, args)
}

Check warning on line 204 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L201-L204

Added lines #L201 - L204 were not covered by tests

span := parentSpan.StartChild("db.sql.exec", 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 := execerContext.ExecContext(ctx, query, args)
if err != nil {
span.Status = sentry.SpanStatusInternalError
return nil, err
}

Check warning on line 225 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L206-L225

Added lines #L206 - L225 were not covered by tests

span.Status = sentry.SpanStatusOK
return rows, nil

Check warning on line 228 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L227-L228

Added lines #L227 - L228 were not covered by tests
}

func (s *sentryConn) Ping(ctx context.Context) error {
pinger, ok := s.originalConn.(driver.Pinger)
if !ok {
return driver.ErrSkip
}

Check warning on line 235 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L231-L235

Added lines #L231 - L235 were not covered by tests

return pinger.Ping(ctx)

Check warning on line 237 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L237

Added line #L237 was not covered by tests
}

func (s *sentryConn) CheckNamedValue(namedValue *driver.NamedValue) error {
namedValueChecker, ok := s.originalConn.(driver.NamedValueChecker)
if !ok {
return driver.ErrSkip
}

Check warning on line 244 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L240-L244

Added lines #L240 - L244 were not covered by tests

return namedValueChecker.CheckNamedValue(namedValue)

Check warning on line 246 in sentrysql/conn.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/conn.go#L246

Added line #L246 was not covered by tests
}
52 changes: 52 additions & 0 deletions sentrysql/driver.go
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
}

Check warning on line 17 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L13-L17

Added lines #L13 - L17 were not covered by tests

connector, err := driverContext.OpenConnector(name)
if err != nil {
return nil, err
}

Check warning on line 22 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L19-L22

Added lines #L19 - L22 were not covered by tests

return &sentrySQLConnector{originalConnector: connector, config: s.config}, nil

Check warning on line 24 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L24

Added line #L24 was not covered by tests
}

func (s *sentrySQLDriver) Open(name string) (driver.Conn, error) {
conn, err := s.originalDriver.Open(name)
if err != nil {
return nil, err
}

Check warning on line 31 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L27-L31

Added lines #L27 - L31 were not covered by tests

return &sentryConn{originalConn: conn, config: s.config}, nil

Check warning on line 33 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L33

Added line #L33 was not covered by tests
}

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
}

Check warning on line 45 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L41-L45

Added lines #L41 - L45 were not covered by tests

return &sentryConn{originalConn: conn, ctx: ctx, config: s.config}, nil

Check warning on line 47 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L47

Added line #L47 was not covered by tests
}

func (s *sentrySQLConnector) Driver() driver.Driver {
return s.originalConnector.Driver()

Check warning on line 51 in sentrysql/driver.go

View check run for this annotation

Codecov / codecov/patch

sentrysql/driver.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
Loading
Loading