diff --git a/go.mod b/go.mod index 81a96a2dd..218695097 100644 --- a/go.mod +++ b/go.mod @@ -45,6 +45,7 @@ require ( cloud.google.com/go/compute/metadata v0.2.3 // indirect cloud.google.com/go/iam v1.1.6 // indirect filippo.io/edwards25519 v1.1.0 // indirect + github.com/XSAM/otelsql v0.29.0 // indirect github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cenkalti/backoff/v4 v4.3.0 // indirect diff --git a/go.sum b/go.sum index 0cb01c401..b1326e055 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU= github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU= +github.com/XSAM/otelsql v0.29.0 h1:pEw9YXXs8ZrGRYfDc0cmArIz9lci5b42gmP5+tA1Huc= +github.com/XSAM/otelsql v0.29.0/go.mod h1:d3/0xGIGC5RVEE+Ld7KotwaLy6zDeaF3fLJHOPpdN2w= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a h1:HbKu58rmZpUGpz5+4FfNmIU+FmZg2P3Xaj2v2bfNWmk= github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a/go.mod h1:SGnFV6hVsYE877CKEZ6tDNTjaSXYUk6QqoIK6PrAtcc= github.com/alicebob/miniredis/v2 v2.32.1 h1:Bz7CciDnYSaa0mX5xODh6GUITRSx+cVhjNoOR4JssBo= diff --git a/pkg/gofr/datasource/sql/sql.go b/pkg/gofr/datasource/sql/sql.go index 7598a1af9..1f55b22b5 100644 --- a/pkg/gofr/datasource/sql/sql.go +++ b/pkg/gofr/datasource/sql/sql.go @@ -8,6 +8,7 @@ import ( "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/XSAM/otelsql" "go.uber.org/mock/gomock" "gofr.dev/pkg/gofr/testutil" @@ -45,7 +46,13 @@ func NewSQL(configs config.Config, logger datasource.Logger, metrics Metrics) *D return nil } - db, err := sql.Open(dbConfig.Dialect, dbConnectionString) + otelRegisteredDialect, err := otelsql.Register(dbConfig.Dialect) + if err != nil { + logger.Errorf("could not register sql dialect '%s' for traces due to error: '%s'", dbConfig.Dialect, err) + return nil + } + + db, err := sql.Open(otelRegisteredDialect, dbConnectionString) if err != nil { logger.Errorf("could not connect with '%s' user to database '%s:%s' error: %v", dbConfig.User, dbConfig.HostName, dbConfig.Port, err) diff --git a/pkg/gofr/datasource/sql/sql_test.go b/pkg/gofr/datasource/sql/sql_test.go index b90151e71..50c5fae6b 100644 --- a/pkg/gofr/datasource/sql/sql_test.go +++ b/pkg/gofr/datasource/sql/sql_test.go @@ -1,6 +1,7 @@ package sql import ( + "fmt" "strings" "testing" @@ -14,7 +15,8 @@ import ( func TestNewSQL_ErrorCase(t *testing.T) { ctrl := gomock.NewController(t) - expectedLog := "could not connect with 'testuser' user to database 'localhost:3306' error" + expectedLog := fmt.Sprintf("could not register sql dialect '%s' for traces due to error: '%s'", "mysql", + "sql: unknown driver \"mysql\" (forgotten import?)") mockConfig := testutil.NewMockConfig(map[string]string{ "DB_DIALECT": "mysql",