Skip to content

Commit

Permalink
Prometheus /api/v1/labels endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alpinskiy committed Dec 2, 2024
1 parent f170ff6 commit 6f1b874
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/statshouse-api/statshouse-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ func run(argv args, cfg *api.Config, vkuthPublicKeys map[string][]byte) error {
m.Path("/prom/api/v1/query").Methods("POST").HandlerFunc(api.HandleInstantQuery)
m.Path("/prom/api/v1/query_range").Methods("GET").HandlerFunc(api.HandleRangeQuery)
m.Path("/prom/api/v1/query_range").Methods("POST").HandlerFunc(api.HandleRangeQuery)
m.Path("/prom/api/v1/label/{name}/values").Methods("GET").HandlerFunc(api.HandlePromLabelValuesQuery)
m.Path("/prom/api/v1/labels").Methods("GET", "POST").HandlerFunc(api.HandlePromLabelsQuery)
m.Path("/prom/api/v1/label/{name}/values").Methods("GET", "POST").HandlerFunc(api.HandlePromLabelValuesQuery)
m.Path("/prom/api/v1/series").Methods("GET").HandlerFunc(api.HandlePromSeriesQuery)
m.Path("/prom/api/v1/series").Methods("POST").HandlerFunc(api.HandlePromSeriesQuery)
m.Path("/debug/pprof/").Methods("GET").HandlerFunc(api.HandleProf)
Expand Down
32 changes: 31 additions & 1 deletion internal/api/promql.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ func HandlePromSeriesQuery(r *httpRequestHandler) {
promRespond(w, res)
}

func HandlePromLabelsQuery(r *httpRequestHandler) {
namespace := r.Header.Get("X-StatsHouse-Namespace")
var prefix string
switch namespace {
case "", "__default":
// no prefix
default:
prefix = namespace + format.NamespaceSeparator
}
_ = r.ParseForm()
var res []string
for _, expr := range r.Form["match[]"] {
if ast, err := parser.ParseExpr(expr); err == nil {
for _, s := range parser.ExtractSelectors(ast) {
for _, sel := range s {
if sel.Name == "__name__" {
metricName := prefix + sel.Value
if meta := r.metricsStorage.GetMetaMetricByName(metricName); meta != nil {
for k := range meta.Name2Tag {
res = append(res, k)
}
}
}
}
}
}
}
promRespond(r.Response(), res)
}

func HandlePromLabelValuesQuery(r *httpRequestHandler) {
namespace := r.Header.Get("X-StatsHouse-Namespace")
var prefix string
Expand All @@ -180,7 +210,7 @@ func HandlePromLabelValuesQuery(r *httpRequestHandler) {
if tagName == "__name__" {
for _, meta := range r.metricsStorage.GetMetaMetricList(r.showInvisible) {
trimmed := strings.TrimPrefix(meta.Name, prefix)
if meta.Name != trimmed && r.accessInfo.CanViewMetric(*meta) {
if (prefix == "" || meta.Name != trimmed) && r.accessInfo.CanViewMetric(*meta) {
res = append(res, trimmed)
}
}
Expand Down

0 comments on commit 6f1b874

Please sign in to comment.