userver
C++ Async Framework
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
/
utils
/
any_storage
.
hpp
>
10
11
#
include
<
userver
/
ugrpc
/
server
/
storage_context
.
hpp
>
12
#
include
<
userver
/
utils
/
impl
/
internal_tag_fwd
.
hpp
>
13
14
USERVER_NAMESPACE_BEGIN
15
16
namespace
ugrpc::server {
17
18
namespace
impl {
19
struct
CallState;
20
}
// namespace impl
21
22
class
CallContextBase
{
23
public
:
24
/// @cond
25
// For internal use only.
26
CallContextBase(
utils
::impl::InternalTag, impl::CallState& state);
27
/// @endcond
28
29
CallContextBase(
CallContextBase
&&) =
delete
;
30
CallContextBase
& operator=(
CallContextBase
&&) =
delete
;
31
32
/// @returns the `ServerContext` used for this RPC
33
grpc::ServerContext&
GetServerContext
();
34
35
/// @overload
36
const
grpc::ServerContext&
GetServerContext
()
const
;
37
38
/// @brief Name of the RPC in the format `full.path.ServiceName/MethodName`
39
std::string_view
GetCallName
()
const
;
40
41
/// @brief Get name of gRPC service
42
std::string_view
GetServiceName
()
const
;
43
44
/// @brief Get name of called gRPC method
45
std::string_view
GetMethodName
()
const
;
46
47
/// @brief Get the span of the current RPC
48
tracing
::Span&
GetSpan
();
49
50
/// @brief Returns call context for storing per-call custom data
51
///
52
/// The context can be used to pass data from server middleware to client
53
/// handler or from one middleware to another one.
54
///
55
/// ## Example usage:
56
///
57
/// In authentication middleware:
58
///
59
/// @code
60
/// if (password_is_correct) {
61
/// // Username is authenticated, set it in per-call storage context
62
/// context.GetStorageContext().Emplace(kAuthUsername, username);
63
/// }
64
/// @endcode
65
///
66
/// In client handler:
67
///
68
/// @code
69
/// const auto& username = context.GetStorageContext().Get(kAuthUsername);
70
/// auto msg = fmt::format("Hello, {}!", username);
71
/// @endcode
72
utils
::AnyStorage<
StorageContext
>&
GetStorageContext
();
73
74
protected
:
75
/// @cond
76
// For internal use only.
77
const
impl::CallState& GetCallState(
utils
::impl::InternalTag)
const
{
return
state_; }
78
79
// For internal use only.
80
impl::CallState& GetCallState(
utils
::impl::InternalTag) {
return
state_; }
81
82
// Prevent destruction via pointer to base.
83
~CallContextBase() =
default
;
84
/// @endcond
85
86
private
:
87
impl::CallState& state_;
88
};
89
90
/// @brief gRPC call context
91
class
CallContext
final
:
public
CallContextBase
{
92
public
:
93
/// @cond
94
using
CallContextBase
::CallContextBase;
95
/// @endcond
96
};
97
98
/// @brief generic gRPC call context
99
class
GenericCallContext
final
:
public
CallContextBase
{
100
public
:
101
/// @cond
102
using
CallContextBase
::CallContextBase;
103
/// @endcond
104
105
/// @brief Set a custom call name for metric labels
106
void
SetMetricsCallName
(std::string_view call_name);
107
};
108
109
}
// namespace ugrpc::server
110
111
USERVER_NAMESPACE_END
userver
ugrpc
server
call_context.hpp
Generated on Wed Apr 1 2026 10:43:20 for userver by
Doxygen
1.13.2