Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup(userspace/libsinsp): improved sinsp metrics_collector to make it more future proof #1745

Merged
merged 1 commit into from
Apr 16, 2024

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Mar 13, 2024

What type of PR is this?

/kind cleanup

Any specific area of the project related to this PR?

/area libsinsp

Does this PR require a change in the driver versions?

What this PR does / why we need it:

A bit tedious but i quite like how it looks now. Basically, refactored metrics_collector in libsinsp to enforce that every declared metric MUST be actually used.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

@FedeDP
Copy link
Contributor Author

FedeDP commented Mar 13, 2024

/cc @incertum
/milestone 0.16.0

@incertum
Copy link
Contributor

Wow @FedeDP 🤣 so fast? @federico-sysdig mind checking given #1693.

@FedeDP FedeDP force-pushed the cleanup/improve_sinsp_metrics_collector branch from 9f04660 to eb107a3 Compare March 13, 2024 15:50
@FedeDP
Copy link
Contributor Author

FedeDP commented Mar 13, 2024

Will double check/fix tests tomorrow :)

std::shared_ptr<const sinsp_stats_v2> sinsp_stats_v2 = m_inspector->get_sinsp_stats_v2();

const std::function<metrics_v2()> sinsp_stats_v2_collectors[] = {
[SINSP_RESOURCE_UTILIZATION_CPU_PERC] = [this,&cpu_usage_perc]() {
Copy link
Contributor

@federico-sysdig federico-sysdig Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is using C99 array designated initializers - available in C++ only through a gcc extension - in a C++ context (lambdas, this, etc.). The compiler allows us to get away with this, so I can live with it and throw away PR #1693 that was supposed to overcome this stretched usage of the language.
It's just that it feels like a lot of machinery to fill an array (sinsp_stats_v2_collectors), that will be used to fill a vector (m_metrics), that is used only in tests, IIUC; at least for the moment, there's probably more use of this lying ahead.

I don't have a solution, but it seems to me there ought to be a simpler way to reach the goal. Have basic C99 designated initializers for structs been tried?
Something like this.

  metrics_v2 sinsp_stats_v2_collectors[] = {
      [SINSP_RESOURCE_UTILIZATION_CPU_PERC] = {
          .name = "cpu_usage_perc",
          .flags = METRICS_V2_RESOURCE_UTILIZATION,
          .metric_type = METRIC_VALUE_METRIC_TYPE_NON_MONOTONIC_CURRENT,
          .value = { .u32 = cpu_usage_perc },
          .type = METRIC_VALUE_TYPE_D,
          .unit = METRIC_VALUE_UNIT_PERC
      },
      // ...
  };

Perhaps it looks a tad verbose, but it is pure C, hence it can stay in a separate C file where this construct is legal; it needs no new_metric template function, no need for lambdas used as factories, and it would represent all the information on metrics in a sort of "tabular" format, as if it were a metrics "data sheet".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just that it feels like a lot of machinery to fill an array (sinsp_stats_v2_collectors), that will be used to fill a vector (m_metrics), that is used only in tests, IIUC; at least for the moment, there's probably more use of this lying ahead.

It's all used in Falco, see for example this PR falcosecurity/falco#3129 where I bump libs.

I probably can't add a lot of value here, other than saying that I liked having what you @federico-sysdig described as

information on metrics in a sort of "tabular" format, as if it were a metrics "data sheet".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it looks a tad verbose, but it is pure C, hence it can stay in a separate C file where this construct is legal; it needs no new_metric template function, no need for lambdas used as factories, and it would represent all the information on metrics in a sort of "tabular" format, as if it were a metrics "data sheet".

While i agree this would be a simple yet descriptive table, i don't get how would one link in the proper value: in your example your are copying cpu_usage_perc by value; does it mean that you expect the table to be defined after all metrics are collected?
Or would we use pointers for the value?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the lambda is to get a value that dynamically changed. Now I see it. What was wrong with the original implementation, then? The fact that there was no explicit link between the index that identifies the metric and its initialization in the vector?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the lambda is to get a value that dynamically changed

Yep :)

What was wrong with the original implementation, then?

The fact that we had no guarantee that a metric whose name was declared in the initial array was then actually used and pushed in code below; now we have that guarantee.
While it might seem "minor", considering the number of metrics we already have (plus more in the future perhaps?) it gets super error prone.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I want to add complexity to the build system but I am wondering whether we should consider code generation to create this table ?
Have one file that encodes all the parameters that defines a metrics and then something parsing it and generating the adequate C code to build it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem would still be how to "capture" the referenced value without doing weird stuff.
Also, yep not a big fan of adding build time complexity!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @jasondellaluce for more ideas :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any consensus re what's an improvement vs what would be too much? Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind bump re consensus :) thanks.

@FedeDP FedeDP force-pushed the cleanup/improve_sinsp_metrics_collector branch from eb107a3 to fe93981 Compare March 14, 2024 07:07
@FedeDP
Copy link
Contributor Author

FedeDP commented Mar 14, 2024

CI Build / run-e2e-tests-amd64 (asan) (pull_request) Failing after 41m

This test fails on every PR and on master :/

… it more future proof.

Signed-off-by: Federico Di Pierro <[email protected]>
@FedeDP FedeDP force-pushed the cleanup/improve_sinsp_metrics_collector branch from fe93981 to 2733774 Compare March 21, 2024 08:02
@FedeDP
Copy link
Contributor Author

FedeDP commented Mar 21, 2024

Rebased on top of master to fix CI.

Copy link
Member

@Andreagit97 Andreagit97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

Copy link
Contributor

@incertum incertum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@poiana
Copy link
Contributor

poiana commented Apr 16, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Andreagit97, FedeDP, incertum

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [Andreagit97,FedeDP,incertum]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit b0da658 into master Apr 16, 2024
43 checks passed
@poiana poiana deleted the cleanup/improve_sinsp_metrics_collector branch April 16, 2024 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants