Skip to content

Commit

Permalink
En/add tracing for sql (#462)
Browse files Browse the repository at this point in the history
* add tracing in db.go using otelsql library

* sort imports

* change log message in case of error
  • Loading branch information
Umang01-hash authored and srijan-27 committed Apr 19, 2024
1 parent d0ad4df commit 38e306e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 8 additions & 1 deletion pkg/gofr/datasource/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pkg/gofr/datasource/sql/sql_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sql

import (
"fmt"
"strings"
"testing"

Expand All @@ -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",
Expand Down

0 comments on commit 38e306e

Please sign in to comment.