Skip to content

Commit

Permalink
Control normalization in network introspection API (#1693)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovalenti authored Jun 12, 2024
1 parent 34ad226 commit 6d37f3f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
15 changes: 13 additions & 2 deletions collector/lib/NetworkStatusInspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const std::string NetworkStatusInspector::kEndpointRoute = kBaseRoute + "/endpoi
const std::string NetworkStatusInspector::kConnectionRoute = kBaseRoute + "/connection";

const std::string NetworkStatusInspector::kQueryParam_container = "container";
const std::string NetworkStatusInspector::kQueryParam_normalize = "normalize";

NetworkStatusInspector::NetworkStatusInspector(const std::shared_ptr<ConnectionTracker> conntracker) : conntracker_(conntracker) {
}
Expand Down Expand Up @@ -60,8 +61,18 @@ static void SerializeEndpoint(const Endpoint& ep, Json::Value& node) {
node["port"] = ep.port();
}

bool NetworkStatusInspector::shouldNormalize(const QueryParams& query_params) const {
std::optional<std::string> normalize_flag = GetParameter(query_params, kQueryParam_normalize);

if (!normalize_flag) {
return true;
}

return *normalize_flag == "true";
}

bool NetworkStatusInspector::handleGetEndpoints(struct mg_connection* conn, const QueryParams& query_params) {
collector::AdvertisedEndpointMap endpoint_states = conntracker_->FetchEndpointState(true, false);
collector::AdvertisedEndpointMap endpoint_states = conntracker_->FetchEndpointState(shouldNormalize(query_params), false);
Json::Value body_root(Json::objectValue);

std::unordered_map<std::string, std::forward_list<collector::ContainerEndpointMap::value_type*>> by_container;
Expand Down Expand Up @@ -108,7 +119,7 @@ bool NetworkStatusInspector::handleGetEndpoints(struct mg_connection* conn, cons
}

bool NetworkStatusInspector::handleGetConnections(struct mg_connection* conn, const QueryParams& query_params) {
collector::ConnMap connection_states = conntracker_->FetchConnState(true, false);
collector::ConnMap connection_states = conntracker_->FetchConnState(shouldNormalize(query_params), false);
Json::Value body_root(Json::objectValue);

std::unordered_map<std::string, std::forward_list<collector::ConnMap::value_type*>> by_container;
Expand Down
5 changes: 3 additions & 2 deletions collector/lib/NetworkStatusInspector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#define COLLECTOR_NETWORKSTATUSINSPECTOR_H

#include <memory>
#include <optional>
#include <unordered_map>

#include <json/writer.h>

Expand All @@ -29,6 +27,9 @@ class NetworkStatusInspector : public IntrospectionEndpoint {
Json::FastWriter writer_;

static const std::string kQueryParam_container; // = "container"
static const std::string kQueryParam_normalize; // = "normalize"

bool shouldNormalize(const QueryParams& queryParams) const;

bool handleGetEndpoints(struct mg_connection* conn, const QueryParams& queryParams);
bool handleGetConnections(struct mg_connection* conn, const QueryParams& queryParams);
Expand Down
8 changes: 6 additions & 2 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,14 @@ Afterglow is not applied to the data returned by this API.
It is possible to filter the items returned per container_id by providing
a query parameter: `container=<container_id>`

Example of connection query, limited to container with identifier `c6f030bc4b42`:
By default, connections and endpoints are normalized in the result. It is
possible to disable normalization with a query parameter: `normalize=false`

Example of connection query, limited to container with identifier `c6f030bc4b42`
and without normalization :

```
$ curl "http://<collector>:8080/state/network/connection?container=c6f030bc4b42"
$ curl "http://<collector>:8080/state/network/connection?container=c6f030bc4b42&normalize=false"
{
"c6f030bc4b42" :
[
Expand Down

0 comments on commit 6d37f3f

Please sign in to comment.