【问题标题】:start a child span from an existing parent span in Go从 Go 中现有的父跨度开始一个子跨度
【发布时间】:2022-01-15 04:08:34
【问题描述】:

I tried using this answer to create a child span from an existing span but it didn't work for me sadly.

每当我进行 gRPC 调用时,我都有一个父跨度,代码 sn-p 是这样的。

type spanctxkey struct{} // pushing span in context to fetch it from anywhere

func StartSpan(ctx context.Context, operationName string) (context.Context, Span) {
parentSpan := sentry.StartSpan(ctx, operationName, sentry.TransactionName(operationName))
return context.WithValue(originalSpan.Context(), spanctxkey{}, &span), Span{
        originalSpan,
    }
}

现在,我正在尝试从另一个包中获取当前正在运行的 span,以便我可以启动一个子 Span,其代码是。

func getCurrentSpan(ctx context.Context) *Span {
    if span, ok := ctx.Value(spanctxkey{}).(*Span); ok {
        return span
    }
    return nil
}
childSpan := getCurrentSpan(ctx)
fmt.Println(childSpan)

当我尝试打印它时,它打印出nil,我很困惑为什么跨度会关闭。

现在的问题可能是父跨度可能在子跨度之前关闭,但不,我也添加了一些打印语句来验证这一点。

--starting span before making a grpc call---
-- starting span before querying the db ---
--closing span after querying the db---
--closing span after making a grpc call---

【问题讨论】:

  • from another package, - pkg1 中的 spanctxkey{} 与 pkg2 中的 spanctxkey{} 不同`
  • 将其导出,然后导入pkg2并再次尝试检索。
  • "现在,我正在尝试获取当前运行的跨度,来自另一个包 - 不同包中未导出的结构不是同一类型,因此您不能使用它们在上下文中查找值
  • 让我试试,我会更新你,谢谢❤️
  • @OlegButuzov 谢谢,像你上面提到的那样在全局创建类型后,我能够获取运行跨度,但是我怎样才能从它开始一个子跨度呢? runningSpan := ctx.Value(apm.Spanctxkey{}).(**apm.Span) 而我的 runningSpan 类型为 var runningSpan **apm.Span

标签: go grpc opentracing distributed-tracing


【解决方案1】:

你试过span.StartChild("foobar")吗? (我是根据sentrysdk判断的)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2021-12-30
    • 2020-01-30
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多