Merge pull request #2076 from huanggze/custom-monitoring

api: list metric labels and values
This commit is contained in:
KubeSphere CI Bot
2020-05-15 19:52:32 +08:00
committed by GitHub
12 changed files with 362 additions and 1 deletions

View File

@@ -19,6 +19,7 @@
package v1alpha3
import (
"errors"
"github.com/emicklei/go-restful"
"k8s.io/client-go/kubernetes"
"kubesphere.io/kubesphere/pkg/api"
@@ -198,6 +199,30 @@ func (h handler) handleMetadataQuery(req *restful.Request, resp *restful.Respons
resp.WriteAsJson(res)
}
func (h handler) handleMetricLabelSetQuery(req *restful.Request, resp *restful.Response) {
var res model.MetricLabelSet
params := parseRequestParams(req)
if params.metric == "" || params.start == "" || params.end == "" {
api.HandleBadRequest(resp, nil, errors.New("required fields are missing: [metric, start, end]"))
return
}
opt, err := h.makeQueryOptions(params, 0)
if err != nil {
if err.Error() == ErrNoHit {
resp.WriteAsJson(res)
return
}
api.HandleBadRequest(resp, nil, err)
return
}
res = h.mo.GetMetricLabelSet(params.metric, params.namespaceName, opt.start, opt.end)
resp.WriteAsJson(res)
}
func (h handler) handleAdhocQuery(req *restful.Request, resp *restful.Response) {
var res monitoring.Metric

View File

@@ -50,6 +50,7 @@ type reqParams struct {
storageClassName string
componentType string
expression string
metric string
}
type queryOptions struct {
@@ -101,6 +102,7 @@ func parseRequestParams(req *restful.Request) reqParams {
r.storageClassName = req.PathParameter("storageclass")
r.componentType = req.PathParameter("component")
r.expression = req.QueryParameter("expr")
r.metric = req.QueryParameter("metric")
return r
}

View File

@@ -409,12 +409,24 @@ func AddToContainer(c *restful.Container, k8sClient kubernetes.Interface, monito
Returns(http.StatusOK, RespOK, model.Metadata{})).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/namespaces/{namespace}/targets/labelsets").
To(h.handleMetricLabelSetQuery).
Doc("List all available labels and values of a metric within a specific time span.").
Param(ws.PathParameter("namespace", "The name of the namespace.").DataType("string").Required(true)).
Param(ws.QueryParameter("metric", "The name of the metric").DataType("string").Required(true)).
Param(ws.QueryParameter("start", "Start time of query. It is a string with Unix time format, eg. 1559347200. ").DataType("string").Required(true)).
Param(ws.QueryParameter("end", "End time of query. It is a string with Unix time format, eg. 1561939200. ").DataType("string").Required(true)).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomMetricsTag}).
Writes(model.MetricLabelSet{}).
Returns(http.StatusOK, RespOK, model.MetricLabelSet{})).
Produces(restful.MIME_JSON)
ws.Route(ws.GET("/namespaces/{namespace}/targets/query").
To(h.handleAdhocQuery).
Doc("Make an ad-hoc query in the specific namespace.").
Param(ws.PathParameter("namespace", "The name of the namespace.").DataType("string").Required(true)).
Param(ws.QueryParameter("expr", "The expression to be evaluated.").DataType("string").Required(false)).
Param(ws.QueryParameter("start", "Start time of query. Use **start** and **end** to retrieve metric data over a time span. It is a string with Unix time format, eg. 1559347200. ").DataType("string").Required(true)).
Param(ws.QueryParameter("start", "Start time of query. Use **start** and **end** to retrieve metric data over a time span. It is a string with Unix time format, eg. 1559347200. ").DataType("string").Required(false)).
Param(ws.QueryParameter("end", "End time of query. Use **start** and **end** to retrieve metric data over a time span. It is a string with Unix time format, eg. 1561939200. ").DataType("string").Required(false)).
Param(ws.QueryParameter("step", "Time interval. Retrieve metric data at a fixed interval within the time range of start and end. It requires both **start** and **end** are provided. The format is [0-9]+[smhdwy]. Defaults to 10m (i.e. 10 min).").DataType("string").DefaultValue("10m").Required(false)).
Param(ws.QueryParameter("time", "A timestamp in Unix time format. Retrieve metric data at a single point in time. Defaults to now. Time and the combination of start, end, step are mutually exclusive.").DataType("string").Required(false)).