Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
so-operation-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
smart-operation
so-operation-api
Commits
6e4b2ca5
Commit
6e4b2ca5
authored
Jul 05, 2023
by
李科
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 普罗米修斯指标标签值
parent
61c8a2bc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
0 deletions
+55
-0
src/bean/vo/request/prometheus.go
src/bean/vo/request/prometheus.go
+6
-0
src/controller/prometheus.go
src/controller/prometheus.go
+16
-0
src/router/prometheusrouter.go
src/router/prometheusrouter.go
+1
-0
src/service/prometheus.go
src/service/prometheus.go
+32
-0
No files found.
src/bean/vo/request/prometheus.go
View file @
6e4b2ca5
...
@@ -3,3 +3,9 @@ package request
...
@@ -3,3 +3,9 @@ package request
type
PrometheusLabel
struct
{
type
PrometheusLabel
struct
{
LabelName
string
`json:"label_name" form:"label_name"`
LabelName
string
`json:"label_name" form:"label_name"`
}
}
type
PrometheusLabelValue
struct
{
MetricName
string
`json:"metric_name" form:"metric_name" binding:"required"`
MetricLabel
string
`json:"metric_label" form:"metric_label" binding:"required"`
Value
string
`json:"value" form:"value"`
}
src/controller/prometheus.go
View file @
6e4b2ca5
...
@@ -23,3 +23,19 @@ func PrometheusLabel(c *gin.Context) {
...
@@ -23,3 +23,19 @@ func PrometheusLabel(c *gin.Context) {
}
}
SendJsonResponse
(
c
,
resp
.
OK
,
data
)
SendJsonResponse
(
c
,
resp
.
OK
,
data
)
}
}
func
PrometheusLabelValue
(
c
*
gin
.
Context
)
{
var
req
request
.
PrometheusLabelValue
if
err
:=
c
.
ShouldBind
(
&
req
);
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
InvalidParam
.
TranslateError
(
err
),
nil
)
return
}
svc
:=
service
.
PrometheusSvc
{
User
:
header
.
GetUser
(
c
)}
data
,
err
:=
svc
.
LabelValue
(
req
)
if
err
!=
nil
{
SendJsonResponse
(
c
,
resp
.
FAIL
.
WithError
(
err
),
nil
)
return
}
SendJsonResponse
(
c
,
resp
.
OK
,
data
)
}
src/router/prometheusrouter.go
View file @
6e4b2ca5
...
@@ -12,5 +12,6 @@ func InitPrometheusRouter(e *gin.Engine) {
...
@@ -12,5 +12,6 @@ func InitPrometheusRouter(e *gin.Engine) {
group
:=
e
.
Group
(
fmt
.
Sprintf
(
"%s/prometheus"
,
conf
.
Options
.
Prefix
))
group
:=
e
.
Group
(
fmt
.
Sprintf
(
"%s/prometheus"
,
conf
.
Options
.
Prefix
))
{
{
group
.
GET
(
""
,
controller
.
PrometheusLabel
)
group
.
GET
(
""
,
controller
.
PrometheusLabel
)
group
.
GET
(
"value"
,
controller
.
PrometheusLabelValue
)
}
}
}
}
src/service/prometheus.go
View file @
6e4b2ca5
...
@@ -11,6 +11,7 @@ import (
...
@@ -11,6 +11,7 @@ import (
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/util"
"net/http"
"net/http"
"sort"
"sort"
"strings"
)
)
type
PrometheusSvc
struct
{
type
PrometheusSvc
struct
{
...
@@ -48,3 +49,34 @@ func (p *PrometheusSvc) Label(req request.PrometheusLabel) (resp response.Promet
...
@@ -48,3 +49,34 @@ func (p *PrometheusSvc) Label(req request.PrometheusLabel) (resp response.Promet
return
return
}
}
func
(
p
*
PrometheusSvc
)
LabelValue
(
req
request
.
PrometheusLabelValue
)
(
resp
response
.
PrometheusList
,
err
error
)
{
var
(
prometheusSeries
response
.
PrometheusSeries
metricLabelMap
=
make
(
map
[
string
][]
string
,
0
)
)
url
:=
fmt
.
Sprintf
(
"%s%s"
,
conf
.
Options
.
PrometheusHost
,
"/api/v1/series"
)
bytes
,
_
:=
util
.
Request
(
url
,
http
.
MethodPost
,
[]
byte
(
fmt
.
Sprintf
(
"match[]=%s"
,
req
.
MetricName
)),
map
[
string
]
string
{
"Content-Type"
:
util
.
MediaTypeForm
})
_
=
json
.
Unmarshal
(
bytes
,
&
prometheusSeries
)
for
_
,
v
:=
range
prometheusSeries
.
Data
{
for
key
,
value
:=
range
v
{
metricLabelMap
[
key
]
=
append
(
metricLabelMap
[
key
],
value
)
}
}
if
values
,
ok
:=
metricLabelMap
[
req
.
MetricLabel
];
ok
{
values
=
funk
.
UniqString
(
values
)
if
req
.
Value
!=
""
{
values
=
funk
.
FilterString
(
values
,
func
(
x
string
)
bool
{
return
strings
.
Contains
(
x
,
req
.
Value
)
})
}
resp
.
List
=
values
}
sort
.
Strings
(
resp
.
List
)
resp
.
TotalCount
=
int64
(
len
(
resp
.
List
))
return
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment