1

I'm trying to use the grafana API to pause and unpause grafana alerts via a mobile app in React-Native and Expo. following the documentation, with postman, the states are changed correctly, but when I make the app calls I can unpause the alerts but I can't pause them. My grafana workspace is in AWS Managed Grafana

This is my function to change state

  const changeAlertsStatus = (item) => {
    var state = {
      paused: item.state === "paused" ? false : true,
    };

    console.log(state);
    const URL = `${myURL}/api/alerts/${item.id}/pause`; 
    try {
      fetch(URL, {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
          Authorization: SynergyGrafanaApiKey,
        },
        body: state,
      })
        .then((response) => response.json())
        .then((json) => {
          console.log(json);
          getAlertsList();
        });
    } catch (error) {
      console.log(error.message);
    }
  };

This is the console logs when passing "paused"=true to pause the alert and "paused"=false to unpause the paused alert (Did it through grafana console)

LOG  {"paused": true}
LOG  {"alertId": 20, "message": "Alert is already un-paused", "state": "un-paused"}
LOG  {"paused": true}
LOG  {"alertId": 6, "message": "Alert is already un-paused", "state": "un-paused"}
LOG  {"paused": false}
LOG  {"alertId": 20, "message": "Alert un-paused", "state": "unknown"}

This is how the app is looking

I would like to understand why, if the alert is paused, the status changes without difficulty, but when I try to pause it, it doesn't allow me and returns that message in the second photo. The call is being made, but for some reason it doesn't change it.

Any other question feel free to ask. I would like to solve this problems. Thanks in advance

markalex
  • 8,623
  • 2
  • 7
  • 32
  • 1
    Welcome to StackOverflow. Please take time to read [why should I not upload images of code/data/errors?](//meta.stackoverflow.com/q/285551) After that [edit] your question and replace first two images with formatted text. – markalex Mar 28 '23 at 13:26
  • thanks @markalex I made the changes, I hope this can help. thanks – Yoslandy Mesa Mar 29 '23 at 12:45
  • Your log messages seems strange. If you pass `paused: true` grafana should try to pause an alert, but your logs suggesting otherwise. Try pause same alert, and also add request to `/api/alerts/:id` between your attempts and show field "state" of response. – markalex Mar 29 '23 at 22:02
  • I say so @markalex. According to the grafana documentation it is simple, even when I pass the pause: false to one that is already paused, it changes the state perfectly. I did what you recommended and it returns the properties of the alert with the actual status it has, either ok or alerting. Even Postman changes the states normally too. I also directly passed the values in the body of the call and the same thing happens. I really don't understand what's going on. – Yoslandy Mesa Mar 30 '23 at 13:11

0 Answers0