Make all options const-redefs, remove all of the change handler code

This commit is contained in:
Tim Wojtulewicz 2024-03-12 09:55:20 -07:00
parent 17d09c657b
commit 643bb38419
4 changed files with 5 additions and 197 deletions

View file

@ -14,13 +14,13 @@ export {
## Frequency for publishing scraped metrics to the target topic. Zeek
## overrides any value provided in zeek_init or earlier at startup if
## the environment variable ZEEK_METRICS_EXPORT_INTERVAL is defined.
option metrics_export_interval = 1 sec;
const metrics_export_interval = 1 sec &redef;
## Target topic for the metrics. Setting a non-empty string starts the
## periodic publishing of local metrics. Zeek overrides any value
## provided in zeek_init or earlier at startup if the environment
## variable ZEEK_METRICS_EXPORT_TOPIC is defined.
option metrics_export_topic = "";
const metrics_export_topic = "" &redef;
## Topics for the telmeetry framework for collecting metrics from other
## peers in the network and including them in the output. Has no effect
@ -28,7 +28,7 @@ export {
##
## Zeek overrides any value provided in zeek_init or earlier at startup
## if the environment variable ZEEK_METRICS_IMPORT_TOPICS is defined.
option metrics_import_topics: vector of string = vector();
const metrics_import_topics: vector of string = vector() &redef;
## ID for the metrics exporter. When setting a target topic for the
## exporter, Broker sets this option to the suffix of the new topic
@ -38,72 +38,10 @@ export {
## good-enough ID. Zeek overrides any value provided in zeek_init or
## earlier at startup if the environment variable
## ZEEK_METRICS_ENDPOINT_NAME is defined.
option metrics_export_endpoint_name = "";
const metrics_export_endpoint_name = "" &redef;
## Selects prefixes from the local metrics. Only metrics with prefixes
## listed in this variable are included when publishing local metrics.
## Setting an empty vector selects *all* metrics.
option metrics_export_prefixes: vector of string = vector();
}
# Needed for the __set methods below
@load base/bif/telemetry.bif
function update_metrics_export_interval(id: string, val: interval): interval
{
Telemetry::__set_metrics_export_interval(val);
return val;
}
function update_metrics_export_topic(id: string, val: string): string
{
Telemetry::__set_metrics_export_topic(val);
return val;
}
function update_metrics_import_topics(id: string, topics: vector of string): vector of string
{
Telemetry::__set_metrics_import_topics(topics);
return topics;
}
function update_metrics_export_endpoint_name(id: string, val: string): string
{
Telemetry::__set_metrics_export_endpoint_name(val);
return val;
}
function update_metrics_export_prefixes(id: string, filter: vector of string): vector of string
{
Telemetry::__set_metrics_export_prefixes(filter);
return filter;
}
event zeek_init()
{
# interval
update_metrics_export_interval("Telemetry::metrics_export_interval",
Telemetry::metrics_export_interval);
Option::set_change_handler("Telemetry::metrics_export_interval",
update_metrics_export_interval);
# topic
update_metrics_export_topic("Telemetry::metrics_export_topic",
Telemetry::metrics_export_topic);
Option::set_change_handler("Telemetry::metrics_export_topic",
update_metrics_export_topic);
# import topics
update_metrics_import_topics("Telemetry::metrics_import_topics",
Telemetry::metrics_import_topics);
Option::set_change_handler("Telemetry::metrics_import_topics",
update_metrics_import_topics);
# endpoint name
update_metrics_export_endpoint_name("Telemetry::metrics_export_endpoint_name",
Telemetry::metrics_export_endpoint_name);
Option::set_change_handler("Telemetry::metrics_export_endpoint_name",
update_metrics_export_endpoint_name);
# prefixes
update_metrics_export_prefixes("Telemetry::metrics_export_prefixes",
Telemetry::metrics_export_prefixes);
Option::set_change_handler("Telemetry::metrics_export_prefixes",
update_metrics_export_prefixes);
const metrics_export_prefixes: vector of string = vector() &redef;
}

View file

@ -251,43 +251,6 @@ ValPtr Manager::CollectHistogramMetrics(std::string_view prefix_pattern, std::st
return ret_val;
}
/**
* Changes the frequency for publishing scraped metrics to the target topic.
* Passing a zero-length interval has no effect.
* @param value Interval between two scrapes in seconds.
*/
void Manager::SetMetricsExportInterval(double value) { export_interval = value; }
/**
* Sets a new target topic for the metrics. Passing an empty string has no
* effect.
* @param value The new topic for publishing local metrics to.
*/
void Manager::SetMetricsExportTopic(std::string value) { export_topic = std::move(value); }
/**
* Sets the import topics for a node importing metrics.
*
* @param topics List of topics from which to import metrics.
*/
void Manager::SetMetricsImportTopics(std::vector<std::string> topics) { import_topics = std::move(topics); }
/**
* Sets a new ID for the metrics exporter. Passing an empty string has no
* effect.
* @param value The new ID of the exporter in published metrics.
*/
void Manager::SetMetricsExportEndpointName(std::string value) { export_endpoint = std::move(value); }
/**
* Sets a prefix selection for the metrics exporter. An empty vector selects
* *all* metrics.
* @param filter List of selected metric prefixes or an empty vector for
* selecting all metrics.
*/
void Manager::SetMetricsExportPrefixes(std::vector<std::string> filter) { export_prefixes = std::move(filter); }
} // namespace zeek::telemetry
// -- unit tests ---------------------------------------------------------------

View file

@ -325,46 +325,6 @@ public:
return HistogramInstance<ValueType>(prefix, name, lbls, bounds, helptext, unit);
}
/**
* Changes the frequency for publishing scraped metrics to the target topic.
* Passing a zero-length interval has no effect.
* @param value Interval between two scrapes in seconds.
*/
void SetMetricsExportInterval(double value);
/**
* Sets a new target topic for the metrics. Passing an empty string has no
* effect.
* @param value The new topic for publishing local metrics to.
*/
void SetMetricsExportTopic(std::string value);
/**
* Sets the import topics for a node importing metrics.
*
* @param topics List of topics from which to import metrics.
*/
void SetMetricsImportTopics(std::vector<std::string> topics);
/**
* Sets a new ID for the metrics exporter. Passing an empty string has no
* effect.
* @param value The new ID of the exporter in published metrics.
*/
void SetMetricsExportEndpointName(std::string value);
/**
* Sets a prefix selection for the metrics exporter. An empty vector selects
* *all* metrics.
* @param filter List of selected metric prefixes or an empty vector for
* selecting all metrics.
*/
void SetMetricsExportPrefixes(std::vector<std::string> filter);
bool IsExporting() const { return ! export_topic.empty() && ! export_endpoint.empty(); }
const std::string& MetricsSchema() const { return metrics_schema; }
std::shared_ptr<MetricFamily> GetFamilyByFullName(const std::string& full_name) const {
if ( auto it = families.find(full_name); it != families.end() )
return it->second;
@ -393,8 +353,6 @@ protected:
private:
std::shared_ptr<MetricFamily> LookupFamily(std::string_view prefix, std::string_view name) const;
std::string metrics_schema;
std::shared_ptr<OtelReader> otel_reader;
std::map<std::string, std::shared_ptr<MetricFamily>> families;

View file

@ -494,54 +494,3 @@ function Telemetry::__collect_histogram_metrics%(prefix: string, name: string%):
%{
return telemetry_mgr->CollectHistogramMetrics(sv(prefix), sv(name));
%}
function Telemetry::__set_metrics_export_interval%(value: interval%): bool
%{
// This BIF may run prior to telemetry::Manager::InitPostScript. In this case,
// telemetry_mgr is still null but we can safely ignore this event because the
// Manager is going to initialize Telemetry using the most recent value of the
// corresponding option.
if ( telemetry_mgr )
telemetry_mgr->SetMetricsExportInterval(value);
return zeek::val_mgr->True();
%}
function Telemetry::__set_metrics_export_topic%(value: string%): bool
%{
if ( telemetry_mgr )
telemetry_mgr->SetMetricsExportTopic(value->ToStdString());
return zeek::val_mgr->True();
%}
function Telemetry::__set_metrics_import_topics%(filter: string_vec%): bool
%{
if ( telemetry_mgr )
{
std::vector<std::string> slist;
auto* vval = filter->AsVectorVal();
for ( unsigned index = 0; index < vval->Size(); ++index )
slist.emplace_back(vval->StringValAt(index)->ToStdString());
telemetry_mgr->SetMetricsImportTopics(std::move(slist));
}
return zeek::val_mgr->True();
%}
function Telemetry::__set_metrics_export_endpoint_name%(value: string%): bool
%{
if ( telemetry_mgr )
telemetry_mgr->SetMetricsExportEndpointName(value->ToStdString());
return zeek::val_mgr->True();
%}
function Telemetry::__set_metrics_export_prefixes%(filter: string_vec%): bool
%{
if ( telemetry_mgr )
{
std::vector<std::string> slist;
auto* vval = filter->AsVectorVal();
for ( unsigned index = 0; index < vval->Size(); ++index )
slist.emplace_back(vval->StringValAt(index)->ToStdString());
telemetry_mgr->SetMetricsExportPrefixes(std::move(slist));
}
return zeek::val_mgr->True();
%}