Skip to content

Commit

Permalink
Merge pull request #1656 from authzed/observable-watch-queries-crdb
Browse files Browse the repository at this point in the history
CRDB: adds missing observability for Watch API
  • Loading branch information
vroldanbet committed Dec 1, 2023
2 parents b08d397 + 3102df4 commit a2274d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit a2274d0

Please sign in to comment.