prometheus-community / json_exporter

A prometheus exporter which scrapes remote JSON by JSONPath
Apache License 2.0
631 stars 191 forks source link

Question: nested json structure #85

Open bb-Ricardo opened 3 years ago

bb-Ricardo commented 3 years ago

Hi,

thank you very much for this neat handy tool.

I try to monitor a project where you can host your own online radio station: https://github.com/AzuraCast/AzuraCast

The most interesting statistics come from here: curl https://demo.azuracast.com/api/nowplaying

[
   {
      "station":{
         "id":1,
         "name":"AzuraTest Radio",
         "shortcode":"azuratest_radio",
         "description":"A test radio station.",
         "frontend":"icecast",
         "backend":"liquidsoap",
         "listen_url":"https://demo.azuracast.com/radio/8000/radio.mp3",
         "url":null,
         "playlist_pls_url":"https://demo.azuracast.com/public/azuratest_radio/playlist.pls",
         "playlist_m3u_url":"https://demo.azuracast.com/public/azuratest_radio/playlist.m3u",
         "is_public":true,
         "mounts":[
            {
               "path":"/radio.mp3",
               "is_default":true,
               "id":1,
               "name":"128kbps MP3",
               "url":"https://demo.azuracast.com/radio/8000/radio.mp3",
               "bitrate":128,
               "format":"mp3",
               "listeners":{
                  "current":0,
                  "unique":0,
                  "total":0
               }
            },
            {
               "path":"/mobile.mp3",
               "is_default":false,
               "id":2,
               "name":"64kbps MP3",
               "url":"https://demo.azuracast.com/radio/8000/mobile.mp3",
               "bitrate":64,
               "format":"mp3",
               "listeners":{
                  "current":0,
                  "unique":0,
                  "total":0
               }
            }
         ],
         "remotes":[

         ]
      },
      "listeners":{
         "current":0,
         "unique":0,
         "total":0
      },
      "live":{
         "is_live":false,
         "streamer_name":"",
         "broadcast_start":null
      },
      "now_playing":{
         "elapsed":21,
         "remaining":291,
         "sh_id":12,
         "played_at":1614584862,
         "duration":312,
         "playlist":"default",
         "streamer":"",
         "is_request":false,
         "song":{
            "id":"7fa040c6121bdea61f79c3d1afaf243b",
            "text":"Nitro Fun & Desso - Believe (feat. Brenton Mattheus)",
            "artist":"Nitro Fun & Desso",
            "title":"Believe (feat. Brenton Mattheus)",
            "album":"",
            "genre":"",
            "lyrics":"I searched the earth for you\r\nwade into the furthest oceans\r\nI made my way through\r\njust going through the motions\r\nI left to chase my dreams\r\nI'll find my way to you\r\nohhh I can see the homeward beams\r\nlike stars that shine for you\r\noh I believe in everything you said x2\r\nI searched the world for you \r\nchsing after both our future\r\nbelieved in everything, that you said\r\nso I had left to chase my dreams to \r\nfind a place in life\r\nyou showed me everything I need my\r\nhome is by your side\r\noh I believe in everything you said x2",
            "art":"https://demo.azuracast.com/api/station/1/art/5543a117d2bf08f65ac16dbb-1614582041.jpg",
            "custom_fields":[

            ]
         }
      },
      "playing_next":{
         "cued_at":1614584857,
         "duration":261,
         "playlist":"default",
         "is_request":false,
         "song":{
            "id":"b2dc32f124d9d0f37b517d7d2fd2e52a",
            "text":"Desh - Halla",
            "artist":"Desh",
            "title":"Halla",
            "album":"",
            "genre":"",
            "lyrics":"",
            "art":"https://demo.azuracast.com/api/station/1/art/e55d13660107291661c1651d-1614582043.jpg",
            "custom_fields":[

            ]
         }
      },
      "song_history":[
         {
            "sh_id":11,
            "played_at":1614584656,
            "duration":209,
            "playlist":"default",
            "streamer":"",
            "is_request":false,
            "song":{
               "id":"ae791aacc23c9322b64bcb56ec15881d",
               "text":"Luke Swanson - Amnesia",
               "artist":"Luke Swanson",
               "title":"Amnesia",
               "album":"",
               "genre":"",
               "lyrics":"",
               "art":"https://demo.azuracast.com/api/station/1/art/3f939c7fd05df64b800456f2-1614582041.jpg",
               "custom_fields":[

               ]
            }
         },
         {
            "sh_id":10,
            "played_at":1614584377,
            "duration":280,
            "playlist":"default",
            "streamer":"",
            "is_request":false,
            "song":{
               "id":"f049ed1d7f7b65d0aa1a1d3dbb240fa2",
               "text":"Internet Empire - Imagine",
               "artist":"Internet Empire",
               "title":"Imagine",
               "album":"",
               "genre":"",
               "lyrics":"",
               "art":"https://demo.azuracast.com/api/station/1/art/ead642b456401c9ec7be3c2a-1614582042.jpg",
               "custom_fields":[

               ]
            }
         },
         {
            "sh_id":9,
            "played_at":1614584212,
            "duration":170,
            "playlist":"default",
            "streamer":"",
            "is_request":false,
            "song":{
               "id":"064cc6e26a2cca5d0a6100e3ee30668c",
               "text":"RVDEKID - Lowkey",
               "artist":"RVDEKID",
               "title":"Lowkey",
               "album":"LO-FI FEELS",
               "genre":"",
               "lyrics":"",
               "art":"https://demo.azuracast.com/api/station/1/art/aba2a061e1f8b04e3e8ed085-1614582042.jpg",
               "custom_fields":[

               ]
            }
         },
         {
            "sh_id":8,
            "played_at":1614583986,
            "duration":228,
            "playlist":"default",
            "streamer":"",
            "is_request":false,
            "song":{
               "id":"0299ae3ceb62eb822a5b2ffd92f31625",
               "text":"Avionics - Flipside",
               "artist":"Avionics",
               "title":"Flipside",
               "album":"",
               "genre":"",
               "lyrics":"",
               "art":"https://demo.azuracast.com/api/station/1/art/f7b6f58b66e825c474883d0f-1614582043.jpg",
               "custom_fields":[

               ]
            }
         },
         {
            "sh_id":7,
            "played_at":1614583639,
            "duration":348,
            "playlist":"default",
            "streamer":"",
            "is_request":false,
            "song":{
               "id":"75dd3eb1f149634eb09df4a6e9cb249d",
               "text":"Lokan - Ex Nihilo",
               "artist":"Lokan",
               "title":"Ex Nihilo",
               "album":"",
               "genre":"",
               "lyrics":"",
               "art":"https://demo.azuracast.com/api/station/1/art/efff738c3f645f11ae1f8f59-1614582044.jpg",
               "custom_fields":[

               ]
            }
         }
      ],
      "is_online":true,
      "cache":"redis"
   }
]

