Skip to content

Commit

Permalink
cleanup(libsinsp): metrics text - pass by string_view
Browse files Browse the repository at this point in the history
Co-authored-by: Federico Aponte <federico.aponte@sysdig.com>
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
  • Loading branch information
incertum and federico-sysdig committed Jan 31, 2024
1 parent d070b52 commit d86a915
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions userspace/libsinsp/metrics_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,9 @@ void metrics_collector::snapshot()
}
}

std::string metrics_collector::convert_metric_to_prometheus_text(std::string metric_name, metrics_v2 metric)
std::string metrics_collector::convert_metric_to_prometheus_text(std::string_view metric_name, metrics_v2 metric)
{
std::string prometheus_text = metric_name;
std::string prometheus_text(metric_name.begin(), metric_name.end());
prometheus_text += "{raw_name=\"" + std::string(metric.name) + "\",unit=\"" + std::string(metrics_unit_name_mappings[metric.unit]) \
+ "\",metric_type=\"" + std::string(metrics_metric_type_name_mappings[metric.metric_type]) + "\"} "; // white space at the end important!
switch (metric.type)
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/metrics_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include <libsinsp/threadinfo.h>
#include <libscap/strl.h>
#include <cmath>
#include <string_view>

struct sinsp_stats_v2
{
Expand Down Expand Up @@ -107,7 +108,7 @@ class metrics_collector
*
* This method is a work in progress.
*/
std::string convert_metric_to_prometheus_text(std::string metric_name, metrics_v2 metric);
std::string convert_metric_to_prometheus_text(std::string_view metric_name, metrics_v2 metric);

/*!
\brief Method to convert memory units; tied to metrics_v2 definitions
Expand Down
3 changes: 2 additions & 1 deletion userspace/libsinsp/test/sinsp_metrics.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ TEST_F(sinsp_with_test_input, sinsp_metrics_collector)
// This resembles the Falco client use case
char metric_name[METRIC_NAME_MAX] = "test.";
strlcat(metric_name, metric.name, sizeof(metric_name));
std::string prometheus_text = metrics_collector->convert_metric_to_prometheus_text(metric_name, metric);
std::string_view s(metric_name);
std::string prometheus_text = metrics_collector->convert_metric_to_prometheus_text(s, metric);
if (strncmp(metric.name, "n_missing_container_images", 17) == 0)
{
std::string prometheus_text_substring = "test.n_missing_container_images{raw_name=\"n_missing_container_images\",unit=\"COUNT\",metric_type=\"NON_MONOTONIC_CURRENT\"} 0";
Expand Down

0 comments on commit d86a915

Please sign in to comment.