Viam C++ SDK current
Loading...
Searching...
No Matches
span_guard.hpp
1#pragma once
2
3#include <exception>
4
5#include <grpcpp/support/status.h>
6
7#include <viam/sdk/common/grpc_fwd.hpp>
8
9#ifdef VIAMCPPSDK_OPENTELEMETRY_TRACING
10#include <opentelemetry/trace/scope.h>
11#include <opentelemetry/trace/span.h>
12#endif
13
14namespace viam {
15namespace sdk {
16namespace impl {
17
28 public:
29 explicit ServerSpanGuard(const GrpcServerContext* ctx, const char* method) noexcept;
30 ~ServerSpanGuard() noexcept; // NOLINT(performance-trivially-destructible)
31
33 ::grpc::Status commit(::grpc::Status status) noexcept;
34
36 void record_exception(const std::exception& xcp) noexcept;
37
39 void record_unknown_exception() noexcept;
40
41 ServerSpanGuard(const ServerSpanGuard&) = delete;
42 ServerSpanGuard& operator=(const ServerSpanGuard&) = delete;
43
44#ifdef VIAMCPPSDK_OPENTELEMETRY_TRACING
45 private:
46 // Builds the span and makes it active; runs before `scope_` in the constructor init list.
47 static opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> start_span_(
48 const GrpcServerContext* ctx, const char* method) noexcept;
49
50 opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span_;
51 opentelemetry::trace::Scope scope_;
52 bool committed_ = false;
53#endif
54};
55
62void inject_trace_context(GrpcClientContext* ctx) noexcept;
63
64#ifdef VIAMCPPSDK_OPENTELEMETRY_TRACING
66void record_exception(opentelemetry::trace::Span* span, const std::exception& xcp) noexcept;
67
70void record_unknown_exception(opentelemetry::trace::Span* span) noexcept;
71#endif
72
73} // namespace impl
74} // namespace sdk
75} // namespace viam
RAII guard that creates a server-side OpenTelemetry span for the duration of a gRPC handler invocatio...
Definition span_guard.hpp:27
void record_unknown_exception() noexcept
Record an unknown (non-std::exception) failure. Caller rethrows.
::grpc::Status commit(::grpc::Status status) noexcept
Record the final gRPC status before destruction and return it unchanged.
void record_exception(const std::exception &xcp) noexcept
Record xcp as an "exception" event and set span status to Error. Caller rethrows.