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

Add support for default values of regex match groups in rules #905

Open
EconomicTouristsArmLate opened this issue Jan 19, 2024 · 2 comments

Comments

@EconomicTouristsArmLate
Copy link

EconomicTouristsArmLate commented Jan 19, 2024

To properly consume some metrics, I need to set a default value on a label.
This label value is defined by an optional match group, example config (ref label version):

---
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem", "Catalina:*"]
blacklistObjectNames: []
rules:
  - pattern: 'Catalina<j2eeType=WebModule, name=//(?:localhost/)?([-a-zA-Z0-9+&@/%?=~_|!:.,;]*[-a-zA-Z0-9+&@/%=~_|])(?:##(.+))?, J2EEApplication=none, J2EEServer=none><>startTime:'
    name: tomcat_module_started_at
    labels:
      module: "$1"
      version: "$2"
    help: Tomcat WebModule startTime (unix time)
    type: GAUGE

Note that version is fed by an optional regex match.
I'd love to be able to write something along the lines of

    labels:
      module: "$1"
      version: "${2:-unversioned}"

though I don't care much about the specific syntax (using bash syntax here, which is also commonly used in ENV substitions, eg in docker compose files).

@dhoard
Copy link
Collaborator

dhoard commented Jan 23, 2024

@EconomicTouristsArmLate I believe I understand what you are trying to accomplish, but don't fully follow the use case.

The MBean should be providing a label. Is there a reason that a lack of a label (i.e. empty string) can't be handled downstream?

@EconomicTouristsArmLate
Copy link
Author

EconomicTouristsArmLate commented Jan 24, 2024

The problem I'm trying to solve is dumb: I need to sort versions via string comparison (this is how tomcat determines the latest / active version of an app). And an empty label value does sort higher than any other value, but it should sort lowest.

This use case is slightly ridiculous, but I'm sure there are other use cases that benefit from a default label value. Providing a default label value simply improves readability of metrics, which is a benefit in its own.

For now I workaround this problem with multiple, more specific match rules, but this is quite cumbersome and error prone:

rules:
  # first rule with only matches `name` with a version, second rule matches without. only first match is used.
  # this is, so that we always have a value in the version label, so that we can string sort by this, in order to find the latest version
  # in the same way tomcat does... the default version string must thus be sorted before numbers, '-' does that.
  - pattern: 'Catalina<j2eeType=WebModule, name=//(?:localhost/)?([-a-zA-Z0-9+&@/%?=~_|!:.,;]*[-a-zA-Z0-9+&@/%=~_|])(?:##(.+)), J2EEApplication=none, J2EEServer=none><>startTime:'
    name: tomcat_module_started_at
    labels:
      module: "$1"
      version: "$2"
    help: Tomcat WebModule startTime (unix time)
    type: GAUGE
  - pattern: 'Catalina<j2eeType=WebModule, name=//(?:localhost/)?([-a-zA-Z0-9+&@/%?=~_|!:.,;]*[-a-zA-Z0-9+&@/%=~_|]), J2EEApplication=none, J2EEServer=none><>startTime:'
    name: tomcat_module_started_at
    labels:
      module: "$1"
      version: '-'          # ← ← ← ← ← ←  note the default value here
    help: Tomcat WebModule startTime (unix time)
    type: GAUGE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants