2014-12-29 20:36:08 +08:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2017-08-23 19:31:26 +08:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-08-22 23:14:15 +08:00
|
|
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
2015-02-05 17:37:13 +08:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-06-03 15:17:36 +08:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-05 17:37:13 +08:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2017-08-23 16:52:31 +08:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2017-01-16 19:16:41 +08:00
|
|
|
)
|
|
|
|
|
2017-08-23 19:31:26 +08:00
|
|
|
const HeaderNameNoBackendCache = "X-Grafana-NoCache"
|
|
|
|
|
2018-09-14 15:30:32 +08:00
|
|
|
func (hs *HTTPServer) getDatasourceFromCache(id int64, c *m.ReqContext) (*m.DataSource, error) {
|
|
|
|
nocache := c.Req.Header.Get(HeaderNameNoBackendCache) == "true"
|
2017-08-23 19:31:26 +08:00
|
|
|
cacheKey := fmt.Sprintf("ds-%d", id)
|
|
|
|
|
|
|
|
if !nocache {
|
|
|
|
if cached, found := hs.cache.Get(cacheKey); found {
|
|
|
|
ds := cached.(*m.DataSource)
|
2018-09-14 15:30:32 +08:00
|
|
|
if ds.OrgId == c.OrgId {
|
2017-08-23 19:31:26 +08:00
|
|
|
return ds, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-14 15:30:32 +08:00
|
|
|
query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId}
|
2015-03-12 00:34:11 +08:00
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2015-10-08 23:30:13 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:31:26 +08:00
|
|
|
hs.cache.Set(cacheKey, query.Result, time.Second*5)
|
2016-06-06 23:11:46 +08:00
|
|
|
return query.Result, nil
|
2015-10-08 23:30:13 +08:00
|
|
|
}
|
|
|
|
|
2018-03-23 05:13:46 +08:00
|
|
|
func (hs *HTTPServer) ProxyDataSourceRequest(c *m.ReqContext) {
|
2016-06-03 15:17:36 +08:00
|
|
|
c.TimeRequest(metrics.M_DataSource_ProxyReq_Timer)
|
|
|
|
|
2018-09-14 15:30:32 +08:00
|
|
|
ds, err := hs.getDatasourceFromCache(c.ParamsInt64(":id"), c)
|
2015-10-08 23:30:13 +08:00
|
|
|
if err != nil {
|
2014-12-29 20:36:08 +08:00
|
|
|
c.JsonApiErr(500, "Unable to load datasource meta data", err)
|
2015-03-12 00:34:11 +08:00
|
|
|
return
|
2014-12-29 20:36:08 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 16:52:31 +08:00
|
|
|
// find plugin
|
|
|
|
plugin, ok := plugins.DataSources[ds.Type]
|
|
|
|
if !ok {
|
|
|
|
c.JsonApiErr(500, "Unable to find datasource plugin", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-22 19:15:18 +08:00
|
|
|
proxyPath := c.Params("*")
|
2018-09-19 02:16:09 +08:00
|
|
|
|
2018-09-20 01:02:04 +08:00
|
|
|
// Check for a trailing slash, and pass it to the proxy
|
|
|
|
// macaron does not include trailing slashes when resolving a wildcard path
|
2018-09-19 02:16:09 +08:00
|
|
|
if len(proxyPath) > 1 {
|
|
|
|
path := c.Req.URL.Path
|
2018-09-20 01:02:04 +08:00
|
|
|
if path[len(path)-1] == '/' && proxyPath[len(proxyPath)-1] != '/' {
|
2018-09-19 02:16:09 +08:00
|
|
|
proxyPath += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:52:31 +08:00
|
|
|
proxy := pluginproxy.NewDataSourceProxy(ds, plugin, c, proxyPath)
|
2017-08-22 23:14:15 +08:00
|
|
|
proxy.HandleRequest()
|
2016-08-17 13:33:59 +08:00
|
|
|
}
|