Skip to content

Commit

Permalink
Record span error (#9)
Browse files Browse the repository at this point in the history
* Record span error

* Fix lint issue
  • Loading branch information
nhatthm authored Feb 11, 2022
1 parent 600ffc0 commit d80f3b0
Show file tree
Hide file tree
Showing 15 changed files with 1,077 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: golangci/[email protected]
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.43.0
version: v1.44.0

# Optional: working directory, useful for monorepos
working-directory: ${{ matrix.module }}
Expand Down
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ issues:
exclude-use-default: false
exclude-rules:
- linters:
- containedctx
- dupl
- funlen
- goconst
- goerr113
- gomnd
- maintidx
- noctx
- rowserrcheck
path: "_test.go"
2 changes: 1 addition & 1 deletion driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"go.opentelemetry.io/otel/trace"
)

const _maxDriver = 100
const _maxDriver = 150

const instrumentationName = "github.com/nhatthm/otelsql"

Expand Down
170 changes: 159 additions & 11 deletions driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestRegister_UnknownDriver(t *testing.T) {
func TestRegister_MaxSlots(t *testing.T) {
t.Parallel()

numSlots := 100
numSlots := 150

sql.Register("max-slots", struct {
driver.Driver
Expand Down Expand Up @@ -323,9 +323,14 @@ func Test_Ping_Error(t *testing.T) {
m.ExpectPing().
WillReturnError(pingErr)
}),
oteltest.TracesEqualJSON(expectedPingTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.TracePing(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.TracePing(),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -631,9 +636,14 @@ func Test_ExecContext_Error(t *testing.T) {
WithArgs("US").
WillReturnError(execErr)
}),
oteltest.TracesEqualJSON(expectedExecTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot(), otelsql.TraceLastInsertID())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
otelsql.TraceRowsAffected(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -992,9 +1002,14 @@ func Test_QueryContext_Error(t *testing.T) {
WithArgs("US").
WillReturnError(queryErr)
}),
oteltest.TracesEqualJSON(expectedQueryTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot(), otelsql.TraceRowsClose())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
otelsql.TraceRowsClose(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand All @@ -1016,9 +1031,13 @@ func Test_Begin_Error(t *testing.T) {
m.ExpectBegin().
WillReturnError(beginErr)
}),
oteltest.TracesEqualJSON(expectedBeginTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -1108,9 +1127,13 @@ func Test_Begin_Commit_Error(t *testing.T) {
m.ExpectCommit().
WillReturnError(commitErr)
}),
oteltest.TracesEqualJSON(expectedBeginCommitTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -1204,9 +1227,13 @@ func Test_Begin_Rollback_Error(t *testing.T) {
m.ExpectRollback().
WillReturnError(rollbackErr)
}),
oteltest.TracesEqualJSON(expectedBeginRollbackTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand All @@ -1222,6 +1249,34 @@ func Test_Begin_Rollback_Error(t *testing.T) {
})
}

func Test_BeginTx_Error(t *testing.T) {
t.Parallel()

const beginErr testError = "begin error"

oteltest.New(
oteltest.MockDatabase(func(m sqlmock.Sqlmock) {
m.ExpectBegin().
WillReturnError(beginErr)
}),
oteltest.TracesEqualJSON(expectedBeginTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck

tx, err := db.BeginTx(context.Background(), &sql.TxOptions{})

assert.Nil(t, tx)
assert.Equal(t, beginErr, err)
})
}

func Test_BeginTx_Commit(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1323,9 +1378,13 @@ func Test_BeginTx_Commit_Error(t *testing.T) {
m.ExpectCommit().
WillReturnError(commitErr)
}),
oteltest.TracesEqualJSON(expectedBeginCommitTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -1442,9 +1501,13 @@ func Test_BeginTx_Rollback_Error(t *testing.T) {
m.ExpectRollback().
WillReturnError(rollbackErr)
}),
oteltest.TracesEqualJSON(expectedBeginRollbackTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand All @@ -1470,9 +1533,13 @@ func Test_PrepareContext_Error(t *testing.T) {
m.ExpectPrepare(`DELETE FROM data WHERE country = $1`).
WillReturnError(prepareError)
}),
oteltest.TracesEqualJSON(expectedPrepareContextTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -1620,9 +1687,14 @@ func Test_PrepareContext_ExecContext_Error(t *testing.T) {
WithArgs("US").
WillReturnError(execErr)
}),
oteltest.TracesEqualJSON(expectedPrepareContextExecContextTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(), otelsql.AllowRoot(), otelsql.TraceLastInsertID())
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
otelsql.TraceRowsAffected(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck
Expand Down Expand Up @@ -1956,6 +2028,46 @@ func Test_PrepareContext_QueryContext(t *testing.T) {
}
}

func Test_PrepareContext_QueryContext_Error(t *testing.T) {
t.Parallel()

const queryErr testError = "query error"

oteltest.New(
oteltest.MockDatabase(func(m sqlmock.Sqlmock) {
stmt := m.ExpectPrepare(`SELECT * FROM data WHERE country = $1`).
WillBeClosed()

stmt.ExpectQuery().
WithArgs("US").
WillReturnError(queryErr)
}),
oteltest.TracesEqualJSON(expectedPrepareContextQueryContextTraceWithError(noParentSpanIDs())),
).
Run(t, func(sc oteltest.SuiteContext) {
db, err := newDB(sc.DatabaseDSN(),
otelsql.WithTracerProvider(sc.TracerProvider()),
otelsql.AllowRoot(),
otelsql.TraceRowsClose(),
)
require.NoError(t, err)

defer db.Close() // nolint: errcheck

stmt, err := db.PrepareContext(context.Background(), `SELECT * FROM data WHERE country = $1`)

require.NotNil(t, stmt)
require.NoError(t, err)

defer stmt.Close() // nolint: errcheck

result, err := stmt.QueryContext(context.Background(), "US")

assert.Nil(t, result)
assert.Equal(t, queryErr, err)
})
}

func Test_PrepareContext_QueryContext_TraceRows(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -2426,10 +2538,18 @@ func expectedPingTrace(parentTraceID trace.TraceID, parentSpanID trace.SpanID) s
return expectedTracesFromFile("ping.json", parentTraceID, parentSpanID)
}

func expectedPingTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("ping_with_error.json", parentTraceID, parentSpanID)
}

func expectedExecTraceNoQuery(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("exec_no_query.json", parentTraceID, parentSpanID)
}

func expectedExecTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("exec_with_error.json", parentTraceID, parentSpanID)
}

func expectedExecTraceWithQuery(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("exec_with_query.json", parentTraceID, parentSpanID)
}
Expand All @@ -2450,6 +2570,10 @@ func expectedQueryTraceNoQuery(parentTraceID trace.TraceID, parentSpanID trace.S
return expectedTracesFromFile("query_no_query.json", parentTraceID, parentSpanID)
}

func expectedQueryTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("query_with_error.json", parentTraceID, parentSpanID)
}

