Merge pull request #2007 from huanggze/dev-custom
feat: custom monitoring
This commit is contained in:
@@ -192,3 +192,36 @@ func (h handler) handleNamedMetricsQuery(resp *restful.Response, q queryOptions)
|
||||
}
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h handler) handleMetadataQuery(req *restful.Request, resp *restful.Response) {
|
||||
res := h.mo.GetMetadata(req.PathParameter("namespace"))
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
|
||||
func (h handler) handleAdhocQuery(req *restful.Request, resp *restful.Response) {
|
||||
var res monitoring.Metric
|
||||
|
||||
params := parseRequestParams(req)
|
||||
opt, err := h.makeQueryOptions(params, 0)
|
||||
if err != nil {
|
||||
if err.Error() == ErrNoHit {
|
||||
resp.WriteAsJson(res)
|
||||
return
|
||||
}
|
||||
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
return
|
||||
}
|
||||
|
||||
if opt.isRangeQuery() {
|
||||
res, err = h.mo.GetMetricOverTime(params.expression, params.namespaceName, opt.start, opt.end, opt.step)
|
||||
} else {
|
||||
res, err = h.mo.GetMetric(params.expression, params.namespaceName, opt.time)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
api.HandleBadRequest(resp, nil, err)
|
||||
} else {
|
||||
resp.WriteAsJson(res)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ type reqParams struct {
|
||||
pvcName string
|
||||
storageClassName string
|
||||
componentType string
|
||||
expression string
|
||||
}
|
||||
|
||||
type queryOptions struct {
|
||||
@@ -99,6 +100,7 @@ func parseRequestParams(req *restful.Request) reqParams {
|
||||
r.pvcName = req.PathParameter("pvc")
|
||||
r.storageClassName = req.PathParameter("storageclass")
|
||||
r.componentType = req.PathParameter("component")
|
||||
r.expression = req.QueryParameter("expr")
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -400,6 +400,29 @@ func AddToContainer(c *restful.Container, k8sClient kubernetes.Interface, monito
|
||||
Returns(http.StatusOK, RespOK, model.Metrics{})).
|
||||
Produces(restful.MIME_JSON)
|
||||
|
||||
ws.Route(ws.GET("/namespaces/{namespace}/targets/metadata").
|
||||
To(h.handleMetadataQuery).
|
||||
Doc("Get metadata of metrics for the specific namespace.").
|
||||
Param(ws.PathParameter("namespace", "The name of the namespace.").DataType("string").Required(true)).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomMetricsTag}).
|
||||
Writes(model.Metadata{}).
|
||||
Returns(http.StatusOK, RespOK, model.Metadata{})).
|
||||
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("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)).
|
||||
Metadata(restfulspec.KeyOpenAPITags, []string{constants.CustomMetricsTag}).
|
||||
Writes(monitoring.Metric{}).
|
||||
Returns(http.StatusOK, RespOK, monitoring.Metric{})).
|
||||
Produces(restful.MIME_JSON)
|
||||
|
||||
c.Add(ws)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user