Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The next release will require at least [Go 1.26].
- Support testing of [Go 1.27]. (#650)
- Add `WithSpanErrorAttributesGetter` option to set additional attributes (e.g., `db.response.status_code`) on spans when an operation returns an error. (#651)
- Add `SpanOptions.RowsChildOfQuery` to create `sql.rows` spans as children of the `sql.conn.query` or `sql.stmt.query` span that produced them, so concurrent queries can be correlated with their result iteration. (#652)
- Support `driver.RowsColumnScanner` on [Go 1.27]. (#649)

### Changed

Expand Down
20 changes: 14 additions & 6 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func rowsContext(ctx, queryCtx context.Context, cfg config) context.Context {
return ctx
}

func newRows(ctx context.Context, rows driver.Rows, cfg config) *otRows {
func newRows(ctx context.Context, rows driver.Rows, cfg config) driver.Rows {
var span trace.Span

method := MethodRows
Expand All @@ -61,12 +61,12 @@ func newRows(ctx context.Context, rows driver.Rows, cfg config) *otRows {
_, span = createSpan(ctx, cfg, method, false, "", nil)
}

return &otRows{
return wrapRowsColumnScanner(&otRows{
Rows: rows,
span: span,
cfg: cfg,
onClose: onClose,
}
})
}

// HasNextResultSet calls the implements the driver.RowsNextResultSet for otRows.
Expand Down Expand Up @@ -153,15 +153,23 @@ func (r otRows) Close() (err error) {
}

func (r otRows) Next(dest []driver.Value) (err error) {
r.beforeNext()

err = r.Rows.Next(dest)
r.afterNext(err)

return
}

func (r otRows) beforeNext() {
if r.cfg.SpanOptions.RowsNext && r.span != nil {
r.span.AddEvent(string(EventRowsNext))
}
}

err = r.Rows.Next(dest)
func (r otRows) afterNext(err error) {
// io.EOF is not an error. It is expected to happen during iteration.
if err != nil && !errors.Is(err, io.EOF) {
recordSpanError(r.span, r.cfg, err)
}

return
}
57 changes: 57 additions & 0 deletions rows_go1.27.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright Sam Xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build go1.27

package otelsql

import "database/sql/driver"

var _ driver.RowsColumnScanner = (*otRowsColumnScanner)(nil)

type otRowsColumnScanner struct {
*otRows

scanner driver.RowsColumnScanner
}

func wrapRowsColumnScanner(rows *otRows) driver.Rows {
scanner, ok := rows.Rows.(driver.RowsColumnScanner)
if !ok {
return rows
}

return &otRowsColumnScanner{
otRows: rows,
scanner: scanner,
}
}

func (r otRowsColumnScanner) NextRow() (err error) {
r.beforeNext()

err = r.scanner.NextRow()
r.afterNext(err)

return
}

func (r otRowsColumnScanner) ScanColumn(scanCtx driver.ScanContext, index int, dest any) (err error) {
err = r.scanner.ScanColumn(scanCtx, index, dest)
if err != nil {
recordSpanError(r.span, r.cfg, err)
}

return
}
Loading
Loading