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
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
go-version:
- "1.22"
- "1.23"
- "1.24"
Expand All @@ -24,7 +24,7 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: Install dependencies
run: go mod tidy -diff

Expand Down
22 changes: 22 additions & 0 deletions operation_name_go124.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build go1.24
// +build go1.24

package otelpgx

import (
"strings"
)

// sqlOperationName attempts to get the first 'word' from a given SQL query, which usually
// is the operation name (e.g. 'SELECT').
func (t *Tracer) sqlOperationName(stmt string) string {
if t.spanNameFunc != nil {
return t.spanNameFunc(stmt)
}

for word := range strings.FieldsSeq(stmt) {
return strings.ToUpper(word)
}

return sqlOperationUnknown
}
33 changes: 33 additions & 0 deletions operation_name_legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build !go1.24
// +build !go1.24

package otelpgx

import (
"strings"
"unicode"
)

// sqlOperationName attempts to get the first 'word' from a given SQL query, which usually
// is the operation name (e.g. 'SELECT').
func (t *Tracer) sqlOperationName(stmt string) string {
// If a custom function is provided, use that. Otherwise, fall back to the
// default implementation. This allows users to override the default
// behavior without having to reimplement it.
if t.spanNameFunc != nil {
return t.spanNameFunc(stmt)
}

stmt = strings.TrimSpace(stmt)
end := strings.IndexFunc(stmt, unicode.IsSpace)
if end < 0 && len(stmt) > 0 {
// No space found, use the whole statement.
end = len(stmt)
} else if end < 0 {
// Fall back to a fixed value to prevent creating lots of tracing operations
// differing only by the amount of whitespace in them (in case we'd fall back
// to the full query or a cut-off version).
return sqlOperationUnknown
}
return strings.ToUpper(stmt[:end])
}
27 changes: 0 additions & 27 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"errors"
"fmt"
"runtime/debug"
"strings"
"time"
"unicode"

"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
Expand Down Expand Up @@ -186,31 +184,6 @@ func (t *Tracer) recordOperationDuration(ctx context.Context, pgxOperation strin
}
}

// sqlOperationName attempts to get the first 'word' from a given SQL query, which usually
// is the operation name (e.g. 'SELECT').
func (t *Tracer) sqlOperationName(stmt string) string {
// If a custom function is provided, use that. Otherwise, fall back to the
// default implementation. This allows users to override the default
// behavior without having to reimplement it.
if t.spanNameFunc != nil {
return t.spanNameFunc(stmt)
}

// NOTE: Can convert to FieldsSeq in go 1.24+ which avoids allocations.
stmt = strings.TrimSpace(stmt)
end := strings.IndexFunc(stmt, unicode.IsSpace)
if end < 0 && len(stmt) > 0 {
// No space found, use the whole statement.
end = len(stmt)
} else if end < 0 {
// Fall back to a fixed value to prevent creating lots of tracing operations
// differing only by the amount of whitespace in them (in case we'd fall back
// to the full query or a cut-off version).
return sqlOperationUnknown
}
return strings.ToUpper(stmt[:end])
}

// connectionAttributesFromConfig returns a SpanStartOption that contains
// attributes from the given connection config.
func connectionAttributesFromConfig(config *pgx.ConnConfig) trace.SpanStartOption {
Expand Down