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
6d6d6d90
Commit
6d6d6d90
authored
Oct 14, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'utkarshcmu-alerting_opentsdb' into alerting_opentsdb
parents
455df7f0
f44e360b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
142 additions
and
3 deletions
+142
-3
pkg/tsdb/opentsdb/opentsdb.go
+124
-3
pkg/tsdb/opentsdb/types.go
+18
-0
No files found.
pkg/tsdb/opentsdb/opentsdb.go
View file @
6d6d6d90
...
...
@@ -2,9 +2,21 @@ package opentsdb
import
(
"context"
"fmt"
"path"
"strconv"
"strings"
"io/ioutil"
"net/http"
"net/url"
//"net/http/httputil"
"encoding/json"
"gopkg.in/guregu/null.v3"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tsdb"
)
...
...
@@ -12,7 +24,7 @@ type OpenTsdbExecutor struct {
*
tsdb
.
DataSourceInfo
}
func
NewOpenTsdbExecutor
Executor
(
dsInfo
*
tsdb
.
DataSourceInfo
)
tsdb
.
Executor
{
func
NewOpenTsdbExecutor
(
dsInfo
*
tsdb
.
DataSourceInfo
)
tsdb
.
Executor
{
return
&
OpenTsdbExecutor
{
dsInfo
}
}
...
...
@@ -23,9 +35,118 @@ var (
func
init
()
{
plog
=
log
.
New
(
"tsdb.opentsdb"
)
tsdb
.
RegisterExecutor
(
"opentsdb"
,
NewOpenTsdbExecutor
Executor
)
tsdb
.
RegisterExecutor
(
"opentsdb"
,
NewOpenTsdbExecutor
)
}
func
(
e
*
OpenTsdbExecutor
)
Execute
(
ctx
context
.
Context
,
queries
tsdb
.
QuerySlice
,
queryContext
*
tsdb
.
QueryContext
)
*
tsdb
.
BatchResult
{
panic
(
"Missing implementation"
)
result
:=
&
tsdb
.
BatchResult
{}
var
tsdbQuery
OpenTsdbQuery
tsdbQuery
.
Start
=
queryContext
.
TimeRange
.
GetFromAsMsEpoch
()
tsdbQuery
.
End
=
queryContext
.
TimeRange
.
GetToAsMsEpoch
()
for
_
,
query
:=
range
queries
{
tsdbQuery
.
Queries
=
[]
OpenTsdbMetric
{
OpenTsdbMetric
{
Metric
:
query
.
Model
.
Get
(
"metric"
)
.
MustString
(),
Aggregator
:
query
.
Model
.
Get
(
"aggregator"
)
.
MustString
(),
},
}
}
if
setting
.
Env
==
setting
.
DEV
{
plog
.
Debug
(
"OpenTsdb request"
,
"params"
,
tsdbQuery
)
}
req
,
err
:=
e
.
createRequest
(
tsdbQuery
)
if
err
!=
nil
{
result
.
Error
=
err
return
result
}
res
,
err
:=
HttpClient
.
Do
(
req
)
if
err
!=
nil
{
result
.
Error
=
err
return
result
}
queryResult
,
err
:=
e
.
parseResponse
(
tsdbQuery
,
res
)
if
err
!=
nil
{
return
result
.
WithError
(
err
)
}
result
.
QueryResults
=
queryResult
return
result
}
func
(
e
*
OpenTsdbExecutor
)
createRequest
(
data
OpenTsdbQuery
)
(
*
http
.
Request
,
error
)
{
u
,
_
:=
url
.
Parse
(
e
.
Url
)
u
.
Path
=
path
.
Join
(
u
.
Path
,
"api/query"
)
postData
,
err
:=
json
.
Marshal
(
data
)
req
,
err
:=
http
.
NewRequest
(
http
.
MethodPost
,
u
.
String
(),
strings
.
NewReader
(
string
(
postData
)))
if
err
!=
nil
{
plog
.
Info
(
"Failed to create request"
,
"error"
,
err
)
return
nil
,
fmt
.
Errorf
(
"Failed to create request. error: %v"
,
err
)
}
req
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
if
e
.
BasicAuth
{
req
.
SetBasicAuth
(
e
.
BasicAuthUser
,
e
.
BasicAuthPassword
)
}
/*
requestDump, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(requestDump))
*/
return
req
,
err
}
func
(
e
*
OpenTsdbExecutor
)
parseResponse
(
query
OpenTsdbQuery
,
res
*
http
.
Response
)
(
map
[
string
]
*
tsdb
.
QueryResult
,
error
)
{
queryResults
:=
make
(
map
[
string
]
*
tsdb
.
QueryResult
)
queryRes
:=
tsdb
.
NewQueryResult
()
body
,
err
:=
ioutil
.
ReadAll
(
res
.
Body
)
defer
res
.
Body
.
Close
()
if
err
!=
nil
{
return
nil
,
err
}
if
res
.
StatusCode
/
100
!=
2
{
plog
.
Info
(
"Request failed"
,
"status"
,
res
.
Status
,
"body"
,
string
(
body
))
return
nil
,
fmt
.
Errorf
(
"Request failed status: %v"
,
res
.
Status
)
}
var
data
[]
OpenTsdbResponse
err
=
json
.
Unmarshal
(
body
,
&
data
)
if
err
!=
nil
{
plog
.
Info
(
"Failed to unmarshal opentsdb response"
,
"error"
,
err
,
"status"
,
res
.
Status
,
"body"
,
string
(
body
))
return
nil
,
err
}
for
_
,
val
:=
range
data
{
series
:=
tsdb
.
TimeSeries
{
Name
:
val
.
Metric
,
}
for
timeString
,
value
:=
range
val
.
DataPoints
{
timestamp
,
err
:=
strconv
.
ParseFloat
(
timeString
,
64
)
if
err
!=
nil
{
plog
.
Info
(
"Failed to unmarshal opentsdb timestamp"
,
"timestamp"
,
timeString
)
return
nil
,
err
}
series
.
Points
=
append
(
series
.
Points
,
tsdb
.
NewTimePoint
(
null
.
FloatFrom
(
value
),
timestamp
))
}
queryRes
.
Series
=
append
(
queryRes
.
Series
,
&
series
)
}
queryResults
[
"A"
]
=
queryRes
return
queryResults
,
nil
}
pkg/tsdb/opentsdb/types.go
0 → 100644
View file @
6d6d6d90
package
opentsdb
type
OpenTsdbQuery
struct
{
Start
int64
`json:"start"`
End
int64
`json:"end"`
Queries
[]
OpenTsdbMetric
`json:"queries"`
}
type
OpenTsdbMetric
struct
{
Metric
string
`json:"metric"`
Aggregator
string
`json:"aggregator"`
}
type
OpenTsdbResponse
struct
{
Metric
string
`json:"metric"`
DataPoints
map
[
string
]
float64
`json:"dps"`
}
\ No newline at end of file
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