joosthooz commented on a change in pull request #12609:
URL: https://github.com/apache/arrow/pull/12609#discussion_r840655386
##########
File path: cpp/src/arrow/util/tracing_internal.h
##########
@@ -101,6 +102,69 @@ AsyncGenerator<T> WrapAsyncGenerator(AsyncGenerator<T>
wrapped,
};
}
+/// \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);
+ return wrapped().Then(
+ [span](const T& result) -> Result<T> {
+ span->SetStatus(opentelemetry::trace::StatusCode::kOk);
+ span->End();
+ return result;
+ },
+ [span](const Status& status) -> Result<T> {
+ MarkSpan(status, span.get());
+ span->End();
+ return status;
+ });
+ };
+}
+
+/// \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) {
+ auto span = GetTracer()->GetCurrentSpan();
+ return TieSpanToAsyncGenerator(wrapped, span);
+}
+
+/// \brief Activate the given span on each invocation of an async generator.
+template <typename T>
+AsyncGenerator<T> PropagateSpanThroughAsyncGenerator(
+ AsyncGenerator<T> wrapped,
+ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span) {
+ return [=]() mutable -> Future<T> {
+ auto scope = GetTracer()->WithActiveSpan(span);
+ return wrapped();
+ };
+}
+
+/// \brief Activate the given span on each invocation of an async generator.
+template <typename T>
+AsyncGenerator<T> PropagateSpanThroughAsyncGenerator(AsyncGenerator<T>
wrapped) {
+ auto span = GetTracer()->GetCurrentSpan();
+ if (!span->GetContext().IsValid()) return wrapped;
+ return PropagateSpanThroughAsyncGenerator(std::move(wrapped),
std::move(span));
+}
+
Review comment:
Agreed; they are so very similar that I've merged 2 of them into 1. The
behavior is then switched with an argument. I'd love to hear your opinion on
that: 71f6989103731be357656f2f9f028130f483adb4
I've also written an addition comment block.
--
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]