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
e234cf5b
Commit
e234cf5b
authored
Dec 22, 2017
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
prom: removes limitation of one query per tsdb call
parent
cd92d219
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
19 deletions
+38
-19
pkg/tsdb/prometheus/prometheus.go
+22
-13
pkg/tsdb/prometheus/prometheus_test.go
+15
-6
pkg/tsdb/prometheus/types.go
+1
-0
No files found.
pkg/tsdb/prometheus/prometheus.go
View file @
e234cf5b
...
@@ -83,24 +83,29 @@ func (e *PrometheusExecutor) getClient(dsInfo *models.DataSource) (apiv1.API, er
...
@@ -83,24 +83,29 @@ func (e *PrometheusExecutor) getClient(dsInfo *models.DataSource) (apiv1.API, er
}
}
func
(
e
*
PrometheusExecutor
)
Query
(
ctx
context
.
Context
,
dsInfo
*
models
.
DataSource
,
tsdbQuery
*
tsdb
.
TsdbQuery
)
(
*
tsdb
.
Response
,
error
)
{
func
(
e
*
PrometheusExecutor
)
Query
(
ctx
context
.
Context
,
dsInfo
*
models
.
DataSource
,
tsdbQuery
*
tsdb
.
TsdbQuery
)
(
*
tsdb
.
Response
,
error
)
{
result
:=
&
tsdb
.
Response
{}
result
:=
&
tsdb
.
Response
{
Results
:
map
[
string
]
*
tsdb
.
QueryResult
{},
}
client
,
err
:=
e
.
getClient
(
dsInfo
)
client
,
err
:=
e
.
getClient
(
dsInfo
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
query
,
err
:=
parseQuery
(
dsInfo
,
tsdbQuery
.
Queries
,
tsdbQuery
)
query
s
,
err
:=
parseQuery
(
dsInfo
,
tsdbQuery
.
Queries
,
tsdbQuery
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
for
_
,
query
:=
range
querys
{
timeRange
:=
apiv1
.
Range
{
timeRange
:=
apiv1
.
Range
{
Start
:
query
.
Start
,
Start
:
query
.
Start
,
End
:
query
.
End
,
End
:
query
.
End
,
Step
:
query
.
Step
,
Step
:
query
.
Step
,
}
}
plog
.
Debug
(
"Sending query"
,
"start"
,
timeRange
.
Start
,
"end"
,
timeRange
.
End
,
"step"
,
timeRange
.
Step
,
"query"
,
query
.
Expr
)
span
,
ctx
:=
opentracing
.
StartSpanFromContext
(
ctx
,
"alerting.prometheus"
)
span
,
ctx
:=
opentracing
.
StartSpanFromContext
(
ctx
,
"alerting.prometheus"
)
span
.
SetTag
(
"expr"
,
query
.
Expr
)
span
.
SetTag
(
"expr"
,
query
.
Expr
)
span
.
SetTag
(
"start_unixnano"
,
int64
(
query
.
Start
.
UnixNano
()))
span
.
SetTag
(
"start_unixnano"
,
int64
(
query
.
Start
.
UnixNano
()))
...
@@ -117,7 +122,9 @@ func (e *PrometheusExecutor) Query(ctx context.Context, dsInfo *models.DataSourc
...
@@ -117,7 +122,9 @@ func (e *PrometheusExecutor) Query(ctx context.Context, dsInfo *models.DataSourc
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
result
.
Results
=
queryResult
result
.
Results
[
query
.
RefId
]
=
queryResult
}
return
result
,
nil
return
result
,
nil
}
}
...
@@ -140,9 +147,9 @@ func formatLegend(metric model.Metric, query *PrometheusQuery) string {
...
@@ -140,9 +147,9 @@ func formatLegend(metric model.Metric, query *PrometheusQuery) string {
return
string
(
result
)
return
string
(
result
)
}
}
func
parseQuery
(
dsInfo
*
models
.
DataSource
,
queries
[]
*
tsdb
.
Query
,
queryContext
*
tsdb
.
TsdbQuery
)
(
*
PrometheusQuery
,
error
)
{
func
parseQuery
(
dsInfo
*
models
.
DataSource
,
queries
[]
*
tsdb
.
Query
,
queryContext
*
tsdb
.
TsdbQuery
)
(
[]
*
PrometheusQuery
,
error
)
{
q
ueryModel
:=
queries
[
0
]
q
s
:=
[]
*
PrometheusQuery
{}
for
_
,
queryModel
:=
range
queries
{
expr
,
err
:=
queryModel
.
Model
.
Get
(
"expr"
)
.
String
()
expr
,
err
:=
queryModel
.
Model
.
Get
(
"expr"
)
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
...
@@ -169,22 +176,25 @@ func parseQuery(dsInfo *models.DataSource, queries []*tsdb.Query, queryContext *
...
@@ -169,22 +176,25 @@ func parseQuery(dsInfo *models.DataSource, queries []*tsdb.Query, queryContext *
interval
:=
intervalCalculator
.
Calculate
(
queryContext
.
TimeRange
,
dsInterval
)
interval
:=
intervalCalculator
.
Calculate
(
queryContext
.
TimeRange
,
dsInterval
)
step
:=
time
.
Duration
(
int64
(
interval
.
Value
)
*
intervalFactor
)
step
:=
time
.
Duration
(
int64
(
interval
.
Value
)
*
intervalFactor
)
return
&
PrometheusQuery
{
qs
=
append
(
qs
,
&
PrometheusQuery
{
Expr
:
expr
,
Expr
:
expr
,
Step
:
step
,
Step
:
step
,
LegendFormat
:
format
,
LegendFormat
:
format
,
Start
:
start
,
Start
:
start
,
End
:
end
,
End
:
end
,
},
nil
RefId
:
queryModel
.
RefId
,
})
}
return
qs
,
nil
}
}
func
parseResponse
(
value
model
.
Value
,
query
*
PrometheusQuery
)
(
map
[
string
]
*
tsdb
.
QueryResult
,
error
)
{
func
parseResponse
(
value
model
.
Value
,
query
*
PrometheusQuery
)
(
*
tsdb
.
QueryResult
,
error
)
{
queryResults
:=
make
(
map
[
string
]
*
tsdb
.
QueryResult
)
queryRes
:=
tsdb
.
NewQueryResult
()
queryRes
:=
tsdb
.
NewQueryResult
()
data
,
ok
:=
value
.
(
model
.
Matrix
)
data
,
ok
:=
value
.
(
model
.
Matrix
)
if
!
ok
{
if
!
ok
{
return
queryRes
ults
,
fmt
.
Errorf
(
"Unsupported result format: %s"
,
value
.
Type
()
.
String
())
return
queryRes
,
fmt
.
Errorf
(
"Unsupported result format: %s"
,
value
.
Type
()
.
String
())
}
}
for
_
,
v
:=
range
data
{
for
_
,
v
:=
range
data
{
...
@@ -204,6 +214,5 @@ func parseResponse(value model.Value, query *PrometheusQuery) (map[string]*tsdb.
...
@@ -204,6 +214,5 @@ func parseResponse(value model.Value, query *PrometheusQuery) (map[string]*tsdb.
queryRes
.
Series
=
append
(
queryRes
.
Series
,
&
series
)
queryRes
.
Series
=
append
(
queryRes
.
Series
,
&
series
)
}
}
queryResults
[
"A"
]
=
queryRes
return
queryRes
,
nil
return
queryResults
,
nil
}
}
pkg/tsdb/prometheus/prometheus_test.go
View file @
e234cf5b
...
@@ -60,9 +60,10 @@ func TestPrometheus(t *testing.T) {
...
@@ -60,9 +60,10 @@ func TestPrometheus(t *testing.T) {
Convey
(
"with 48h time range"
,
func
()
{
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"12h"
,
"now"
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"12h"
,
"now"
)
model
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
models
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Second
*
30
)
So
(
model
.
Step
,
ShouldEqual
,
time
.
Second
*
30
)
})
})
})
})
...
@@ -83,18 +84,22 @@ func TestPrometheus(t *testing.T) {
...
@@ -83,18 +84,22 @@ func TestPrometheus(t *testing.T) {
Convey
(
"with 48h time range"
,
func
()
{
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
model
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
model
s
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
2
)
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
2
)
})
})
Convey
(
"with 1h time range"
,
func
()
{
Convey
(
"with 1h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"1h"
,
"now"
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"1h"
,
"now"
)
model
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
model
s
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Second
*
15
)
So
(
model
.
Step
,
ShouldEqual
,
time
.
Second
*
15
)
})
})
})
})
...
@@ -116,9 +121,11 @@ func TestPrometheus(t *testing.T) {
...
@@ -116,9 +121,11 @@ func TestPrometheus(t *testing.T) {
Convey
(
"with 48h time range"
,
func
()
{
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
model
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
model
s
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
20
)
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
20
)
})
})
})
})
...
@@ -139,9 +146,11 @@ func TestPrometheus(t *testing.T) {
...
@@ -139,9 +146,11 @@ func TestPrometheus(t *testing.T) {
Convey
(
"with 48h time range"
,
func
()
{
Convey
(
"with 48h time range"
,
func
()
{
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
queryContext
.
TimeRange
=
tsdb
.
NewTimeRange
(
"48h"
,
"now"
)
model
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
model
s
,
err
:=
parseQuery
(
dsInfo
,
queryModels
,
queryContext
)
So
(
err
,
ShouldBeNil
)
So
(
err
,
ShouldBeNil
)
model
:=
models
[
0
]
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
2
)
So
(
model
.
Step
,
ShouldEqual
,
time
.
Minute
*
2
)
})
})
})
})
...
...
pkg/tsdb/prometheus/types.go
View file @
e234cf5b
...
@@ -8,4 +8,5 @@ type PrometheusQuery struct {
...
@@ -8,4 +8,5 @@ type PrometheusQuery struct {
LegendFormat
string
LegendFormat
string
Start
time
.
Time
Start
time
.
Time
End
time
.
Time
End
time
.
Time
RefId
string
}
}
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