Skip to content

api: add selector-based filtering for custom metrics#543

Merged
patapenka-alexey merged 1 commit into
masterfrom
mandesero/tnt-11358-expose-custom-metrics
Jun 9, 2026
Merged

api: add selector-based filtering for custom metrics#543
patapenka-alexey merged 1 commit into
masterfrom
mandesero/tnt-11358-expose-custom-metrics

Conversation

@mandesero

@mandesero mandesero commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

This patch adds selector-based filtering for custom metrics.

Before this patch, the metrics module allowed applications to register custom collectors and callbacks, but there was no common way to group them and enable or disable them later through metrics.cfg().

The patch introduces selectors for custom metrics. A selector may be attached to a collector or callback via metadata:

local metrics = require('metrics')

metrics.register_callback(function()
    in_flight:set(get_in_flight())
end, {selector = 'roles.crud-router'})

For convenience, metrics may also be registered through a namespace:

local metrics = require('metrics')

local crud = metrics.namespace('roles.crud-router')

crud:gauge('crud_requests'):set(1)
crud:gauge('crud_errors'):set(1)

The namespace prefixes registered collectors with hierarchical selectors:

roles.crud-router.crud_requests
roles.crud-router.crud_errors

Selectors can be filtered with the new metrics.set_filter() API:

metrics.set_filter(
    {{selector = 'roles.crud-router'}},
    {{selector = 'roles.crud-router.crud_errors'}}
)

include enables matching selectors, exclude disables matching selectors, and exclude has priority. Matching is hierarchical, so roles.crud-router matches roles.crud-router.crud_requests.

metrics.cfg() now also accepts custom selector strings in include and exclude:

metrics.cfg{
    include = {'info', 'roles.crud-router'},
    exclude = {'roles.crud-router.crud_errors'},
}

Built-in Tarantool metric groups keep the existing behavior. Unknown strings in include / exclude are treated as custom selectors and are applied through the registry filter.

This lets applications and Tarantool roles expose custom metrics while still using the existing metrics.include / metrics.exclude configuration model.

Part of tarantool/tarantool#11358

@mandesero mandesero force-pushed the mandesero/tnt-11358-expose-custom-metrics branch from 2b758f7 to 6a2f505 Compare June 1, 2026 10:28
@mandesero mandesero changed the title namespace and selectors for custom metrics [WIP] api: add selector-based filtering for custom metrics Jun 1, 2026
@mandesero mandesero marked this pull request as ready for review June 1, 2026 10:33
@patapenka-alexey

patapenka-alexey commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Please add this test, it indicates break in backward compatibility.

group.test_cfg_clean_user_metrics = function(cg)
    cg.server:exec(function()
        local metrics = require('metrics')

        -- Register a custom metric WITHOUT a selector (how all apps did it before)
        local my_gauge = metrics.gauge('my_custom_metric', 'My custom metric')
        my_gauge:set(42)

        metrics.cfg{
            include = {'info'}, -- before the patch, it turn off only default metrics, but now it hides custom metrics
            exclude = {},
        }

        local observations = metrics.collect{invoke_callbacks = true}

        local found = false
        for _, obs in ipairs(observations) do
            if obs.metric_name == 'my_custom_metric' then
                found = true
                break
            end
        end

        t.assert_equals(found, true)
    end)
end

@mandesero mandesero force-pushed the mandesero/tnt-11358-expose-custom-metrics branch from 6a2f505 to 895bc56 Compare June 2, 2026 14:08
@patapenka-alexey patapenka-alexey requested review from bigbes and removed request for oleg-jukovec June 3, 2026 11:37

@bigbes bigbes left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ty for you patch! Logic seems legit, but it's new functionality test coverage worth to be extended (for example - more cases for 'set_filter', reconfiguration, etc)

Comment thread metrics/cfg.lua Outdated
Comment thread metrics/registry.lua Outdated
Comment thread metrics/registry.lua Outdated
Comment thread metrics/api.lua
@mandesero mandesero force-pushed the mandesero/tnt-11358-expose-custom-metrics branch 2 times, most recently from 586a244 to 41a7c22 Compare June 9, 2026 09:58
@mandesero mandesero requested a review from bigbes June 9, 2026 10:11

@patapenka-alexey patapenka-alexey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for the patch!

This patch adds selector-based filtering for custom metrics. Custom
metrics can now be associated with hierarchical selectors and controlled
via `metrics.set_filter()` or `metrics.cfg()` include/exclude options.

Unknown include/exclude entries are treated as custom selectors, while
built-in metric group names keep the existing behavior.

Part of tarantool/tarantool#11358
@mandesero mandesero force-pushed the mandesero/tnt-11358-expose-custom-metrics branch from 41a7c22 to 9a9984d Compare June 9, 2026 11:36
@patapenka-alexey patapenka-alexey merged commit bb82e8c into master Jun 9, 2026
16 checks passed
@patapenka-alexey patapenka-alexey deleted the mandesero/tnt-11358-expose-custom-metrics branch June 9, 2026 12:06
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.

3 participants