Skip to content

Commit

Permalink
Name context functions consistently (#481)
Browse files Browse the repository at this point in the history
It would be nice to follow a single schema for naming context
functions. In the trace package we followed the form FooFromContext
and ContextWithFoo. Do the same in the correlation package. The schema
WithFoo is mainly used for functions following the options pattern.

Not sure about a name of the NewContext function, though. For now I
have left it alone.

Co-authored-by: Rahul Patel <[email protected]>
  • Loading branch information
krnowak and rghetia authored Feb 27, 2020
1 parent a6b8058 commit 8ebc7bb
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 17 deletions.
10 changes: 5 additions & 5 deletions api/correlation/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ type correlationsType struct{}

var correlationsKey = &correlationsType{}

// WithMap returns a context with the Map entered into it.
func WithMap(ctx context.Context, m Map) context.Context {
// ContextWithMap returns a context with the Map entered into it.
func ContextWithMap(ctx context.Context, m Map) context.Context {
return context.WithValue(ctx, correlationsKey, m)
}

// NewContext returns a context with the map from passed context
// updated with the passed key-value pairs.
func NewContext(ctx context.Context, keyvalues ...core.KeyValue) context.Context {
return WithMap(ctx, FromContext(ctx).Apply(MapUpdate{
return ContextWithMap(ctx, MapFromContext(ctx).Apply(MapUpdate{
MultiKV: keyvalues,
}))
}

// FromContext gets the current Map from a Context.
func FromContext(ctx context.Context) Map {
// MapFromContext gets the current Map from a Context.
func MapFromContext(ctx context.Context) Map {
if m, ok := ctx.Value(correlationsKey).(Map); ok {
return m
}
Expand Down
6 changes: 3 additions & 3 deletions api/correlation/correlation_context_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func DefaultHTTPPropagator() propagation.HTTPPropagator {

// Inject implements HTTPInjector.
func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
correlationCtx := FromContext(ctx)
correlationCtx := MapFromContext(ctx)
firstIter := true
var headerValueBuilder strings.Builder
correlationCtx.Foreach(func(kv core.KeyValue) bool {
Expand All @@ -52,7 +52,7 @@ func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPS
func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
correlationContext := supplier.Get(CorrelationContextHeader)
if correlationContext == "" {
return WithMap(ctx, NewEmptyMap())
return ContextWithMap(ctx, NewEmptyMap())
}

contextValues := strings.Split(correlationContext, ",")
Expand Down Expand Up @@ -88,7 +88,7 @@ func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTP

keyValues = append(keyValues, key.New(trimmedName).String(trimmedValueWithProps.String()))
}
return WithMap(ctx, NewMap(MapUpdate{
return ContextWithMap(ctx, NewMap(MapUpdate{
MultiKV: keyValues,
}))
}
Expand Down
6 changes: 3 additions & 3 deletions api/correlation/correlation_context_propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {

ctx := context.Background()
ctx = propagation.ExtractHTTP(ctx, props, req.Header)
gotCorCtx := correlation.FromContext(ctx)
gotCorCtx := correlation.MapFromContext(ctx)
wantCorCtx := correlation.NewMap(correlation.MapUpdate{MultiKV: tt.wantKVs})
if gotCorCtx.Len() != wantCorCtx.Len() {
t.Errorf(
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestExtractInvalidDistributedContextFromHTTPReq(t *testing.T) {

ctx := context.Background()
ctx = propagation.ExtractHTTP(ctx, props, req.Header)
gotCorCtx := correlation.FromContext(ctx)
gotCorCtx := correlation.MapFromContext(ctx)
if gotCorCtx.Len() != 0 {
t.Errorf("Got and Want CorCtx are not the same size %d != %d", gotCorCtx.Len(), 0)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestInjectCorrelationContextToHTTPReq(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
ctx := correlation.WithMap(context.Background(), correlation.NewMap(correlation.MapUpdate{MultiKV: tt.kvs}))
ctx := correlation.ContextWithMap(context.Background(), correlation.NewMap(correlation.MapUpdate{MultiKV: tt.kvs}))
propagation.InjectHTTP(ctx, props, req.Header)

gotHeader := req.Header.Get("Correlation-Context")
Expand Down
2 changes: 1 addition & 1 deletion bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (s *MockSpan) AddEvent(ctx context.Context, name string, attrs ...otelcore.

func (s *MockSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...otelcore.KeyValue) {
s.Events = append(s.Events, MockEvent{
CtxAttributes: otelcorrelation.FromContext(ctx),
CtxAttributes: otelcorrelation.MapFromContext(ctx),
Timestamp: timestamp,
Name: name,
Attributes: otelcorrelation.NewMap(otelcorrelation.MapUpdate{
Expand Down
2 changes: 1 addition & 1 deletion example/grpc/middleware/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.Una
metadataCopy := requestMetadata.Copy()

entries, spanCtx := grpctrace.Extract(ctx, &metadataCopy)
ctx = correlation.WithMap(ctx, correlation.NewMap(correlation.MapUpdate{
ctx = correlation.ContextWithMap(ctx, correlation.NewMap(correlation.MapUpdate{
MultiKV: entries,
}))

Expand Down
2 changes: 1 addition & 1 deletion example/http-stackdriver/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
helloHandler := func(w http.ResponseWriter, req *http.Request) {
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)

req = req.WithContext(correlation.WithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
req = req.WithContext(correlation.ContextWithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
MultiKV: entries,
})))

Expand Down
2 changes: 1 addition & 1 deletion example/http/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
helloHandler := func(w http.ResponseWriter, req *http.Request) {
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)

req = req.WithContext(correlation.WithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
req = req.WithContext(correlation.ContextWithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
MultiKV: entries,
})))

Expand Down
2 changes: 1 addition & 1 deletion plugin/grpctrace/grpctrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func Extract(ctx context.Context, metadata *metadata.MD) ([]core.KeyValue, core.

spanContext := trace.RemoteSpanContextFromContext(ctx)
var correlationCtxKVs []core.KeyValue
correlation.FromContext(ctx).Foreach(func(kv core.KeyValue) bool {
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
correlationCtxKVs = append(correlationCtxKVs, kv)
return true
})
Expand Down
2 changes: 1 addition & 1 deletion plugin/httptrace/httptrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Extract(ctx context.Context, req *http.Request) ([]core.KeyValue, []core.Ke
}

var correlationCtxKVs []core.KeyValue
correlation.FromContext(ctx).Foreach(func(kv core.KeyValue) bool {
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
correlationCtxKVs = append(correlationCtxKVs, kv)
return true
})
Expand Down

0 comments on commit 8ebc7bb

Please sign in to comment.