aws-xray-sdk-go icon indicating copy to clipboard operation
aws-xray-sdk-go copied to clipboard

Subroutines

Open cperezortiz opened this issue 3 years ago • 5 comments

Does this sdk support multi threading? I mean when I try to trace calls in go subroutines, I think I'm losing the context, here's an example of my code:

func sendEventsAsync(items []ItemType, ctx context.Context) {
	wg := new(sync.WaitGroup)
	for _, item := range items {
		wg.Add(1)
		go sendEvent(wg, item, ctx)
	}
	wg.Wait()
}

func sendEvent(wg *sync.WaitGroup, item ItemType, ctx context.Context) {
	defer wg.Done()

	subCtx, subseg := xray.BeginSubsegment(ctx, "send-event")
	defer subseg.Close(nil)

        someHttpRequest(subCtx)
        someSqsRequest(subCtx)

       ...
}

My expectation is that I'd see multiple subsegments of "send-event" (I'm sending 4 items in my example), but for some reason the trace console is showing less than that, as you can see in the picture:

Screen Shot 2021-04-02 at 10 21 17 AM

When I do this synchronously (without go subroutines), I can see the tracing for all my iterations (my 4 items) with the same code. However, when I introduce the go subroutines, the issue described is happening.

Again, my believe is that I'm losing the context somehow because of the multi-threaded environment, any suggestions on how I should handle this scenario?

cperezortiz avatar Apr 02 '21 14:04 cperezortiz

Update:

I've been testing this calling the "invoke function" button in the lambda console, and I noticed that (for example) out of X number of calls, let's say 4, it's always the first invocation that shows the breakdown as in the original post. However, the next invocations always show up the breakdown correctly:

Screen Shot 2021-04-05 at 8 13 00 AM

But for some reason, If I wait around 5 minutes until I do a next call, and again, I do like 4 or more invocations, the first one always miss information in the tracing breakdown, whereas the next invocations show up correctly.

What could be the reason for that?

cperezortiz avatar Apr 05 '21 12:04 cperezortiz

Update:

I realized the first invocation has a initialization segment that the subsequent invocations don't show, I thought all lambda functions were idempotent, but looks like that's not necessarily true?

cperezortiz avatar Apr 05 '21 12:04 cperezortiz

Update:

I looked up the documentation on the initialization phase lambdas have. That should be why my first invocation doesn't show my tracing correctly, I wonder If there's a way to make it work even for the invocation that includes the initialization.

cperezortiz avatar Apr 05 '21 15:04 cperezortiz

Hi @cperezortiz ,

I think X-Ray Go SDK does not support tracing async calls. However, you can achieve this manually using X-Ray Go SDK public APIs. You probably have to manually propagate the context in order to see correct representation of your call hierarchy. As per my understanding initialization subsegment means function's initialization code that is run before function handler and it does not have to execute for every invocation so my guess would be that is why you see the initialization subsegment when you first invoke the function and not for the consecutive runs. However, I don't understand what do you mean in this comment - https://github.com/aws/aws-xray-sdk-go/issues/294#issuecomment-813364817 . Can you please explain more about it? Also, small repro code for this function with instructions on how to execute would be easy to understand what is going on here :)

bhautikpip avatar Apr 05 '21 19:04 bhautikpip

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs in next 7 days. Thank you for your contributions.

stale[bot] avatar Jan 09 '22 01:01 stale[bot]