-2

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

I have a parent span starting whenever I make my gRPC call, the code snippet goes like this.

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,
    }
}

Now, I am trying to get the current running span, from another package, so that I can start a child Span, the code for that is.

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

When I try to print it, it prints nil, I am confused on to why the span is getting closed.

Now the question can be that the parent span might be getting closed before the child span , but no, I added some print statements to verify that as well.

--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---
Rajat Singh
  • 653
  • 6
  • 15
  • 29
  • 2
    `from another package,` - `spanctxkey{}` in pkg1 isn't same as `spanctxkey{}` in pkg2` – Oleg Butuzov Dec 10 '21 at 13:09
  • Make it exported, then import in pkg2 and try to retrieve again. – Oleg Butuzov Dec 10 '21 at 13:10
  • 1
    "Now, I am trying to get the current running span, **from another package** — unexported structs in different packages are not the same type so you can't use them to look up values in the context – blackgreen Dec 10 '21 at 13:12
  • let me try that guys, I will update you on that,thanks ❤️ – Rajat Singh Dec 10 '21 at 13:12
  • @OlegButuzov thanks, I was able to grab the running span after making the the type globally like you mentioned above, but how can I start a child span out of it?. `runningSpan := ctx.Value(apm.Spanctxkey{}).(**apm.Span)` and my runningSpan is of type `var runningSpan **apm.Span` – Rajat Singh Dec 10 '21 at 13:21

1 Answers1

0

Have you tried to span.StartChild("foobar") ? (I am judging based on sentry sdk)

Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33