Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
0e6a19a7
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
未验证
提交
0e6a19a7
编写于
9月 19, 2019
作者:
H
huanggze
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
comment-1
Signed-off-by:
N
huanggze
<
loganhuang@yunify.com
>
上级
df6ed5e9
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
54 addition
and
51 deletion
+54
-51
pkg/api/monitoring/v1alpha2/types.go
pkg/api/monitoring/v1alpha2/types.go
+23
-0
pkg/models/metrics/metrics.go
pkg/models/metrics/metrics.go
+3
-4
pkg/models/metrics/namespaces.go
pkg/models/metrics/namespaces.go
+12
-9
pkg/models/metrics/types.go
pkg/models/metrics/types.go
+2
-2
pkg/models/metrics/util.go
pkg/models/metrics/util.go
+10
-11
pkg/simple/client/prometheus/prometheus.go
pkg/simple/client/prometheus/prometheus.go
+4
-25
未找到文件。
pkg/api/monitoring/v1alpha2/types.go
0 → 100644
浏览文件 @
0e6a19a7
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"`
}
pkg/models/metrics/metrics.go
浏览文件 @
0e6a19a7
...
...
@@ -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
},
},
...
...
pkg/models/metrics/namespaces.go
浏览文件 @
0e6a19a7
...
...
@@ -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
)
}
}
}
...
...
pkg/models/metrics/types.go
浏览文件 @
0e6a19a7
...
...
@@ -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
{
...
...
pkg/models/metrics/util.go
浏览文件 @
0e6a19a7
...
...
@@ -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
{
g
log
.
Errorln
(
err
)
k
log
.
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
{
g
log
.
Errorln
(
err
)
k
log
.
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
{
g
log
.
Errorln
(
err
)
k
log
.
Errorln
(
err
)
}
else
{
if
l
>
0
{
limit
=
l
...
...
pkg/simple/client/prometheus/prometheus.go
浏览文件 @
0e6a19a7
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录