github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/labels.go (about)

     1  package server
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/pyroscope-io/pyroscope/pkg/server/httputils"
     7  	"github.com/pyroscope-io/pyroscope/pkg/storage"
     8  	"github.com/pyroscope-io/pyroscope/pkg/util/attime"
     9  )
    10  
    11  func (ctrl *Controller) labelsHandler() http.HandlerFunc {
    12  	return NewLabelsHandler(ctrl.storage, ctrl.httpUtils).ServeHTTP
    13  }
    14  
    15  func NewLabelsHandler(s storage.LabelsGetter, httpUtils httputils.Utils) http.HandlerFunc {
    16  	return func(w http.ResponseWriter, r *http.Request) {
    17  		ctx := r.Context()
    18  		v := r.URL.Query()
    19  
    20  		in := storage.GetLabelKeysByQueryInput{
    21  			StartTime: attime.Parse(v.Get("from")),
    22  			EndTime:   attime.Parse(v.Get("until")),
    23  			Query:     v.Get("query"),
    24  		}
    25  
    26  		keys := make([]string, 0)
    27  		if in.Query != "" {
    28  			output, err := s.GetKeysByQuery(ctx, in)
    29  			if err != nil {
    30  				httpUtils.WriteInvalidParameterError(r, w, err)
    31  				return
    32  			}
    33  			keys = append(keys, output.Keys...)
    34  		} else {
    35  			s.GetKeys(ctx, func(k string) bool {
    36  				keys = append(keys, k)
    37  				return true
    38  			})
    39  		}
    40  
    41  		httpUtils.WriteResponseJSON(r, w, keys)
    42  	}
    43  }