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

Issues with custom field multiple selection list #169

Closed
candlerb opened this issue Mar 26, 2024 · 1 comment
Closed

Issues with custom field multiple selection list #169

candlerb opened this issue Mar 26, 2024 · 1 comment
Labels

Comments

@candlerb
Copy link
Contributor

candlerb commented Mar 26, 2024

Version

# /opt/netbox/venv/bin/pip freeze | grep prometheus-sd
netbox-plugin-prometheus-sd @ git+https://github.com/FlxPeters/netbox-plugin-prometheus-sd@686cb3ba3cd726a163659f2303aad822414bb6a7

running under Netbox 3.7.4

Issues

I have a couple of problems with labels from a custom field, where the value it contains is a list. The example here is a custom field called "snmp_module" of type "multiple selection", linked to a choice set with strings.

Problem 1: the label value returned is a Python-style repr().

# curl -sS -H "Authorization: Token $TOKEN" "$NETBOX/api/plugins/prometheus-sd/devices/?cf_snmp_module__ic=i" | jq .
...
      "__meta_netbox_custom_field_snmp_module": "['hrDevice', 'hrStorage', 'if_mib', 'mikrotik']",

In order to get this into a simple comma-separated list (as required by snmp_exporter with multiple modules) I have to use a very ugly rewrite:

      - source_labels: [__meta_netbox_custom_field_snmp_module]
        target_label: __param_module
      # Ugh: multiselect is of form ['foo', 'bar'] and we need foo,bar. There is no gsub.
      - source_labels: [__param_module]
        regex: "\\['(.*)'\\]"
        target_label: __param_module
      - source_labels: [__param_module]
        regex: "(.*)', *'(.*)"
        replacement: "$1,$2"
        target_label: __param_module
      - source_labels: [__param_module]
        regex: "(.*)', *'(.*)"
        replacement: "$1,$2"
        target_label: __param_module
      - source_labels: [__param_module]
        regex: "(.*)', *'(.*)"
        replacement: "$1,$2"
        target_label: __param_module

... and this only works up to a fixed maximum number of values (4 here), as there's no "gsub" in prometheus rewriting.

Problem 2: when the custom field is unset the label gets the string value "None", whereas I would expect either empty string or the entire label to be omitted.

...
      "__meta_netbox_custom_field_snmp_module": "None",

(This is minor: I could always match this value explicitly in a rewrite rule, and I'm unlikely to use "None" as a genuine choice value)

Proposal

For values of a multiple selection type, I'd like to see such a list returned as a plain comma-separated list of strings.

What about other types of custom field that might return a list? I can only see two that might make a difference:

  • JSON fields. These can stay as is. JSON is JSON, after all.
  • Multiple Object Selection. I haven't tested what these do today. Maybe simplest is to generate a comma-separated list of the str(...) representation of each object, so you get whatever the default display is. (I suppose it could also be a comma-separated list of object ID, or of slug for object classes which have slugs, but there's no obvious one-size-fits-all)

The question is whether the prometheus-sd plugin would have to dig into the definition of a custom field to determine whether it's JSON or not.

References

Relates to #34, #65

Copy link

This issue has been automatically closed because it has been inactive for more than 60 days.
Please reopen if you still intend to submit this pull request.

@github-actions github-actions bot added the Stale label May 26, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant