userver: userver/ugrpc/server/call_context.hpp Source File
Loading...
Searching...
No Matches
call_context.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/call_context.hpp
4/// @brief @copybrief ugrpc::server::CallContext
5
6#include <grpcpp/server_context.h>
7
8#include <userver/tracing/span.hpp>
9#include <userver/ugrpc/server/storage_context.hpp>
10#include <userver/utils/any_storage.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace ugrpc::server {
15
16class CallAnyBase;
17
18/// @brief gRPC call context
20public:
21 /// @cond
22 explicit CallContext(CallAnyBase& call);
23 /// @endcond
24
25 /// @returns the `ServerContext` used for this RPC
26 grpc::ServerContext& GetServerContext();
27
28 /// @brief Name of the RPC in the format `full.path.ServiceName/MethodName`
30
31 /// @brief Get name of gRPC service
33
34 /// @brief Get name of called gRPC method
36
37 /// @brief Get the span of the current RPC
38 tracing::Span& GetSpan();
39
40 /// @brief Returns call context for storing per-call custom data
41 ///
42 /// The context can be used to pass data from server middleware to client
43 /// handler or from one middleware to another one.
44 ///
45 /// ## Example usage:
46 ///
47 /// In authentication middleware:
48 ///
49 /// @code
50 /// if (password_is_correct) {
51 /// // Username is authenticated, set it in per-call storage context
52 /// ctx.GetCall().GetStorageContext().Emplace(kAuthUsername, username);
53 /// }
54 /// @endcode
55 ///
56 /// In client handler:
57 ///
58 /// @code
59 /// const auto& username = context.GetStorageContext().Get(kAuthUsername);
60 /// auto msg = fmt::format("Hello, {}!", username);
61 /// @endcode
63
64protected:
65 /// @cond
66 const CallAnyBase& GetCall() const;
67
68 CallAnyBase& GetCall();
69 /// @endcond
70
71private:
72 CallAnyBase& call_;
73};
74
75/// @brief generic gRPC call context
77public:
78 /// @cond
79 using CallContext::CallContext;
80 /// @endcond
81
82 /// @brief Set a custom call name for metric labels
83 void SetMetricsCallName(std::string_view call_name);
84};
85
86} // namespace ugrpc::server
87
88USERVER_NAMESPACE_END