package prom import ( "net/url" "strings" ) // ParseGeneratorURL splits an Alertmanager generatorURL into the Prometheus it // points at and the expression of the rule that fired. Prometheus builds it as // /graph?g0.expr=&g0.tab=1 (/query on Prometheus 3), and the // external URL may carry a path prefix, which is kept in base. // // ok is false for anything else: a non-HTTP scheme, credentials in the URL, a // page that is not the graph page, or no expression. The caller then has // nothing to query and falls back to showing the link. func ParseGeneratorURL(raw string) (base *url.URL, expr string, ok bool) { u, err := url.Parse(strings.TrimSpace(raw)) if err != nil || u.Host == "" || u.User != nil { return nil, "", false } if u.Scheme != "http" && u.Scheme != "https" { return nil, "", false } prefix := "" switch { case strings.HasSuffix(u.Path, "/graph"): prefix = strings.TrimSuffix(u.Path, "/graph") case strings.HasSuffix(u.Path, "/query"): prefix = strings.TrimSuffix(u.Path, "/query") default: return nil, "", false } expr = strings.TrimSpace(u.Query().Get("g0.expr")) if expr == "" { return nil, "", false } return &url.URL{Scheme: u.Scheme, Host: u.Host, Path: prefix}, expr, true }