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

filterx: update_metric() #220

Merged
merged 22 commits into from
Aug 6, 2024
Merged

Conversation

alltilla
Copy link
Member

@alltilla alltilla commented Jul 22, 2024

Example:

update_metric("filterx_metric", labels={"msg": $MSG, "foo": "foovalue"}, level=1, increment=$INCREMENT)

@alltilla alltilla force-pushed the filterx-metrics-probe branch 4 times, most recently from d3d37cc to db74448 Compare July 25, 2024 10:53
@alltilla alltilla changed the title filterx: metrics_probe() filterx: update_metric() Jul 25, 2024
@alltilla alltilla force-pushed the filterx-metrics-probe branch 4 times, most recently from 27789cc to 5a8a604 Compare July 26, 2024 10:07
@alltilla
Copy link
Member Author

Ready for review.

@alltilla alltilla requested review from bshifter and MrAnno July 26, 2024 10:08
@alltilla alltilla force-pushed the filterx-metrics-probe branch from 5a8a604 to 496b2a0 Compare August 2, 2024 11:04
OverOrion
OverOrion previously approved these changes Aug 2, 2024
@alltilla alltilla force-pushed the filterx-metrics-probe branch from 496b2a0 to 595a7d2 Compare August 3, 2024 08:02
@alltilla
Copy link
Member Author

alltilla commented Aug 3, 2024

rebased to main

gboolean
filterx_metrics_labels_format(FilterXMetricsLabels *self, StatsClusterLabel **labels, gsize *len)
{
metrics_cache_reset_labels(metrics_tls_cache());
Copy link
Member

Choose a reason for hiding this comment

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

is there a point in saving the metrics_tls_cache() value and not do it 3x?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed in a new commit.

return FALSE;
}

StatsClusterLabel *label = metrics_cache_alloc_label(metrics_tls_cache());
Copy link
Member

Choose a reason for hiding this comment

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

actually it might even make sense to cache the metrics_tls_cache() across the entire iteration. Getting the value of a TLS variable is not free, especially when wrapped into an external function, like in this case.

Copy link
Member

Choose a reason for hiding this comment

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

Just to clarify, I meant that it would make sense to pass the value around to callbacks that iterate possibly multiple times per message to avoid the TLS overhead.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did not think about the overhead. Thanks, fixed this in a new commit.

return FALSE;
}

self->key.str = g_strdup(filterx_string_get_value(key_obj, NULL));
Copy link
Member

Choose a reason for hiding this comment

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

probably filterx_object_extract_string() would make sense here (depends on PR #229)

Copy link
Member

Choose a reason for hiding this comment

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

maybe the plan is to merge this first, in that case of course #229 should address this.

Copy link
Member Author

Choose a reason for hiding this comment

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

In this specific scenario, we don't need to call the extract function, as we check before that it is a literal, and there are no literal message values.

}

GString *name_buffer = scratch_buffers_alloc();
g_string_append(name_buffer, name);
Copy link
Member

Choose a reason for hiding this comment

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

I guess you were trying to avoid a g_strdup() here. in that case, it makes sense to use g_string_append_len(), otherwise you'd be doing an strlen() as a part of the copy, while the length is already available.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed and squashed to the original commit.

return TRUE;
}

FilterXObject *increment_obj = filterx_expr_eval_typed(self->increment.expr);
Copy link
Member

Choose a reason for hiding this comment

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

extract_integer.

Copy link
Member

Choose a reason for hiding this comment

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

would depend on #229, so depends on merge order.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

exit:
if (!success)
{
msg_error("FilterX: Failed to process update_metric()", filterx_format_last_error());
Copy link
Member

Choose a reason for hiding this comment

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

I think it is a bad idea to start generating a message in these cases. I think it'd be much better to add an "error" counter along with all update_metric() counters and increment that every time something fails.

With that we could even add limits on how many counters we would be adding in case there's a cardinality explosion.

it does not need to be done now, just an idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, changed the log level to debug in a new commit. We can introduce a counter later.

*/
if (!self->is_optimized)
{
if (!_optimize(self))
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs to be protected by a lock, quite probably we can reuse the starts_lock() in this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! Fixed in a new commit.

bazsi
bazsi previously approved these changes Aug 4, 2024
@alltilla alltilla dismissed stale reviews from bazsi and OverOrion via 16a8251 August 6, 2024 07:38
@alltilla alltilla force-pushed the filterx-metrics-probe branch from 595a7d2 to 16a8251 Compare August 6, 2024 07:38
@alltilla
Copy link
Member Author

alltilla commented Aug 6, 2024

Thanks for the thorough review @bazsi.

I have fixed all the comments, most of them went to new commits to make it easier to review, starting with the commit filterx/metrics-labels: reduce the number of tls cache acquirals

The order of this PR and #229 is undecided, I will update the one which gets merged later.

alltilla added 22 commits August 6, 2024 10:11
Signed-off-by: Attila Szakacs <[email protected]>
This class is responsible for formatting a `FilterXExpr`
to an array of `StatsClusterLabel`s.

The expression must be evaluate to a `Dict[str, Any]`.
The formatting is cached wherever it is possible.

The implementation uses TLS metrics cache, as `FilterXExpr`s
must be stateless currently, but if the need arises, the code
can be easily changed to use an arbitrary metrics cache.

Signed-off-by: Attila Szakacs <[email protected]>
Signed-off-by: Attila Szakacs <[email protected]>
This class is responsible for managing `StatsCounterItem`s
based on `key` and `labels` `FilterXExpr`s.

`key` must evaluate to a `str`.
`labels` must evaluate to a `Dict[str, Any]` is handled by
`FilterXMetricsLabels`.

The implementation uses TLS metrics cache, as `FilterXExpr`s
must be stateless currently, but if the need arises, the code
can be easily changed to use an arbitrary metrics cache

Signed-off-by: Attila Szakacs <[email protected]>
We need to delay the optimization to the first get() call
as we don't have stats options when FilterXExprs are being
created.

Signed-off-by: Attila Szakacs <[email protected]>
It is not used anywhere.

Signed-off-by: Attila Szakacs <[email protected]>
This could easily flood the logs if something is
incorrectly configured.

Signed-off-by: Attila Szakacs <[email protected]>
If optimization did not succeed, probably it won't
in the future, so just skip it.

Signed-off-by: Attila Szakacs <[email protected]>
@alltilla alltilla force-pushed the filterx-metrics-probe branch from 16a8251 to ab668f7 Compare August 6, 2024 11:12
@alltilla alltilla merged commit 8581cd8 into axoflow:main Aug 6, 2024
22 checks passed
fekete-robert pushed a commit to axoflow/axosyslog-core-docs that referenced this pull request Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants