Hacker News new | ask | show | jobs
by tdrd 3477 days ago
Cockroach Labs employee here - the implementation only uses a single context key, so traversing the context linked list is only necessary when the context in hand is a derivative of the annotated context (e.g. obtained via WithTimeout or WithCancel), which is relatively uncommon in practice.

https://github.com/cockroachdb/cockroach/blob/6b0698f/pkg/ut...

1 comments

Yeah, I thought about that and it definitely is an improvement, but IMHO it still suffers similar problems. In practice, you won't just have logging; you'll add grpc Metadata, user info and whatnot and each thing will slow down all the other things. Is my intuition. I'm skeptical of saving any data in context, for that reason (and this skepticism has prevented me from finishing my ctx-logging-package, that does exactly what you describe :) ).

I'd just like to see good'ol'benchmarks that this is not a problem, before I trust the Context for data :)

In the majority of cases, the current layer would have added a log tag to the Context, so looking up the log tags structure will be immediate. On the other hand, we also use OpenTracing and we do have to go through the hierarchy to find the current Span, which will be slower with deep hierarchies.

The other thing to consider is that logging a message carries other costs, so we can't go crazy with very frequent log messages anyway. In our case the message usually also makes its way into an OpenTracing span and that involves memory allocation and/or data copying. My guess is that going through a few linked Contexts is not a big deal, comparatively speaking.

I don't have the kind of benchmark that you are looking for (with vs without using Context), but I can tell you that in the workloads we looked at, tracing was taking a few percentage points (1-2%) of CPU usage, and I don't think a significant part of that was in Context code.

Finally, Context is an interface, so if need be, we can make our own Context implementation that always stores the frequently looked-up things (like log tags, tracing Spans) to short-circuit that cost.