未验证 提交 0e6a19a7 编写于 作者: H huanggze

comment-1

Signed-off-by: Nhuanggze <loganhuang@yunify.com>
上级 df6ed5e9
package v1alpha2
// Prometheus query api response
type APIResponse struct {
Status string `json:"status" description:"result status, one of error, success"`
Data QueryResult `json:"data" description:"actual metric result"`
ErrorType string `json:"errorType,omitempty"`
Error string `json:"error,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
// QueryResult includes result data from a query.
type QueryResult struct {
ResultType string `json:"resultType" description:"result type, one of matrix, vector"`
Result []QueryValue `json:"result" description:"metric data including labels, time series and values"`
}
// Time Series
type QueryValue struct {
Metric map[string]string `json:"metric,omitempty" description:"time series labels"`
Value []interface{} `json:"value,omitempty" description:"time series, values of vector type"`
Values [][]interface{} `json:"values,omitempty" description:"time series, values of matrix type"`
}
......@@ -22,6 +22,7 @@ import (
"fmt"
"github.com/json-iterator/go"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
"kubesphere.io/kubesphere/pkg/models/workspaces"
cs "kubesphere.io/kubesphere/pkg/simple/client"
"net/url"
......@@ -29,8 +30,6 @@ import (
"strings"
"sync"
"time"
"kubesphere.io/kubesphere/pkg/simple/client/prometheus"
)
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary
......@@ -789,9 +788,9 @@ func GetWorkspaceStatistics(workspaceName string) *Response {
func (response *APIResponse) withMetricResult(time int64, value int) {
response.Status = "success"
response.Data = prometheus.QueryResult{
response.Data = v1alpha2.QueryResult{
ResultType: "vector",
Result: []prometheus.QueryValue{
Result: []v1alpha2.QueryValue{
{
Value: []interface{}{time, value},
},
......
......@@ -43,16 +43,19 @@ func GetNamespacesWithMetrics(namespaces []*v1.Namespace) []*v1.Namespace {
for _, result := range rawMetrics.Results {
for _, data := range result.Data.Result {
if ns, exist := data.Metric["namespace"]; exist {
if len(data.Value) == 2 {
for i := 0; i < len(namespaces); i++ {
if namespaces[i].Name == ns {
if namespaces[i].Annotations == nil {
namespaces[i].Annotations = make(map[string]string, 0)
}
namespaces[i].Annotations[result.MetricName] = data.Value[1].(string)
}
ns, exist := data.Metric["namespace"]
if !exist || len(data.Value) != 2 {
continue
}
for _, item := range namespaces {
if item.Name == ns {
if item.Annotations == nil {
item.Annotations = make(map[string]string, 0)
}
item.Annotations[result.MetricName] = data.Value[1].(string)
}
}
}
......
......@@ -19,7 +19,7 @@
package metrics
import (
"kubesphere.io/kubesphere/pkg/simple/client/prometheus"
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
"net/url"
)
......@@ -47,7 +47,7 @@ type RequestParams struct {
type APIResponse struct {
MetricName string `json:"metric_name,omitempty" description:"metric name, eg. scheduler_up_sum"`
prometheus.APIResponse
v1alpha2.APIResponse
}
type Response struct {
......
......@@ -20,15 +20,14 @@ package metrics
import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
"kubesphere.io/kubesphere/pkg/informers"
"kubesphere.io/kubesphere/pkg/simple/client/prometheus"
"math"
"sort"
"strconv"
"runtime/debug"
"github.com/golang/glog"
)
const (
......@@ -44,8 +43,8 @@ const (
)
type FormatedMetricDataWrapper struct {
fmtMetricData prometheus.QueryResult
by func(p, q *prometheus.QueryValue) bool
fmtMetricData v1alpha2.QueryResult
by func(p, q *v1alpha2.QueryValue) bool
}
func (wrapper FormatedMetricDataWrapper) Len() int {
......@@ -64,7 +63,7 @@ func (wrapper FormatedMetricDataWrapper) Swap(i, j int) {
func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Response, int) {
defer func() {
if err := recover(); err != nil {
glog.Errorln(err)
klog.Errorln(err)
debug.PrintStack()
}
}()
......@@ -92,7 +91,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res
if metricItem.MetricName == sortMetricName {
if sortType == ResultSortTypeAsc {
// asc
sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *prometheus.QueryValue) bool {
sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *v1alpha2.QueryValue) bool {
value1 := p.Value
value2 := q.Value
v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64)
......@@ -107,7 +106,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res
}})
} else {
// desc
sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *prometheus.QueryValue) bool {
sort.Sort(FormatedMetricDataWrapper{metricItem.Data, func(p, q *v1alpha2.QueryValue) bool {
value1 := p.Value
value2 := q.Value
v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64)
......@@ -164,7 +163,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res
for i := 0; i < len(rawMetrics.Results); i++ {
re := rawMetrics.Results[i]
if re.Data.ResultType == ResultTypeVector && re.Status == MetricStatusSuccess {
sortedMetric := make([]prometheus.QueryValue, len(indexMap))
sortedMetric := make([]v1alpha2.QueryValue, len(indexMap))
for j := 0; j < len(re.Data.Result); j++ {
r := re.Data.Result[j]
k, exist := r.Metric[ResultItemMetricResourceName]
......@@ -200,7 +199,7 @@ func (fmtLevelMetric *Response) Page(pageNum string, limitNum string, maxLength
if pageNum != "" {
p, err := strconv.Atoi(pageNum)
if err != nil {
glog.Errorln(err)
klog.Errorln(err)
} else {
if p > 0 {
page = p
......@@ -216,7 +215,7 @@ func (fmtLevelMetric *Response) Page(pageNum string, limitNum string, maxLength
if limitNum != "" {
l, err := strconv.Atoi(limitNum)
if err != nil {
glog.Errorln(err)
klog.Errorln(err)
} else {
if l > 0 {
limit = l
......
......@@ -22,32 +22,11 @@ import (
jsoniter "github.com/json-iterator/go"
"io/ioutil"
"k8s.io/klog"
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
"net/http"
"time"
)
// Prometheus query api response
type APIResponse struct {
Status string `json:"status" description:"result status, one of error, success"`
Data QueryResult `json:"data" description:"actual metric result"`
ErrorType string `json:"errorType,omitempty"`
Error string `json:"error,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
// QueryResult includes result data from a query.
type QueryResult struct {
ResultType string `json:"resultType" description:"result type, one of matrix, vector"`
Result []QueryValue `json:"result" description:"metric data including labels, time series and values"`
}
// Time Series
type QueryValue struct {
Metric map[string]string `json:"metric,omitempty" description:"time series labels"`
Value []interface{} `json:"value,omitempty" description:"time series, values of vector type"`
Values [][]interface{} `json:"values,omitempty" description:"time series, values of matrix type"`
}
type PrometheusClient struct {
client *http.Client
endpoint string
......@@ -64,17 +43,17 @@ func NewPrometheusClient(options *PrometheusOptions) (*PrometheusClient, error)
}, nil
}
func (c *PrometheusClient) QueryToK8SPrometheus(queryType string, params string) (apiResponse APIResponse) {
func (c *PrometheusClient) QueryToK8SPrometheus(queryType string, params string) (apiResponse v1alpha2.APIResponse) {
return c.query(c.endpoint, queryType, params)
}
func (c *PrometheusClient) QueryToK8SSystemPrometheus(queryType string, params string) (apiResponse APIResponse) {
func (c *PrometheusClient) QueryToK8SSystemPrometheus(queryType string, params string) (apiResponse v1alpha2.APIResponse) {
return c.query(c.secondaryEndpoint, queryType, params)
}
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary
func (c *PrometheusClient) query(endpoint string, queryType string, params string) (apiResponse APIResponse) {
func (c *PrometheusClient) query(endpoint string, queryType string, params string) (apiResponse v1alpha2.APIResponse) {
url := fmt.Sprintf("%s/api/v1/%s?%s", endpoint, queryType, params)
response, err := c.client.Get(url)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册