func expectedQueryTraceWithQuery(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("query_with_query.json", parentTraceID, parentSpanID)
}
Expand All @@ -2470,18 +2594,38 @@ func expectedQueryTraceWithRowsNextAndClose(parentTraceID trace.TraceID, parentS
return expectedTracesFromFile("query_with_rows_next_close.json", parentTraceID, parentSpanID)
}

func expectedBeginTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("begin_with_error.json", parentTraceID, parentSpanID)
}

func expectedBeginCommitTrace(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("begin_commit.json", parentTraceID, parentSpanID)
}

func expectedBeginCommitTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("begin_commit_with_error.json", parentTraceID, parentSpanID)
}

func expectedBeginRollbackTrace(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("begin_rollback.json", parentTraceID, parentSpanID)
}

func expectedBeginRollbackTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("begin_rollback_with_error.json", parentTraceID, parentSpanID)
}

func expectedPrepareContextTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("prepare_context_with_error.json", parentTraceID, parentSpanID)
}

func expectedPrepareContextExecContextTraceNoQuery(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("prepare_context_exec_context_no_query.json", parentTraceID, parentSpanID)
}

func expectedPrepareContextExecContextTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("prepare_context_exec_context_with_error.json", parentTraceID, parentSpanID)
}

func expectedPrepareContextExecContextTraceWithQuery(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("prepare_context_exec_context_with_query.json", parentTraceID, parentSpanID)
}
Expand All @@ -2502,6 +2646,10 @@ func expectedPrepareContextQueryContextTraceNoQuery(parentTraceID trace.TraceID,
return expectedTracesFromFile("prepare_context_query_context_no_query.json", parentTraceID, parentSpanID)
}

func expectedPrepareContextQueryContextTraceWithError(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("prepare_context_query_context_with_error.json", parentTraceID, parentSpanID)
}

func expectedPrepareContextQueryContextTraceWithQuery(parentTraceID trace.TraceID, parentSpanID trace.SpanID) string {
return expectedTracesFromFile("prepare_context_query_context_with_query.json", parentTraceID, parentSpanID)
}
Expand Down
Loading

0 comments on commit d80f3b0

Please sign in to comment.