Skip to content

Commit 2fe5b8a

Browse files
troshko111mattklein123
authored andcommitted
stats: add unit support to histogram (envoyproxy#8484)
Signed-off-by: Taras Roshko <[email protected]>
1 parent 55ab495 commit 2fe5b8a

File tree

77 files changed

+770
-364
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+770
-364
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ BROWSE
1111
compile_commands.json
1212
cscope.*
1313
.deps
14+
.devcontainer.json
1415
/docs/landing_source/.bundle
1516
/generated
1617
.idea/

docs/root/intro/version_history.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Version history
8282
* server: added :ref:`per-handler listener stats <config_listener_stats_per_handler>` and
8383
:ref:`per-worker watchdog stats <operations_performance_watchdog>` to help diagnosing event
8484
loop imbalance and general performance issues.
85+
* stats: added unit support to histogram.
8586
* thrift_proxy: fix crashing bug on invalid transport/protocol framing
8687
* tls: added verification of IP address SAN fields in certificates against configured SANs in the
8788
* tracing: added support to the Zipkin reporter for sending list of spans as Zipkin JSON v2 and protobuf message over HTTP.

include/envoy/event/dispatcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ namespace Event {
3030
*/
3131
// clang-format off
3232
#define ALL_DISPATCHER_STATS(HISTOGRAM) \
33-
HISTOGRAM(loop_duration_us) \
34-
HISTOGRAM(poll_delay_us)
33+
HISTOGRAM(loop_duration_us, Microseconds) \
34+
HISTOGRAM(poll_delay_us, Microseconds)
3535
// clang-format on
3636

3737
/**

include/envoy/stats/BUILD

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@ envoy_cc_library(
5656
)
5757

5858
envoy_cc_library(
59-
name = "timespan",
59+
name = "timespan_interface",
6060
hdrs = ["timespan.h"],
61-
deps = [
62-
":stats_interface",
63-
"//include/envoy/common:time_interface",
64-
],
6561
)
6662

6763
envoy_cc_library(

include/envoy/stats/histogram.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,37 @@ class HistogramStatistics {
6767

6868
/**
6969
* A histogram that records values one at a time.
70-
* Note: Histograms now incorporate what used to be timers because the only difference between the
71-
* two stat types was the units being represented. It is assumed that no downstream user of this
72-
* class (Sinks, in particular) will need to explicitly differentiate between histograms
73-
* representing durations and histograms representing other types of data.
70+
* Note: Histograms now incorporate what used to be timers because the only
71+
* difference between the two stat types was the units being represented.
7472
*/
7573
class Histogram : public Metric {
7674
public:
75+
/**
76+
* Histogram values represent scalar quantity like time, length, mass,
77+
* distance, or in general anything which has only magnitude and no other
78+
* characteristics. These are often accompanied by a unit of measurement.
79+
* This enum defines units for commonly measured quantities. Base units
80+
* are preferred unless they are not granular enough to be useful as an
81+
* integer.
82+
*/
83+
enum class Unit {
84+
Null, // The histogram has been rejected, i.e. it's a null histogram and is not recording
85+
// anything.
86+
Unspecified, // Measured quantity does not require a unit, e.g. "items".
87+
Bytes,
88+
Microseconds,
89+
Milliseconds,
90+
};
91+
7792
~Histogram() override = default;
7893

7994
/**
80-
* Records an unsigned value. If a timer, values are in units of milliseconds.
95+
* @return the unit of measurement for values recorded by the histogram.
96+
*/
97+
virtual Unit unit() const PURE;
98+
99+
/**
100+
* Records an unsigned value in the unit specified during the construction.
81101
*/
82102
virtual void recordValue(uint64_t value) PURE;
83103
};

include/envoy/stats/scope.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ class Scope {
8181

8282
/**
8383
* @param name The name of the stat, obtained from the SymbolTable.
84+
* @param unit The unit of measurement.
8485
* @return a histogram within the scope's namespace with a particular value type.
8586
*/
86-
virtual Histogram& histogramFromStatName(StatName name) PURE;
87+
virtual Histogram& histogramFromStatName(StatName name, Histogram::Unit unit) PURE;
8788

8889
/**
8990
* TODO(#6667): this variant is deprecated: use histogramFromStatName.
9091
* @param name The name, expressed as a string.
92+
* @param unit The unit of measurement.
9193
* @return a histogram within the scope's namespace with a particular value type.
9294
*/
93-
virtual Histogram& histogram(const std::string& name) PURE;
95+
virtual Histogram& histogram(const std::string& name, Histogram::Unit unit) PURE;
9496

9597
/**
9698
* @param The name of the stat, obtained from the SymbolTable.

include/envoy/stats/stats_macros.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Envoy {
1717
* #define MY_COOL_STATS(COUNTER, GAUGE, HISTOGRAM) \
1818
* COUNTER(counter1) \
1919
* GAUGE(gauge1, mode) \
20-
* HISTOGRAM(histogram1)
20+
* HISTOGRAM(histogram1, unit)
2121
* ...
2222
*
2323
* By convention, starting with #7083, we sort the lines of this macro block, so
@@ -38,10 +38,11 @@ namespace Envoy {
3838
// Fully-qualified for use in external callsites.
3939
#define GENERATE_COUNTER_STRUCT(NAME) Envoy::Stats::Counter& NAME##_;
4040
#define GENERATE_GAUGE_STRUCT(NAME, MODE) Envoy::Stats::Gauge& NAME##_;
41-
#define GENERATE_HISTOGRAM_STRUCT(NAME) Envoy::Stats::Histogram& NAME##_;
41+
#define GENERATE_HISTOGRAM_STRUCT(NAME, UNIT) Envoy::Stats::Histogram& NAME##_;
4242

4343
#define FINISH_STAT_DECL_(X) #X)),
4444
#define FINISH_STAT_DECL_MODE_(X, MODE) #X), Envoy::Stats::Gauge::ImportMode::MODE),
45+
#define FINISH_STAT_DECL_UNIT_(X, UNIT) #X), Envoy::Stats::Histogram::Unit::UNIT),
4546

4647
static inline std::string statPrefixJoin(absl::string_view prefix, absl::string_view token) {
4748
if (prefix.empty()) {
@@ -55,7 +56,7 @@ static inline std::string statPrefixJoin(absl::string_view prefix, absl::string_
5556

5657
#define POOL_COUNTER_PREFIX(POOL, PREFIX) (POOL).counter(Envoy::statPrefixJoin(PREFIX, FINISH_STAT_DECL_
5758
#define POOL_GAUGE_PREFIX(POOL, PREFIX) (POOL).gauge(Envoy::statPrefixJoin(PREFIX, FINISH_STAT_DECL_MODE_
58-
#define POOL_HISTOGRAM_PREFIX(POOL, PREFIX) (POOL).histogram(Envoy::statPrefixJoin(PREFIX, FINISH_STAT_DECL_
59+
#define POOL_HISTOGRAM_PREFIX(POOL, PREFIX) (POOL).histogram(Envoy::statPrefixJoin(PREFIX, FINISH_STAT_DECL_UNIT_
5960

6061
#define POOL_COUNTER(POOL) POOL_COUNTER_PREFIX(POOL, "")
6162
#define POOL_GAUGE(POOL) POOL_GAUGE_PREFIX(POOL, "")

include/envoy/stats/timespan.h

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#include <chrono>
44
#include <memory>
55

6-
#include "envoy/common/time.h"
7-
#include "envoy/stats/histogram.h"
8-
#include "envoy/stats/stats.h"
6+
#include "envoy/common/pure.h"
97

108
namespace Envoy {
119
namespace Stats {
@@ -18,43 +16,18 @@ class CompletableTimespan {
1816
virtual ~CompletableTimespan() = default;
1917

2018
/**
21-
* Complete the timespan.
22-
*/
23-
virtual void complete() PURE;
24-
};
25-
26-
/**
27-
* An individual timespan that flushes its measured value in time unit (e.g
28-
* std::chrono::milliseconds). The initial time is captured on construction. A timespan must be
29-
* completed via complete() for it to be stored. If the timespan is deleted this will be treated as
30-
* a cancellation.
31-
*/
32-
template <class TimeUnit> class TimespanWithUnit : public CompletableTimespan {
33-
public:
34-
TimespanWithUnit(Histogram& histogram, TimeSource& time_source)
35-
: time_source_(time_source), histogram_(histogram), start_(time_source.monotonicTime()) {}
36-
37-
/**
38-
* Complete the timespan and send the time to the histogram.
19+
* Time elapsed since the creation of the timespan.
3920
*/
40-
void complete() override { histogram_.recordValue(getRawDuration().count()); }
21+
virtual std::chrono::milliseconds elapsed() const PURE;
4122

4223
/**
43-
* Get duration in the time unit since the creation of the span.
24+
* Complete the timespan.
4425
*/
45-
TimeUnit getRawDuration() {
46-
return std::chrono::duration_cast<TimeUnit>(time_source_.monotonicTime() - start_);
47-
}
48-
49-
private:
50-
TimeSource& time_source_;
51-
Histogram& histogram_;
52-
const MonotonicTime start_;
26+
virtual void complete() PURE;
5327
};
5428

55-
using Timespan = TimespanWithUnit<std::chrono::milliseconds>;
29+
using Timespan = CompletableTimespan;
5630
using TimespanPtr = std::unique_ptr<Timespan>;
57-
using CompletableTimespanPtr = std::unique_ptr<CompletableTimespan>;
5831

5932
} // namespace Stats
6033
} // namespace Envoy

