openconfig / gnmic

gNMIc is a gNMI CLI client and collector
https://gnmic.openconfig.net
Apache License 2.0
168 stars 54 forks source link

Event Processor Not Applying Transformation as Expected #408

Closed ndmitri closed 3 months ago

ndmitri commented 4 months ago

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.

karimra commented 4 months ago

Unfortunately that's not how event processors function:

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.

dimas-5-1 commented 4 months ago

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

dnikoliouk-dn commented 4 months ago

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

karimra commented 4 months ago

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:

 ```text
Name: sub1
Interface Name: ethernet-1/1
Source: $YOUR_ROUTER_NAME
Subscription Name: sub1
InOctets: 3857
dimas-5-1 commented 4 months ago

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!

karimra commented 4 months ago

Yes all gomplate functions are imported