Description
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.