include/envoy/upstream/upstream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ class PrioritySet {
586586
GAUGE(upstream_rq_active, Accumulate) \
587587
GAUGE(upstream_rq_pending_active, Accumulate) \
588588
GAUGE(version, NeverImport) \
589-
HISTOGRAM(upstream_cx_connect_ms) \
590-
HISTOGRAM(upstream_cx_length_ms)
589+
HISTOGRAM(upstream_cx_connect_ms, Milliseconds) \
590+
HISTOGRAM(upstream_cx_length_ms, Milliseconds)
591591

592592
/**
593593
* All cluster load report stats. These are only use for EDS load reporting and not sent to the

source/common/http/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ envoy_cc_library(
169169
"//include/envoy/ssl:connection_interface",
170170
"//include/envoy/stats:stats_interface",
171171
"//include/envoy/stats:stats_macros",
172-
"//include/envoy/stats:timespan",
172+
"//include/envoy/stats:timespan_interface",
173173
"//include/envoy/upstream:upstream_interface",
174174
"//source/common/access_log:access_log_formatter_lib",
175175
"//source/common/buffer:buffer_lib",
@@ -186,6 +186,7 @@ envoy_cc_library(
186186
"//source/common/network:utility_lib",
187187
"//source/common/router:config_lib",
188188
"//source/common/runtime:uuid_util_lib",
189+
"//source/common/stats:timespan_lib",
189190
"//source/common/stream_info:stream_info_lib",
190191
"//source/common/tracing:http_tracer_lib",
191192
],
@@ -289,7 +290,7 @@ envoy_cc_library(
289290
"//include/envoy/network:connection_interface",
290291
"//include/envoy/stats:stats_interface",
291292
"//include/envoy/stats:stats_macros",
292-
"//include/envoy/stats:timespan",
293+
"//include/envoy/stats:timespan_interface",
293294
],
294295
)
295296

0 commit comments

Comments
 (0)