github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/server/labelvalues.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) labelValuesHandler() http.HandlerFunc {
    12  	return NewLabelValuesHandler(ctrl.storage, ctrl.httpUtils)
    13  }
    14  
    15  func NewLabelValuesHandler(s storage.LabelValuesGetter, 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.GetLabelValuesByQueryInput{
    21  			StartTime: attime.Parse(v.Get("from")),
    22  			EndTime:   attime.Parse(v.Get("until")),
    23  			Label:     v.Get("label"),
    24  			Query:     v.Get("query"),
    25  		}
    26  
    27  		if in.Label == "" {
    28  			httpUtils.WriteInvalidParameterError(r, w, errLabelIsRequired)
    29  			return
    30  		}
    31  
    32  		values := make([]string, 0)
    33  		if in.Query != "" {
    34  			output, err := s.GetValuesByQuery(ctx, in)
    35  			if err != nil {
    36  				httpUtils.WriteInvalidParameterError(r, w, err)
    37  				return
    38  			}
    39  			values = append(values, output.Values...)
    40  		} else {
    41  			s.GetValues(ctx, in.Label, func(v string) bool {
    42  				values = append(values, v)
    43  				return true
    44  			})
    45  		}
    46  
    47  		httpUtils.WriteResponseJSON(r, w, values)
    48  	}
    49  }