本文整理汇总了Golang中github.com/opentracing/opentracing-go.SpanFromContext函数的典型用法代码示例。如果您正苦于以下问题:Golang SpanFromContext函数的具体用法?Golang SpanFromContext怎么用?Golang SpanFromContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SpanFromContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: withEventLogInternal
// withEventLogInternal embeds a trace.EventLog in the context, causing future
// logging and event calls to go to the EventLog. The current context must not
// have an existing open span.
func withEventLogInternal(ctx context.Context, eventLog trace.EventLog) context.Context {
if opentracing.SpanFromContext(ctx) != nil {
panic("event log under span")
}
val := &ctxEventLog{eventLog: eventLog}
return context.WithValue(ctx, ctxEventLogKey{}, val)
}
示例2: SpanFromContext
// SpanFromContext wraps opentracing.SpanFromContext so that the returned
// Span is never nil.
func SpanFromContext(ctx context.Context) opentracing.Span {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
return NoopSpan()
}
return sp
}
示例3: EnsureContext
// EnsureContext checks whether the given context.Context contains a Span. If
// not, it creates one using the provided Tracer and wraps it in the returned
// Span. The returned closure must be called after the request has been fully
// processed.
func EnsureContext(ctx context.Context, tracer opentracing.Tracer) (context.Context, func()) {
_, _, funcName := caller.Lookup(1)
if opentracing.SpanFromContext(ctx) == nil {
sp := tracer.StartSpan(funcName)
return opentracing.ContextWithSpan(ctx, sp), sp.Finish
}
return ctx, func() {}
}
示例4: ChildSpan
// ChildSpan opens a span as a child of the current span in the context (if
// there is one).
//
// Returns the new context and the new span (if any). The span should be
// closed via FinishSpan.
func ChildSpan(ctx context.Context, opName string) (context.Context, opentracing.Span) {
span := opentracing.SpanFromContext(ctx)
if span == nil {
return ctx, nil
}
newSpan := span.Tracer().StartSpan(opName, opentracing.ChildOf(span.Context()))
return opentracing.ContextWithSpan(ctx, newSpan), newSpan
}
示例5: SpanFromContext
// SpanFromContext returns the Span obtained from the context or, if none is
// found, a new one started through the tracer. Callers should call (or defer)
// the returned cleanup func as well to ensure that the span is Finish()ed, but
// callers should *not* attempt to call Finish directly -- in the case where the
// span was obtained from the context, it is not the caller's to Finish.
func SpanFromContext(opName string, tracer opentracing.Tracer, ctx context.Context) (opentracing.Span, func()) {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
sp = tracer.StartSpan(opName)
return sp, sp.Finish
}
return sp, func() {}
}
示例6: SetFlowRequestTrace
// SetFlowRequestTrace populates req.Trace with the context of the current Span
// in the context (if any).
func SetFlowRequestTrace(ctx context.Context, req *SetupFlowRequest) error {
sp := opentracing.SpanFromContext(ctx)
if sp == nil {
return nil
}
req.TraceContext = &tracing.SpanContextCarrier{}
tracer := sp.Tracer()
return tracer.Inject(sp.Context(), basictracer.Delegator, req.TraceContext)
}
示例7: AnnotateCtx
// AnnotateCtx annotates a given context with the information in AmbientContext:
// - the EventLog is embedded in the context if the context doesn't already
// have en event log or an open trace.
// - the log tags in AmbientContext are added (if ctx doesn't already have them).
//
// For background operations, context.Background() should be passed; however, in
// that case it is strongly recommended to open a span if possible (using
// AnnotateCtxWithSpan).
func (ac *AmbientContext) AnnotateCtx(ctx context.Context) context.Context {
// TODO(radu): We could keep a cached context based off of
// context.Background() to avoid allocations in that case.
if ac.eventLog != nil && opentracing.SpanFromContext(ctx) == nil && eventLogFromCtx(ctx) == nil {
ctx = embedCtxEventLog(ctx, ac.eventLog)
}
if ac.tags != nil {
ctx = copyTagChain(ctx, ac.tags)
}
return ctx
}
示例8: getSpanOrEventLog
// getSpanOrEventLog returns the current Span. If there is no Span, it returns
// the current ctxEventLog. If neither (or the Span is NoopTracer), returns
// false.
func getSpanOrEventLog(ctx context.Context) (opentracing.Span, *ctxEventLog, bool) {
if sp := opentracing.SpanFromContext(ctx); sp != nil {
if sp.Tracer() == noopTracer {
return nil, nil, false
}
return sp, nil, true
}
if el := eventLogFromCtx(ctx); el != nil {
return nil, el, true
}
return nil, nil, false
}
示例9: newFlow
func newFlow(flowCtx FlowCtx, flowReg *flowRegistry, simpleFlowConsumer RowReceiver) *Flow {
if opentracing.SpanFromContext(flowCtx.Context) == nil {
panic("flow context has no span")
}
flowCtx.Context = log.WithLogTagStr(flowCtx.Context, "f", flowCtx.id.Short())
return &Flow{
FlowCtx: flowCtx,
flowRegistry: flowReg,
simpleFlowConsumer: simpleFlowConsumer,
status: FlowNotStarted,
}
}
示例10: sendSingleRange
// sendSingleRange gathers and rearranges the replicas, and makes an RPC call.
func (ds *DistSender) sendSingleRange(
ctx context.Context, ba roachpb.BatchRequest, desc *roachpb.RangeDescriptor,
) (*roachpb.BatchResponse, *roachpb.Error) {
// Hack: avoid formatting the message passed to Span.LogEvent for
// opentracing.noopSpans. We can't actually tell if we have a noopSpan, but
// we can see if the span as a NoopTracer. Note that this particular
// invocation is expensive because we're pretty-printing keys.
//
// TODO(tschottdorf): This hack can go away when something like
// Span.LogEventf is added.
sp := opentracing.SpanFromContext(ctx)
if sp != nil && sp.Tracer() != (opentracing.NoopTracer{}) {
sp.LogEvent(fmt.Sprintf("sending RPC to [%s, %s)", desc.StartKey, desc.EndKey))
}
// Try to send the call.
replicas := newReplicaSlice(ds.gossip, desc)
// Rearrange the replicas so that those replicas with long common
// prefix of attributes end up first. If there's no prefix, this is a
// no-op.
order := ds.optimizeReplicaOrder(replicas)
// If this request needs to go to a leader and we know who that is, move
// it to the front.
if !(ba.IsReadOnly() && ba.ReadConsistency == roachpb.INCONSISTENT) {
if leader := ds.leaderCache.Lookup(roachpb.RangeID(desc.RangeID)); leader.StoreID > 0 {
if i := replicas.FindReplica(leader.StoreID); i >= 0 {
replicas.MoveToFront(i)
order = orderStable
}
}
}
// TODO(tschottdorf): should serialize the trace here, not higher up.
br, pErr := ds.sendRPC(ctx, desc.RangeID, replicas, order, ba)
if pErr != nil {
return nil, pErr
}
// If the reply contains a timestamp, update the local HLC with it.
if br.Error != nil && br.Error.Now != roachpb.ZeroTimestamp {
ds.clock.Update(br.Error.Now)
} else if br.Now != roachpb.ZeroTimestamp {
ds.clock.Update(br.Now)
}
// Untangle the error from the received response.
pErr = br.Error
br.Error = nil // scrub the response error
return br, pErr
}
示例11: ForkCtxSpan
// ForkCtxSpan checks if ctx has a Span open; if it does, it creates a new Span
// that follows from the original Span. This allows the resulting context to be
// used in an async task that might outlive the original operation.
//
// Returns the new context and the new span (if any). The span should be
// closed via FinishSpan.
func ForkCtxSpan(ctx context.Context, opName string) (context.Context, opentracing.Span) {
if span := opentracing.SpanFromContext(ctx); span != nil {
if span.BaggageItem(Snowball) == "1" {
// If we are doing snowball tracing, the span might outlive the snowball
// tracer (calling the record function when it is no longer legal to do
// so). Return a context with no span in this case.
return opentracing.ContextWithSpan(ctx, nil), nil
}
tr := span.Tracer()
newSpan := tr.StartSpan(opName, opentracing.FollowsFrom(span.Context()))
return opentracing.ContextWithSpan(ctx, newSpan), newSpan
}
return ctx, nil
}
示例12: Cleanup
// Cleanup should be called when the flow completes (after all processors and
// mailboxes exited).
func (f *Flow) Cleanup() {
if f.status == FlowFinished {
panic("flow cleanup called twice")
}
if log.V(1) {
log.Infof(f.Context, "cleaning up")
}
sp := opentracing.SpanFromContext(f.Context)
sp.Finish()
if f.status != FlowNotStarted {
f.flowRegistry.UnregisterFlow(f.id)
}
f.status = FlowFinished
}
示例13: AnnotateCtxWithSpan
// AnnotateCtxWithSpan annotates the given context with the information in
// AmbientContext (see AnnotateCtx) and opens a span.
//
// If the given context has a span, the new span is a child of that span.
// Otherwise, the Tracer in AmbientContext is used to create a new root span.
//
// The caller is responsible for closing the span (via Span.Finish).
func (ac *AmbientContext) AnnotateCtxWithSpan(
ctx context.Context, opName string,
) (context.Context, opentracing.Span) {
if ac.tags != nil {
ctx = copyTagChain(ctx, ac.tags)
}
var span opentracing.Span
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
tracer := parentSpan.Tracer()
span = tracer.StartSpan(opName, opentracing.ChildOf(parentSpan.Context()))
} else {
if ac.Tracer == nil {
panic("no tracer in AmbientContext for root span")
}
span = ac.Tracer.StartSpan(opName)
}
return opentracing.ContextWithSpan(ctx, span), span
}
示例14: NewSession
// NewSession creates and initializes a new Session object.
// remote can be nil.
func NewSession(
ctx context.Context, args SessionArgs, e *Executor, remote net.Addr, memMetrics *MemoryMetrics,
) *Session {
ctx = e.AnnotateCtx(ctx)
s := &Session{
Database: args.Database,
SearchPath: []string{"pg_catalog"},
User: args.User,
Location: time.UTC,
virtualSchemas: e.virtualSchemas,
memMetrics: memMetrics,
}
cfg, cache := e.getSystemConfig()
s.planner = planner{
leaseMgr: e.cfg.LeaseManager,
systemConfig: cfg,
databaseCache: cache,
session: s,
execCfg: &e.cfg,
}
s.PreparedStatements = makePreparedStatements(s)
s.PreparedPortals = makePreparedPortals(s)
if opentracing.SpanFromContext(ctx) == nil {
remoteStr := "<admin>"
if remote != nil {
remoteStr = remote.String()
}
// Set up an EventLog for session events.
ctx = log.WithEventLog(ctx, fmt.Sprintf("sql [%s]", args.User), remoteStr)
s.finishEventLog = true
}
s.context, s.cancel = context.WithCancel(ctx)
return s
}
示例15: Tracef
// Tracef looks for an opentracing.Trace in the context and formats and logs
// the given message to it on success.
func Tracef(ctx context.Context, format string, args ...interface{}) {
sp := opentracing.SpanFromContext(ctx)
if sp != nil && sp.Tracer() != noopTracer {
sp.LogEvent(fmt.Sprintf(format, args...))
}
}