I tried following config and failed:

metrics:
- name: total_current_listeners
  path: "{ [*].listeners.current }"
  help: Example of a top-level global value scrape in the json
  labels:
    station: "{[*].station.name}"

- name: per_mount
  type: object
  path: "{ [*].station.mounts[*] }"
  help: num of current mount listeners
  labels:
    station: "{..name}"
    name: "{ .name }"
    bitrate: "{ .bitrate }"
    format: "{ .format }"
  values:
    listeners: "{ .listeners.current }"

Result:

# HELP per_mount_listeners num of current mount listeners
# TYPE per_mount_listeners untyped
per_mount_listeners{bitrate="128",format="mp3",name="128kbps MP3",station="128kbps MP3"} 0
per_mount_listeners{bitrate="64",format="mp3",name="64kbps MP3",station="64kbps MP3"} 0
# HELP total_current_listeners Example of a top-level global value scrape in the json
# TYPE total_current_listeners untyped
total_current_listeners{station="AzuraTest Radio"} 0

I can't get the station label per mount to work.

Also tried other options according to this page: https://kubernetes.io/docs/reference/kubectl/jsonpath/

Can anyone point me in the right direction please?

BaldStrong commented 1 year ago

@bb-Ricardo Try this? station: "{[*].station.name}"

metrics:
- name: total_current_listeners
  path: "{ [*].listeners.current }"
  help: Example of a top-level global value scrape in the json
  labels:
    station: "{[*].station.name}"

- name: per_mount
  type: object
  path: "{ [*].station.mounts[*] }"
  help: num of current mount listeners
  labels:
    station: "{[*].station.name}"
    name: "{ .name }"
    bitrate: "{ .bitrate }"
    format: "{ .format }"
  values:
    listeners: "{ .listeners.current }"