Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin: order stats in clusters json admin #4306

Merged
merged 4 commits into from
Sep 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/envoy/admin/v2alpha/clusters.proto
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ message HostStatus {
// Address of this host.
envoy.api.v2.core.Address address = 1;

// Mapping from the name of the statistic to the current value.
map<string, SimpleMetric> stats = 2;
// list of stats specific to this host.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: List (to match the surrounding comments)

repeated SimpleMetric stats = 2;

// The host's current health status.
HostHealthStatus health_status = 3;
Expand Down
7 changes: 5 additions & 2 deletions api/envoy/admin/v2alpha/metrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ message SimpleMetric {
GAUGE = 1;
}

// Type of metric represented.
Type type = 1;
// Name of the metric.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kept this in this order so that it comes in that order

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused here; are you saying the textual ordering in the proto spec dictates the wire format or JSON order? I would have thought the field numbers would dictate this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. Some thing wrong. Kept the same order as earlier

string name = 3;

// Current metric value.
uint64 value = 2;

// Type of the metric represented.
Type type = 1;
}
13 changes: 7 additions & 6 deletions source/server/http/admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,18 @@ void AdminImpl::writeClustersAsJson(Buffer::Instance& response) {
envoy::admin::v2alpha::HostStatus& host_status = *cluster_status.add_host_statuses();
Network::Utility::addressToProtobufAddress(*host->address(),
*host_status.mutable_address());

for (const Stats::CounterSharedPtr& counter : host->counters()) {
auto& metric = (*host_status.mutable_stats())[counter->name()];
metric.set_type(envoy::admin::v2alpha::SimpleMetric::COUNTER);
auto& metric = *host_status.add_stats();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the order that you iterate through these stats is dictated by their order in this unordered_map. In what situations would you like to have the ordering stay the same? I think your test could break in the future if the unordered_map internals (like its hash function) change. There are a few solutions here:

  1. Change the std::unordered_map in the isolated store to an std::map everywhere or by specifying the data structure as a template parameter so it could be done in the Host object case only.
  2. Sort the entries when exporting.

metric.set_name(counter->name());
metric.set_value(counter->value());
metric.set_type(envoy::admin::v2alpha::SimpleMetric::COUNTER);
}

for (const Stats::GaugeSharedPtr& gauge : host->gauges()) {
auto& metric = (*host_status.mutable_stats())[gauge->name()];
metric.set_type(envoy::admin::v2alpha::SimpleMetric::GAUGE);
auto& metric = *host_status.add_stats();
metric.set_name(gauge->name());
metric.set_value(gauge->value());
metric.set_type(envoy::admin::v2alpha::SimpleMetric::GAUGE);
}

envoy::admin::v2alpha::HostHealthStatus& health_status =
Expand All @@ -314,7 +315,7 @@ void AdminImpl::writeClustersAsJson(Buffer::Instance& response) {
}
}
}
response.add(MessageUtil::getJsonStringFromMessage(clusters, true)); // pretty-print
response.add(MessageUtil::getJsonStringFromMessage(clusters, true, true)); // pretty-print
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a quick comment on this behavior change? I think if we want to do this, we should probably change it in the /config_dump json output, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I think then, I lean to revert this for consistency.

}

void AdminImpl::writeClustersAsText(Buffer::Instance& response) {
Expand Down
29 changes: 25 additions & 4 deletions test/server/http/admin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,13 @@ TEST_P(AdminInstanceTest, ClustersJson) {
Network::Utility::resolveUrl("tcp://1.2.3.4:80");
ON_CALL(*host, address()).WillByDefault(Return(address));

// Add stats in random order and validate that they come in order.
Stats::IsolatedStoreImpl store;
store.counter("test_counter").add(10);
store.counter("rest_counter").add(10);
store.counter("arest_counter").add(5);
store.gauge("test_gauge").set(11);
store.gauge("atest_gauge").set(10);
ON_CALL(*host, gauges()).WillByDefault(Invoke([&store]() { return store.gauges(); }));
ON_CALL(*host, counters()).WillByDefault(Invoke([&store]() { return store.counters(); }));

Expand Down Expand Up @@ -776,16 +780,33 @@ TEST_P(AdminInstanceTest, ClustersJson) {
"port_value": 80
}
},
"stats": {
"test_counter": {
"stats": [
{
"name": "arest_counter",
"value": "5",
"type": "COUNTER"
},
{
"name": "rest_counter",
mrice32 marked this conversation as resolved.
Show resolved Hide resolved
"value": "10",
"type": "COUNTER"
},
{
"name": "test_counter",
"value": "10",
"type": "COUNTER"
},
"test_gauge": {
{
"name": "atest_gauge",
"value": "10",
"type": "GAUGE"
},
{
"name": "test_gauge",
"value": "11",
"type": "GAUGE"
},
},
],
"health_status": {
"eds_health_status": "HEALTHY",
"failed_active_health_check": true,
Expand Down