fix bug for node metrics on overview page

Signed-off-by: zhu733756 <talonzhu@yunify.com>
This commit is contained in:
zhu733756
2021-04-12 13:49:38 +08:00
parent 26e5c31d8f
commit f49a9c1620
4 changed files with 17 additions and 3 deletions

View File

@@ -90,6 +90,7 @@ type reqParams struct {
cluster string cluster string
services string services string
pvcFilter string pvcFilter string
queryType string
} }
type queryOptions struct { type queryOptions struct {
@@ -145,6 +146,7 @@ func parseRequestParams(req *restful.Request) reqParams {
r.componentType = req.PathParameter("component") r.componentType = req.PathParameter("component")
r.expression = req.QueryParameter("expr") r.expression = req.QueryParameter("expr")
r.metric = req.QueryParameter("metric") r.metric = req.QueryParameter("metric")
r.queryType = req.QueryParameter("type")
return r return r
} }
@@ -213,6 +215,7 @@ func (h handler) makeQueryOptions(r reqParams, lvl monitoring.Level) (q queryOpt
NodeName: r.nodeName, NodeName: r.nodeName,
PVCFilter: r.pvcFilter, // metering pvc PVCFilter: r.pvcFilter, // metering pvc
StorageClassName: r.storageClassName, // metering pvc StorageClassName: r.storageClassName, // metering pvc
QueryType: r.queryType,
} }
q.namedMetrics = model.NodeMetrics q.namedMetrics = model.NodeMetrics

View File

@@ -80,6 +80,7 @@ func AddToContainer(c *restful.Container, k8sClient kubernetes.Interface, monito
Param(ws.QueryParameter("sort_type", "Sort order. One of asc, desc.").DefaultValue("desc.").DataType("string").Required(false)). Param(ws.QueryParameter("sort_type", "Sort order. One of asc, desc.").DefaultValue("desc.").DataType("string").Required(false)).
Param(ws.QueryParameter("page", "The page number. This field paginates result data of each metric, then returns a specific page. For example, setting **page** to 2 returns the second page. It only applies to sorted metric data.").DataType("integer").Required(false)). Param(ws.QueryParameter("page", "The page number. This field paginates result data of each metric, then returns a specific page. For example, setting **page** to 2 returns the second page. It only applies to sorted metric data.").DataType("integer").Required(false)).
Param(ws.QueryParameter("limit", "Page size, the maximum number of results in a single page. Defaults to 5.").DataType("integer").Required(false).DefaultValue("5")). Param(ws.QueryParameter("limit", "Page size, the maximum number of results in a single page. Defaults to 5.").DataType("integer").Required(false).DefaultValue("5")).
Param(ws.QueryParameter("type", "The query type. This field can be set to 'rank' for node ranking query or '' for others. Defaults to ''.").DataType("string").Required(false).DefaultValue("")).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.NodeMetricsTag}). Metadata(restfulspec.KeyOpenAPITags, []string{constants.NodeMetricsTag}).
Writes(model.Metrics{}). Writes(model.Metrics{}).
Returns(http.StatusOK, respOK, model.Metrics{})). Returns(http.StatusOK, respOK, model.Metrics{})).

View File

@@ -115,16 +115,23 @@ func (mo monitoringOperator) GetMetricOverTime(expr, namespace string, start, en
func (mo monitoringOperator) GetNamedMetrics(metrics []string, time time.Time, opt monitoring.QueryOption) Metrics { func (mo monitoringOperator) GetNamedMetrics(metrics []string, time time.Time, opt monitoring.QueryOption) Metrics {
ress := mo.prometheus.GetNamedMetrics(metrics, time, opt) ress := mo.prometheus.GetNamedMetrics(metrics, time, opt)
if mo.metricsserver != nil { opts := &monitoring.QueryOptions{}
opt.Apply(opts)
var isNodeRankingQuery bool
if opts.QueryType == "rank" {
isNodeRankingQuery = true
}
if mo.metricsserver != nil {
//Merge edge node metrics data //Merge edge node metrics data
edgeMetrics := make(map[string]monitoring.MetricData) edgeMetrics := make(map[string]monitoring.MetricData)
for i, ressMetric := range ress { for i, ressMetric := range ress {
metricName := ressMetric.MetricName metricName := ressMetric.MetricName
ressMetricValues := ressMetric.MetricData.MetricValues ressMetricValues := ressMetric.MetricData.MetricValues
if len(ressMetricValues) == 0 { if len(ressMetricValues) == 0 || isNodeRankingQuery {
// this metric has no prometheus metrics data // this metric has no prometheus metrics data or the request need to list all nodes metrics
if len(edgeMetrics) == 0 { if len(edgeMetrics) == 0 {
// start to request monintoring metricsApi data // start to request monintoring metricsApi data
mr := mo.metricsserver.GetNamedMetrics(metrics, time, opt) mr := mo.metricsserver.GetNamedMetrics(metrics, time, opt)

View File

@@ -67,6 +67,7 @@ type QueryOptions struct {
Level Level Level Level
NamespacedResourcesFilter string NamespacedResourcesFilter string
QueryType string
ResourceFilter string ResourceFilter string
NodeName string NodeName string
WorkspaceName string WorkspaceName string
@@ -98,6 +99,7 @@ type NodeOption struct {
NodeName string NodeName string
PVCFilter string PVCFilter string
StorageClassName string StorageClassName string
QueryType string
} }
func (no NodeOption) Apply(o *QueryOptions) { func (no NodeOption) Apply(o *QueryOptions) {
@@ -106,6 +108,7 @@ func (no NodeOption) Apply(o *QueryOptions) {
o.NodeName = no.NodeName o.NodeName = no.NodeName
o.PVCFilter = no.PVCFilter o.PVCFilter = no.PVCFilter
o.StorageClassName = no.StorageClassName o.StorageClassName = no.StorageClassName
o.QueryType = no.QueryType
} }
type WorkspaceOption struct { type WorkspaceOption struct {