Commit eb29ecff authored by Thierry Sallé's avatar Thierry Sallé
Browse files

Search grafana_dashboard label by name

parent c11a4543
Pipeline #103082294 failed with stages
in 43 seconds
......@@ -60,7 +60,13 @@ type GrafanaAnnotation struct {
IsRegion bool `json:"isRegion,omitempty"`
TimeEnd int64 `json:"timeEnd,omitempty"`
Tags []string `json:"tags,omitempty"`
Text string `json:"text,omitempty"`
Text string `json:"text"`
}
type SearchResult struct {
Id int `json:"id"`
Uid string `json:"uid"`
title string `json:"string"`
}
func prom2Grafana(w http.ResponseWriter, r *http.Request, g GrafanaConfig, debug bool) {
......@@ -88,7 +94,8 @@ func prom2Grafana(w http.ResponseWriter, r *http.Request, g GrafanaConfig, debug
// Create Grafana annotation
for label, val := range alert.Labels {
if label == "grafana_dashboard" {
ga.DashboardId, _ = strconv.Atoi(val)
ga.DashboardId = getDashboardId(g, val)
//ga.DashboardId, _ = strconv.Atoi(val)
continue
}
if label == "grafana_panel" {
......@@ -100,6 +107,8 @@ func prom2Grafana(w http.ResponseWriter, r *http.Request, g GrafanaConfig, debug
if val, ok := alert.Annotations["summary"]; ok {
ga.Text = val
} else {
ga.Text = "Added by prom2grafana"
}
ga.Time = alert.StartsAt.UnixNano() / 1000000 // convert to milliseconds
if alert.EndsAt.IsZero() != true {
......@@ -151,6 +160,38 @@ func prom2Grafana(w http.ResponseWriter, r *http.Request, g GrafanaConfig, debug
w.Write(body)
}
func getDashboardId(g GrafanaConfig, dashboardName string) int {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/search?query=%s", g.GrafanaURL, dashboardName), nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
if g.GrafanaAPIKey != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", g.GrafanaAPIKey))
} else {
if g.Username != "" && g.Password != "" {
req.SetBasicAuth(g.Username, g.Password)
}
}
// create a Client
client := &http.Client{}
// Do sends an HTTP request and
resp, err := client.Do(req)
if err != nil {
log.Printf("error in send req: %s", err.Error())
}
var dashboard []SearchResult
err = json.NewDecoder(resp.Body).Decode(&dashboard)
resp.Body.Close()
if len(dashboard) > 0 {
return dashboard[0].Id
} else {
return 0
}
}
func main() {
args := Args{}
flag.StringVar(&args.Address, "listen", ":8080", "address in which to listen for http requests")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment