userver: userver/ugrpc/impl/statistics_scope.hpp Source File
Loading...
Searching...
No Matches
statistics_scope.hpp
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <optional>
6
7#include <grpcpp/support/status.h>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace ugrpc::impl {
12
13class MethodStatistics;
14
15class RpcStatisticsScope final {
16 public:
17 explicit RpcStatisticsScope(MethodStatistics& statistics);
18
19 ~RpcStatisticsScope();
20
21 void OnExplicitFinish(grpc::StatusCode code);
22
23 void OnCancelledByDeadlinePropagation();
24
25 void OnDeadlinePropagated();
26
27 void OnCancelled();
28
29 void OnNetworkError();
30
31 void Flush();
32
33 private:
34 // Represents how the RPC was finished. Kinds with higher numeric values
35 // override those with lower ones.
36 enum class FinishKind {
37 // The user didn't finish the RPC explicitly (sometimes due to an
38 // exception), which indicates an internal service error
39 kAutomatic = 0,
40
41 // The user has finished the RPC (with some status code)
42 kExplicit = 1,
43
44 // A network error occurred (RpcInterruptedError)
45 kNetworkError = 2,
46
47 // Closed by deadline propagation
48 kDeadlinePropagation = 3,
49
50 // Task was cancelled
51 kCancelled = 4,
52 };
53
54 void AccountTiming();
55
56 std::atomic<bool> is_cancelled_{false};
57 MethodStatistics& statistics_;
58 std::optional<std::chrono::steady_clock::time_point> start_time_;
59 FinishKind finish_kind_{FinishKind::kAutomatic};
60 grpc::StatusCode finish_code_{};
61};
62
63} // namespace ugrpc::impl
64
65USERVER_NAMESPACE_END