Skip to content

Commit 513dfc2

Browse files
author
Otto van der Schaaf
authored
SimpleStatistics: implement serialization roundtrip (envoyproxy#659)
Part of the horizontal scaling effort: `SimpleStatistic` and `StreamingStatistic ` get used for tracking header byte counts, hence we should be able to wire transfer statistics for those to be able to properly aggregate. Signed-off-by: Otto van der Schaaf <[email protected]>
1 parent 9b8c950 commit 513dfc2

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

internal_proto/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Internal proto definitions
2+
3+
Warning: this directory contains proto defininitions which are used internally by Nighthawk, and are subject to change.

internal_proto/statistic/BUILD

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@envoy_api//bazel:api_build_system.bzl", "api_cc_py_proto_library")
2+
3+
licenses(["notice"]) # Apache 2
4+
5+
api_cc_py_proto_library(
6+
name = "statistic",
7+
srcs = [
8+
"statistic.proto",
9+
],
10+
visibility = ["//visibility:public"],
11+
)
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
syntax = "proto3";
2+
3+
package nighthawk.internal;
4+
5+
// This package contains proto definitions which allow serialization / deserialization
6+
// of statistics implementations. Naming maps 1:1 with the statistics implementations
7+
// over at source/common/statistics_impl.h. See the code & doc comments there for further
8+
// information about the corresponding statistics implementations.
9+
10+
message SimpleStatistic {
11+
uint64 count = 1;
12+
string id = 2;
13+
uint64 min = 5;
14+
uint64 max = 6;
15+
double sum_x = 7;
16+
double sum_x_2 = 8;
17+
}
18+
19+
message StreamingStatistic {
20+
uint64 count = 1;
21+
string id = 2;
22+
uint64 min = 5;
23+
uint64 max = 6;
24+
double mean = 7;
25+
double accumulated_variance = 8;
26+
}

source/common/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ envoy_cc_library(
146146
"//api/client:grpc_service_lib",
147147
"//include/nighthawk/client:client_includes",
148148
"//include/nighthawk/common:base_includes",
149+
"//internal_proto/statistic:statistic_cc_proto",
149150
"@com_google_absl//absl/random",
150151
"@com_google_absl//absl/strings",
151152
"@dep_hdrhistogram_c//:hdrhistogram_c",

source/common/statistic_impl.cc

+66
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "external/envoy/source/common/common/assert.h"
1010
#include "external/envoy/source/common/protobuf/utility.h"
1111

12+
#include "internal_proto/statistic/statistic.pb.h"
13+
1214
namespace Nighthawk {
1315

1416
namespace {
@@ -104,6 +106,38 @@ StatisticPtr SimpleStatistic::combine(const Statistic& statistic) const {
104106
return combined;
105107
}
106108

109+
absl::StatusOr<std::unique_ptr<std::istream>> SimpleStatistic::serializeNative() const {
110+
nighthawk::internal::SimpleStatistic proto;
111+
proto.set_id(id());
112+
proto.set_count(count());
113+
proto.set_min(min());
114+
proto.set_max(max());
115+
proto.set_sum_x(sum_x_);
116+
proto.set_sum_x_2(sum_x2_);
117+
118+
std::string tmp;
119+
proto.SerializeToString(&tmp);
120+
auto write_stream = std::make_unique<std::stringstream>();
121+
*write_stream << tmp;
122+
return write_stream;
123+
}
124+
125+
absl::Status SimpleStatistic::deserializeNative(std::istream& stream) {
126+
nighthawk::internal::SimpleStatistic proto;
127+
std::string tmp(std::istreambuf_iterator<char>(stream), {});
128+
if (!proto.ParseFromString(tmp)) {
129+
ENVOY_LOG(error, "Failed to read back SimpleStatistic data.");
130+
return absl::Status(absl::StatusCode::kInternal, "Failed to read back SimpleStatistic data");
131+
}
132+
id_ = proto.id();
133+
count_ = proto.count();
134+
min_ = proto.min();
135+
max_ = proto.max();
136+
sum_x_ = proto.sum_x();
137+
sum_x2_ = proto.sum_x_2();
138+
return absl::OkStatus();
139+
}
140+
107141
void StreamingStatistic::addValue(uint64_t value) {
108142
double delta, delta_n;
109143
StatisticImpl::addValue(value);
@@ -142,6 +176,38 @@ StatisticPtr StreamingStatistic::combine(const Statistic& statistic) const {
142176
return combined;
143177
}
144178

179+
absl::StatusOr<std::unique_ptr<std::istream>> StreamingStatistic::serializeNative() const {
180+
nighthawk::internal::StreamingStatistic proto;
181+
proto.set_id(id());
182+
proto.set_count(count());
183+
proto.set_min(min());
184+
proto.set_max(max());
185+
proto.set_mean(mean_);
186+
proto.set_accumulated_variance(accumulated_variance_);
187+
188+
std::string tmp;
189+
proto.SerializeToString(&tmp);
190+
auto write_stream = std::make_unique<std::stringstream>();
191+
*write_stream << tmp;
192+
return write_stream;
193+
}
194+
195+
absl::Status StreamingStatistic::deserializeNative(std::istream& stream) {
196+
nighthawk::internal::StreamingStatistic proto;
197+
std::string tmp(std::istreambuf_iterator<char>(stream), {});
198+
if (!proto.ParseFromString(tmp)) {
199+
ENVOY_LOG(error, "Failed to read back StreamingStatistic data.");
200+
return absl::Status(absl::StatusCode::kInternal, "Failed to read back StreamingStatistic data");
201+
}
202+
id_ = proto.id();
203+
count_ = proto.count();
204+
min_ = proto.min();
205+
max_ = proto.max();
206+
mean_ = proto.mean();
207+
accumulated_variance_ = proto.accumulated_variance();
208+
return absl::OkStatus();
209+
}
210+
145211
InMemoryStatistic::InMemoryStatistic() : streaming_stats_(std::make_unique<StreamingStatistic>()) {}
146212

147213
void InMemoryStatistic::addValue(uint64_t sample_value) {

source/common/statistic_impl.h

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class SimpleStatistic : public StatisticImpl {
6969
StatisticPtr createNewInstanceOfSameType() const override {
7070
return std::make_unique<SimpleStatistic>();
7171
};
72+
absl::StatusOr<std::unique_ptr<std::istream>> serializeNative() const override;
73+
absl::Status deserializeNative(std::istream&) override;
7274

7375
private:
7476
double sum_x_{0};
@@ -94,6 +96,8 @@ class StreamingStatistic : public StatisticImpl {
9496
StatisticPtr createNewInstanceOfSameType() const override {
9597
return std::make_unique<StreamingStatistic>();
9698
};
99+
absl::StatusOr<std::unique_ptr<std::istream>> serializeNative() const override;
100+
absl::Status deserializeNative(std::istream&) override;
97101

98102
private:
99103
double mean_{0};

0 commit comments

Comments
 (0)