gnmic icon indicating copy to clipboard operation
gnmic copied to clipboard

Event Processor Not Applying Transformation as Expected

Open ndmitri opened this issue 1 year ago • 6 comments

I am experiencing an issue with gNMIc where the configured event processor does not seem to apply the specified jq transformation. Despite a straightforward configuration aiming to test the processor functionality, the output remains unchanged from the input, suggesting that the processor might not be triggered or is not applying the transformation correctly.

Configuration Details:

username: cisco
password: cisco
port: 50051
timeout: 5s
skip-verify: false
tls-ca: ./ca.cert.pem
log-file: /tmp/gnmic.log
debug: true

targets:
  192.168.2.104:
    timeout: 2s
    subscriptions:
      - system-name
    outputs:
      - stdout

subscriptions:
  system-name:
    paths:
      - "/System/name"
    stream-mode: sample
    sample-interval: 30s
    encoding: json

outputs:
  stdout:
    type: file
    file-type: stdout
    event-processors:
      - event-jq-processor

processors:
  event-jq-processor:
    event-jq:
      expression: '.[] | {test: "output is working"}'
      debug: true

Expected Output:

{"test": "output is working"}

Actual Output:

{
  "source": "192.168.2.104",
  "subscription-name": "system-name",
  "timestamp": 1712973442344331082,
  "time": "2024-04-13T01:57:22.344331082Z",
  "updates": [
    {
      "Path": "device:System",
      "values": {
        "System": {
          "name": "LEAF2"
        }
      }
    }
  ]
}

Steps to Reproduce:

  1. Configure gnmic using the above YAML configuration.
  2. Execute the command gnmic --config gnmic.yaml subscribe.
  3. Observe the output, which should have been transformed by the event processor but remains unchanged.

Additional Information: gNMIc version: 0.36.2 Operating System: Ubuntu 22.04.4 LTS gnmic.log

Expected Behavior: The output should be transformed by the event-jq processor according to the specified expression, showing {"test": "output is working"} instead of the untransformed JSON data from the subscription.

Actual Behavior: The processor does not modify the output as expected. The raw data from the subscription is displayed instead of the transformed data.

ndmitri avatar Apr 13 '24 02:04 ndmitri

Unfortunately that's not how event processors function:

  • Processors apply only when the output format is set to event, the default format is json.
  • Processors should always output a list of event messages (you cannot create an arbitrary json object using them) In your case, the processor is not being applied at all (format: event missing) and even if you set the format it will result in an error because what it returns is not a list of event messages.

If your goal is to return an arbitrary message (json or other) you can use the field msg-template: under your file output. It takes a Gotemplate that rendered with each received message as input.

karimra avatar Apr 13 '24 16:04 karimra

Hello! Can U put here some basic example of msg-template usage? Thank you!

dimas-5-1 avatar Apr 22 '24 12:04 dimas-5-1

I used TCP output to forward raw JSON and processed them with PyJQ. Works great. Thank you 🙂

dnikoliouk-dn avatar Apr 22 '24 14:04 dnikoliouk-dn

Hello! Can U put here some basic example of msg-template usage? Thank you!

subscriptions:
  sub1:
    paths:
      - /interface/statistics
 
outputs:
  out1:
    type: file
    format: event
    msg-template: |
      {{range .}}
      {{if index .values "/interface/statistics/in-octets"}}
      Name: {{.name}}
      Interface Name: {{(index .tags "interface_name")}}
      Source: {{(index .tags "source")}}
      Subscription Name: {{(index .tags "subscription-name")}}
      InOctets: {{ index .values "/interface/statistics/in-octets" }}
      {{- end}}
      {{- end}}

This should print:

Name: sub1
Interface Name: ethernet-1/1
Source: $YOUR_ROUTER_NAME
Subscription Name: sub1
InOctets: 3857

karimra avatar Apr 23 '24 00:04 karimra

Hello! Can U put here some basic example of msg-template usage? Thank you!

subscriptions:
  sub1:
    paths:
      - /interface/statistics
 
outputs:
  out1:
    type: file
    format: event
    msg-template: |
      {{range .}}
      {{if index .values "/interface/statistics/in-octets"}}
      Name: {{.name}}
      Interface Name: {{(index .tags "interface_name")}}
      Source: {{(index .tags "source")}}
      Subscription Name: {{(index .tags "subscription-name")}}
      InOctets: {{ index .values "/interface/statistics/in-octets" }}
      {{- end}}
      {{- end}}

This should print:

Name: sub1
Interface Name: ethernet-1/1
Source: $YOUR_ROUTER_NAME
Subscription Name: sub1
InOctets: 3857

Thanks a lot! And a small clarification - does the template supports gotemplate functions, string processing, for example? https://docs.gomplate.ca/functions/strings/ Thanks again!

dimas-5-1 avatar Apr 23 '24 07:04 dimas-5-1

Yes all gomplate functions are imported

karimra avatar Apr 25 '24 07:04 karimra