Skip to content

Commit 8ebc7bb

Browse files
krnowakrghetia
andauthored
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 <[email protected]>
1 parent a6b8058 commit 8ebc7bb

File tree

9 files changed

+17
-17
lines changed

9 files changed

+17
-17
lines changed

api/correlation/context.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ type correlationsType struct{}
2424

2525
var correlationsKey = &correlationsType{}
2626

27-
// WithMap returns a context with the Map entered into it.
28-
func WithMap(ctx context.Context, m Map) context.Context {
27+
// ContextWithMap returns a context with the Map entered into it.
28+
func ContextWithMap(ctx context.Context, m Map) context.Context {
2929
return context.WithValue(ctx, correlationsKey, m)
3030
}
3131

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

40-
// FromContext gets the current Map from a Context.
41-
func FromContext(ctx context.Context) Map {
40+
// MapFromContext gets the current Map from a Context.
41+
func MapFromContext(ctx context.Context) Map {
4242
if m, ok := ctx.Value(correlationsKey).(Map); ok {
4343
return m
4444
}

api/correlation/correlation_context_propagator.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func DefaultHTTPPropagator() propagation.HTTPPropagator {
2929

3030
// Inject implements HTTPInjector.
3131
func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPSupplier) {
32-
correlationCtx := FromContext(ctx)
32+
correlationCtx := MapFromContext(ctx)
3333
firstIter := true
3434
var headerValueBuilder strings.Builder
3535
correlationCtx.Foreach(func(kv core.KeyValue) bool {
@@ -52,7 +52,7 @@ func (CorrelationContext) Inject(ctx context.Context, supplier propagation.HTTPS
5252
func (CorrelationContext) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
5353
correlationContext := supplier.Get(CorrelationContextHeader)
5454
if correlationContext == "" {
55-
return WithMap(ctx, NewEmptyMap())
55+
return ContextWithMap(ctx, NewEmptyMap())
5656
}
5757

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

8989
keyValues = append(keyValues, key.New(trimmedName).String(trimmedValueWithProps.String()))
9090
}
91-
return WithMap(ctx, NewMap(MapUpdate{
91+
return ContextWithMap(ctx, NewMap(MapUpdate{
9292
MultiKV: keyValues,
9393
}))
9494
}

api/correlation/correlation_context_propagator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestExtractValidDistributedContextFromHTTPReq(t *testing.T) {
7878

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

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

190190
gotHeader := req.Header.Get("Correlation-Context")

bridge/opentracing/internal/mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (s *MockSpan) AddEvent(ctx context.Context, name string, attrs ...otelcore.
269269

270270
func (s *MockSpan) AddEventWithTimestamp(ctx context.Context, timestamp time.Time, name string, attrs ...otelcore.KeyValue) {
271271
s.Events = append(s.Events, MockEvent{
272-
CtxAttributes: otelcorrelation.FromContext(ctx),
272+
CtxAttributes: otelcorrelation.MapFromContext(ctx),
273273
Timestamp: timestamp,
274274
Name: name,
275275
Attributes: otelcorrelation.NewMap(otelcorrelation.MapUpdate{

example/grpc/middleware/tracing/tracing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.Una
3939
metadataCopy := requestMetadata.Copy()
4040

4141
entries, spanCtx := grpctrace.Extract(ctx, &metadataCopy)
42-
ctx = correlation.WithMap(ctx, correlation.NewMap(correlation.MapUpdate{
42+
ctx = correlation.ContextWithMap(ctx, correlation.NewMap(correlation.MapUpdate{
4343
MultiKV: entries,
4444
}))
4545

example/http-stackdriver/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func main() {
5858
helloHandler := func(w http.ResponseWriter, req *http.Request) {
5959
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)
6060

61-
req = req.WithContext(correlation.WithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
61+
req = req.WithContext(correlation.ContextWithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
6262
MultiKV: entries,
6363
})))
6464

example/http/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func main() {
5252
helloHandler := func(w http.ResponseWriter, req *http.Request) {
5353
attrs, entries, spanCtx := httptrace.Extract(req.Context(), req)
5454

55-
req = req.WithContext(correlation.WithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
55+
req = req.WithContext(correlation.ContextWithMap(req.Context(), correlation.NewMap(correlation.MapUpdate{
5656
MultiKV: entries,
5757
})))
5858

plugin/grpctrace/grpctrace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func Extract(ctx context.Context, metadata *metadata.MD) ([]core.KeyValue, core.
6969

7070
spanContext := trace.RemoteSpanContextFromContext(ctx)
7171
var correlationCtxKVs []core.KeyValue
72-
correlation.FromContext(ctx).Foreach(func(kv core.KeyValue) bool {
72+
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
7373
correlationCtxKVs = append(correlationCtxKVs, kv)
7474
return true
7575
})

plugin/httptrace/httptrace.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Extract(ctx context.Context, req *http.Request) ([]core.KeyValue, []core.Ke
4747
}
4848

4949
var correlationCtxKVs []core.KeyValue
50-
correlation.FromContext(ctx).Foreach(func(kv core.KeyValue) bool {
50+
correlation.MapFromContext(ctx).Foreach(func(kv core.KeyValue) bool {
5151
correlationCtxKVs = append(correlationCtxKVs, kv)
5252
return true
5353
})

0 commit comments

Comments
 (0)