From 8ebc7bbad01b1aa109ac802e1d5c4ec067045637 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Thu, 27 Feb 2020 18:10:52 +0100 Subject: [PATCH] Name context functions consistently (#481) 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 --- api/correlation/context.go | 10 +++++----- api/correlation/correlation_context_propagator.go | 6 +++--- api/correlation/correlation_context_propagator_test.go | 6 +++--- bridge/opentracing/internal/mock.go | 2 +- example/grpc/middleware/tracing/tracing.go | 2 +- example/http-stackdriver/server/server.go | 2 +- example/http/server/server.go | 2 +- plugin/grpctrace/grpctrace.go | 2 +- plugin/httptrace/httptrace.go | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/api/correlation/context.go b/api/correlation/context.go index edfa7d3fbea..cda4f1518f5 100644 --- a/api/correlation/context.go +++ b/api/correlation/context.go @@ -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 } diff --git a/api/correlation/correlation_context_propagator.go b/api/correlation/correlation_context_propagator.go index 50af2bf9810..846f7d81fb1 100644 --- a/api/correlation/correlation_context_propagator.go +++ b/api/correlation/correlation_context_propagator.go @@ -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 { @@ -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, ",") @@ -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, })) } diff --git a/api/correlation/correlation_context_propagator_test.go b/api/correlation/correlation_context_propagator_test.go index c0f77379fe1..a18c2473872 100644 --- a/api/correlation/correlation_context_propagator_test.go +++ b/api/correlation/correlation_context_propagator_test.go @@ -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( @@ -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) } @@ -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") diff --git a/bridge/opentracing/internal/mock.go b/bridge/opentracing/internal/mock.go index 17eab6e08f9..41c34442fd0 100644 --- a/bridge/opentracing/internal/mock.go +++ b/bridge/opentracing/internal/mock.go @@ -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{ diff --git a/example/grpc/middleware/tracing/tracing.go b/example/grpc/middleware/tracing/tracing.go index b3d4f812133..035c8a4a28b 100644 --- a/example/grpc/middleware/tracing/tracing.go +++ b/example/grpc/middleware/tracing/tracing.go @@ -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, })) diff --git a/example/http-stackdriver/server/server.go b/example/http-stackdriver/server/server.go index 601a2f1535a..233a1b26657 100644 --- a/example/http-stackdriver/server/server.go +++ b/example/http-stackdriver/server/server.go @@ -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, }))) diff --git a/example/http/server/server.go b/example/http/server/server.go index 3c7f7970270..07a092207b7 100644 --- a/example/http/server/server.go +++ b/example/http/server/server.go @@ -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, }))) diff --git a/plugin/grpctrace/grpctrace.go b/plugin/grpctrace/grpctrace.go index b53713d6584..9204c1ccb29 100644 --- a/plugin/grpctrace/grpctrace.go +++ b/plugin/grpctrace/grpctrace.go @@ -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 }) diff --git a/plugin/httptrace/httptrace.go b/plugin/httptrace/httptrace.go index da91d36a2aa..60ec8785772 100644 --- a/plugin/httptrace/httptrace.go +++ b/plugin/httptrace/httptrace.go @@ -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 })