add NewContext
This commit is contained in:
Родитель
27a7af64ed
Коммит
bca7481a73
|
@ -60,6 +60,14 @@ func (t *Trace) FromContext(ctx context.Context) trace.Spanner {
|
|||
return &Span{span: sp}
|
||||
}
|
||||
|
||||
// NewContext returns a new context with the given Span attached.
|
||||
func (t *Trace) NewContext(ctx context.Context, span trace.Spanner) context.Context {
|
||||
if sp, ok := span.InternalSpan().(*oct.Span); ok {
|
||||
return oct.NewContext(ctx, sp)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// AddAttributes sets attributes in the span.
|
||||
//
|
||||
// Existing attributes whose keys appear in the attributes parameter are overwritten.
|
||||
|
@ -83,6 +91,11 @@ func (s *Span) Inject(carrier trace.Carrier) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// InternalSpan returns the real implementation of the Span
|
||||
func (s *Span) InternalSpan() interface{} {
|
||||
return s.span
|
||||
}
|
||||
|
||||
func toOCOption(opts ...interface{}) []oct.StartOption {
|
||||
var ocStartOptions []oct.StartOption
|
||||
for _, opt := range opts {
|
||||
|
|
|
@ -52,6 +52,14 @@ func (t *Trace) FromContext(ctx context.Context) trace.Spanner {
|
|||
return &Span{span: sp}
|
||||
}
|
||||
|
||||
// NewContext returns a new context with the given Span attached.
|
||||
func (t *Trace) NewContext(ctx context.Context, span trace.Spanner) context.Context {
|
||||
if sp, ok := span.InternalSpan().(opentracing.Span); ok {
|
||||
return opentracing.ContextWithSpan(ctx, sp)
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// AddAttributes a tags to the span.
|
||||
//
|
||||
// If there is a pre-existing tag set for `key`, it is overwritten.
|
||||
|
@ -80,6 +88,11 @@ func (s *Span) Inject(carrier trace.Carrier) error {
|
|||
return opentracing.GlobalTracer().Inject(s.span.Context(), opentracing.TextMap, carrierAdapter{carrier: carrier})
|
||||
}
|
||||
|
||||
// InternalSpan returns the real implementation of the Span
|
||||
func (s *Span) InternalSpan() interface{} {
|
||||
return s.span
|
||||
}
|
||||
|
||||
// Set a key and value on the carrier
|
||||
func (ca *carrierAdapter) Set(key, value string) {
|
||||
ca.carrier.Set(key, value)
|
||||
|
|
|
@ -69,6 +69,7 @@ type (
|
|||
End()
|
||||
Logger() Logger
|
||||
Inject(carrier Carrier) error
|
||||
InternalSpan() interface{}
|
||||
}
|
||||
|
||||
// Tracer is an abstraction over OpenTracing and OpenCensus trace implementations
|
||||
|
@ -76,6 +77,7 @@ type (
|
|||
StartSpan(ctx context.Context, operationName string, opts ...interface{}) (context.Context, Spanner)
|
||||
StartSpanWithRemoteParent(ctx context.Context, operationName string, carrier Carrier, opts ...interface{}) (context.Context, Spanner)
|
||||
FromContext(ctx context.Context) Spanner
|
||||
NewContext(parent context.Context, span Spanner) context.Context
|
||||
}
|
||||
|
||||
// Logger is a generic interface for logging
|
||||
|
@ -112,6 +114,11 @@ func (ns *noOpSpanner) Inject(carrier Carrier) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// InternalSpan returns nil
|
||||
func (ns *noOpSpanner) InternalSpan() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// For will return a logger for a given context
|
||||
func For(ctx context.Context) Logger {
|
||||
if span := tracer.FromContext(ctx); span != nil {
|
||||
|
|
Загрузка…
Ссылка в новой задаче