Skip to content

Commit

Permalink
Merge pull request #1587 from jzelinskie/consistency-metric
Browse files Browse the repository at this point in the history
middleware: add metric for tracking consistency
  • Loading branch information
jzelinskie authored Oct 17, 2023
2 parents a070d54 + 8776d48 commit 1296f82
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions internal/middleware/consistency/consistency.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -19,13 +21,16 @@ import (
"github.com/authzed/spicedb/pkg/zedtoken"
)

type hasConsistency interface {
GetConsistency() *v1.Consistency
}
var ConsistentyCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "spicedb",
Subsystem: "middleware",
Name: "consistency_assigned_total",
Help: "Count of the consistencies used per request",
}, []string{"method", "source"})

type hasOptionalCursor interface {
GetOptionalCursor() *v1.Cursor
}
type hasConsistency interface{ GetConsistency() *v1.Consistency }

type hasOptionalCursor interface{ GetOptionalCursor() *v1.Cursor }

type ctxKeyType struct{}

Expand Down Expand Up @@ -84,6 +89,8 @@ func addRevisionToContextFromConsistency(ctx context.Context, req hasConsistency
switch {
case hasOptionalCursor && withOptionalCursor.GetOptionalCursor() != nil:
// Always use the revision encoded in the cursor.
ConsistentyCounter.WithLabelValues("snapshot", "cursor").Inc()

requestedRev, err := cursor.DecodeToDispatchRevision(withOptionalCursor.GetOptionalCursor(), ds)
if err != nil {
return rewriteDatastoreError(ctx, err)
Expand All @@ -98,6 +105,12 @@ func addRevisionToContextFromConsistency(ctx context.Context, req hasConsistency

case consistency == nil || consistency.GetMinimizeLatency():
// Minimize Latency: Use the datastore's current revision, whatever it may be.
source := "request"
if consistency == nil {
source = "server"
}
ConsistentyCounter.WithLabelValues("minlatency", source).Inc()

databaseRev, err := ds.OptimizedRevision(ctx)
if err != nil {
return rewriteDatastoreError(ctx, err)
Expand All @@ -106,6 +119,8 @@ func addRevisionToContextFromConsistency(ctx context.Context, req hasConsistency

case consistency.GetFullyConsistent():
// Fully Consistent: Use the datastore's synchronized revision.
ConsistentyCounter.WithLabelValues("full", "request").Inc()

databaseRev, err := ds.HeadRevision(ctx)
if err != nil {
return rewriteDatastoreError(ctx, err)
Expand All @@ -115,6 +130,8 @@ func addRevisionToContextFromConsistency(ctx context.Context, req hasConsistency
case consistency.GetAtLeastAsFresh() != nil:
// At least as fresh as: Pick one of the datastore's revision and that specified, which
// ever is later.
ConsistentyCounter.WithLabelValues("atleast", "request").Inc()

picked, err := pickBestRevision(ctx, consistency.GetAtLeastAsFresh(), ds)
if err != nil {
return rewriteDatastoreError(ctx, err)
Expand All @@ -123,6 +140,8 @@ func addRevisionToContextFromConsistency(ctx context.Context, req hasConsistency

case consistency.GetAtExactSnapshot() != nil:
// Exact snapshot: Use the revision as encoded in the zed token.
ConsistentyCounter.WithLabelValues("snapshot", "request").Inc()

requestedRev, err := zedtoken.DecodeRevision(consistency.GetAtExactSnapshot(), ds)
if err != nil {
return errInvalidZedToken
Expand Down Expand Up @@ -186,9 +205,7 @@ type recvWrapper struct {
ctx context.Context
}

func (s *recvWrapper) Context() context.Context {
return s.ctx
}
func (s *recvWrapper) Context() context.Context { return s.ctx }

func (s *recvWrapper) RecvMsg(m interface{}) error {
if err := s.ServerStream.RecvMsg(m); err != nil {
Expand Down

0 comments on commit 1296f82

Please sign in to comment.