github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/graph/context.go (about) 1 package graph 2 3 import ( 4 "context" 5 6 "go.opentelemetry.io/otel/trace" 7 8 log "github.com/authzed/spicedb/internal/logging" 9 datastoremw "github.com/authzed/spicedb/internal/middleware/datastore" 10 "github.com/authzed/spicedb/pkg/middleware/requestid" 11 ) 12 13 // branchContext returns a context disconnected from the parent context, but populated with the datastore. 14 // Also returns a function for canceling the newly created context, without canceling the parent context. 15 func branchContext(ctx context.Context) (context.Context, func(cancelErr error)) { 16 // Add tracing to the context. 17 span := trace.SpanFromContext(ctx) 18 detachedContext := trace.ContextWithSpan(context.Background(), span) 19 20 // Add datastore to the context. 21 ds := datastoremw.FromContext(ctx) 22 detachedContext = datastoremw.ContextWithDatastore(detachedContext, ds) 23 24 // Add logging to the context. 25 loggerFromContext := log.Ctx(ctx) 26 if loggerFromContext != nil { 27 detachedContext = loggerFromContext.WithContext(detachedContext) 28 } 29 30 detachedContext = requestid.PropagateIfExists(ctx, detachedContext) 31 32 return context.WithCancelCause(detachedContext) 33 }