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
6358d3f3
Commit
6358d3f3
authored
Sep 27, 2018
by
Daniel Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
stackdriver: set target to be raw query
parent
d405d8f2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
42 deletions
+21
-42
pkg/tsdb/stackdriver/stackdriver.go
+18
-33
pkg/tsdb/stackdriver/stackdriver_test.go
+3
-9
No files found.
pkg/tsdb/stackdriver/stackdriver.go
View file @
6358d3f3
...
...
@@ -121,34 +121,18 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
for
_
,
query
:=
range
tsdbQuery
.
Queries
{
var
target
string
if
fullTarget
,
err
:=
query
.
Model
.
Get
(
"targetFull"
)
.
String
();
err
==
nil
{
target
=
fixIntervalFormat
(
fullTarget
)
}
else
{
target
=
fixIntervalFormat
(
query
.
Model
.
Get
(
"target"
)
.
MustString
())
}
metricType
:=
query
.
Model
.
Get
(
"metricType"
)
.
MustString
()
filterParts
:=
query
.
Model
.
Get
(
"filters"
)
.
MustArray
()
filterString
:=
""
for
i
,
part
:=
range
filterParts
{
mod
:=
i
%
4
if
part
==
"AND"
{
filterString
+=
" "
}
else
if
mod
==
2
{
filterString
+=
fmt
.
Sprintf
(
`"%s"`
,
part
)
}
else
{
filterString
+=
part
.
(
string
)
}
}
params
:=
url
.
Values
{}
params
.
Add
(
"interval.startTime"
,
startTime
.
UTC
()
.
Format
(
time
.
RFC3339
))
params
.
Add
(
"interval.endTime"
,
endTime
.
UTC
()
.
Format
(
time
.
RFC3339
))
params
.
Add
(
"filter"
,
strings
.
Trim
(
fmt
.
Sprintf
(
`metric.type="%s" %s`
,
metricType
,
filterString
),
" "
))
params
.
Add
(
"filter"
,
buildFilterString
(
metricType
,
filterParts
))
params
.
Add
(
"view"
,
query
.
Model
.
Get
(
"view"
)
.
MustString
())
setAggParams
(
&
params
,
query
,
durationSeconds
)
target
=
params
.
Encode
()
if
setting
.
Env
==
setting
.
DEV
{
slog
.
Debug
(
"Stackdriver request"
,
"params"
,
params
)
}
...
...
@@ -173,6 +157,21 @@ func (e *StackdriverExecutor) buildQueries(tsdbQuery *tsdb.TsdbQuery) ([]*Stackd
return
stackdriverQueries
,
nil
}
func
buildFilterString
(
metricType
string
,
filterParts
[]
interface
{})
string
{
filterString
:=
""
for
i
,
part
:=
range
filterParts
{
mod
:=
i
%
4
if
part
==
"AND"
{
filterString
+=
" "
}
else
if
mod
==
2
{
filterString
+=
fmt
.
Sprintf
(
`"%s"`
,
part
)
}
else
{
filterString
+=
part
.
(
string
)
}
}
return
strings
.
Trim
(
fmt
.
Sprintf
(
`metric.type="%s" %s`
,
metricType
,
filterString
),
" "
)
}
func
setAggParams
(
params
*
url
.
Values
,
query
*
tsdb
.
Query
,
durationSeconds
int
)
{
primaryAggregation
:=
query
.
Model
.
Get
(
"primaryAggregation"
)
.
MustString
()
perSeriesAligner
:=
query
.
Model
.
Get
(
"perSeriesAligner"
)
.
MustString
()
...
...
@@ -457,17 +456,3 @@ func (e *StackdriverExecutor) createRequest(ctx context.Context, dsInfo *models.
return
req
,
nil
}
func
fixIntervalFormat
(
target
string
)
string
{
rMinute
:=
regexp
.
MustCompile
(
`'(\d+)m'`
)
rMin
:=
regexp
.
MustCompile
(
"m"
)
target
=
rMinute
.
ReplaceAllStringFunc
(
target
,
func
(
m
string
)
string
{
return
rMin
.
ReplaceAllString
(
m
,
"min"
)
})
rMonth
:=
regexp
.
MustCompile
(
`'(\d+)M'`
)
rMon
:=
regexp
.
MustCompile
(
"M"
)
target
=
rMonth
.
ReplaceAllStringFunc
(
target
,
func
(
M
string
)
string
{
return
rMon
.
ReplaceAllString
(
M
,
"mon"
)
})
return
target
}
pkg/tsdb/stackdriver/stackdriver_test.go
View file @
6358d3f3
...
...
@@ -27,7 +27,6 @@ func TestStackdriver(t *testing.T) {
Queries
:
[]
*
tsdb
.
Query
{
{
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"target"
:
"target"
,
"metricType"
:
"a/metric/type"
,
"view"
:
"FULL"
,
"aliasBy"
:
"testalias"
,
...
...
@@ -44,7 +43,7 @@ func TestStackdriver(t *testing.T) {
So
(
len
(
queries
),
ShouldEqual
,
1
)
So
(
queries
[
0
]
.
RefID
,
ShouldEqual
,
"A"
)
So
(
queries
[
0
]
.
Target
,
ShouldEqual
,
"
target
"
)
So
(
queries
[
0
]
.
Target
,
ShouldEqual
,
"
aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL
"
)
So
(
len
(
queries
[
0
]
.
Params
),
ShouldEqual
,
7
)
So
(
queries
[
0
]
.
Params
[
"interval.startTime"
][
0
],
ShouldEqual
,
"2018-03-15T13:00:00Z"
)
So
(
queries
[
0
]
.
Params
[
"interval.endTime"
][
0
],
ShouldEqual
,
"2018-03-15T13:34:00Z"
)
...
...
@@ -56,7 +55,6 @@ func TestStackdriver(t *testing.T) {
Convey
(
"and query has filters"
,
func
()
{
tsdbQuery
.
Queries
[
0
]
.
Model
=
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"target"
:
"target"
,
"metricType"
:
"a/metric/type"
,
"filters"
:
[]
interface
{}{
"key"
,
"="
,
"value"
,
"AND"
,
"key2"
,
"="
,
"value2"
},
})
...
...
@@ -71,7 +69,6 @@ func TestStackdriver(t *testing.T) {
Convey
(
"and IntervalMs is larger than 60"
,
func
()
{
tsdbQuery
.
Queries
[
0
]
.
IntervalMs
=
1000000
tsdbQuery
.
Queries
[
0
]
.
Model
=
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"target"
:
"target"
,
"alignmentPeriod"
:
"grafana-auto"
,
"filters"
:
[]
interface
{}{
"key"
,
"="
,
"value"
,
"AND"
,
"key2"
,
"="
,
"value2"
},
})
...
...
@@ -83,7 +80,6 @@ func TestStackdriver(t *testing.T) {
Convey
(
"and IntervalMs is less than 60"
,
func
()
{
tsdbQuery
.
Queries
[
0
]
.
IntervalMs
=
30
tsdbQuery
.
Queries
[
0
]
.
Model
=
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"target"
:
"target"
,
"alignmentPeriod"
:
"grafana-auto"
,
"filters"
:
[]
interface
{}{
"key"
,
"="
,
"value"
,
"AND"
,
"key2"
,
"="
,
"value2"
},
})
...
...
@@ -120,7 +116,6 @@ func TestStackdriver(t *testing.T) {
Convey
(
"and query has aggregation mean set"
,
func
()
{
tsdbQuery
.
Queries
[
0
]
.
Model
=
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"target"
:
"target"
,
"metricType"
:
"a/metric/type"
,
"primaryAggregation"
:
"REDUCE_MEAN"
,
"view"
:
"FULL"
,
...
...
@@ -131,7 +126,7 @@ func TestStackdriver(t *testing.T) {
So
(
len
(
queries
),
ShouldEqual
,
1
)
So
(
queries
[
0
]
.
RefID
,
ShouldEqual
,
"A"
)
So
(
queries
[
0
]
.
Target
,
ShouldEqual
,
"
target
"
)
So
(
queries
[
0
]
.
Target
,
ShouldEqual
,
"
aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_MEAN&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL
"
)
So
(
len
(
queries
[
0
]
.
Params
),
ShouldEqual
,
7
)
So
(
queries
[
0
]
.
Params
[
"interval.startTime"
][
0
],
ShouldEqual
,
"2018-03-15T13:00:00Z"
)
So
(
queries
[
0
]
.
Params
[
"interval.endTime"
][
0
],
ShouldEqual
,
"2018-03-15T13:34:00Z"
)
...
...
@@ -144,7 +139,6 @@ func TestStackdriver(t *testing.T) {
Convey
(
"and query has group bys"
,
func
()
{
tsdbQuery
.
Queries
[
0
]
.
Model
=
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"target"
:
"target"
,
"metricType"
:
"a/metric/type"
,
"primaryAggregation"
:
"REDUCE_NONE"
,
"groupBys"
:
[]
interface
{}{
"metric.label.group1"
,
"metric.label.group2"
},
...
...
@@ -156,7 +150,7 @@ func TestStackdriver(t *testing.T) {
So
(
len
(
queries
),
ShouldEqual
,
1
)
So
(
queries
[
0
]
.
RefID
,
ShouldEqual
,
"A"
)
So
(
queries
[
0
]
.
Target
,
ShouldEqual
,
"
target
"
)
So
(
queries
[
0
]
.
Target
,
ShouldEqual
,
"
aggregation.alignmentPeriod=%2B60s&aggregation.crossSeriesReducer=REDUCE_NONE&aggregation.groupByFields=metric.label.group1&aggregation.groupByFields=metric.label.group2&aggregation.perSeriesAligner=ALIGN_MEAN&filter=metric.type%3D%22a%2Fmetric%2Ftype%22&interval.endTime=2018-03-15T13%3A34%3A00Z&interval.startTime=2018-03-15T13%3A00%3A00Z&view=FULL
"
)
So
(
len
(
queries
[
0
]
.
Params
),
ShouldEqual
,
8
)
So
(
queries
[
0
]
.
Params
[
"interval.startTime"
][
0
],
ShouldEqual
,
"2018-03-15T13:00:00Z"
)
So
(
queries
[
0
]
.
Params
[
"interval.endTime"
][
0
],
ShouldEqual
,
"2018-03-15T13:34:00Z"
)
...
...
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