mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Ensure the order of label values matches the label names
This commit is contained in:
parent
87717fed0a
commit
b1578d4ded
2 changed files with 33 additions and 19 deletions
|
@ -259,6 +259,32 @@ static bool compare_histograms(const std::optional<ZVal>& a, const std::optional
|
||||||
return comparer(a, b, metric_record_type);
|
return comparer(a, b, metric_record_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VectorValPtr build_label_values_vector(const std::vector<prometheus::ClientMetric::Label>& prom_labels,
|
||||||
|
const VectorValPtr& record_label_names) {
|
||||||
|
static auto string_vec_type = zeek::id::find_type<zeek::VectorType>("string_vec");
|
||||||
|
auto label_values_vec = make_intrusive<VectorVal>(string_vec_type);
|
||||||
|
|
||||||
|
// This feels really bad, since it's an O(m*n) search to bulld the vector,
|
||||||
|
// but prometheus-cpp returns us a vector of labels and so we just have to
|
||||||
|
// search through it.
|
||||||
|
int i = 0;
|
||||||
|
for ( const auto& name : record_label_names->RawVec() ) {
|
||||||
|
auto n = name->AsString()->ToStdStringView();
|
||||||
|
auto it = std::find_if(prom_labels.begin(), prom_labels.end(),
|
||||||
|
[n](const prometheus::ClientMetric::Label& l) { return l.name == n; });
|
||||||
|
if ( it != prom_labels.end() )
|
||||||
|
label_values_vec->Assign(i, make_intrusive<StringVal>(it->value));
|
||||||
|
|
||||||
|
// See the comment in GetMetricOptsRecord about how labels from non-Zeek
|
||||||
|
// metrics within the same family can have different labels from each
|
||||||
|
// other. In this case we might leave some fields null in the output.
|
||||||
|
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return label_values_vec;
|
||||||
|
}
|
||||||
|
|
||||||
ValPtr Manager::CollectMetrics(std::string_view prefix_pattern, std::string_view name_pattern) {
|
ValPtr Manager::CollectMetrics(std::string_view prefix_pattern, std::string_view name_pattern) {
|
||||||
static auto metrics_vector_type = zeek::id::find_type<VectorType>("Telemetry::MetricVector");
|
static auto metrics_vector_type = zeek::id::find_type<VectorType>("Telemetry::MetricVector");
|
||||||
static auto string_vec_type = zeek::id::find_type<zeek::VectorType>("string_vec");
|
static auto string_vec_type = zeek::id::find_type<zeek::VectorType>("string_vec");
|
||||||
|
@ -287,17 +313,11 @@ ValPtr Manager::CollectMetrics(std::string_view prefix_pattern, std::string_view
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
RecordValPtr opts_record = GetMetricOptsRecord(fam);
|
RecordValPtr opts_record = GetMetricOptsRecord(fam);
|
||||||
|
const auto& label_names = opts_record->GetField<VectorVal>("labels");
|
||||||
|
|
||||||
for ( const auto& inst : fam.metric ) {
|
for ( const auto& inst : fam.metric ) {
|
||||||
auto label_values_vec = make_intrusive<VectorVal>(string_vec_type);
|
|
||||||
for ( const auto& label : inst.label ) {
|
|
||||||
// We don't include the endpoint key/value unless it's a prometheus request
|
|
||||||
if ( label.name != "endpoint" )
|
|
||||||
label_values_vec->Append(make_intrusive<StringVal>(label.value));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto r = make_intrusive<zeek::RecordVal>(metric_record_type);
|
auto r = make_intrusive<zeek::RecordVal>(metric_record_type);
|
||||||
r->Assign(labels_idx, label_values_vec);
|
r->Assign(labels_idx, build_label_values_vector(inst.label, label_names));
|
||||||
r->Assign(opts_idx, opts_record);
|
r->Assign(opts_idx, opts_record);
|
||||||
|
|
||||||
if ( fam.type == prometheus::MetricType::Counter )
|
if ( fam.type == prometheus::MetricType::Counter )
|
||||||
|
@ -360,17 +380,11 @@ ValPtr Manager::CollectHistogramMetrics(std::string_view prefix_pattern, std::st
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
RecordValPtr opts_record = GetMetricOptsRecord(fam);
|
RecordValPtr opts_record = GetMetricOptsRecord(fam);
|
||||||
|
const auto& label_names = opts_record->GetField<VectorVal>("labels");
|
||||||
|
|
||||||
for ( const auto& inst : fam.metric ) {
|
for ( const auto& inst : fam.metric ) {
|
||||||
auto label_values_vec = make_intrusive<VectorVal>(string_vec_type);
|
|
||||||
for ( const auto& label : inst.label ) {
|
|
||||||
// We don't include the endpoint key/value unless it's a prometheus request
|
|
||||||
if ( label.name != "endpoint" )
|
|
||||||
label_values_vec->Append(make_intrusive<StringVal>(label.value));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto r = make_intrusive<zeek::RecordVal>(histogram_metric_type);
|
auto r = make_intrusive<zeek::RecordVal>(histogram_metric_type);
|
||||||
r->Assign(labels_idx, label_values_vec);
|
r->Assign(labels_idx, build_label_values_vector(inst.label, label_names));
|
||||||
r->Assign(opts_idx, opts_record);
|
r->Assign(opts_idx, opts_record);
|
||||||
|
|
||||||
auto double_values_vec = make_intrusive<zeek::VectorVal>(double_vec_type);
|
auto double_values_vec = make_intrusive<zeek::VectorVal>(double_vec_type);
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
XXXXXXXXXX.XXXXXX zeek counter zeek_log_stream_writes_total module,stream Conn,Conn::LOG 34.0
|
XXXXXXXXXX.XXXXXX zeek counter zeek_log_stream_writes_total module,stream Conn,Conn::LOG 34.0
|
||||||
XXXXXXXXXX.XXXXXX zeek counter zeek_log_stream_writes_total module,stream DNS,DNS::LOG 34.0
|
XXXXXXXXXX.XXXXXX zeek counter zeek_log_stream_writes_total module,stream DNS,DNS::LOG 34.0
|
||||||
XXXXXXXXXX.XXXXXX zeek counter zeek_log_stream_writes_total module,stream HTTP,HTTP::LOG 14.0
|
XXXXXXXXXX.XXXXXX zeek counter zeek_log_stream_writes_total module,stream HTTP,HTTP::LOG 14.0
|
||||||
XXXXXXXXXX.XXXXXX zeek counter zeek_log_writer_writes_total writer,module,stream,filter-name,path default,Conn,conn,Conn::LOG,Log::WRITER_ASCII 30.0
|
XXXXXXXXXX.XXXXXX zeek counter zeek_log_writer_writes_total writer,module,stream,filter-name,path Log::WRITER_ASCII,Conn,Conn::LOG,-,conn 30.0
|
||||||
XXXXXXXXXX.XXXXXX zeek counter zeek_log_writer_writes_total writer,module,stream,filter-name,path default,DNS,dns,DNS::LOG,Log::WRITER_ASCII 23.0
|
XXXXXXXXXX.XXXXXX zeek counter zeek_log_writer_writes_total writer,module,stream,filter-name,path Log::WRITER_ASCII,DNS,DNS::LOG,-,dns 23.0
|
||||||
XXXXXXXXXX.XXXXXX zeek counter zeek_log_writer_writes_total writer,module,stream,filter-name,path default,HTTP,http,HTTP::LOG,Log::WRITER_ASCII 10.0
|
XXXXXXXXXX.XXXXXX zeek counter zeek_log_writer_writes_total writer,module,stream,filter-name,path Log::WRITER_ASCII,HTTP,HTTP::LOG,-,http 10.0
|
||||||
#close XXXX-XX-XX-XX-XX-XX
|
#close XXXX-XX-XX-XX-XX-XX
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue