Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
53eabed3
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看板
提交
53eabed3
编写于
7月 02, 2020
作者:
J
junotx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lazy init events client and optimize events search
上级
de5f4c36
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
82 addition
and
14 deletion
+82
-14
pkg/simple/client/events/elasticsearch/clients.go
pkg/simple/client/events/elasticsearch/clients.go
+6
-3
pkg/simple/client/events/elasticsearch/elasticsearch.go
pkg/simple/client/events/elasticsearch/elasticsearch.go
+76
-11
未找到文件。
pkg/simple/client/events/elasticsearch/clients.go
浏览文件 @
53eabed3
...
...
@@ -41,7 +41,8 @@ func (e Error) Error() string {
type
ClientV5
es5
.
Client
func
(
c
*
ClientV5
)
ExSearch
(
r
*
Request
)
(
*
Response
,
error
)
{
return
c
.
parse
(
c
.
Search
(
c
.
Search
.
WithIndex
(
r
.
Index
),
c
.
Search
.
WithBody
(
r
.
Body
)))
return
c
.
parse
(
c
.
Search
(
c
.
Search
.
WithIndex
(
r
.
Index
),
c
.
Search
.
WithBody
(
r
.
Body
),
c
.
Search
.
WithIgnoreUnavailable
(
true
)))
}
func
(
c
*
ClientV5
)
parse
(
resp
*
es5api
.
Response
,
err
error
)
(
*
Response
,
error
)
{
if
err
!=
nil
{
...
...
@@ -85,7 +86,8 @@ func (c *ClientV5) Version() (string, error) {
type
ClientV6
es6
.
Client
func
(
c
*
ClientV6
)
ExSearch
(
r
*
Request
)
(
*
Response
,
error
)
{
return
c
.
parse
(
c
.
Search
(
c
.
Search
.
WithIndex
(
r
.
Index
),
c
.
Search
.
WithBody
(
r
.
Body
)))
return
c
.
parse
(
c
.
Search
(
c
.
Search
.
WithIndex
(
r
.
Index
),
c
.
Search
.
WithBody
(
r
.
Body
),
c
.
Search
.
WithIgnoreUnavailable
(
true
)))
}
func
(
c
*
ClientV6
)
parse
(
resp
*
es6api
.
Response
,
err
error
)
(
*
Response
,
error
)
{
if
err
!=
nil
{
...
...
@@ -114,7 +116,8 @@ func (c *ClientV6) parse(resp *es6api.Response, err error) (*Response, error) {
type
ClientV7
es7
.
Client
func
(
c
*
ClientV7
)
ExSearch
(
r
*
Request
)
(
*
Response
,
error
)
{
return
c
.
parse
(
c
.
Search
(
c
.
Search
.
WithIndex
(
r
.
Index
),
c
.
Search
.
WithBody
(
r
.
Body
)))
return
c
.
parse
(
c
.
Search
(
c
.
Search
.
WithIndex
(
r
.
Index
),
c
.
Search
.
WithBody
(
r
.
Body
),
c
.
Search
.
WithIgnoreUnavailable
(
true
)))
}
func
(
c
*
ClientV7
)
parse
(
resp
*
es7api
.
Response
,
err
error
)
(
*
Response
,
error
)
{
if
err
!=
nil
{
...
...
pkg/simple/client/events/elasticsearch/elasticsearch.go
浏览文件 @
53eabed3
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"strings"
"sync"
"time"
es5
"github.com/elastic/go-elasticsearch/v5"
...
...
@@ -12,18 +13,19 @@ import (
jsoniter
"github.com/json-iterator/go"
corev1
"k8s.io/api/core/v1"
"kubesphere.io/kubesphere/pkg/simple/client/events"
"kubesphere.io/kubesphere/pkg/utils/esutil"
)
var
json
=
jsoniter
.
ConfigCompatibleWithStandardLibrary
type
E
lasticsearch
struct
{
type
e
lasticsearch
struct
{
c
client
opts
struct
{
index
string
index
Prefix
string
}
}
func
(
es
*
E
lasticsearch
)
SearchEvents
(
filter
*
events
.
Filter
,
from
,
size
int64
,
func
(
es
*
e
lasticsearch
)
SearchEvents
(
filter
*
events
.
Filter
,
from
,
size
int64
,
sort
string
)
(
*
events
.
Events
,
error
)
{
queryPart
:=
parseToQueryPart
(
filter
)
if
sort
==
""
{
...
...
@@ -44,7 +46,7 @@ func (es *Elasticsearch) SearchEvents(filter *events.Filter, from, size int64,
return
nil
,
err
}
resp
,
err
:=
es
.
c
.
ExSearch
(
&
Request
{
Index
:
es
.
opts
.
index
,
Index
:
resolveIndexNames
(
es
.
opts
.
indexPrefix
,
filter
.
StartTime
,
filter
.
EndTime
)
,
Body
:
bytes
.
NewBuffer
(
body
),
})
if
err
!=
nil
||
resp
==
nil
{
...
...
@@ -64,7 +66,7 @@ func (es *Elasticsearch) SearchEvents(filter *events.Filter, from, size int64,
return
&
evts
,
nil
}
func
(
es
*
E
lasticsearch
)
CountOverTime
(
filter
*
events
.
Filter
,
interval
string
)
(
*
events
.
Histogram
,
error
)
{
func
(
es
*
e
lasticsearch
)
CountOverTime
(
filter
*
events
.
Filter
,
interval
string
)
(
*
events
.
Histogram
,
error
)
{
if
interval
==
""
{
interval
=
"15m"
}
...
...
@@ -90,7 +92,7 @@ func (es *Elasticsearch) CountOverTime(filter *events.Filter, interval string) (
return
nil
,
err
}
resp
,
err
:=
es
.
c
.
ExSearch
(
&
Request
{
Index
:
es
.
opts
.
index
,
Index
:
resolveIndexNames
(
es
.
opts
.
indexPrefix
,
filter
.
StartTime
,
filter
.
EndTime
)
,
Body
:
bytes
.
NewBuffer
(
body
),
})
if
err
!=
nil
||
resp
==
nil
{
...
...
@@ -116,7 +118,7 @@ func (es *Elasticsearch) CountOverTime(filter *events.Filter, interval string) (
return
&
histo
,
nil
}
func
(
es
*
E
lasticsearch
)
StatisticsOnResources
(
filter
*
events
.
Filter
)
(
*
events
.
Statistics
,
error
)
{
func
(
es
*
e
lasticsearch
)
StatisticsOnResources
(
filter
*
events
.
Filter
)
(
*
events
.
Statistics
,
error
)
{
queryPart
:=
parseToQueryPart
(
filter
)
aggName
:=
"resources_count"
aggsPart
:=
map
[
string
]
interface
{}{
...
...
@@ -137,7 +139,7 @@ func (es *Elasticsearch) StatisticsOnResources(filter *events.Filter) (*events.S
return
nil
,
err
}
resp
,
err
:=
es
.
c
.
ExSearch
(
&
Request
{
Index
:
es
.
opts
.
index
,
Index
:
resolveIndexNames
(
es
.
opts
.
indexPrefix
,
filter
.
StartTime
,
filter
.
EndTime
)
,
Body
:
bytes
.
NewBuffer
(
body
),
})
if
err
!=
nil
||
resp
==
nil
{
...
...
@@ -158,7 +160,7 @@ func (es *Elasticsearch) StatisticsOnResources(filter *events.Filter) (*events.S
},
nil
}
func
NewClient
(
options
*
Options
)
(
*
E
lasticsearch
,
error
)
{
func
newClient
(
options
*
Options
)
(
*
e
lasticsearch
,
error
)
{
clientV5
:=
func
()
(
*
ClientV5
,
error
)
{
c
,
err
:=
es5
.
NewClient
(
es5
.
Config
{
Addresses
:
[]
string
{
options
.
Host
}})
if
err
!=
nil
{
...
...
@@ -183,10 +185,10 @@ func NewClient(options *Options) (*Elasticsearch, error) {
var
(
version
=
options
.
Version
es
=
E
lasticsearch
{}
es
=
e
lasticsearch
{}
err
error
)
es
.
opts
.
index
=
fmt
.
Sprintf
(
"%s*"
,
options
.
IndexPrefix
)
es
.
opts
.
index
Prefix
=
options
.
IndexPrefix
if
options
.
Version
==
""
{
var
c5
*
ClientV5
...
...
@@ -218,6 +220,58 @@ func NewClient(options *Options) (*Elasticsearch, error) {
return
&
es
,
nil
}
type
Elasticsearch
struct
{
innerEs
*
elasticsearch
options
Options
mutex
sync
.
Mutex
}
func
(
es
*
Elasticsearch
)
SearchEvents
(
filter
*
events
.
Filter
,
from
,
size
int64
,
sort
string
)
(
*
events
.
Events
,
error
)
{
ies
,
e
:=
es
.
getInnerEs
()
if
e
!=
nil
{
return
nil
,
e
}
return
ies
.
SearchEvents
(
filter
,
from
,
size
,
sort
)
}
func
(
es
*
Elasticsearch
)
CountOverTime
(
filter
*
events
.
Filter
,
interval
string
)
(
*
events
.
Histogram
,
error
)
{
ies
,
e
:=
es
.
getInnerEs
()
if
e
!=
nil
{
return
nil
,
e
}
return
ies
.
CountOverTime
(
filter
,
interval
)
}
func
(
es
*
Elasticsearch
)
StatisticsOnResources
(
filter
*
events
.
Filter
)
(
*
events
.
Statistics
,
error
)
{
ies
,
e
:=
es
.
getInnerEs
()
if
e
!=
nil
{
return
nil
,
e
}
return
ies
.
StatisticsOnResources
(
filter
)
}
func
(
es
*
Elasticsearch
)
getInnerEs
()
(
*
elasticsearch
,
error
)
{
if
es
.
innerEs
!=
nil
{
return
es
.
innerEs
,
nil
}
es
.
mutex
.
Lock
()
defer
es
.
mutex
.
Unlock
()
if
es
.
innerEs
!=
nil
{
return
es
.
innerEs
,
nil
}
ies
,
err
:=
newClient
(
&
es
.
options
)
if
err
!=
nil
{
return
nil
,
err
}
es
.
innerEs
=
ies
return
es
.
innerEs
,
nil
}
func
NewClient
(
options
*
Options
)
(
*
Elasticsearch
,
error
)
{
return
&
Elasticsearch
{
options
:
*
options
},
nil
}
func
parseToQueryPart
(
f
*
events
.
Filter
)
interface
{}
{
if
f
==
nil
{
return
nil
...
...
@@ -347,3 +401,14 @@ func parseToQueryPart(f *events.Filter) interface{} {
return
queryBody
}
func
resolveIndexNames
(
prefix
string
,
start
,
end
*
time
.
Time
)
string
{
var
s
,
e
time
.
Time
if
start
!=
nil
{
s
=
*
start
}
if
end
!=
nil
{
e
=
*
end
}
return
esutil
.
ResolveIndexNames
(
prefix
,
s
,
e
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录