Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRDB: adds missing observability for Watch API #1656

Merged
merged 1 commit into from
Dec 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/datastore/crdb/pool/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/time/rate"

pgxcommon "github.com/authzed/spicedb/internal/datastore/postgres/common"
log "github.com/authzed/spicedb/internal/logging"
)

Expand Down Expand Up @@ -40,7 +41,7 @@ type NodeHealthTracker struct {

// NewNodeHealthChecker builds a health checker that polls the cluster at the given url.
func NewNodeHealthChecker(url string) (*NodeHealthTracker, error) {
connConfig, err := pgx.ParseConfig(url)
connConfig, err := pgxcommon.ParseConfigWithInstrumentation(url)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/datastore/crdb/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sort"
"time"

"github.com/jackc/pgx/v5"
"github.com/prometheus/client_golang/prometheus"

"github.com/authzed/spicedb/internal/datastore/common"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (cds *crdbDatastore) Watch(ctx context.Context, afterRevision datastore.Rev
// changefeed data, instead of using a connection pool as most client
// drivers do by default."
// see: https://www.cockroachlabs.com/docs/v22.2/changefeed-for#considerations
conn, err := pgx.Connect(ctx, cds.dburl)
conn, err := pgxcommon.ConnectWithInstrumentation(ctx, cds.dburl)
if err != nil {
errs <- err
return updates, errs
Expand Down Expand Up @@ -299,7 +298,7 @@ func (cds *crdbDatastore) watchSchemaWithoutRetry(ctx context.Context, afterRevi
// changefeed data, instead of using a connection pool as most client
// drivers do by default."
// see: https://www.cockroachlabs.com/docs/v22.2/changefeed-for#considerations
conn, err := pgx.Connect(ctx, cds.dburl)
conn, err := pgxcommon.ConnectWithInstrumentation(ctx, cds.dburl)
if err != nil {
processUpdate(nil, err)
return
Expand Down
23 changes: 23 additions & 0 deletions internal/datastore/postgres/common/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ func queryTuples(ctx context.Context, sqlStatement string, args []any, span trac
return tuples, nil
}

// ParseConfigWithInstrumentation returns a pgx.ConnConfig that has been instrumented for observability
func ParseConfigWithInstrumentation(url string) (*pgx.ConnConfig, error) {
connConfig, err := pgx.ParseConfig(url)
if err != nil {
return nil, err
}

ConfigurePGXLogger(connConfig)
ConfigureOTELTracer(connConfig)

return connConfig, nil
}

// ConnectWithInstrumentation returns a pgx.Conn that has been instrumented for observability
func ConnectWithInstrumentation(ctx context.Context, url string) (*pgx.Conn, error) {
connConfig, err := ParseConfigWithInstrumentation(url)
if err != nil {
return nil, err
}

return pgx.ConnectConfig(ctx, connConfig)
}

// ConfigurePGXLogger sets zerolog global logger into the connection pool configuration, and maps
// info level events to debug, as they are rather verbose for SpiceDB's info level
func ConfigurePGXLogger(connConfig *pgx.ConnConfig) {
Expand Down