diff --git a/README.md b/README.md index efabd5375..37bd8c71b 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,58 @@ visor: type: dns ``` +#### Policy attributes + +A policy can have an `attributes` mapping, next to `kind`, `input`, `handlers` and `config`. Every series of every handler +of the policy carries these attributes, in the OpenTelemetry (OTLP) push and in the Prometheus output. The JSON output +does not carry them. + +```yaml +visor: + policies: + mysocket: + kind: collection + attributes: + netbox.site.name: dc-west + netbox.tenant.name: acme + netbox_id: "007" + input: + tap: unix_dnstap + input_type: dnstap + handlers: + modules: + default_dns: + type: dns +``` + +* A value is a scalar, kept as the text written: `"007"` stays `007`. Quote a value whose exact text matters. A map, a list, a + null or an empty value is refused. The one exception is a backslash followed by three digits, which is rewritten as `\092` + and those digits (`C:\123` becomes `C:\092123`), as in every label value pktvisor exports. +* A key starts with an ASCII letter and is valid UTF-8; an empty key, or one that starts with a digit, an underscore or any + other character (`__name__` included), is refused. A null `attributes` is the same as none. +* OTLP carries each key as written. Prometheus writes each key with every character outside `[A-Za-z0-9_]` replaced by `_`, + one `_` per character, so `netbox.site.name` becomes `netbox_site_name` and `sité` becomes `sit_`. +* The policy does not load if two keys have the same Prometheus name (`netbox.site.name` and `netbox_site_name`). +* The policy does not load if a key is, or becomes after that conversion, a label pktvisor sets on the policy's series: `policy`, + `handler`, `tap`, `le`, `quantile`, `instance` when `--prom-instance` (or `prom_instance` in the configuration file) is set, or a label of one of the policy's handlers. + The labels of each handler are: + + | Handler | Own labels | + |---|---| + | `net` v1 | `lat`, `lon`, `geo_loc`, `asn`, `ipv4`, `ipv6` | + | `net` v2 | `direction`, `lat`, `lon`, `geo_loc`, `asn`, `ipv4`, `ipv6` | + | `dns` v1 | `lat`, `lon`, `geo_loc`, `asn`, `ecs`, `qname`, `port`, `qtype`, `rcode` | + | `dns` v2 | `direction`, `lat`, `lon`, `geo_loc`, `asn`, `ecs`, `qname`, `port`, `qtype`, `rcode` | + | `dhcp` | `client`, `server` | + | `flow` | `device`, `device_interface`, `lat`, `lon`, `conversation`, `geo_loc`, `asn`, `ip`, `port`, `ip_port`, `dscp`, `ecn`, `interface` | + | `netprobe` | `target`, `status_code`, `rcode` | + | `pcap`, `bgp`, `input_resources` | none | + +* The `netprobe` handler also takes attributes per target, see its [README](src/handlers/netprobe/README.md). +* A pktvisor version without this feature ignores the policy's own `attributes` and runs the policy without them. It reads + per-target `attributes` as input config: an all-digit value past 2^64 makes it refuse the policy, and policies that differ + only in their target attributes get separate inputs. + If running in a Docker container, you must mount the configuration file into the container. For example, if the configuration file is on the host at `/local/pktvisor/agent.yaml`, you can mount it into the container and use it with this command: diff --git a/RFCs/2021-04-16-76-collection-policies.md b/RFCs/2021-04-16-76-collection-policies.md index ed756a674..dbf6d4ef0 100644 --- a/RFCs/2021-04-16-76-collection-policies.md +++ b/RFCs/2021-04-16-76-collection-policies.md @@ -16,6 +16,28 @@ explicit support for the handler type upstream to them, otherwise the policy wil Policies require a `kind` to indicate the type of policy being applied. +A policy may also have an `attributes` mapping at the top level, next to `kind`, `input`, `handlers` and `config`. Each +entry is added as a label (an attribute, in OTLP) to every series of every handler of the policy, in the OTLP push and in +the Prometheus output: + +```yaml + anycast_dns: + kind: collection + attributes: + netbox.site.name: dc-west + netbox_id: "007" +``` + +Values are scalars kept as the text written (a backslash followed by three digits is rewritten, as in every exported label +value). A map, list, null or empty value is refused. A key starts with an ASCII letter and is valid UTF-8; a null +`attributes` is none. OTLP uses the keys as written; Prometheus replaces every character outside `[A-Za-z0-9_]` in a key +with `_`, one per character. A policy does not load when two keys share a +Prometheus name, or when a key is, or becomes, a label pktvisor sets itself (`policy`, `handler`, `tap`, `le`, `quantile`, +`instance` with `--prom-instance` or `prom_instance`, and the labels of the policy's handlers). The `netprobe` handler also accepts +`attributes` per target under `input.config.targets..attributes`; see its README. A pktvisor that does not know +`attributes` ignores the policy's own, but reads per-target attributes as input config: an all-digit value past 2^64 makes it +refuse the policy, and policies that differ only in their target attributes get separate inputs. + NOTE: this is not yet a complete, formal specification, and not all of it is implemented. See src/tests/test_policies.cpp for current, tested implementation. diff --git a/src/AbstractMetricsManager.h b/src/AbstractMetricsManager.h index d62b24d9b..f3215cdcb 100644 --- a/src/AbstractMetricsManager.h +++ b/src/AbstractMetricsManager.h @@ -295,6 +295,7 @@ class AbstractMetricsManager _metric_buckets[0]->configure_groups(_groups); _metric_buckets[0]->set_start_tstamp(stamp); _metric_buckets[0]->update_topn_metrics(_topn_count, _topn_percentile_threshold); + on_new_bucket(*_metric_buckets[0]); if (_recorded_stream) { _metric_buckets[0]->set_recorded_stream(); } @@ -391,6 +392,14 @@ class AbstractMetricsManager { } + /** + * call back for the live bucket a period shift opens, made under the bucket lock before any event or export reaches + * it. The first bucket is made in the constructor, before a derived class exists, so this is not called for it. + */ + virtual void on_new_bucket([[maybe_unused]] MetricsBucketClass &bucket) + { + } + public: AbstractMetricsManager(const Configurable *window_config) : _metric_buckets{} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4b2124f6e..50fc2f02e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,6 +24,7 @@ add_library(visor-core CoreRegistry.cpp Metrics.cpp Policies.cpp + PolicyAttributes.cpp ThreadName.cpp IpPort.cpp Taps.cpp) @@ -89,6 +90,7 @@ add_executable(unit-tests-visor-core tests/test_ipport.cpp tests/test_taps.cpp tests/test_policies.cpp + tests/test_policy_attributes.cpp tests/test_handlers.cpp tests/test_module_plugins.cpp tests/test_prometheus_serializer.cpp diff --git a/src/CoreServer.cpp b/src/CoreServer.cpp index 60313f3c3..8035772f1 100644 --- a/src/CoreServer.cpp +++ b/src/CoreServer.cpp @@ -376,11 +376,14 @@ void get_default_prometheus(PolicyManager &policies, httplib::Response &res) return; } auto logger = spdlog::get("visor"); + // the legacy shape: the policy's attributes and policy, no handler label + auto labels = policy->attributes(); + labels["policy"] = "default"; for (auto &mod : policy->modules()) { auto hmod = dynamic_cast(mod); if (hmod) { spdlog::stopwatch sw; - hmod->window_prometheus(ser, {{"policy", "default"}}); + hmod->window_prometheus(ser, labels); logger->debug("{} window_prometheus elapsed time: {}", hmod->name(), sw); } } diff --git a/src/MetricLabels.h b/src/MetricLabels.h index f7dd55b8c..188bf7482 100644 --- a/src/MetricLabels.h +++ b/src/MetricLabels.h @@ -22,6 +22,28 @@ bool is_utf8(std::string_view value); bool label_value_needs_escape(std::string_view value); std::string utf8_label_value(std::string_view value); +// The Prometheus label name of a label key: every character outside [A-Za-z0-9_] becomes _, one _ per +// code point (the bytes after the first of a UTF-8 sequence write nothing), as the OTLP to Prometheus +// translators do. append_prometheus_label_name writes it onto `out` without a temporary string. +inline void append_prometheus_label_name(std::string &out, std::string_view key) +{ + for (unsigned char c : key) { + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_') { + out.push_back(static_cast(c)); + } else if ((c & 0xC0) != 0x80) { + out.push_back('_'); + } + } +} + +inline std::string prometheus_label_name(std::string_view key) +{ + std::string name; + name.reserve(key.size()); + append_prometheus_label_name(name, key); + return name; +} + // Process-global static labels (e.g. instance). Storage defined in Metrics.cpp. LabelMap &prometheus_static_labels_mutable(); inline const LabelMap &prometheus_static_labels() { return prometheus_static_labels_mutable(); } diff --git a/src/Policies.cpp b/src/Policies.cpp index 54a7dd6c1..66d56fe44 100644 --- a/src/Policies.cpp +++ b/src/Policies.cpp @@ -6,6 +6,7 @@ #include "CoreRegistry.h" #include "HandlerManager.h" #include "InputStreamManager.h" +#include "PolicyAttributes.h" #include "PrometheusSerializer.h" #include "Taps.h" #include @@ -55,8 +56,12 @@ std::vector PolicyManager::load(const YAML::Node &policy_yaml, bool si std::unique_lock lock(_load_mutex); auto policy_name = _get_policy_name(it); - // Input Section - auto input_node = it->second["input"]; + // Input Section. The attributes of each netprobe target belong to the policy, not to the input: they are + // read from the YAML as written, like the policy's own, and the input is built and named from a copy + // without them, so they are never typed as config values and do not split one input in two + const auto raw_input_node = it->second["input"]; + auto target_attributes = std::make_shared(parse_target_attributes(raw_input_node, policy_name)); + auto input_node = without_target_attributes(raw_input_node); auto [input_config, input_filter] = _registry->input_manager()->get_config_and_filter(input_node); // Handler Default Section auto handler_node = it->second["handlers"]; @@ -81,6 +86,10 @@ std::vector PolicyManager::load(const YAML::Node &policy_yaml, bool si } } + if (it->second["attributes"]) { + policy_ptr->set_attributes(parse_attributes(it->second["attributes"], fmt::format("policy '{}'", policy_name))); + } + std::vector added_inputs; std::vector added_handlers; // by name, like the resources policies below: the rollback looks each one up again @@ -184,7 +193,20 @@ std::vector PolicyManager::load(const YAML::Node &policy_yaml, bool si handler_modules.emplace_back(std::move(handler_module)); } + // the labels pktvisor sets itself, so an attribute cannot replace one: the policy's own, the static + // ones, and those of every handler it runs. A refusal rolls the load back like any other failure + std::vector handler_labels; + for (const auto &m : handler_modules) { + auto names = m->own_label_names(); + handler_labels.insert(handler_labels.end(), names.begin(), names.end()); + } + auto reserved = reserved_label_names(handler_labels); + check_reserved(policy_ptr->attributes(), reserved, fmt::format("policy '{}'", policy_name)); + check_target_attributes(*target_attributes, policy_ptr->attributes(), reserved, policy_name); + for (auto &m : handler_modules) { + // before the handler starts, and never changed after + m->set_target_attributes(target_attributes); auto hname = m->name(); _registry->handler_manager()->module_add(std::move(m)); added_handlers.push_back(hname); @@ -494,14 +516,14 @@ void Policy::prometheus_metrics(PrometheusSerializer &ser) if (_merge_like_handlers) { auto bucket_map = _get_merged_buckets(); for (auto &[bucket, hmod] : bucket_map) { - hmod->window_prometheus(ser, bucket.get(), {{"policy", name()}, {"handler", hmod->schema_key() + "_merged"}}); + hmod->window_prometheus(ser, bucket.get(), series_labels(hmod->schema_key() + "_merged")); } } else { for (auto &mod : modules()) { auto hmod = dynamic_cast(mod); if (hmod) { spdlog::stopwatch sw; - hmod->window_prometheus(ser, {{"policy", name()}, {"handler", hmod->name()}}); + hmod->window_prometheus(ser, series_labels(hmod->name())); spdlog::get("visor")->debug("{} window_prometheus elapsed time: {}", hmod->name(), sw); } } @@ -575,7 +597,7 @@ void Policy::_merged_opentelemetry(metrics::v1::ScopeMetrics &scope) auto metrics_before = scope.metrics_size(); try { for (auto &[bucket, hmod] : bucket_map) { - hmod->window_opentelemetry(scope, bucket.get(), {{"policy", name()}, {"handler", hmod->schema_key() + "_merged"}}); + hmod->window_opentelemetry(scope, bucket.get(), series_labels(hmod->schema_key() + "_merged")); } } catch (...) { scope.mutable_metrics()->DeleteSubrange(metrics_before, scope.metrics_size() - metrics_before); @@ -627,7 +649,7 @@ void Policy::opentelemetry_metrics(metrics::v1::ScopeMetrics &scope) // it fails what it wrote is dropped and sent whole next time. auto metrics_before = scope.metrics_size(); try { - hmod->window_opentelemetry(scope, {{"policy", name()}, {"handler", hmod->name()}}); + hmod->window_opentelemetry(scope, series_labels(hmod->name())); } catch (...) { scope.mutable_metrics()->DeleteSubrange(metrics_before, scope.metrics_size() - metrics_before); throw; diff --git a/src/Policies.h b/src/Policies.h index 6f3635068..f4befa12d 100644 --- a/src/Policies.h +++ b/src/Policies.h @@ -10,6 +10,7 @@ #include "HandlerModulePlugin.h" #include "InputModulePlugin.h" #include "OpenTelemetry.h" +#include "PolicyAttributes.h" #include "Taps.h" #include #include @@ -65,6 +66,9 @@ class Policy : public AbstractRunnableModule BucketMap _get_merged_buckets(const std::function(StreamHandler *, AbstractMetricsBucket *)> &merge); void _merged_opentelemetry(metrics::v1::ScopeMetrics &scope); + // set on every series the policy's handlers produce, under the labels pktvisor sets itself + LabelMap _attributes; + public: Policy(const std::string &name) : AbstractRunnableModule(name) @@ -76,6 +80,26 @@ class Policy : public AbstractRunnableModule return "policy"; } + void set_attributes(LabelMap attrs) + { + _attributes = std::move(attrs); + } + + const LabelMap &attributes() const + { + return _attributes; + } + + // The labels of every series of this policy's handler: the policy's attributes, then + // `policy` and `handler` on top. + LabelMap series_labels(const std::string &handler_label) const + { + auto labels = _attributes; + labels["policy"] = name(); + labels["handler"] = handler_label; + return labels; + } + void set_modules_sequence(bool sequence) { _modules_sequence = sequence; diff --git a/src/PolicyAttributes.cpp b/src/PolicyAttributes.cpp new file mode 100644 index 000000000..ddfaae5ec --- /dev/null +++ b/src/PolicyAttributes.cpp @@ -0,0 +1,169 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "PolicyAttributes.h" +#include "Policies.h" +#include +#include +#include + +namespace visor { + +LabelMap parse_attributes(const YAML::Node &node, const std::string &where) +{ + LabelMap attrs; + if (node.IsNull()) { + return attrs; + } + if (!node.IsMap()) { + throw PolicyException(fmt::format("attributes of {} is not a map", where)); + } + std::map written_as; // Prometheus name -> key + for (const auto &kv : node) { + if (!kv.first.IsScalar()) { + throw PolicyException(fmt::format("attributes of {} has a key that is not a scalar", where)); + } + const auto key = kv.first.Scalar(); + if (key.empty()) { + throw PolicyException(fmt::format("attributes of {} has an empty key", where)); + } + if (!is_utf8(key)) { + throw PolicyException(fmt::format("attributes of {} has a key that is not valid UTF-8", where)); + } + if (!((key[0] >= 'A' && key[0] <= 'Z') || (key[0] >= 'a' && key[0] <= 'z'))) { + throw PolicyException(fmt::format("attribute '{}' of {} must start with a letter (A-Z, a-z)", key, where)); + } + if (!kv.second.IsScalar()) { + throw PolicyException(fmt::format("attribute '{}' of {} must be a scalar value", key, where)); + } + const auto &value = kv.second.Scalar(); + if (value.empty()) { + throw PolicyException(fmt::format("attribute '{}' of {} must not be empty", key, where)); + } + auto [seen, added] = written_as.emplace(prometheus_label_name(key), key); + if (!added) { + throw PolicyException(fmt::format("attributes '{}' and '{}' of {} are both written as the Prometheus label '{}'", seen->second, key, where, seen->first)); + } + attrs[key] = value; + } + return attrs; +} + +void check_reserved(const LabelMap &attrs, const std::set &reserved, const std::string &where) +{ + for (const auto &[key, value] : attrs) { + auto name = prometheus_label_name(key); + if (reserved.count(name)) { + throw PolicyException(fmt::format("attribute '{}' of {} collides with the label '{}' that pktvisor sets itself", key, where, name)); + } + } +} + +namespace { + +// input.config.targets of a policy's input node, when it is a map. Each level is checked before it is indexed: +// yaml-cpp throws on a type check of a key that is not there. +std::optional targets_map(const YAML::Node &input_node) +{ + if (!input_node || !input_node.IsMap()) { + return std::nullopt; + } + const auto config = input_node["config"]; + if (!config || !config.IsMap()) { + return std::nullopt; + } + const auto targets = config["targets"]; + if (!targets || !targets.IsMap()) { + return std::nullopt; + } + return targets; +} + +} + +TargetAttributes parse_target_attributes(const YAML::Node &input_node, const std::string &policy_name) +{ + TargetAttributes targets; + auto config_targets = targets_map(input_node); + if (!config_targets) { + return targets; + } + for (const auto &kv : *config_targets) { + // a target that is not a map of its own is the input's to refuse + if (!kv.first.IsScalar() || !kv.second.IsMap() || !kv.second["attributes"]) { + continue; + } + const auto &name = kv.first.Scalar(); + auto attrs = parse_attributes(kv.second["attributes"], fmt::format("policy '{}' target '{}'", policy_name, name)); + if (!attrs.empty()) { + targets[name] = std::move(attrs); + } + } + return targets; +} + +YAML::Node without_target_attributes(const YAML::Node &input_node) +{ + // a missing or malformed input is for the input manager to refuse, exactly as written + if (!input_node || !input_node.IsMap()) { + return input_node; + } + // YAML::Clone gives every node a default position, so a clone would send the input's YAML errors to line 1 + auto original_targets = targets_map(input_node); + if (!original_targets) { + return input_node; + } + bool any = false; + for (const auto &kv : *original_targets) { + if (kv.second.IsMap() && kv.second["attributes"]) { + any = true; + break; + } + } + if (!any) { + return input_node; + } + auto cleaned = YAML::Clone(input_node); + if (auto config_targets = targets_map(cleaned)) { + for (auto kv : *config_targets) { + if (kv.second.IsMap()) { + kv.second.remove("attributes"); + } + } + } + return cleaned; +} + +std::set reserved_label_names(const std::vector &handler_labels) +{ + std::set reserved{"policy", "handler", "tap", "le", "quantile"}; + for (const auto &[label, value] : prometheus_static_labels()) { + reserved.insert(prometheus_label_name(label)); + } + for (const auto &label : handler_labels) { + reserved.insert(prometheus_label_name(label)); + } + return reserved; +} + +void check_target_attributes(const TargetAttributes &targets, const LabelMap &policy_attrs, const std::set &reserved, const std::string &policy_name) +{ + std::map policy_keys; // Prometheus name -> key; parse_attributes keeps them one to one + for (const auto &[key, value] : policy_attrs) { + policy_keys.emplace(prometheus_label_name(key), key); + } + for (const auto &[target, attrs] : targets) { + auto where = fmt::format("policy '{}' target '{}'", policy_name, target); + check_reserved(attrs, reserved, where); + for (const auto &[key, value] : attrs) { + auto name = prometheus_label_name(key); + auto found = policy_keys.find(name); + if (found != policy_keys.end() && found->second != key) { + throw PolicyException(fmt::format("attribute '{}' of {} and attribute '{}' of policy '{}' are both written as the Prometheus label '{}'", key, where, found->second, policy_name, name)); + } + } + } +} + +} diff --git a/src/PolicyAttributes.h b/src/PolicyAttributes.h new file mode 100644 index 000000000..6cbdc755f --- /dev/null +++ b/src/PolicyAttributes.h @@ -0,0 +1,52 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#pragma once + +#include "MetricLabels.h" +#include +#include +#include +#include +#include + +namespace visor { + +// The attributes of each netprobe target, by target name. +using TargetAttributes = std::map; + +// Reads the `attributes` mapping of a policy (`where` names it in errors). Values are the text +// as written in the YAML, so "007" stays 007 and "True" stays True. A null node is no attributes. +// Throws PolicyException for a node that is not a map, for a key that is empty, is not valid UTF-8 or +// does not start with an ASCII letter, and for a value that is not a scalar, is null or is empty. +LabelMap parse_attributes(const YAML::Node &node, const std::string &where); + +// Reads the `attributes` of every target in input.config.targets of a policy's `input` node, each as +// parse_attributes does, from the YAML as written. Targets without attributes are left out, and the +// result is empty when there is no targets map. +TargetAttributes parse_target_attributes(const YAML::Node &input_node, const std::string &policy_name); + +// A copy of a policy's `input` node with the `attributes` of every target in input.config.targets removed, the +// node the policy builds and names its input from. Target attributes belong to the policy, not to the input: kept +// out of its config, they are never read as typed config values, and policies that differ only in them share one +// input. `input_node` itself is left as it is, and one with no target attributes, or that is not a map, +// comes back as it is (a clone loses the positions that YAML errors report). +YAML::Node without_target_attributes(const YAML::Node &input_node); + +// Throws PolicyException, naming `where` and the key, when the Prometheus name of an attribute key is in +// `reserved` (labels pktvisor sets itself, in their Prometheus form). A collision is refused, never resolved. +void check_reserved(const LabelMap &attrs, const std::set &reserved, const std::string &where); + +// The labels pktvisor sets itself on a policy's series, in their Prometheus form: those of every policy +// (policy, handler, tap, le, quantile), the static labels, and `handler_labels`, the own_label_names of +// the policy's handlers. +std::set reserved_label_names(const std::vector &handler_labels); + +// Throws PolicyException, naming the policy, the target and the key, when a target's attributes would +// put on its series a label pktvisor sets itself (one in `reserved`), or a key written as the Prometheus +// label of a different key in the policy's own attributes: laid over those, both would reach the series +// under one label name. The same key, spelled the same, is an override and is allowed. +void check_target_attributes(const TargetAttributes &targets, const LabelMap &policy_attrs, const std::set &reserved, const std::string &policy_name); + +} diff --git a/src/PrometheusSerializer.h b/src/PrometheusSerializer.h index a3f2a760b..7e78a8b3c 100644 --- a/src/PrometheusSerializer.h +++ b/src/PrometheusSerializer.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -129,17 +130,24 @@ class PrometheusSerializer line.push_back('{'); bool any = false; auto append_one = [&](const std::string &k, const std::string &v) { - line.append(k); + append_prometheus_label_name(line, k); line.append("=\""); append_escaped(line, v); line.append("\","); any = true; }; - // Static labels first, but skip any key also given per-call so the per-call value wins — - // emitting the same label name twice in one sample is invalid exposition. - for (const auto &[k, v] : prometheus_static_labels()) { - if (labels.find(k) == labels.end()) { - append_one(k, v); + // Static labels first, but skip any whose written name is also given per-call so the per-call + // value wins — emitting the same label name twice in one sample is invalid exposition. + const auto &statics = prometheus_static_labels(); + if (!statics.empty()) { + std::set call_names; + for (const auto &kv : labels) { + call_names.insert(prometheus_label_name(kv.first)); + } + for (const auto &[k, v] : statics) { + if (call_names.find(prometheus_label_name(k)) == call_names.end()) { + append_one(k, v); + } } } for (const auto &[k, v] : labels) { diff --git a/src/StreamHandler.h b/src/StreamHandler.h index 7a18bbc02..7010e41db 100644 --- a/src/StreamHandler.h +++ b/src/StreamHandler.h @@ -8,14 +8,18 @@ #include "AbstractModule.h" #include "CoreRegistry.h" #include "InputEventProxy.h" +#include "PolicyAttributes.h" #include "PrometheusSerializer.h" #include #include #include #include +#include #include #include #include +#include +#include namespace visor { @@ -72,6 +76,20 @@ class StreamHandler : public AbstractRunnableModule return _version; } + // Every label key this handler writes into a label map (its labels["..."] keys, plus the item_key of every + // TopN it builds). A policy attribute whose Prometheus name is one of these is refused at load. + virtual std::vector own_label_names() const + { + return {}; + } + + // The attributes of each netprobe target of the policy, set once before the handler starts. A handler + // with a series per target lays a target's attributes over the policy's on that target's series; every + // other handler ignores them. + virtual void set_target_attributes(std::shared_ptr) + { + } + virtual void window_json(json &j, uint64_t period, bool merged) = 0; virtual void window_json(json &j, AbstractMetricsBucket *bucket) = 0; virtual void window_prometheus(PrometheusSerializer &ser, Metric::LabelMap add_labels = {}) = 0; diff --git a/src/handlers/bgp/BgpStreamHandler.h b/src/handlers/bgp/BgpStreamHandler.h index 6c9840750..c99ebcd3f 100644 --- a/src/handlers/bgp/BgpStreamHandler.h +++ b/src/handlers/bgp/BgpStreamHandler.h @@ -152,6 +152,11 @@ class BgpStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {}; + } + std::string schema_key() const override { return BGP_SCHEMA; diff --git a/src/handlers/dhcp/DhcpStreamHandler.h b/src/handlers/dhcp/DhcpStreamHandler.h index e036ecc39..6eb312f1e 100644 --- a/src/handlers/dhcp/DhcpStreamHandler.h +++ b/src/handlers/dhcp/DhcpStreamHandler.h @@ -174,6 +174,11 @@ class DhcpStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"client", "server"}; + } + std::string schema_key() const override { return DHCP_SCHEMA; diff --git a/src/handlers/dns/v1/DnsStreamHandler.h b/src/handlers/dns/v1/DnsStreamHandler.h index f9390982c..e30e18ce4 100644 --- a/src/handlers/dns/v1/DnsStreamHandler.h +++ b/src/handlers/dns/v1/DnsStreamHandler.h @@ -427,6 +427,11 @@ class DnsStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"lat", "lon", "geo_loc", "asn", "ecs", "qname", "port", "qtype", "rcode"}; + } + std::string schema_key() const override { return DNS_SCHEMA; diff --git a/src/handlers/dns/v2/DnsStreamHandler.h b/src/handlers/dns/v2/DnsStreamHandler.h index 66f21dd97..180b874d8 100644 --- a/src/handlers/dns/v2/DnsStreamHandler.h +++ b/src/handlers/dns/v2/DnsStreamHandler.h @@ -593,6 +593,11 @@ class DnsStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"direction", "lat", "lon", "geo_loc", "asn", "ecs", "qname", "port", "qtype", "rcode"}; + } + std::string schema_key() const override { return DNS_SCHEMA; diff --git a/src/handlers/flow/FlowStreamHandler.h b/src/handlers/flow/FlowStreamHandler.h index 4061a02ad..3e27bd0b8 100644 --- a/src/handlers/flow/FlowStreamHandler.h +++ b/src/handlers/flow/FlowStreamHandler.h @@ -425,6 +425,11 @@ class FlowStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"device", "device_interface", "lat", "lon", "conversation", "geo_loc", "asn", "ip", "port", "ip_port", "dscp", "ecn", "interface"}; + } + std::string schema_key() const override { return FLOW_SCHEMA; diff --git a/src/handlers/flow/README.md b/src/handlers/flow/README.md index 29216f7b8..821b1722b 100644 --- a/src/handlers/flow/README.md +++ b/src/handlers/flow/README.md @@ -5,3 +5,12 @@ This directory contains the Flow stream handler It can attach to flow input streams and process and summarize general flow traffic. [FlowStreamHandler.h](FlowStreamHandler.h) contains the list of metrics. + +## Policy attributes + +A policy that runs a flow handler can set `attributes` at the top level of the policy. Every flow series carries them, in the +OpenTelemetry push and in the Prometheus output. Only policy-level attributes apply to flow; there are no per-target +attributes. The attributes cannot reuse a label the flow handler sets (`device`, `device_interface`, `lat`, `lon`, +`conversation`, `geo_loc`, `asn`, `ip`, `port`, `ip_port`, `dscp`, `ecn`, `interface`) or the common ones (`policy`, +`handler`, `tap`, `le`, `quantile`): the policy does not load. `device_map` is unchanged and still maps device addresses to +the `device` label. See the main [README](../../../README.md#policy-attributes). diff --git a/src/handlers/input_resources/InputResourcesStreamHandler.h b/src/handlers/input_resources/InputResourcesStreamHandler.h index b127ed6c5..9f1c0adde 100644 --- a/src/handlers/input_resources/InputResourcesStreamHandler.h +++ b/src/handlers/input_resources/InputResourcesStreamHandler.h @@ -115,6 +115,11 @@ class InputResourcesStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {}; + } + std::string schema_key() const override { return "input_resources"; diff --git a/src/handlers/net/v1/NetStreamHandler.h b/src/handlers/net/v1/NetStreamHandler.h index 29959221e..ed2edc20a 100644 --- a/src/handlers/net/v1/NetStreamHandler.h +++ b/src/handlers/net/v1/NetStreamHandler.h @@ -240,6 +240,11 @@ class NetStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"lat", "lon", "geo_loc", "asn", "ipv4", "ipv6"}; + } + std::string schema_key() const override { return NET_SCHEMA; diff --git a/src/handlers/net/v2/NetStreamHandler.h b/src/handlers/net/v2/NetStreamHandler.h index bd50548a9..4861aab7a 100644 --- a/src/handlers/net/v2/NetStreamHandler.h +++ b/src/handlers/net/v2/NetStreamHandler.h @@ -290,6 +290,11 @@ class NetStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"direction", "lat", "lon", "geo_loc", "asn", "ipv4", "ipv6"}; + } + std::string schema_key() const override { return NET_SCHEMA; diff --git a/src/handlers/netprobe/NetProbeStreamHandler.cpp b/src/handlers/netprobe/NetProbeStreamHandler.cpp index e780943cd..f9cd5b7c3 100644 --- a/src/handlers/netprobe/NetProbeStreamHandler.cpp +++ b/src/handlers/netprobe/NetProbeStreamHandler.cpp @@ -144,6 +144,11 @@ void NetProbeMetricsBucket::specialized_merge(const AbstractMetricsBucket &o, Me std::shared_lock r_lock(other._mutex); std::unique_lock w_lock(_mutex); + // a merge starts from a fresh bucket, which has no target attributes until a bucket of the window hands it them + if (!_target_attributes) { + _target_attributes = other._target_attributes; + } + for (const auto &target : other._targets_metrics) { const auto &targetId = target.first; get_or_create_target(targetId); @@ -184,14 +189,26 @@ void NetProbeMetricsBucket::specialized_merge(const AbstractMetricsBucket &o, Me } } +Metric::LabelMap NetProbeMetricsBucket::labels_for_target(const Metric::LabelMap &add_labels, const std::string &target) const +{ + auto labels = add_labels; + if (_target_attributes) { + if (auto attrs = _target_attributes->find(target); attrs != _target_attributes->end()) { + for (const auto &[key, value] : attrs->second) { + labels[key] = value; + } + } + } + labels["target"] = target; + return labels; +} + void NetProbeMetricsBucket::to_prometheus(PrometheusSerializer &ser, Metric::LabelMap add_labels) const { std::shared_lock r_lock(_mutex); for (const auto &target : _targets_metrics) { - auto target_labels = add_labels; - auto targetId = target.first; - target_labels["target"] = targetId; + auto target_labels = labels_for_target(add_labels, target.first); if (group_enabled(group::NetProbeMetrics::Counters)) { target.second->attempts.to_prometheus(ser, target_labels); @@ -266,9 +283,7 @@ void NetProbeMetricsBucket::to_opentelemetry(metrics::v1::ScopeMetrics &scope, t std::shared_lock r_lock(_mutex); for (const auto &target : _targets_metrics) { - auto target_labels = add_labels; - auto targetId = target.first; - target_labels["target"] = targetId; + auto target_labels = labels_for_target(add_labels, target.first); if (group_enabled(group::NetProbeMetrics::Counters)) { target.second->attempts.to_opentelemetry(scope, start_ts, end_ts, target_labels); diff --git a/src/handlers/netprobe/NetProbeStreamHandler.h b/src/handlers/netprobe/NetProbeStreamHandler.h index 5bbf4e7cf..bd4016df4 100644 --- a/src/handlers/netprobe/NetProbeStreamHandler.h +++ b/src/handlers/netprobe/NetProbeStreamHandler.h @@ -104,15 +104,29 @@ class NetProbeMetricsBucket final : public visor::AbstractMetricsBucket size_t _topn_count{10}; uint64_t _topn_percentile_threshold{0}; + // The attributes of each target, from the policy. Shared by every bucket of the window and never + // changed once set, so the bucket's lock is all that reading it needs. + std::shared_ptr _target_attributes; + // Return the metrics for `target`, creating it (with the current top-N settings applied to its // TopN metrics) if it doesn't exist yet. Callers must already hold _mutex. Target &get_or_create_target(const std::string &target); + // The labels of `target`'s series: add_labels (the policy's attributes, policy, handler and tap), the + // target's attributes over them, and `target` itself last. Callers must already hold _mutex. + Metric::LabelMap labels_for_target(const Metric::LabelMap &add_labels, const std::string &target) const; + public: NetProbeMetricsBucket() { } + void set_target_attributes(std::shared_ptr attrs) + { + std::unique_lock lock(_mutex); + _target_attributes = std::move(attrs); + } + // visor::AbstractMetricsBucket void specialized_merge(const AbstractMetricsBucket &other, Metric::Aggregate agg_operator) override; void to_json(json &j) const override; @@ -147,6 +161,13 @@ class NetProbeMetricsManager final : public visor::AbstractMetricsManager> NetProbeTransactionManager; std::unique_ptr _request_reply_manager; + // set before the handler starts, then only read + std::shared_ptr _target_attributes; + + void on_new_bucket(NetProbeMetricsBucket &bucket) override + { + bucket.set_target_attributes(_target_attributes); + } public: NetProbeMetricsManager(const Configurable *window_config) @@ -161,6 +182,14 @@ class NetProbeMetricsManager final : public visor::AbstractMetricsManagerclear(); } + // Before the handler starts, while the live bucket is the only one: it gets the map here, and each bucket a + // period shift opens gets it in on_new_bucket. A merge carries it to the merged bucket. + void set_target_attributes(std::shared_ptr attrs) + { + _target_attributes = attrs; + live_bucket()->set_target_attributes(std::move(attrs)); + } + void set_xact_ttl(uint32_t ttl) { _request_reply_manager = std::make_unique(ttl); @@ -213,6 +242,16 @@ class NetProbeStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {"target", "status_code", "rcode"}; + } + + void set_target_attributes(std::shared_ptr attrs) override + { + _metrics->set_target_attributes(std::move(attrs)); + } + std::string schema_key() const override { return NET_PROBE_SCHEMA; diff --git a/src/handlers/netprobe/README.md b/src/handlers/netprobe/README.md index 078e372ea..b98aefd0d 100644 --- a/src/handlers/netprobe/README.md +++ b/src/handlers/netprobe/README.md @@ -235,6 +235,43 @@ handlers: --- +## Target attributes + +Besides the policy's own `attributes` (see the main [README](../../../README.md#policy-attributes)), each target can have +`attributes` under `input.config.targets..attributes`: + +```yaml +policy_name: + kind: collection + attributes: + netbox.site.name: dc-west + netbox.tenant.name: acme + input: + tap: probes + input_type: netprobe + config: + test_type: ping + targets: + gw1: + target: 192.0.2.1 + attributes: + netbox.site.name: dc-east + netbox_id: "42" + device_ip: 192.0.2.1 +``` + +* The attributes of a target are laid over the policy's, key by key: the target's value wins. Then `target` is set. + Here `gw1`'s series carry `netbox.site.name: dc-east`, `netbox.tenant.name: acme`, `netbox_id`, `device_ip` and + `target`. +* The same rules apply as for the policy's attributes: scalar values kept as written, and a refused policy when a key is a + label pktvisor sets (here also `target`, `status_code` and `rcode`). +* A target key whose Prometheus name is the same as that of a different policy key is refused (`netbox.site.name` on the + policy and `netbox_site_name` on a target). The same key, written the same way, is an override. +* Target attributes are not part of the input's config. Policies that differ only in them share one input. +* Targets declared in a tap's config rather than in the policy get no per-target attributes; they carry the policy's. + +--- + ## Testing notes Automated test coverage (`test_netprobe.cpp`) exercises the `tls.*` config-validation paths diff --git a/src/handlers/netprobe/test_net_probe.cpp b/src/handlers/netprobe/test_net_probe.cpp index b86bd8385..b5b279ed6 100644 --- a/src/handlers/netprobe/test_net_probe.cpp +++ b/src/handlers/netprobe/test_net_probe.cpp @@ -36,11 +36,15 @@ struct UnitFixture { visor::InputEventProxy *proxy; std::unique_ptr handler; - explicit UnitFixture(uint64_t num_periods = 1) + explicit UnitFixture(uint64_t num_periods = 1, std::shared_ptr target_attributes = nullptr) { c.config_set("num_periods", num_periods); proxy = stream.add_event_proxy(c); handler = std::make_unique("netprobe-unit", proxy, &c); + // as a policy load does, before the handler starts + if (target_attributes) { + handler->set_target_attributes(target_attributes); + } handler->start(); } @@ -1048,3 +1052,145 @@ TEST_CASE("Net Probe TCP tests", "[netprobe][tcp]") CHECK(j["targets"]["my_target"]["attempts"] >= 0); } + +namespace { + +using Labels = std::map; + +std::shared_ptr gw1_attributes() +{ + return std::make_shared(visor::TargetAttributes{ + {"gw1", {{"netbox.site.name", "dc-east"}, {"netbox_id", "netbox-device-1"}}}}); +} + +// What a policy hands its handlers: its own attributes, then `policy` and `handler`. +const visor::LabelMap policy_labels{{"netbox.site.name", "dc-west"}, {"policy", "p"}, {"handler", "h"}}; + +// The attributes of every datapoint in the scope, by the target they are for. +std::multimap datapoints_by_target(const opentelemetry::proto::metrics::v1::ScopeMetrics &scope) +{ + std::multimap points; + auto add = [&points](const auto &kvs) { + Labels labels; + for (const auto &kv : kvs) { + labels[kv.key()] = kv.value().string_value(); + } + points.emplace(labels["target"], labels); + }; + for (const auto &metric : scope.metrics()) { + for (const auto &point : metric.gauge().data_points()) { + add(point.attributes()); + } + for (const auto &point : metric.summary().data_points()) { + add(point.attributes()); + } + for (const auto &point : metric.histogram().data_points()) { + add(point.attributes()); + } + } + return points; +} + +// gw1's series carry its own attributes over the policy's; gw2's only the policy's. +void check_target_datapoints(const opentelemetry::proto::metrics::v1::ScopeMetrics &scope) +{ + auto points = datapoints_by_target(scope); + REQUIRE(points.count("gw1") > 0); + REQUIRE(points.count("gw2") > 0); + CHECK(points.size() == points.count("gw1") + points.count("gw2")); + for (auto [it, end] = points.equal_range("gw1"); it != end; ++it) { + const auto &labels = it->second; + CHECK(labels.at("netbox.site.name") == "dc-east"); + CHECK(labels.at("netbox_id") == "netbox-device-1"); + CHECK(labels.at("policy") == "p"); + CHECK(labels.at("handler") == "h"); + } + for (auto [it, end] = points.equal_range("gw2"); it != end; ++it) { + const auto &labels = it->second; + CHECK(labels.at("netbox.site.name") == "dc-west"); + CHECK(labels.count("netbox_id") == 0); + CHECK(labels.at("policy") == "p"); + CHECK(labels.at("handler") == "h"); + } +} + +// The same in Prometheus output, where netbox.site.name is written netbox_site_name. +void check_target_samples(const std::string &text) +{ + size_t gw1 = 0; + size_t gw2 = 0; + std::istringstream in(text); + for (std::string line; std::getline(in, line);) { + if (line.empty() || line[0] == '#') { + continue; + } + CHECK(line.find("netbox.site.name") == std::string::npos); + CHECK(line.find("policy=\"p\"") != std::string::npos); + if (line.find("target=\"gw1\"") != std::string::npos) { + ++gw1; + CHECK(line.find("netbox_site_name=\"dc-east\"") != std::string::npos); + CHECK(line.find("netbox_id=\"netbox-device-1\"") != std::string::npos); + } else if (line.find("target=\"gw2\"") != std::string::npos) { + ++gw2; + CHECK(line.find("netbox_site_name=\"dc-west\"") != std::string::npos); + CHECK(line.find("netbox_id=") == std::string::npos); + } else { + FAIL("a sample for neither target: " << line); + } + } + CHECK(gw1 > 0); + CHECK(gw2 > 0); +} + +void check_target_series(const NetProbeMetricsBucket &bucket) +{ + opentelemetry::proto::metrics::v1::ScopeMetrics scope; + timespec start_ts{0, 0}; + timespec end_ts{60, 0}; + bucket.to_opentelemetry(scope, start_ts, end_ts, policy_labels); + check_target_datapoints(scope); + + visor::PrometheusSerializer ser; + bucket.to_prometheus(ser, policy_labels); + check_target_samples(ser.finalize()); +} + +} + +TEST_CASE("Per-target attributes override the policy's on that target's series only", "[netprobe][attributes]") +{ + UnitFixture fx(1, gw1_attributes()); + fx.manager()->live_bucket()->process_attempts(false, "gw1"); + fx.manager()->live_bucket()->process_attempts(false, "gw2"); + + check_target_series(*fx.manager()->bucket(0)); +} + +TEST_CASE("Per-target attributes survive a merge", "[netprobe][attributes]") +{ + UnitFixture fx_a(1, gw1_attributes()); + UnitFixture fx_b(1, gw1_attributes()); + fx_a.manager()->live_bucket()->process_attempts(false, "gw1"); + fx_b.manager()->live_bucket()->process_attempts(false, "gw2"); + + // as a policy merges like handlers: the first merge starts a fresh bucket, the next merges into it + auto merged = fx_a.handler->merge(nullptr, 0, false, false); + REQUIRE(merged); + CHECK_FALSE(fx_b.handler->merge(merged.get(), 0, false, false)); + + check_target_series(static_cast(*merged)); +} + +TEST_CASE("Per-target attributes reach the bucket a period shift opens", "[netprobe][attributes]") +{ + UnitFixture fx(2, gw1_attributes()); + timespec later; + timespec_get(&later, TIME_UTC); + later.tv_sec += 61; + fx.handler->check_period_shift(later); + REQUIRE(fx.manager()->current_periods() == 2); + fx.manager()->live_bucket()->process_attempts(false, "gw1"); + fx.manager()->live_bucket()->process_attempts(false, "gw2"); + + check_target_series(*fx.manager()->bucket(0)); +} diff --git a/src/handlers/pcap/PcapStreamHandler.h b/src/handlers/pcap/PcapStreamHandler.h index 196206498..1fb1f24b9 100644 --- a/src/handlers/pcap/PcapStreamHandler.h +++ b/src/handlers/pcap/PcapStreamHandler.h @@ -107,6 +107,11 @@ class PcapStreamHandler final : public visor::StreamMetricsHandler own_label_names() const override + { + return {}; + } + std::string schema_key() const override { return PCAP_SCHEMA; diff --git a/src/inputs/netprobe/test_netprobe.cpp b/src/inputs/netprobe/test_netprobe.cpp index 78cfb5f61..01751fcbe 100644 --- a/src/inputs/netprobe/test_netprobe.cpp +++ b/src/inputs/netprobe/test_netprobe.cpp @@ -977,6 +977,47 @@ TEST_CASE("NetProbe v3 config: per-target ip_version + resolve also parse for do stream.stop(); } +TEST_CASE("A netprobe target with attributes starts like one without", "[netprobe][attributes]") +{ + // A policy keeps its targets' attributes out of the input's config, but targets declared in a tap's config reach + // the input as written, attributes included: the input must neither refuse them nor probe differently. + auto start = [](const std::string &name, const std::string &gw1_attributes) { + NetProbeInputStream stream{name}; + stream.config_set_yaml(YAML::Load(R"( +test_type: tcp +interval_msec: 60000 +timeout_msec: 100 +targets: + gw1: + target: 127.0.0.1 + port: 9 +)" + gw1_attributes + R"( + gw2: + target: localhost + port: 9 +)")); + CHECK_NOTHROW(stream.start()); + json j; + stream.info_json(j); + stream.stop(); + std::vector targets; + for (const auto &target : j["module"]["config"]["targets"].items()) { + targets.push_back(target.key()); + } + return std::make_pair(j[stream.schema_key()]["current_targets_total"], targets); + }; + + auto with = start("net-probe-test-target-attributes", R"( attributes: + netbox.site.name: dc-east + netbox_id: netbox-device-1 + zero: "007" + flag: True +)"); + auto without = start("net-probe-test-no-target-attributes", ""); + CHECK(with.first == 2); + CHECK(with == without); +} + TEST_CASE("ICMPv6 reply carrier survives the fan-out Packet deep-copy", "[netprobe][ipv6]") { // Wire bytes of an ICMPv6 echo REPLY: type=129, code=0, checksum=0, id=0xBEEF, seq=0x0102 (network order). diff --git a/src/tests/test_core_server.cpp b/src/tests/test_core_server.cpp index 9c0b9e178..dcb470f37 100644 --- a/src/tests/test_core_server.cpp +++ b/src/tests/test_core_server.cpp @@ -186,6 +186,24 @@ TEST_CASE("Default policy routes answer 404 for a default policy a concurrent re } } +TEST_CASE("The default policy's Prometheus path carries its attributes, with policy on top and no handler label", "[api][policies]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load_tap(registry)); + auto yaml = policy("default"); + yaml.replace(yaml.find(" input:"), 0, " attributes: {site: dc-west}\n"); + REQUIRE_NOTHROW(registry.policy_manager()->load_from_str(yaml)); + + httplib::Response res; + api::get_default_prometheus(*registry.policy_manager(), res); + CHECK(status(res) == 200); + CHECK_THAT(res.body, Catch::Matchers::ContainsSubstring(R"(policy="default")")); + CHECK_THAT(res.body, Catch::Matchers::ContainsSubstring(R"(site="dc-west")")); + CHECK_THAT(res.body, !Catch::Matchers::ContainsSubstring("handler=")); +} + TEST_CASE("Policy metrics answer 404 for a removed policy and __all skips one removed after it was listed", "[api][policies]") { CoreRegistry registry; diff --git a/src/tests/test_policy_attributes.cpp b/src/tests/test_policy_attributes.cpp new file mode 100644 index 000000000..05dad9344 --- /dev/null +++ b/src/tests/test_policy_attributes.cpp @@ -0,0 +1,888 @@ +#include "BuiltinPlugins.h" +#include "CoreRegistry.h" +#include "HandlerManager.h" +#include "InputStreamManager.h" +#include "Policies.h" +#include "PrometheusSerializer.h" +#include "handlers/flow/FlowStreamHandler.h" +#include "handlers/net/v1/NetStreamHandler.h" +#include "handlers/netprobe/NetProbeStreamHandler.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace visor; +using Catch::Matchers::ContainsSubstring; + +namespace { + +using Attrs = std::map; + +Attrs attributes_of(const google::protobuf::RepeatedPtrField &kvs) +{ + Attrs attrs; + for (const auto &kv : kvs) { + attrs[kv.key()] = kv.value().string_value(); + } + return attrs; +} + +// The attributes of every datapoint of every metric in the scope. +std::vector datapoint_attributes(const metrics::v1::ScopeMetrics &scope) +{ + std::vector all; + for (const auto &metric : scope.metrics()) { + for (const auto &point : metric.gauge().data_points()) { + all.push_back(attributes_of(point.attributes())); + } + for (const auto &point : metric.sum().data_points()) { + all.push_back(attributes_of(point.attributes())); + } + for (const auto &point : metric.summary().data_points()) { + all.push_back(attributes_of(point.attributes())); + } + for (const auto &point : metric.histogram().data_points()) { + all.push_back(attributes_of(point.attributes())); + } + } + return all; +} + +timespec after_now() +{ + timespec t; + timespec_get(&t, TIME_UTC); + t.tv_sec += 61; + return t; +} + +void close_windows(Policy *policy) +{ + auto stamp = after_now(); + for (auto mod : policy->modules()) { + if (auto net = dynamic_cast(mod)) { + net->check_period_shift(stamp); + } else if (auto flow = dynamic_cast(mod)) { + flow->check_period_shift(stamp); + } else if (auto probe = dynamic_cast(mod)) { + probe->check_period_shift(stamp); + } + } +} + +std::vector sample_lines(const std::string &text) +{ + std::vector lines; + std::istringstream in(text); + for (std::string line; std::getline(in, line);) { + if (!line.empty() && line[0] != '#') { + lines.push_back(line); + } + } + return lines; +} + +constexpr auto pcap_tap = R"( +visor: + taps: + recorded: + input_type: pcap + config: + pcap_file: tests/fixtures/dns_ipv4_udp.pcap +)"; + +std::string pcap_policy(const std::string &name, const std::string &attributes, const std::string &config = "") +{ + return fmt::format(R"( +visor: + policies: + {}: + kind: collection +{} +{} + input: + tap: recorded + input_type: pcap + handlers: + modules: + recorded_net: + type: net +)", + name, config, attributes); +} + +constexpr auto attributes_yaml = R"( attributes: + netbox.site.name: "dc-west" + netbox_id: "netbox-device-12" + zero: "007" + flag: "True" +)"; + +void load(CoreRegistry ®istry, const std::string &tap_yaml, const std::string &policy_yaml) +{ + auto taps = YAML::Load(tap_yaml); + auto policies = YAML::Load(policy_yaml); + registry.tap_manager()->load(taps["visor"]["taps"]); + registry.policy_manager()->load(policies["visor"]["policies"]); +} + +} + +TEST_CASE("Policy attributes reach every OpenTelemetry datapoint", "[policies][attributes]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, pcap_tap, pcap_policy("attr_view", attributes_yaml))); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("attr_view"); + CHECK(policy->attributes().at("zero") == "007"); + close_windows(policy); + metrics::v1::ScopeMetrics scope; + policy->opentelemetry_metrics(scope); + auto points = datapoint_attributes(scope); + REQUIRE_FALSE(points.empty()); + for (const auto &attrs : points) { + CHECK(attrs.at("netbox.site.name") == "dc-west"); + CHECK(attrs.at("netbox_id") == "netbox-device-12"); + CHECK(attrs.at("zero") == "007"); + CHECK(attrs.at("flag") == "True"); + CHECK(attrs.at("policy") == "attr_view"); + CHECK(attrs.at("handler") == "attr_view-recorded-recorded_net"); + } +} + +TEST_CASE("Policy attributes reach Prometheus output under converted names", "[policies][attributes][prometheus]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, pcap_tap, pcap_policy("attr_view", attributes_yaml))); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("attr_view"); + close_windows(policy); + std::stringstream out; + policy->prometheus_metrics(out); + auto lines = sample_lines(out.str()); + REQUIRE_FALSE(lines.empty()); + for (const auto &line : lines) { + CHECK_THAT(line, ContainsSubstring("netbox_site_name=\"dc-west\"")); + CHECK_THAT(line, ContainsSubstring("zero=\"007\"")); + CHECK_THAT(line, ContainsSubstring("flag=\"True\"")); + CHECK_THAT(line, ContainsSubstring("policy=\"attr_view\"")); + } +} + +TEST_CASE("Policy attributes reach the merged-handlers path", "[policies][attributes]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + auto policy_yaml = fmt::format(R"( +visor: + policies: + merged_view: + kind: collection + config: + merge_like_handlers: true +{} + input: + tap: recorded + input_type: pcap + handlers: + modules: + net_1: + type: net + net_2: + type: net +)", + attributes_yaml); + REQUIRE_NOTHROW(load(registry, pcap_tap, policy_yaml)); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("merged_view"); + close_windows(policy); + + metrics::v1::ScopeMetrics scope; + policy->opentelemetry_metrics(scope); + auto points = datapoint_attributes(scope); + REQUIRE_FALSE(points.empty()); + for (const auto &attrs : points) { + CHECK(attrs.at("netbox.site.name") == "dc-west"); + CHECK(attrs.at("handler") == "packets_merged"); + } + + std::stringstream out; + policy->prometheus_metrics(out); + auto lines = sample_lines(out.str()); + REQUIRE_FALSE(lines.empty()); + for (const auto &line : lines) { + CHECK_THAT(line, ContainsSubstring("netbox_site_name=\"dc-west\"")); + } +} + +TEST_CASE("Policy attributes reach a flow policy's series", "[policies][attributes][flow]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + auto taps = R"( +visor: + taps: + flows: + input_type: flow +)"; + auto policy_yaml = R"( +visor: + policies: + flow_view: + kind: collection + attributes: + netbox.tenant.name: "acme" + input: + tap: flows + input_type: flow + config: + flow_type: sflow + pcap_file: tests/fixtures/ecmp.pcap + handlers: + modules: + sflow_flow: + type: flow + config: + device_map: + 192.168.0.13: + name: router13 + interfaces: + 52: + name: eth52 +)"; + REQUIRE_NOTHROW(load(registry, taps, policy_yaml)); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("flow_view"); + close_windows(policy); + metrics::v1::ScopeMetrics scope; + policy->opentelemetry_metrics(scope); + auto points = datapoint_attributes(scope); + REQUIRE_FALSE(points.empty()); + bool named_device = false; + for (const auto &attrs : points) { + CHECK(attrs.at("netbox.tenant.name") == "acme"); + CHECK(attrs.count("device") == 1); + named_device = named_device || attrs.at("device") == "router13"; + } + CHECK(named_device); +} + +TEST_CASE("Two policies on one input carry only their own attributes", "[policies][attributes]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + auto tap = R"( +visor: + taps: + shared: + input_type: mock + config: + iface: eth0 +)"; + auto policy_yaml = [](const std::string &name, const std::string &site) { + return fmt::format(R"( +visor: + policies: + {0}: + kind: collection + attributes: + site: {1} + input: + tap: shared + input_type: mock + config: + sample: value + handlers: + modules: + {0}_net: + type: net +)", + name, site); + }; + REQUIRE_NOTHROW(load(registry, tap, policy_yaml("p1", "a"))); + auto second = YAML::Load(policy_yaml("p2", "b")); + REQUIRE_NOTHROW(registry.policy_manager()->load(second["visor"]["policies"])); + + for (const auto &name : {"p1", "p2"}) { + auto [policy, lock] = registry.policy_manager()->module_get_locked(name); + close_windows(policy); + } + metrics::v1::ResourceMetrics resource; + REQUIRE(registry.policy_manager()->opentelemetry_metrics(resource)); + std::map seen; + for (const auto &scope : resource.scope_metrics()) { + auto points = datapoint_attributes(scope); + REQUIRE_FALSE(points.empty()); + for (const auto &attrs : points) { + CHECK(attrs.at("site") == (scope.scope().name() == "pktvisor/p1" ? "a" : "b")); + } + seen[scope.scope().name()] = points.front().at("site"); + } + CHECK(seen == std::map{{"pktvisor/p1", "a"}, {"pktvisor/p2", "b"}}); +} + +TEST_CASE("Attribute values with quotes, backslashes and non-ASCII are escaped like any label value", "[policies][attributes][prometheus]") +{ + const std::string value = "S\xc3\xa3o \"Paulo\" \\ 1"; + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + auto attributes = std::string(" attributes:\n city: 'S\xc3\xa3o \"Paulo\" \\ 1'\n"); + REQUIRE_NOTHROW(load(registry, pcap_tap, pcap_policy("esc_view", attributes))); + + // how the serializer renders a label of the same text + PrometheusSerializer ser; + ser.write("m", PrometheusSerializer::Type::Gauge, "h", {}, {{"city", value}}, 1); + auto rendered = sample_lines(ser.finalize()).at(0); + auto expected = rendered.substr(rendered.find("city=\"")); + expected = expected.substr(0, expected.find("\",") == std::string::npos ? expected.find("\"}") + 1 : expected.find("\",") + 1); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("esc_view"); + close_windows(policy); + std::stringstream out; + policy->prometheus_metrics(out); + auto lines = sample_lines(out.str()); + REQUIRE_FALSE(lines.empty()); + for (const auto &line : lines) { + CHECK_THAT(line, ContainsSubstring(expected)); + } + + metrics::v1::ScopeMetrics scope; + policy->opentelemetry_metrics(scope); + auto points = datapoint_attributes(scope); + REQUIRE_FALSE(points.empty()); + for (const auto &attrs : points) { + CHECK(attrs.at("city") == value); + } +} + +TEST_CASE("Malformed attributes are refused at load", "[policies][attributes]") +{ + struct Case { + std::string attributes; + std::string key; + }; + const std::vector cases = { + {" attributes: [a, b]\n", "attributes"}, + {" attributes: {k: {x: 1}}\n", "'k'"}, + {" attributes: {k: [1]}\n", "'k'"}, + {" attributes: {k: ~}\n", "'k'"}, + {" attributes: {k: \"\"}\n", "'k'"}, + {" attributes: {netbox.site.name: a, netbox_site_name: b}\n", "'netbox.site.name'"}, + {" attributes: {netbox.site.name: a, netbox_site_name: b}\n", "'netbox_site_name'"}, + {" attributes: {\"\": a}\n", "attributes of policy 'bad_view'"}, + {" attributes: {1st_floor: a}\n", "'1st_floor'"}, + {" attributes: {_x: a}\n", "'_x'"}, + {" attributes: {__name__: a}\n", "'__name__'"}, + {" attributes: {\"a\xff\": a}\n", "UTF-8"}, + {" attributes: {sit\xc3\xa9: a, sit_: b}\n", "'sit_'"}, + }; + for (const auto &c : cases) { + DYNAMIC_SECTION(c.attributes << c.key) + { + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + std::string message; + try { + load(registry, pcap_tap, pcap_policy("bad_view", c.attributes)); + } catch (const PolicyException &e) { + message = e.what(); + } + CHECK_THAT(message, ContainsSubstring(c.key)); + CHECK_THAT(message, ContainsSubstring("bad_view")); + CHECK_FALSE(registry.policy_manager()->module_exists("bad_view")); + } + } +} + +namespace { + +constexpr auto flow_tap = R"( +visor: + taps: + flows: + input_type: flow +)"; + +constexpr auto netprobe_tap = R"( +visor: + taps: + probes: + input_type: netprobe +)"; + +std::string handler_policy(const std::string &name, const std::string &tap, const std::string &input_type, const std::string &input_config, const std::string &handler_type, const std::string &attributes) +{ + return fmt::format(R"( +visor: + policies: + {}: + kind: collection + attributes: + {} + input: + tap: {} + input_type: {} + config: +{} + handlers: + modules: + the_handler: + type: {} +)", + name, attributes, tap, input_type, input_config, handler_type); +} + +constexpr auto flow_input = R"( flow_type: sflow + pcap_file: tests/fixtures/ecmp.pcap)"; + +constexpr auto netprobe_input = R"( test_type: http + targets: + one: + target: http://localhost:1/)"; + +constexpr auto pcap_input = R"( pcap_file: tests/fixtures/dns_ipv4_udp.pcap)"; + +} + +TEST_CASE("An attribute named after a label pktvisor sets is refused", "[policies][attributes]") +{ + struct Case { + std::string key; + std::string tap_yaml; + std::string tap; + std::string input_type; + std::string input_config; + std::string handler; + }; + const std::string pcap = "pcap"; + std::vector cases; + for (const std::string key : {"policy", "handler", "tap", "le", "quantile"}) { + cases.push_back({key, pcap_tap, "recorded", pcap, pcap_input, "net"}); + } + // direction is a label of the 2.0 handlers only + const std::string v2 = "\n require_version: \"2.0\""; + cases.push_back({"direction", pcap_tap, "recorded", pcap, pcap_input, "net" + v2}); + cases.push_back({"direction", pcap_tap, "recorded", pcap, pcap_input, "dns" + v2}); + cases.push_back({"lat", pcap_tap, "recorded", pcap, pcap_input, "dns"}); + cases.push_back({"device", flow_tap, "flows", "flow", flow_input, "flow"}); + cases.push_back({"device.interface", flow_tap, "flows", "flow", flow_input, "flow"}); + cases.push_back({"target", netprobe_tap, "probes", "netprobe", netprobe_input, "netprobe"}); + cases.push_back({"status_code", netprobe_tap, "probes", "netprobe", netprobe_input, "netprobe"}); + for (const auto &c : cases) { + DYNAMIC_SECTION(c.handler << " handler: " << c.key) + { + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + std::string message; + try { + load(registry, c.tap_yaml, handler_policy("reserved_view", c.tap, c.input_type, c.input_config, c.handler, c.key + ": v")); + } catch (const PolicyException &e) { + message = e.what(); + } + CHECK_THAT(message, ContainsSubstring("'" + c.key + "'")); + CHECK_THAT(message, ContainsSubstring("reserved_view")); + CHECK_FALSE(registry.policy_manager()->module_exists("reserved_view")); + } + } +} + +TEST_CASE("Attributes that no label pktvisor sets are accepted", "[policies][attributes]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, flow_tap, handler_policy("free_view", "flows", "flow", flow_input, "flow", R"(netbox.site.name: dc + netbox_id: "12" + device_ip: 10.0.0.1)"))); + CHECK(registry.policy_manager()->module_exists("free_view")); +} + +TEST_CASE("An attribute named after a static label is refused", "[policies][attributes]") +{ + Metric::reset_static_labels(); + Metric::add_static_label("instance_id", "x"); + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + std::string message; + try { + load(registry, pcap_tap, pcap_policy("static_view", " attributes: {instance.id: v}\n")); + } catch (const PolicyException &e) { + message = e.what(); + } + Metric::reset_static_labels(); + CHECK_THAT(message, ContainsSubstring("'instance.id'")); + CHECK_FALSE(registry.policy_manager()->module_exists("static_view")); +} + +TEST_CASE("A refused policy leaves nothing behind", "[policies][attributes]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + CHECK_THROWS_AS(load(registry, flow_tap, handler_policy("refused_view", "flows", "flow", flow_input, "flow", "device: v")), PolicyException); + CHECK_FALSE(registry.policy_manager()->module_exists("refused_view")); + CHECK_FALSE(registry.handler_manager()->module_exists("refused_view-flows-the_handler")); + CHECK(registry.input_manager()->module_get_keys().empty()); +} + +namespace { + +// A netprobe policy probing gw1 and gw2. Either attributes argument is a YAML flow map, or empty for none. +std::string netprobe_policy(const std::string &name, const std::string &policy_attributes, const std::string &gw1_attributes) +{ + return fmt::format(R"( +visor: + policies: + {}: + kind: collection + {} + input: + tap: probes + input_type: netprobe + config: + test_type: http + interval_msec: 60000 + targets: + gw1: + target: http://localhost:1/ + {} + gw2: + target: http://localhost:1/ + handlers: + modules: + probe: + type: netprobe +)", + name, + policy_attributes.empty() ? "" : "attributes: " + policy_attributes, + gw1_attributes.empty() ? "" : "attributes: " + gw1_attributes); +} + +// Loads a policy that must be refused, and returns the refusal. Nothing the load made may be left behind. +std::string refusal(const std::string &policy_name, const std::string &policy_yaml) +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + std::string message; + try { + load(registry, netprobe_tap, policy_yaml); + } catch (const PolicyException &e) { + message = e.what(); + } + CHECK_FALSE(registry.policy_manager()->module_exists(policy_name)); + CHECK_FALSE(registry.handler_manager()->module_exists(policy_name + "-probes-probe")); + CHECK(registry.input_manager()->module_get_keys().empty()); + return message; +} + +// Records a timeout for gw1 and gw2 on the policy's netprobe handler, as a probe would. False if it has none. +bool feed_targets(Policy *policy) +{ + bool fed = false; + for (auto mod : policy->modules()) { + if (auto probe = dynamic_cast(mod)) { + auto metrics = const_cast(probe->metrics()); + metrics->process_failure(input::netprobe::ErrorType::Timeout, "gw1"); + metrics->process_failure(input::netprobe::ErrorType::Timeout, "gw2"); + fed = true; + } + } + return fed; +} + +} + +TEST_CASE("Netprobe target attributes are parsed from the raw policy YAML", "[policies][attributes]") +{ + auto input = YAML::Load(R"( +input_type: netprobe +config: + test_type: ping + targets: + gw1: + target: 192.0.2.1 + attributes: + zero: "007" + flag: True + gw2: + target: 192.0.2.2 +)"); + CHECK(parse_target_attributes(input, "probe_view") == TargetAttributes{{"gw1", {{"zero", "007"}, {"flag", "True"}}}}); + + CHECK(parse_target_attributes(YAML::Load("input_type: netprobe"), "probe_view").empty()); + CHECK(parse_target_attributes(YAML::Load("{input_type: pcap, config: {pcap_file: x.pcap}}"), "probe_view").empty()); + + std::string message; + try { + parse_target_attributes(YAML::Load("{config: {targets: {gw1: {target: 192.0.2.1, attributes: [a, b]}}}}"), "probe_view"); + } catch (const PolicyException &e) { + message = e.what(); + } + CHECK_THAT(message, ContainsSubstring("probe_view")); + CHECK_THAT(message, ContainsSubstring("gw1")); +} + +TEST_CASE("A target attribute named after a pktvisor label is refused", "[policies][attributes]") +{ + for (const std::string key : {"target", "policy", "rcode", "status_code", "le"}) { + DYNAMIC_SECTION(key) + { + auto message = refusal("target_view", netprobe_policy("target_view", "", "{" + key + ": v}")); + CHECK_THAT(message, ContainsSubstring("'" + key + "'")); + CHECK_THAT(message, ContainsSubstring("gw1")); + CHECK_THAT(message, ContainsSubstring("target_view")); + } + } +} + +TEST_CASE("A target attribute written as the Prometheus label of another policy attribute is refused", "[policies][attributes]") +{ + for (const auto &[policy_key, target_key] : std::vector>{ + {"netbox.site.name", "netbox_site_name"}, + {"netbox_site_name", "netbox.site.name"}}) { + DYNAMIC_SECTION(policy_key << " / " << target_key) + { + auto message = refusal("clash_view", netprobe_policy("clash_view", "{" + policy_key + ": dc-west}", "{" + target_key + ": dc-east}")); + CHECK_THAT(message, ContainsSubstring("'" + policy_key + "'")); + CHECK_THAT(message, ContainsSubstring("'" + target_key + "'")); + CHECK_THAT(message, ContainsSubstring("gw1")); + CHECK_THAT(message, ContainsSubstring("clash_view")); + } + } +} + +TEST_CASE("A netprobe policy's target attributes reach that target's series", "[policies][attributes][prometheus]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, netprobe_tap, netprobe_policy("probe_view", "{netbox.site.name: dc-west}", R"({netbox.site.name: dc-east, netbox_id: netbox-device-1, device_ip: 192.0.2.1, zero: "007"})"))); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("probe_view"); + REQUIRE(feed_targets(policy)); + + std::stringstream out; + policy->prometheus_metrics(out); + size_t gw1 = 0; + size_t gw2 = 0; + for (const auto &line : sample_lines(out.str())) { + CHECK_THAT(line, ContainsSubstring("policy=\"probe_view\"")); + if (line.find("target=\"gw1\"") != std::string::npos) { + ++gw1; + CHECK_THAT(line, ContainsSubstring("netbox_site_name=\"dc-east\"")); + CHECK_THAT(line, ContainsSubstring("netbox_id=\"netbox-device-1\"")); + CHECK_THAT(line, ContainsSubstring("device_ip=\"192.0.2.1\"")); + CHECK_THAT(line, ContainsSubstring("zero=\"007\"")); + } else if (line.find("target=\"gw2\"") != std::string::npos) { + ++gw2; + CHECK_THAT(line, ContainsSubstring("netbox_site_name=\"dc-west\"")); + CHECK(line.find("netbox_id=") == std::string::npos); + } + } + CHECK(gw1 > 0); + CHECK(gw2 > 0); +} + +TEST_CASE("A target attribute too long to be a number reaches its series as written", "[policies][attributes]") +{ + constexpr auto serial = "1234567890123456789012345"; + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, netprobe_tap, netprobe_policy("serial_view", "", fmt::format(R"({{serial: "{}"}})", serial)))); + + auto [policy, lock] = registry.policy_manager()->module_get_locked("serial_view"); + REQUIRE(feed_targets(policy)); + close_windows(policy); + metrics::v1::ScopeMetrics scope; + policy->opentelemetry_metrics(scope); + size_t gw1 = 0; + size_t gw2 = 0; + for (const auto &attrs : datapoint_attributes(scope)) { + auto target = attrs.find("target"); + REQUIRE(target != attrs.end()); + if (target->second == "gw1") { + ++gw1; + CHECK(attrs.at("serial") == serial); + } else if (target->second == "gw2") { + ++gw2; + CHECK(attrs.count("serial") == 0); + } + } + CHECK(gw1 > 0); + CHECK(gw2 > 0); +} + +TEST_CASE("Policies that differ only in their target attributes share one input", "[policies][attributes]") +{ + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, netprobe_tap, netprobe_policy("east_view", "", "{netbox.site.name: dc-east}"))); + REQUIRE_NOTHROW(registry.policy_manager()->load(YAML::Load(netprobe_policy("west_view", "", "{netbox.site.name: dc-west}"))["visor"]["policies"])); + + CHECK(registry.input_manager()->module_get_keys().size() == 1); + for (const auto &[name, site] : std::vector>{{"east_view", "dc-east"}, {"west_view", "dc-west"}}) { + auto [policy, lock] = registry.policy_manager()->module_get_locked(name); + REQUIRE(feed_targets(policy)); + std::stringstream out; + policy->prometheus_metrics(out); + size_t gw1 = 0; + for (const auto &line : sample_lines(out.str())) { + if (line.find("target=\"gw1\"") != std::string::npos) { + ++gw1; + CHECK_THAT(line, ContainsSubstring("netbox_site_name=\"" + site + "\"")); + CHECK_THAT(line, ContainsSubstring("policy=\"" + name + "\"")); + } + } + CHECK(gw1 > 0); + } +} + +TEST_CASE("A malformed target attribute is refused, naming the target and the key", "[policies][attributes]") +{ + for (const std::string value : {"[{x: 1}]", "{x: 1}", "~", "\"\""}) { + DYNAMIC_SECTION(value) + { + auto message = refusal("malformed_view", netprobe_policy("malformed_view", "", "{k: " + value + "}")); + CHECK_THAT(message, ContainsSubstring("'k'")); + CHECK_THAT(message, ContainsSubstring("gw1")); + CHECK_THAT(message, ContainsSubstring("malformed_view")); + } + } +} + +TEST_CASE("The input node a policy builds its input from has no target attributes", "[policies][attributes]") +{ + const auto raw = YAML::Load(R"( +tap: probes +input_type: netprobe +config: + test_type: http + targets: + gw1: + target: http://localhost:1/ + attributes: {serial: "1234567890123456789012345"} + gw2: + target: http://localhost:1/ +)"); + const auto cleaned = without_target_attributes(raw); + CHECK_FALSE(cleaned["config"]["targets"]["gw1"]["attributes"].IsDefined()); + CHECK(cleaned["config"]["targets"]["gw1"]["target"].Scalar() == "http://localhost:1/"); + CHECK(cleaned["config"]["targets"]["gw2"]["target"].Scalar() == "http://localhost:1/"); + CHECK(cleaned["config"]["test_type"].Scalar() == "http"); + CHECK(cleaned["input_type"].Scalar() == "netprobe"); + CHECK(cleaned["tap"].Scalar() == "probes"); + // a clone: the node as read keeps them + CHECK(raw["config"]["targets"]["gw1"]["attributes"]["serial"].Scalar() == "1234567890123456789012345"); + + // nothing to remove: the same node back + for (const std::string yaml : {"{input_type: pcap, config: {pcap_file: x.pcap}}", "{input_type: netprobe}", "[a, b]"}) { + CHECK(YAML::Dump(without_target_attributes(YAML::Load(yaml))) == YAML::Dump(YAML::Load(yaml))); + } +} + +TEST_CASE("A malformed key of a target attribute is refused, naming the target", "[policies][attributes]") +{ + for (const std::string key : {"\"\"", "1st_floor", "_x", "__name__", "\"a\xff\""}) { + DYNAMIC_SECTION(key) + { + auto message = refusal("keyed_view", netprobe_policy("keyed_view", "", "{" + key + ": v}")); + CHECK_THAT(message, ContainsSubstring("gw1")); + CHECK_THAT(message, ContainsSubstring("keyed_view")); + CHECK(is_utf8(message)); + } + } +} + +TEST_CASE("A non-ASCII attribute key is written as a Prometheus label one underscore per character", "[policies][attributes]") +{ + CHECK(prometheus_label_name("sit\xc3\xa9") == "sit_"); + CHECK(prometheus_label_name("a\xe2\x82\xac.b") == "a__b"); + CHECK(prometheus_label_name("netbox.site") == "netbox_site"); +} + +TEST_CASE("A null attributes means none", "[policies][attributes]") +{ + for (const std::string attributes : {" attributes:\n", " attributes: ~\n"}) { + DYNAMIC_SECTION("policy " << attributes) + { + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, pcap_tap, pcap_policy("null_view", attributes))); + auto [policy, lock] = registry.policy_manager()->module_get_locked("null_view"); + CHECK(policy->attributes().empty()); + } + } + for (const std::string attributes : {"attributes:", "attributes: ~"}) { + DYNAMIC_SECTION("target " << attributes) + { + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + REQUIRE_NOTHROW(load(registry, netprobe_tap, netprobe_policy("null_view", "", ""))); + auto yaml = netprobe_policy("null_target_view", "", ""); + yaml.replace(yaml.find(" target: http://localhost:1/"), 0, " " + attributes + "\n"); + REQUIRE_NOTHROW(registry.policy_manager()->load(YAML::Load(yaml)["visor"]["policies"])); + auto [policy, lock] = registry.policy_manager()->module_get_locked("null_target_view"); + REQUIRE(feed_targets(policy)); + std::stringstream out; + policy->prometheus_metrics(out); + CHECK_FALSE(sample_lines(out.str()).empty()); + for (const auto &line : sample_lines(out.str())) { + CHECK_THAT(line, ContainsSubstring("policy=\"null_target_view\"")); + } + } + } + CHECK(parse_target_attributes(YAML::Load("{config: {targets: {gw1: {target: x, attributes: ~}}}}"), "p").empty()); +} + +TEST_CASE("A bad input config value of a policy without target attributes reports its own position", "[policies][attributes]") +{ + auto yaml = netprobe_policy("pos_view", "", ""); + const auto at = yaml.find("interval_msec: 60000"); + yaml.replace(at, 20, "interval_msec: 99999999999999999999999"); + const auto line = 1 + std::count(yaml.begin(), yaml.begin() + at, '\n'); + CoreRegistry registry; + visor::load_builtin_plugins(registry); + registry.start(nullptr); + std::string message; + try { + load(registry, netprobe_tap, yaml); + } catch (const std::exception &e) { + message = e.what(); + } + INFO(message); + CHECK_THAT(message, ContainsSubstring(fmt::format("line {}", line))); + CHECK_THAT(message, !ContainsSubstring("line 1, column 1")); +} + +TEST_CASE("An input node without target attributes is returned as it is", "[policies][attributes]") +{ + const auto raw = YAML::Load("input_type: netprobe\nconfig:\n targets:\n gw1:\n target: x\n"); + const auto same = without_target_attributes(raw); + CHECK(same["config"]["targets"]["gw1"].Mark().line == raw["config"]["targets"]["gw1"].Mark().line); + CHECK(same["config"]["targets"]["gw1"].Mark().line == 4); + CHECK(same.is(raw)); +} diff --git a/src/tests/test_prometheus_serializer.cpp b/src/tests/test_prometheus_serializer.cpp index b5ca92d26..9e6636856 100644 --- a/src/tests/test_prometheus_serializer.cpp +++ b/src/tests/test_prometheus_serializer.cpp @@ -145,3 +145,28 @@ TEST_CASE("per-call label overrides static label of the same key (no duplicate)" CHECK(out.find("static_inst") == std::string::npos); // static value suppressed; key emitted once Metric::reset_static_labels(); } + +TEST_CASE("Label keys outside [A-Za-z0-9_] are written with underscores", "[prometheus]") +{ + Metric::reset_static_labels(); + PrometheusSerializer s; + s.write("m", Type::Gauge, "h", {}, {{"netbox.site.name", "x"}, {"a-b", "y"}, {"ok_key", "z"}}, 1); + CHECK(s.finalize() == + "# HELP m h\n" + "# TYPE m gauge\n" + "m{a_b=\"y\",netbox_site_name=\"x\",ok_key=\"z\"} 1\n"); +} + +TEST_CASE("A per-call label replaces a static label with the same written name", "[prometheus]") +{ + Metric::reset_static_labels(); + Metric::add_static_label("a.b", "static"); + PrometheusSerializer s; + s.write("m", Type::Gauge, "h", {}, {{"a_b", "call"}}, 1); + auto out = s.finalize(); + Metric::reset_static_labels(); + CHECK(out == + "# HELP m h\n" + "# TYPE m gauge\n" + "m{a_b=\"call\"} 1\n"); +}