Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
3e73be8d
Commit
3e73be8d
authored
Sep 21, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(prometheus): improve error handling
parent
4c88db3e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
13 deletions
+35
-13
pkg/tsdb/prometheus/prometheus.go
+34
-12
pkg/tsdb/prometheus/prometheus_test.go
+1
-1
No files found.
pkg/tsdb/prometheus/prometheus.go
View file @
3e73be8d
...
...
@@ -49,14 +49,17 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
client
,
err
:=
e
.
getClient
()
if
err
!=
nil
{
result
.
Error
=
err
return
result
return
resultWithError
(
result
,
err
)
}
from
,
_
:=
queryContext
.
TimeRange
.
FromTime
()
to
,
_
:=
queryContext
.
TimeRange
.
ToTime
()
query
:=
parseQuery
(
queries
)
query
,
err
:=
parseQuery
(
queries
)
if
err
!=
nil
{
return
resultWithError
(
result
,
err
)
}
timeRange
:=
prometheus
.
Range
{
Start
:
from
,
...
...
@@ -67,15 +70,14 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
value
,
err
:=
client
.
QueryRange
(
context
.
Background
(),
query
.
Expr
,
timeRange
)
if
err
!=
nil
{
result
.
Error
=
err
return
result
return
resultWithError
(
result
,
err
)
}
result
.
QueryResults
=
parseResponse
(
value
,
query
)
return
result
}
func
formatLegend
(
metric
pmodel
.
Metric
,
query
PrometheusQuery
)
string
{
func
formatLegend
(
metric
pmodel
.
Metric
,
query
*
PrometheusQuery
)
string
{
r
,
_
:=
regexp
.
Compile
(
`\{\{\s*(.+?)\s*\}\}`
)
result
:=
r
.
ReplaceAllFunc
([]
byte
(
query
.
LegendFormat
),
func
(
in
[]
byte
)
[]
byte
{
...
...
@@ -90,17 +92,32 @@ func formatLegend(metric pmodel.Metric, query PrometheusQuery) string {
return
string
(
result
)
}
func
parseQuery
(
queries
tsdb
.
QuerySlice
)
PrometheusQuery
{
func
parseQuery
(
queries
tsdb
.
QuerySlice
)
(
*
PrometheusQuery
,
error
)
{
queryModel
:=
queries
[
0
]
return
PrometheusQuery
{
Expr
:
queryModel
.
Model
.
Get
(
"expr"
)
.
MustString
(),
Step
:
time
.
Second
*
time
.
Duration
(
queryModel
.
Model
.
Get
(
"step"
)
.
MustInt64
(
1
)),
LegendFormat
:
queryModel
.
Model
.
Get
(
"legendFormat"
)
.
MustString
(),
expr
,
err
:=
queryModel
.
Model
.
Get
(
"expr"
)
.
String
()
if
err
!=
nil
{
return
nil
,
err
}
step
,
err
:=
queryModel
.
Model
.
Get
(
"step"
)
.
Int64
()
if
err
!=
nil
{
return
nil
,
err
}
format
,
err
:=
queryModel
.
Model
.
Get
(
"legendFormat"
)
.
String
()
if
err
!=
nil
{
return
nil
,
err
}
return
&
PrometheusQuery
{
Expr
:
expr
,
Step
:
time
.
Second
*
time
.
Duration
(
step
),
LegendFormat
:
format
,
},
nil
}
func
parseResponse
(
value
pmodel
.
Value
,
query
PrometheusQuery
)
map
[
string
]
*
tsdb
.
QueryResult
{
func
parseResponse
(
value
pmodel
.
Value
,
query
*
PrometheusQuery
)
map
[
string
]
*
tsdb
.
QueryResult
{
queryResults
:=
make
(
map
[
string
]
*
tsdb
.
QueryResult
)
queryRes
:=
&
tsdb
.
QueryResult
{}
...
...
@@ -123,3 +140,8 @@ func parseResponse(value pmodel.Value, query PrometheusQuery) map[string]*tsdb.Q
queryResults
[
"A"
]
=
queryRes
return
queryResults
}
func
resultWithError
(
result
*
tsdb
.
BatchResult
,
err
error
)
*
tsdb
.
BatchResult
{
result
.
Error
=
err
return
result
}
pkg/tsdb/prometheus/prometheus_test.go
View file @
3e73be8d
...
...
@@ -16,7 +16,7 @@ func TestPrometheus(t *testing.T) {
p
.
LabelName
(
"device"
)
:
p
.
LabelValue
(
"mobile"
),
}
query
:=
PrometheusQuery
{
query
:=
&
PrometheusQuery
{
LegendFormat
:
"legend {{app}} {{device}} {{broken}}"
,
}
...
...
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