westonpace commented on a change in pull request #11964:
URL: https://github.com/apache/arrow/pull/11964#discussion_r771734507
##########
File path: cpp/src/arrow/util/tracing_internal.h
##########
@@ -97,6 +98,57 @@ AsyncGenerator<T> WrapAsyncGenerator(AsyncGenerator<T>
wrapped,
return fut;
};
}
+
+/// \brief Start a new span for each invocation of a generator.
+///
+/// The parent span of the new span will be the currently active span
+/// (if any) as of when WrapAsyncGenerator was itself called.
+template <typename T>
+AsyncGenerator<T> WrapAsyncGenerator(AsyncGenerator<T> wrapped,
+ const std::string& span_name) {
+ opentelemetry::trace::StartSpanOptions options;
+ options.parent = GetTracer()->GetCurrentSpan()->GetContext();
+ return WrapAsyncGenerator(std::move(wrapped), std::move(options), span_name);
+}
+
+/// \brief End the given span when the given async generator ends.
+///
+/// The span will be made the active span each time the generator is called.
+template <typename T>
+AsyncGenerator<T> TieSpanToAsyncGenerator(
+ AsyncGenerator<T> wrapped,
+ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span) {
+ return [=]() mutable -> Future<T> {
+ auto scope = GetTracer()->WithActiveSpan(span);
+ auto fut = wrapped();
+ fut.AddCallback([span](const Result<T>& result) {
+ if (!result.ok() || IsIterationEnd(*result)) {
+ MarkSpan(result.status(), span.get());
+ span->End();
+ }
+ });
+ return fut;
Review comment:
The order of callback can be undetermined in the event of:
```
Future<> fut = DoThing(); // Future unfinished at this point
fut.AddCallback(...); // Callback actually added
// Future finishes but callbacks haven't run yet...
fut.AddCallback(...); // Runs immediately
```
This has caused several subtle bugs and left me paranoid of `AddCallback`.
That being said, if the ordering truly doesn't matter, then it should be fine
here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]