userver: userver/tracing/scope_time.hpp Source File
Loading...
Searching...
No Matches
scope_time.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/tracing/scope_time.hpp
4/// @brief @copybrief tracing::ScopeTime
5
6#include <atomic>
7#include <chrono>
8#include <optional>
9#include <string>
10
11#include <userver/logging/log_extra.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace tracing {
16
17namespace impl {
18
19class TimeStorage;
20
21} // namespace impl
22
23/// @brief Type to measure execution time of a scope
24///
25/// Use tracing::Span::CreateScopeTime() to construct
26class ScopeTime {
27public:
28 using Duration = std::chrono::nanoseconds;
29 using DurationMillis = std::chrono::duration<double, std::milli>;
30
31 /// @brief Creates a tracing::ScopeTime attached to
32 /// tracing::Span::CurrentSpan().
33 ///
34 /// Equivalent to tracing::Span::CurrentSpan().CreateScopeTime()
36
37 /// @brief Creates a tracing::ScopeTime attached to
38 /// tracing::Span::CurrentSpan() and starts measuring execution time.
39 /// Tag `{scope_name}_time` with elapsed time is added to result span.
40 ///
41 /// Equivalent to tracing::Span::CurrentSpan().CreateScopeTime(scope_name)
42 ///
43 /// @note `scope_name` parameter is expected to satisfy snake case.
44 /// Otherwise, it is converted to snake case.
45 explicit ScopeTime(std::string scope_name);
46
47 /// @brief If there exists a tracing::Span::CurrentSpan(),
48 /// Creates a tracing::ScopeTime attached to that Span,
49 /// otherwise return std::nullopt.
50 static std::optional<ScopeTime> CreateOptionalScopeTime();
51
52 /// @brief If there exists a tracing::Span::CurrentSpan(),
53 /// Creates a tracing::ScopeTime attached to that Span and starts measuring
54 /// execution time, otherwise return std::nullopt.
55 static std::optional<ScopeTime> CreateOptionalScopeTime(std::string_view name);
56
57 /// @cond
58 // Constructors for internal use
59 explicit ScopeTime(impl::TimeStorage& ts);
60 ScopeTime(impl::TimeStorage& ts, std::string scope_name);
61 /// @endcond
62
63 ScopeTime(const ScopeTime&) = delete;
64 ScopeTime(ScopeTime&&) = default;
65 ~ScopeTime();
66
67 /// Records the current scope time if the name is set, and stops the timer
68 Duration Reset();
69
70 /// Records the current scope time if the name is set, and starts a new one
71 Duration Reset(std::string scope_name);
72
73 /// Stops the timer without recording its value
74 void Discard();
75
76 /// Returns time elapsed since last reset
77 /// Will return 0 if the timer is stopped
78 Duration DurationSinceReset() const;
79
80 /// Returns total time elapsed for a certain scope. If there is no record for
81 /// the scope, returns 0
82 Duration DurationTotal(const std::string& scope_name) const;
83
84 /// Returns total time elapsed for current scope
85 /// Will return 0 if the timer is stopped
86 Duration DurationTotal() const;
87
88 /// Returns time elapsed since last reset, returns 0 if the timer is stopped.
89 ///
90 /// Prefer using ScopeTime::DurationSinceReset()
91 DurationMillis ElapsedSinceReset() const;
92
93 /// Returns total time elapsed for a certain scope. If there is no record for
94 /// the scope, returns 0.
95 ///
96 /// Prefer using ScopeTime::DurationTotal()
97 DurationMillis ElapsedTotal(const std::string& scope_name) const;
98
99 /// Returns total time elapsed for current scope
100 /// Will return 0 if the timer is stopped.
101 ///
102 /// Prefer using ScopeTime::DurationTotal()
103 DurationMillis ElapsedTotal() const;
104
105 const std::string& CurrentScope() const { return scope_name_; }
106
107private:
108 impl::TimeStorage& ts_;
109 std::chrono::steady_clock::time_point start_;
110 std::string scope_name_;
111};
112
113} // namespace tracing
114
115USERVER_NAMESPACE_END