karimra/gnmic

string mappings processor

Sparc0 opened this issue · 2 comments

I would like a way to map string values to int values so that i can use them in prometheus.

I expect maybe that the config would look something similar to this

processors:
  int-mappings:
    value-mappings:
      value-names:
        - "/interfaces/interface/state/admin-status"
        - "/interfaces/interface/state/oper-status"
	string-mappings:
	  - UP: 1
          - DOWN: 0
  bgp-state-mappings:
    value-mappings:
      value-names:
        - "/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state/session-state"
	  string-mappings:
            - IDLE: 0
            - CONNECT: 1
            - ACTIVE: 2
            - OPENSENT: 3
            - OPENCONFIRM: 4
            - ESTABLISHED: 5

And the result will be like this

# Before
{
  "name": "interface_state",
  "timestamp": 1657801178370627152,
  "tags": {
    "interface_name": "et-7/3/1",
    "source": "<removed>",
    "subscription-name": "interface_state",
  },
  "values": {
    "/interfaces/interface/state/admin-status": "UP",
    "/interfaces/interface/state/oper-status": "DOWN"
  }
}

# After
{
  "name": "interface_state",
  "timestamp": 1657801178370627152,
  "tags": {
    "interface_name": "et-7/3/1",
    "source": "<removed>",
    "subscription-name": "interface_state",
  },
  "values": {
    "/interfaces/interface/state/admin-status": "1",
    "/interfaces/interface/state/oper-status": "0"
  }
}
# Before
{
  "name": "bgp_state",
  "timestamp": 1657804165515931016,
  "tags": {
    "neighbor_neighbor-address": "<removed>",
    "network-instance_instance-name": "master",
    "source": "<removed>",
    "subscription-name": "bgp_state",
  },
  "values": {
    "/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state/session-state": "CONNECT"
  }
}
{
  "name": "bgp_state",
  "timestamp": 1657804165557577400,
  "tags": {
    "neighbor_neighbor-address": "<removed>",
    "network-instance_instance-name": "<removed>",
    "source": "<removed>",
    "subscription-name": "bgp_state",
  },
  "values": {
    "/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state/session-state": "ESTABLISHED"
  }
}

# After
{
  "name": "bgp_state",
  "timestamp": 1657804165515931016,
  "tags": {
    "neighbor_neighbor-address": "<removed>",
    "network-instance_instance-name": "master",
    "source": "<removed>",
    "subscription-name": "bgp_state",
  },
  "values": {
    "/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state/session-state": "1"
  }
}
{
  "name": "bgp_state",
  "timestamp": 1657804165557577400,
  "tags": {
    "neighbor_neighbor-address": "<removed>",
    "network-instance_instance-name": "<removed>",
    "source": "<removed>",
    "subscription-name": "bgp_state",
  },
  "values": {
    "/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state/session-state": "5"
  }
}

it's possible to achieve what you need using event-strings processor with transfom replace: https://gnmic.kmrd.dev/user_guide/event_processors/event_strings/

The equivalent config to your suggestion would be:

processors:
  int-mappings:
    event-strings:
      value-names:
        - "/interfaces/interface/state/admin-status"
        - "/interfaces/interface/state/oper-status"
      transforms:
        - replace:
            apply-on: "value"
            old: "UP"
            new: "1"
        - replace:
            apply-on: "value"
            old: "DOWN"
            new: "0"
  bgp-state-mappings:
    event-strings:
      value-names:
        - "/network-instances/network-instance/protocols/protocol/bgp/neighbors/neighbor/state/session-state"
      transforms:
        - replace:
            apply-on: "value"
            old: "IDLE"
            new: "0"
        - replace:
            apply-on: "value"
            old: "CONNECT"
            new: "1"
        - replace:
            apply-on: "value"
            old: "ACTIVE"
            new: "2"
        - replace:
            apply-on: "value"
            old: "OPENSENT"
            new: "3"
        - replace:
            apply-on: "value"
            old: "OPENCONFIRM"
            new: "4"
        - replace:
            apply-on: "value"
            old: "OPENCONFIRM"
            new: "5"        

Note that you can use regular expressions to match values and tags as well as do regular expression based replaces, i.e the fields value-names, tag-names, tags, values and .transforms[].replace.old can be regular expressions.

thanks i must have missed the apply-on: value when i read the docs.