Skip to content

Commit

Permalink
Merge cb8a406 into dc59508
Browse files Browse the repository at this point in the history
  • Loading branch information
andrehp committed Jan 12, 2019
2 parents dc59508 + cb8a406 commit be39f78
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
1 change: 1 addition & 0 deletions constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
ErrGroupNotFound = errors.New("group not found")
ErrIllegalUID = errors.New("illegal uid")
ErrInvalidCertificates = errors.New("certificates must be exactly two")
ErrInvalidSpanCarrier = errors.New("tracing: invalid span carrier")
ErrKickingUsers = errors.New("failed to kick users, check array with failed uids")
ErrMemberAlreadyExists = errors.New("member already exists in group")
ErrMemberNotFound = errors.New("member not found in the group")
Expand Down
2 changes: 1 addition & 1 deletion context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func AddToPropagateCtx(ctx context.Context, key string, val interface{}) context
return context.WithValue(ctx, constants.PropagateCtxKey, propagate)
}

// GetFromPropagateCtx adds a key and value to the propagate
// GetFromPropagateCtx get a value from the propagate
func GetFromPropagateCtx(ctx context.Context, key string) interface{} {
propagate := ToMap(ctx)
if val, ok := propagate[key]; ok {
Expand Down
40 changes: 26 additions & 14 deletions tracing/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,32 @@
package tracing

import (
"bytes"
"context"
"encoding/base64"

opentracing "github.com/opentracing/opentracing-go"
"github.com/topfreegames/pitaya/constants"
pcontext "github.com/topfreegames/pitaya/context"
"github.com/topfreegames/pitaya/logger"
)

func castValueToCarrier(val interface{}) (opentracing.TextMapCarrier, error) {
if v, ok := val.(opentracing.TextMapCarrier); ok {
return v, nil
}
if m, ok := val.(map[string]interface{}); ok {
carrier := map[string]string{}
for k, v := range m {
if s, ok := v.(string); ok {
carrier[k] = s
} else {
logger.Log.Warnf("value from span carrier cannot be cast to string: %+v", v)
}
}
return opentracing.TextMapCarrier(carrier), nil
}
return nil, constants.ErrInvalidSpanCarrier
}

// ExtractSpan retrieves an opentracing span context from the given context.Context
// The span context can be received directly (inside the context) or via an RPC call
// (encoded in binary format)
Expand All @@ -38,18 +55,13 @@ func ExtractSpan(ctx context.Context) (opentracing.SpanContext, error) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
if s := pcontext.GetFromPropagateCtx(ctx, constants.SpanPropagateCtxKey); s != nil {
var data []byte
var err error
if dataString, ok := s.(string); ok {
data, err = base64.StdEncoding.DecodeString(dataString)
if err != nil {
return nil, err
}
} else if sData, ok := s.([]byte); ok {
data = sData
carrier, err := castValueToCarrier(s)
if err != nil {
return nil, err
}
tracer := opentracing.GlobalTracer()
spanCtx, err = tracer.Extract(opentracing.Binary, bytes.NewBuffer(data))
spanCtx, err = tracer.Extract(opentracing.TextMap, carrier)
if err != nil {
return nil, err
}
Expand All @@ -69,13 +81,13 @@ func InjectSpan(ctx context.Context) (context.Context, error) {
if span == nil {
return ctx, nil
}
spanData := new(bytes.Buffer)
spanData := opentracing.TextMapCarrier{}
tracer := opentracing.GlobalTracer()
err := tracer.Inject(span.Context(), opentracing.Binary, spanData)
err := tracer.Inject(span.Context(), opentracing.TextMap, spanData)
if err != nil {
return nil, err
}
return pcontext.AddToPropagateCtx(ctx, constants.SpanPropagateCtxKey, spanData.Bytes()), nil
return pcontext.AddToPropagateCtx(ctx, constants.SpanPropagateCtxKey, spanData), nil
}

// StartSpan starts a new span with a given parent context, operation name, tags and
Expand Down
10 changes: 5 additions & 5 deletions tracing/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package tracing

import (
"bytes"
"context"
"errors"
"io"
Expand Down Expand Up @@ -62,10 +61,11 @@ func TestExtractSpan(t *testing.T) {

func TestExtractSpanInjectedSpan(t *testing.T) {
span := opentracing.StartSpan("op", opentracing.ChildOf(nil))
spanData := new(bytes.Buffer)
spanData := opentracing.TextMapCarrier{}
tracer := opentracing.GlobalTracer()
err := tracer.Inject(span.Context(), opentracing.Binary, spanData)
ctx := pcontext.AddToPropagateCtx(context.Background(), constants.SpanPropagateCtxKey, spanData.Bytes())
err := tracer.Inject(span.Context(), opentracing.TextMap, spanData)
assert.NoError(t, err)
ctx := pcontext.AddToPropagateCtx(context.Background(), constants.SpanPropagateCtxKey, spanData)

spanCtx, err := ExtractSpan(ctx)
assert.NoError(t, err)
Expand All @@ -81,7 +81,7 @@ func TestExtractSpanNoSpan(t *testing.T) {
func TestExtractSpanBadInjected(t *testing.T) {
ctx := pcontext.AddToPropagateCtx(context.Background(), constants.SpanPropagateCtxKey, []byte("nope"))
spanCtx, err := ExtractSpan(ctx)
assert.Equal(t, errors.New("opentracing: SpanContext data corrupted in Extract carrier"), err)
assert.Equal(t, constants.ErrInvalidSpanCarrier, err)
assert.Nil(t, spanCtx)
}

Expand Down

0 comments on commit be39f78

Please sign in to comment.