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
4c88db3e
Commit
4c88db3e
authored
Sep 21, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(prometheus): add support for legend formatting
parent
43d8bd5a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
74 additions
and
9 deletions
+74
-9
pkg/services/alerting/conditions/query.go
+1
-1
pkg/tsdb/graphite/graphite.go
+1
-1
pkg/tsdb/models.go
+3
-0
pkg/tsdb/prometheus/prometheus.go
+35
-6
pkg/tsdb/prometheus/prometheus_test.go
+26
-0
pkg/tsdb/prometheus/types.go
+8
-0
public/app/plugins/datasource/prometheus/datasource.ts
+0
-1
No files found.
pkg/services/alerting/conditions/query.go
View file @
4c88db3e
...
...
@@ -111,7 +111,7 @@ func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource, timera
Queries
:
[]
*
tsdb
.
Query
{
{
RefId
:
"A"
,
Query
:
c
.
Query
.
Model
.
Get
(
"target"
)
.
MustString
()
,
Model
:
c
.
Query
.
Model
,
DataSource
:
&
tsdb
.
DataSourceInfo
{
Id
:
datasource
.
Id
,
Name
:
datasource
.
Name
,
...
...
pkg/tsdb/graphite/graphite.go
View file @
4c88db3e
...
...
@@ -54,7 +54,7 @@ func (e *GraphiteExecutor) Execute(queries tsdb.QuerySlice, context *tsdb.QueryC
}
for
_
,
query
:=
range
queries
{
formData
[
"target"
]
=
[]
string
{
query
.
Query
}
formData
[
"target"
]
=
[]
string
{
query
.
Model
.
Get
(
"target"
)
.
MustString
()
}
}
if
setting
.
Env
==
setting
.
DEV
{
...
...
pkg/tsdb/models.go
View file @
4c88db3e
package
tsdb
import
"github.com/grafana/grafana/pkg/components/simplejson"
type
Query
struct
{
RefId
string
Query
string
Model
*
simplejson
.
Json
Depends
[]
string
DataSource
*
DataSourceInfo
Results
[]
*
TimeSeries
...
...
pkg/tsdb/prometheus/prometheus.go
View file @
4c88db3e
...
...
@@ -3,6 +3,8 @@ package prometheus
import
(
"context"
"net/http"
"regexp"
"strings"
"time"
"github.com/grafana/grafana/pkg/log"
...
...
@@ -53,25 +55,52 @@ func (e *PrometheusExecutor) Execute(queries tsdb.QuerySlice, queryContext *tsdb
from
,
_
:=
queryContext
.
TimeRange
.
FromTime
()
to
,
_
:=
queryContext
.
TimeRange
.
ToTime
()
query
:=
parseQuery
(
queries
)
timeRange
:=
prometheus
.
Range
{
Start
:
from
,
End
:
to
,
Step
:
time
.
Second
,
Step
:
query
.
Step
,
}
ctx
:=
context
.
Background
()
value
,
err
:=
client
.
QueryRange
(
ctx
,
"counters_logins"
,
timeRange
)
value
,
err
:=
client
.
QueryRange
(
context
.
Background
(),
query
.
Expr
,
timeRange
)
if
err
!=
nil
{
result
.
Error
=
err
return
result
}
result
.
QueryResults
=
parseResponse
(
value
)
result
.
QueryResults
=
parseResponse
(
value
,
query
)
return
result
}
func
parseResponse
(
value
pmodel
.
Value
)
map
[
string
]
*
tsdb
.
QueryResult
{
func
formatLegend
(
metric
pmodel
.
Metric
,
query
PrometheusQuery
)
string
{
r
,
_
:=
regexp
.
Compile
(
`\{\{\s*(.+?)\s*\}\}`
)
result
:=
r
.
ReplaceAllFunc
([]
byte
(
query
.
LegendFormat
),
func
(
in
[]
byte
)
[]
byte
{
ind
:=
strings
.
Replace
(
strings
.
Replace
(
string
(
in
),
"{{"
,
""
,
1
),
"}}"
,
""
,
1
)
if
val
,
exists
:=
metric
[
pmodel
.
LabelName
(
ind
)];
exists
{
return
[]
byte
(
val
)
}
return
in
})
return
string
(
result
)
}
func
parseQuery
(
queries
tsdb
.
QuerySlice
)
PrometheusQuery
{
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
(),
}
}
func
parseResponse
(
value
pmodel
.
Value
,
query
PrometheusQuery
)
map
[
string
]
*
tsdb
.
QueryResult
{
queryResults
:=
make
(
map
[
string
]
*
tsdb
.
QueryResult
)
queryRes
:=
&
tsdb
.
QueryResult
{}
...
...
@@ -86,7 +115,7 @@ func parseResponse(value pmodel.Value) map[string]*tsdb.QueryResult {
}
queryRes
.
Series
=
append
(
queryRes
.
Series
,
&
tsdb
.
TimeSeries
{
Name
:
v
.
Metric
.
String
(
),
Name
:
formatLegend
(
v
.
Metric
,
query
),
Points
:
points
,
})
}
...
...
pkg/tsdb/prometheus/prometheus_test.go
0 → 100644
View file @
4c88db3e
package
prometheus
import
(
"testing"
p
"github.com/prometheus/common/model"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestPrometheus
(
t
*
testing
.
T
)
{
Convey
(
"Prometheus"
,
t
,
func
()
{
Convey
(
"converting metric name"
,
func
()
{
metric
:=
map
[
p
.
LabelName
]
p
.
LabelValue
{
p
.
LabelName
(
"app"
)
:
p
.
LabelValue
(
"backend"
),
p
.
LabelName
(
"device"
)
:
p
.
LabelValue
(
"mobile"
),
}
query
:=
PrometheusQuery
{
LegendFormat
:
"legend {{app}} {{device}} {{broken}}"
,
}
So
(
formatLegend
(
metric
,
query
),
ShouldEqual
,
"legend backend mobile {{broken}}"
)
})
})
}
pkg/tsdb/prometheus/types.go
View file @
4c88db3e
package
prometheus
import
"time"
type
PrometheusQuery
struct
{
Expr
string
Step
time
.
Duration
LegendFormat
string
}
public/app/plugins/datasource/prometheus/datasource.ts
View file @
4c88db3e
...
...
@@ -113,7 +113,6 @@ export function PrometheusDatasource(instanceSettings, $q, backendSrv, templateS
throw
response
.
error
;
}
delete
self
.
lastErrors
.
query
;
_
.
each
(
response
.
data
.
data
.
result
,
function
(
metricData
)
{
result
.
push
(
self
.
transformMetricData
(
metricData
,
activeTargets
[
index
],
start
,
end
));
});
...
...
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