userver: userver/ugrpc/impl/statistics.hpp Source File
Loading...
Searching...
No Matches
statistics.hpp
1#pragma once
2
3#include <array>
4#include <atomic>
5#include <chrono>
6#include <cstddef>
7#include <cstdint>
8
9#include <grpcpp/support/status.h>
10
11#include <userver/utils/fixed_array.hpp>
12#include <userver/utils/statistics/fwd.hpp>
13#include <userver/utils/statistics/percentile.hpp>
14#include <userver/utils/statistics/rate_counter.hpp>
15#include <userver/utils/statistics/recentperiod.hpp>
16
17#include <userver/ugrpc/impl/static_metadata.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace ugrpc::impl {
22
23class MethodStatistics final {
24 public:
25 MethodStatistics();
26
27 void AccountStarted() noexcept;
28
29 void AccountStatus(grpc::StatusCode code) noexcept;
30
31 void AccountTiming(std::chrono::milliseconds timing) noexcept;
32
33 // All errors without gRPC status codes are categorized as "network errors".
34 // See server::RpcInterruptedError.
35 void AccountNetworkError() noexcept;
36
37 // Occurs when the service forgot to finish a request, oftentimes due to a
38 // thrown exception. Always indicates a programming error in our service.
39 // UNKNOWN status code is automatically returned in this case.
40 void AccountInternalError() noexcept;
41
42 void AccountCancelledByDeadlinePropagation() noexcept;
43
44 void AccountDeadlinePropagated() noexcept;
45
46 void AccountCancelled() noexcept;
47
48 friend void DumpMetric(utils::statistics::Writer& writer,
49 const MethodStatistics& stats);
50
51 private:
52 using Percentile =
53 utils::statistics::Percentile<2000, std::uint32_t, 256, 100>;
54 using RateCounter = utils::statistics::RateCounter;
55 // StatusCode enum cases have consecutive underlying values, starting from 0.
56 // UNAUTHENTICATED currently has the largest value.
57 static constexpr std::size_t kCodesCount =
58 static_cast<std::size_t>(grpc::StatusCode::UNAUTHENTICATED) + 1;
59
60 RateCounter started_{0};
61 std::array<RateCounter, kCodesCount> status_codes_{};
62 utils::statistics::RecentPeriod<Percentile, Percentile> timings_;
63 RateCounter network_errors_{0};
64 RateCounter internal_errors_{0};
65 RateCounter cancelled_{0};
66
67 RateCounter deadline_updated_{0};
68 RateCounter deadline_cancelled_{0};
69};
70
71class ServiceStatistics final {
72 public:
73 explicit ServiceStatistics(const StaticServiceMetadata& metadata);
74
75 ~ServiceStatistics();
76
77 MethodStatistics& GetMethodStatistics(std::size_t method_id);
78 const MethodStatistics& GetMethodStatistics(std::size_t method_id) const;
79
80 const StaticServiceMetadata& GetMetadata() const;
81
82 friend void DumpMetric(utils::statistics::Writer& writer,
83 const ServiceStatistics& stats);
84
85 private:
86 const StaticServiceMetadata metadata_;
87 utils::FixedArray<MethodStatistics> method_statistics_;
88};
89
90} // namespace ugrpc::impl
91
92USERVER_NAMESPACE_END