From 093dd57a787f6bfe18a990d17354ad096f06f349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Wed, 3 Apr 2024 11:48:24 +0300 Subject: [PATCH] fix(db): wrap BeginTx in spans to get acquire metrics (#7689) feat(db): wrap BeginTx in spans to get acquire metrics This changes adds a span around most db.BeginTx calls so we can get tracings about the connection pool acquire process. This might help us pinpoint why sometimes some query package traces show longer execution times, while this was not reflected on database side execution times. Co-authored-by: Silvan --- internal/crypto/database/database.go | 3 +++ internal/database/database.go | 5 +++++ internal/eventstore/handler/v2/handler.go | 3 +++ internal/eventstore/v3/push.go | 3 +++ internal/query/current_state.go | 2 ++ 5 files changed, 16 insertions(+) diff --git a/internal/crypto/database/database.go b/internal/crypto/database/database.go index 7b27d223a4..09af9a5e31 100644 --- a/internal/crypto/database/database.go +++ b/internal/crypto/database/database.go @@ -8,6 +8,7 @@ import ( "github.com/zitadel/zitadel/internal/crypto" z_db "github.com/zitadel/zitadel/internal/database" + "github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/zerrors" ) @@ -113,7 +114,9 @@ func (d *Database) CreateKeys(ctx context.Context, keys ...*crypto.Key) error { if err != nil { return zerrors.ThrowInternal(err, "", "unable to insert new keys") } + ctx, spanBeginTx := tracing.NewNamedSpan(ctx, "db.BeginTx") tx, err := d.client.BeginTx(ctx, nil) + spanBeginTx.EndWithError(err) if err != nil { return zerrors.ThrowInternal(err, "", "unable to insert new keys") } diff --git a/internal/database/database.go b/internal/database/database.go index cd72d6d242..e64645294b 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -14,6 +14,7 @@ import ( _ "github.com/zitadel/zitadel/internal/database/cockroach" "github.com/zitadel/zitadel/internal/database/dialect" _ "github.com/zitadel/zitadel/internal/database/postgres" + "github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/zerrors" ) @@ -38,7 +39,9 @@ func (db *DB) Query(scan func(*sql.Rows) error, query string, args ...any) error } func (db *DB) QueryContext(ctx context.Context, scan func(rows *sql.Rows) error, query string, args ...any) (err error) { + ctx, spanBeginTx := tracing.NewNamedSpan(ctx, "db.BeginTx") tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) + spanBeginTx.EndWithError(err) if err != nil { return err } @@ -71,7 +74,9 @@ func (db *DB) QueryRow(scan func(*sql.Row) error, query string, args ...any) (er } func (db *DB) QueryRowContext(ctx context.Context, scan func(row *sql.Row) error, query string, args ...any) (err error) { + ctx, spanBeginTx := tracing.NewNamedSpan(ctx, "db.BeginTx") tx, err := db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) + spanBeginTx.EndWithError(err) if err != nil { return err } diff --git a/internal/eventstore/handler/v2/handler.go b/internal/eventstore/handler/v2/handler.go index b2da67d0c4..d9a78262f1 100644 --- a/internal/eventstore/handler/v2/handler.go +++ b/internal/eventstore/handler/v2/handler.go @@ -21,6 +21,7 @@ import ( "github.com/zitadel/zitadel/internal/migration" "github.com/zitadel/zitadel/internal/repository/instance" "github.com/zitadel/zitadel/internal/repository/pseudo" + "github.com/zitadel/zitadel/internal/telemetry/tracing" ) type EventStore interface { @@ -479,7 +480,9 @@ func (h *Handler) processEvents(ctx context.Context, config *triggerConfig) (add defer cancel() } + ctx, spanBeginTx := tracing.NewNamedSpan(ctx, "db.BeginTx") tx, err := h.client.BeginTx(txCtx, nil) + spanBeginTx.EndWithError(err) if err != nil { return false, err } diff --git a/internal/eventstore/v3/push.go b/internal/eventstore/v3/push.go index 3d0831e9a1..c217359828 100644 --- a/internal/eventstore/v3/push.go +++ b/internal/eventstore/v3/push.go @@ -15,11 +15,14 @@ import ( "github.com/zitadel/logging" "github.com/zitadel/zitadel/internal/eventstore" + "github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/zerrors" ) func (es *Eventstore) Push(ctx context.Context, commands ...eventstore.Command) (events []eventstore.Event, err error) { + ctx, spanBeginTx := tracing.NewNamedSpan(ctx, "db.BeginTx") tx, err := es.client.BeginTx(ctx, nil) + spanBeginTx.EndWithError(err) if err != nil { return nil, err } diff --git a/internal/query/current_state.go b/internal/query/current_state.go index b54d67853b..4d984f9e3b 100644 --- a/internal/query/current_state.go +++ b/internal/query/current_state.go @@ -90,7 +90,9 @@ func (q *Queries) latestState(ctx context.Context, projections ...table) (state } func (q *Queries) ClearCurrentSequence(ctx context.Context, projectionName string) (err error) { + ctx, spanBeginTx := tracing.NewNamedSpan(ctx, "db.BeginTx") tx, err := q.client.BeginTx(ctx, nil) + spanBeginTx.EndWithError(err) if err != nil { return zerrors.ThrowInternal(err, "QUERY-9iOpr", "Errors.RemoveFailed") }