From b8422064fb3f45b683e35dc464223b75a0384809 Mon Sep 17 00:00:00 2001 From: Eric <842626+skarm@users.noreply.github.com> Date: Tue, 27 May 2025 14:13:07 +0500 Subject: [PATCH 1/4] change meter names type - var to const (#56) Co-authored-by: Skarm --- meter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meter.go b/meter.go index 3e6938b..b4affde 100644 --- a/meter.go +++ b/meter.go @@ -18,7 +18,7 @@ const ( defaultMinimumReadDBStatsInterval = time.Second ) -var ( +const ( pgxPoolAcquireCount = "pgxpool.acquires" pgxPoolAcquireDuration = "pgxpool.acquire_duration" pgxPoolAcquiredConnections = "pgxpool.acquired_connections" From 584280621b4149cb92cd7fb4d8835b74a2c37d1a Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Mon, 2 Jun 2025 17:20:44 +0200 Subject: [PATCH 2/4] feat(ci): test different go versions; ensure tidy --- .github/workflows/test.yaml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 782db56..843eeac 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,15 +1,25 @@ -name: Test & Build +name: Test on: [push] jobs: test: runs-on: ubuntu-latest + strategy: + matrix: + go-version: + - "1.22" + - "1.23" + - "1.24" steps: - uses: actions/checkout@v3 - name: Setup Go uses: actions/setup-go@v3 with: - go-version: ">=1.18.0" + go-version: ${{ matrix.go-version }} + + - name: Install dependencies + run: go mod tidy -diff + - name: Run tests run: make test From 55854d0ba3507c1eebc963ed92462c07775bc29e Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Mon, 2 Jun 2025 17:20:53 +0200 Subject: [PATCH 3/4] feat(ci): also test on PRs --- .github/workflows/test.yaml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 843eeac..13eb6ef 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,6 +1,12 @@ name: Test -on: [push] +on: + push: + branches: + - main + pull_request: + branches: + - main jobs: test: @@ -13,6 +19,7 @@ jobs: - "1.24" steps: - uses: actions/checkout@v3 + - name: Setup Go uses: actions/setup-go@v3 with: From 2c9294052f26c7089ae6d1c197a1a10ad14f6d38 Mon Sep 17 00:00:00 2001 From: Skarm Date: Sun, 25 May 2025 13:15:57 +0500 Subject: [PATCH 4/4] Add FieldsSeq --- .github/workflows/test.yaml | 4 ++-- operation_name_go124.go | 22 ++++++++++++++++++++++ operation_name_legacy.go | 33 +++++++++++++++++++++++++++++++++ tracer.go | 27 --------------------------- 4 files changed, 57 insertions(+), 29 deletions(-) create mode 100644 operation_name_go124.go create mode 100644 operation_name_legacy.go diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 13eb6ef..f7b4abf 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: + go-version: - "1.22" - "1.23" - "1.24" @@ -24,7 +24,7 @@ jobs: uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - + - name: Install dependencies run: go mod tidy -diff diff --git a/operation_name_go124.go b/operation_name_go124.go new file mode 100644 index 0000000..98726ed --- /dev/null +++ b/operation_name_go124.go @@ -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 +} diff --git a/operation_name_legacy.go b/operation_name_legacy.go new file mode 100644 index 0000000..45442e2 --- /dev/null +++ b/operation_name_legacy.go @@ -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]) +} diff --git a/tracer.go b/tracer.go index 670d0f0..d70b91d 100644 --- a/tracer.go +++ b/tracer.go @@ -6,9 +6,7 @@ import ( "errors" "fmt" "runtime/debug" - "strings" "time" - "unicode" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" @@ -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 {