23
pkg/api/monitoring/v1alpha2/types.go
Normal file
23
pkg/api/monitoring/v1alpha2/types.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
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"
|
"fmt"
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
|
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
|
||||||
"kubesphere.io/kubesphere/pkg/models/workspaces"
|
"kubesphere.io/kubesphere/pkg/models/workspaces"
|
||||||
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
cs "kubesphere.io/kubesphere/pkg/simple/client"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -29,8 +30,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/prometheus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary
|
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
@@ -789,9 +788,9 @@ func GetWorkspaceStatistics(workspaceName string) *Response {
|
|||||||
|
|
||||||
func (response *APIResponse) withMetricResult(time int64, value int) {
|
func (response *APIResponse) withMetricResult(time int64, value int) {
|
||||||
response.Status = "success"
|
response.Status = "success"
|
||||||
response.Data = prometheus.QueryResult{
|
response.Data = v1alpha2.QueryResult{
|
||||||
ResultType: "vector",
|
ResultType: "vector",
|
||||||
Result: []prometheus.QueryValue{
|
Result: []v1alpha2.QueryValue{
|
||||||
{
|
{
|
||||||
Value: []interface{}{time, value},
|
Value: []interface{}{time, value},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -43,16 +43,19 @@ func GetNamespacesWithMetrics(namespaces []*v1.Namespace) []*v1.Namespace {
|
|||||||
|
|
||||||
for _, result := range rawMetrics.Results {
|
for _, result := range rawMetrics.Results {
|
||||||
for _, data := range result.Data.Result {
|
for _, data := range result.Data.Result {
|
||||||
if ns, exist := data.Metric["namespace"]; exist {
|
|
||||||
if len(data.Value) == 2 {
|
ns, exist := data.Metric["namespace"]
|
||||||
for i := 0; i < len(namespaces); i++ {
|
|
||||||
if namespaces[i].Name == ns {
|
if !exist || len(data.Value) != 2 {
|
||||||
if namespaces[i].Annotations == nil {
|
continue
|
||||||
namespaces[i].Annotations = make(map[string]string, 0)
|
}
|
||||||
}
|
|
||||||
namespaces[i].Annotations[result.MetricName] = data.Value[1].(string)
|
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
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/prometheus"
|
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ type RequestParams struct {
|
|||||||
|
|
||||||
type APIResponse struct {
|
type APIResponse struct {
|
||||||
MetricName string `json:"metric_name,omitempty" description:"metric name, eg. scheduler_up_sum"`
|
MetricName string `json:"metric_name,omitempty" description:"metric name, eg. scheduler_up_sum"`
|
||||||
prometheus.APIResponse
|
v1alpha2.APIResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
|||||||
@@ -20,15 +20,14 @@ package metrics
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
"k8s.io/klog"
|
||||||
|
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
|
||||||
"kubesphere.io/kubesphere/pkg/informers"
|
"kubesphere.io/kubesphere/pkg/informers"
|
||||||
"kubesphere.io/kubesphere/pkg/simple/client/prometheus"
|
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -44,8 +43,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FormatedMetricDataWrapper struct {
|
type FormatedMetricDataWrapper struct {
|
||||||
fmtMetricData prometheus.QueryResult
|
fmtMetricData v1alpha2.QueryResult
|
||||||
by func(p, q *prometheus.QueryValue) bool
|
by func(p, q *v1alpha2.QueryValue) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wrapper FormatedMetricDataWrapper) Len() int {
|
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) {
|
func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Response, int) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
glog.Errorln(err)
|
klog.Errorln(err)
|
||||||
debug.PrintStack()
|
debug.PrintStack()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -92,7 +91,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res
|
|||||||
if metricItem.MetricName == sortMetricName {
|
if metricItem.MetricName == sortMetricName {
|
||||||
if sortType == ResultSortTypeAsc {
|
if sortType == ResultSortTypeAsc {
|
||||||
// asc
|
// 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
|
value1 := p.Value
|
||||||
value2 := q.Value
|
value2 := q.Value
|
||||||
v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64)
|
v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64)
|
||||||
@@ -107,7 +106,7 @@ func (rawMetrics *Response) SortBy(sortMetricName string, sortType string) (*Res
|
|||||||
}})
|
}})
|
||||||
} else {
|
} else {
|
||||||
// desc
|
// 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
|
value1 := p.Value
|
||||||
value2 := q.Value
|
value2 := q.Value
|
||||||
v1, _ := strconv.ParseFloat(value1[len(value1)-1].(string), 64)
|
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++ {
|
for i := 0; i < len(rawMetrics.Results); i++ {
|
||||||
re := rawMetrics.Results[i]
|
re := rawMetrics.Results[i]
|
||||||
if re.Data.ResultType == ResultTypeVector && re.Status == MetricStatusSuccess {
|
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++ {
|
for j := 0; j < len(re.Data.Result); j++ {
|
||||||
r := re.Data.Result[j]
|
r := re.Data.Result[j]
|
||||||
k, exist := r.Metric[ResultItemMetricResourceName]
|
k, exist := r.Metric[ResultItemMetricResourceName]
|
||||||
@@ -200,7 +199,7 @@ func (fmtLevelMetric *Response) Page(pageNum string, limitNum string, maxLength
|
|||||||
if pageNum != "" {
|
if pageNum != "" {
|
||||||
p, err := strconv.Atoi(pageNum)
|
p, err := strconv.Atoi(pageNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorln(err)
|
klog.Errorln(err)
|
||||||
} else {
|
} else {
|
||||||
if p > 0 {
|
if p > 0 {
|
||||||
page = p
|
page = p
|
||||||
@@ -216,7 +215,7 @@ func (fmtLevelMetric *Response) Page(pageNum string, limitNum string, maxLength
|
|||||||
if limitNum != "" {
|
if limitNum != "" {
|
||||||
l, err := strconv.Atoi(limitNum)
|
l, err := strconv.Atoi(limitNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorln(err)
|
klog.Errorln(err)
|
||||||
} else {
|
} else {
|
||||||
if l > 0 {
|
if l > 0 {
|
||||||
limit = l
|
limit = l
|
||||||
|
|||||||
@@ -22,32 +22,11 @@ import (
|
|||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"k8s.io/klog"
|
"k8s.io/klog"
|
||||||
|
"kubesphere.io/kubesphere/pkg/api/monitoring/v1alpha2"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"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 {
|
type PrometheusClient struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
endpoint string
|
endpoint string
|
||||||
@@ -64,17 +43,17 @@ func NewPrometheusClient(options *PrometheusOptions) (*PrometheusClient, error)
|
|||||||
}, nil
|
}, 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)
|
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)
|
return c.query(c.secondaryEndpoint, queryType, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
var jsonIter = jsoniter.ConfigCompatibleWithStandardLibrary
|
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)
|
url := fmt.Sprintf("%s/api/v1/%s?%s", endpoint, queryType, params)
|
||||||
|
|
||||||
response, err := c.client.Get(url)
|
response, err := c.client.Get(url)
|
||||||
|
|||||||
Reference in New Issue
Block a user