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 std::uint64_t GetStarted() const noexcept;
52
53 private:
54 using Percentile =
55 utils::statistics::Percentile<2000, std::uint32_t, 256, 100>;
56 using RateCounter = utils::statistics::RateCounter;
57 // StatusCode enum cases have consecutive underlying values, starting from 0.
58 // UNAUTHENTICATED currently has the largest value.
59 static constexpr std::size_t kCodesCount =
60 static_cast<std::size_t>(grpc::StatusCode::UNAUTHENTICATED) + 1;
61
62 RateCounter started_{0};
63 std::array<RateCounter, kCodesCount> status_codes_{};
64 utils::statistics::RecentPeriod<Percentile, Percentile> timings_;
65 RateCounter network_errors_{0};
66 RateCounter internal_errors_{0};
67 RateCounter cancelled_{0};
68
69 RateCounter deadline_updated_{0};
70 RateCounter deadline_cancelled_{0};
71};
72
73class ServiceStatistics final {
74 public:
75 explicit ServiceStatistics(const StaticServiceMetadata& metadata);
76
77 ~ServiceStatistics();
78
79 MethodStatistics& GetMethodStatistics(std::size_t method_id);
80 const MethodStatistics& GetMethodStatistics(std::size_t method_id) const;
81
82 const StaticServiceMetadata& GetMetadata() const;
83
84 std::uint64_t GetStartedRequests() const;
85
86 friend void DumpMetric(utils::statistics::Writer& writer,
87 const ServiceStatistics& stats);
88
89 private:
90 const StaticServiceMetadata metadata_;
91 utils::FixedArray<MethodStatistics> method_statistics_;
92};
93
94} // namespace ugrpc::impl
95
96USERVER_NAMESPACE_END