Skip to content

Commit

Permalink
updated sql instrumentation unit test with opt in changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Angith committed Mar 25, 2024
1 parent 1f075b9 commit 0fba787
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 55 deletions.
44 changes: 32 additions & 12 deletions instrumentation_sql_go1.10_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
)

func TestWrapSQLConnector_Exec(t *testing.T) {
beforeTest()
defer afterTest()

recorder := instana.NewTestRecorder()
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{
Service: "go-sensor-test",
Expand All @@ -29,7 +28,13 @@ func TestWrapSQLConnector_Exec(t *testing.T) {

db := sql.OpenDB(instana.WrapSQLConnector(s, "connection string", sqlConnector{}))

res, err := db.Exec("TEST QUERY")
pSpan := s.Tracer().StartSpan("parent-span")
ctx := context.Background()
if pSpan != nil {
ctx = instana.ContextWithSpan(ctx, pSpan)
}

res, err := db.ExecContext(ctx, "TEST QUERY")
require.NoError(t, err)

lastID, err := res.LastInsertId()
Expand Down Expand Up @@ -62,20 +67,25 @@ func TestWrapSQLConnector_Exec(t *testing.T) {
}

func TestWrapSQLConnector_Exec_Error(t *testing.T) {
beforeTest()
defer afterTest()

recorder := instana.NewTestRecorder()
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{
Service: "go-sensor-test",
AgentClient: alwaysReadyClient{},
}, recorder))
defer instana.ShutdownSensor()

pSpan := s.Tracer().StartSpan("parent-span")
ctx := context.Background()
if pSpan != nil {
ctx = instana.ContextWithSpan(ctx, pSpan)
}

db := sql.OpenDB(instana.WrapSQLConnector(s, "connection string", sqlConnector{
Error: errors.New("something went wrong"),
}))

_, err := db.Exec("TEST QUERY")
_, err := db.ExecContext(ctx, "TEST QUERY")
assert.Error(t, err)

spans := recorder.GetQueuedSpans()
Expand Down Expand Up @@ -105,18 +115,23 @@ func TestWrapSQLConnector_Exec_Error(t *testing.T) {
}

func TestWrapSQLConnector_Query(t *testing.T) {
beforeTest()
defer afterTest()

recorder := instana.NewTestRecorder()
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{
Service: "go-sensor-test",
AgentClient: alwaysReadyClient{},
}, recorder))
defer instana.ShutdownSensor()

pSpan := s.Tracer().StartSpan("parent-span")
ctx := context.Background()
if pSpan != nil {
ctx = instana.ContextWithSpan(ctx, pSpan)
}

db := sql.OpenDB(instana.WrapSQLConnector(s, "connection string", sqlConnector{}))

res, err := db.Query("TEST QUERY")
res, err := db.QueryContext(ctx, "TEST QUERY")
require.NoError(t, err)

cols, err := res.Columns()
Expand Down Expand Up @@ -149,8 +164,7 @@ func TestWrapSQLConnector_Query(t *testing.T) {
}

func TestWrapSQLConnector_Query_Error(t *testing.T) {
beforeTest()
defer afterTest()

recorder := instana.NewTestRecorder()
s := instana.NewSensorWithTracer(instana.NewTracerWithEverything(&instana.Options{
Service: "go-sensor-test",
Expand All @@ -163,7 +177,13 @@ func TestWrapSQLConnector_Query_Error(t *testing.T) {
Error: dbErr,
}))

_, err := db.Query("TEST QUERY")
pSpan := s.Tracer().StartSpan("parent-span")
ctx := context.Background()
if pSpan != nil {
ctx = instana.ContextWithSpan(ctx, pSpan)
}

_, err := db.QueryContext(ctx, "TEST QUERY")
assert.Error(t, err)

spans := recorder.GetQueuedSpans()
Expand Down
Loading

0 comments on commit 0fba787

Please sign in to comment.