2021-03-02 07:55:16 -06:00
|
|
|
package loki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-05 03:25:31 -05:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
2021-03-02 07:55:16 -06:00
|
|
|
"fmt"
|
2021-05-12 04:20:16 -05:00
|
|
|
"net/http"
|
2021-03-02 07:55:16 -06:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
2021-03-26 10:47:46 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
2021-05-19 16:53:41 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/httpclient"
|
2021-03-02 07:55:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2021-11-01 04:53:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2021-08-05 03:25:31 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
|
2021-12-14 08:22:40 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-03-02 07:55:16 -06:00
|
|
|
"github.com/grafana/loki/pkg/logcli/client"
|
|
|
|
"github.com/grafana/loki/pkg/loghttp"
|
|
|
|
"github.com/grafana/loki/pkg/logproto"
|
2021-08-05 03:25:31 -05:00
|
|
|
|
2021-03-02 07:55:16 -06:00
|
|
|
"github.com/opentracing/opentracing-go"
|
2021-04-15 08:06:26 -05:00
|
|
|
"github.com/prometheus/common/config"
|
2021-03-02 07:55:16 -06:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
)
|
|
|
|
|
2021-12-14 08:22:40 -06:00
|
|
|
const pluginID = "loki"
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
type Service struct {
|
2021-11-19 10:20:32 -06:00
|
|
|
im instancemgmt.InstanceManager
|
|
|
|
plog log.Logger
|
2021-08-25 08:11:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:22:40 -06:00
|
|
|
func ProvideService(cfg *setting.Cfg, httpClientProvider httpclient.Provider, pluginStore plugins.Store) (*Service, error) {
|
2021-08-25 08:11:22 -05:00
|
|
|
im := datasource.NewInstanceManager(newInstanceSettings(httpClientProvider))
|
|
|
|
s := &Service{
|
2021-11-19 10:20:32 -06:00
|
|
|
im: im,
|
|
|
|
plog: log.New("tsdb.loki"),
|
2021-08-25 08:11:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
factory := coreplugin.New(backend.ServeOpts{
|
|
|
|
QueryDataHandler: s,
|
|
|
|
})
|
2021-03-02 07:55:16 -06:00
|
|
|
|
2021-12-14 08:22:40 -06:00
|
|
|
resolver := plugins.CoreDataSourcePathResolver(cfg, pluginID)
|
|
|
|
if err := pluginStore.AddWithFactory(context.Background(), pluginID, factory, resolver); err != nil {
|
2021-08-25 08:11:22 -05:00
|
|
|
s.plog.Error("Failed to register plugin", "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
2021-03-08 00:02:49 -06:00
|
|
|
}
|
2021-03-02 07:55:16 -06:00
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
var (
|
2021-03-02 07:55:16 -06:00
|
|
|
legendFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
|
2021-03-08 00:02:49 -06:00
|
|
|
)
|
2021-03-02 07:55:16 -06:00
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
type datasourceInfo struct {
|
|
|
|
HTTPClient *http.Client
|
|
|
|
URL string
|
|
|
|
TLSClientConfig *tls.Config
|
|
|
|
BasicAuthUser string
|
|
|
|
BasicAuthPassword string
|
|
|
|
TimeInterval string `json:"timeInterval"`
|
|
|
|
}
|
|
|
|
|
2021-12-07 00:57:52 -06:00
|
|
|
type QueryModel struct {
|
2021-08-05 03:25:31 -05:00
|
|
|
Expr string `json:"expr"`
|
|
|
|
LegendFormat string `json:"legendFormat"`
|
|
|
|
Interval string `json:"interval"`
|
|
|
|
IntervalMS int `json:"intervalMS"`
|
2021-08-16 07:02:13 -05:00
|
|
|
Resolution int64 `json:"resolution"`
|
2021-08-05 03:25:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newInstanceSettings(httpClientProvider httpclient.Provider) datasource.InstanceFactoryFunc {
|
|
|
|
return func(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
|
|
|
|
opts, err := settings.HTTPClientOptions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := httpClientProvider.New(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClientConfig, err := httpClientProvider.GetTLSConfig(opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonData := datasourceInfo{}
|
|
|
|
err = json.Unmarshal(settings.JSONData, &jsonData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
model := &datasourceInfo{
|
|
|
|
HTTPClient: client,
|
|
|
|
URL: settings.URL,
|
|
|
|
TLSClientConfig: tlsClientConfig,
|
|
|
|
TimeInterval: jsonData.TimeInterval,
|
|
|
|
BasicAuthUser: settings.BasicAuthUser,
|
|
|
|
BasicAuthPassword: settings.DecryptedSecureJSONData["basicAuthPassword"],
|
|
|
|
}
|
|
|
|
return model, nil
|
2021-04-15 08:06:26 -05:00
|
|
|
}
|
2021-08-05 03:25:31 -05:00
|
|
|
}
|
2021-04-15 08:06:26 -05:00
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
|
|
|
result := backend.NewQueryDataResponse()
|
|
|
|
queryRes := backend.DataResponse{}
|
|
|
|
|
|
|
|
dsInfo, err := s.getDSInfo(req.PluginContext)
|
2021-05-12 04:20:16 -05:00
|
|
|
if err != nil {
|
2021-08-05 03:25:31 -05:00
|
|
|
return result, err
|
2021-05-12 04:20:16 -05:00
|
|
|
}
|
|
|
|
|
2021-03-02 07:55:16 -06:00
|
|
|
client := &client.DefaultClient{
|
2021-08-05 03:25:31 -05:00
|
|
|
Address: dsInfo.URL,
|
2021-03-02 07:55:16 -06:00
|
|
|
Username: dsInfo.BasicAuthUser,
|
2021-08-05 03:25:31 -05:00
|
|
|
Password: dsInfo.BasicAuthPassword,
|
2021-04-15 08:06:26 -05:00
|
|
|
TLSConfig: config.TLSConfig{
|
2021-08-05 03:25:31 -05:00
|
|
|
InsecureSkipVerify: dsInfo.TLSClientConfig.InsecureSkipVerify,
|
2021-04-15 08:06:26 -05:00
|
|
|
},
|
2021-05-12 04:20:16 -05:00
|
|
|
Tripperware: func(t http.RoundTripper) http.RoundTripper {
|
2021-08-05 03:25:31 -05:00
|
|
|
return dsInfo.HTTPClient.Transport
|
2021-05-12 04:20:16 -05:00
|
|
|
},
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
2021-12-09 06:16:52 -06:00
|
|
|
queries, err := parseQuery(req)
|
2021-03-02 07:55:16 -06:00
|
|
|
if err != nil {
|
2021-08-05 03:25:31 -05:00
|
|
|
return result, err
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, query := range queries {
|
2021-08-25 08:11:22 -05:00
|
|
|
s.plog.Debug("Sending query", "start", query.Start, "end", query.End, "step", query.Step, "query", query.Expr)
|
2021-03-02 07:55:16 -06:00
|
|
|
span, _ := opentracing.StartSpanFromContext(ctx, "alerting.loki")
|
|
|
|
span.SetTag("expr", query.Expr)
|
|
|
|
span.SetTag("start_unixnano", query.Start.UnixNano())
|
|
|
|
span.SetTag("stop_unixnano", query.End.UnixNano())
|
|
|
|
defer span.Finish()
|
|
|
|
|
2021-11-24 04:52:33 -06:00
|
|
|
// `limit` only applies to log-producing queries, and we
|
|
|
|
// currently only support metric queries, so this can be set to any value.
|
|
|
|
limit := 1
|
|
|
|
|
|
|
|
// we do not use `interval`, so we set it to zero
|
|
|
|
interval := time.Duration(0)
|
2021-03-02 07:55:16 -06:00
|
|
|
|
|
|
|
value, err := client.QueryRange(query.Expr, limit, query.Start, query.End, logproto.BACKWARD, query.Step, interval, false)
|
|
|
|
if err != nil {
|
2021-08-05 03:25:31 -05:00
|
|
|
return result, err
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
frames, err := parseResponse(value, query)
|
2021-03-02 07:55:16 -06:00
|
|
|
if err != nil {
|
2021-08-05 03:25:31 -05:00
|
|
|
return result, err
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
2021-08-05 03:25:31 -05:00
|
|
|
queryRes.Frames = frames
|
|
|
|
result.Responses[query.RefID] = queryRes
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//If legend (using of name or pattern instead of time series name) is used, use that name/pattern for formatting
|
2021-03-08 00:02:49 -06:00
|
|
|
func formatLegend(metric model.Metric, query *lokiQuery) string {
|
2021-03-02 07:55:16 -06:00
|
|
|
if query.LegendFormat == "" {
|
|
|
|
return metric.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
result := legendFormat.ReplaceAllFunc([]byte(query.LegendFormat), func(in []byte) []byte {
|
|
|
|
labelName := strings.Replace(string(in), "{{", "", 1)
|
|
|
|
labelName = strings.Replace(labelName, "}}", "", 1)
|
|
|
|
labelName = strings.TrimSpace(labelName)
|
|
|
|
if val, exists := metric[model.LabelName(labelName)]; exists {
|
|
|
|
return []byte(val)
|
|
|
|
}
|
|
|
|
return []byte{}
|
|
|
|
})
|
|
|
|
|
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
func parseResponse(value *loghttp.QueryResponse, query *lokiQuery) (data.Frames, error) {
|
2021-03-26 10:47:46 -05:00
|
|
|
frames := data.Frames{}
|
2021-03-02 07:55:16 -06:00
|
|
|
|
|
|
|
//We are currently processing only matrix results (for alerting)
|
2021-03-26 10:47:46 -05:00
|
|
|
matrix, ok := value.Data.Result.(loghttp.Matrix)
|
2021-03-02 07:55:16 -06:00
|
|
|
if !ok {
|
2021-08-05 03:25:31 -05:00
|
|
|
return frames, fmt.Errorf("unsupported result format: %q", value.Data.ResultType)
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
2021-03-26 10:47:46 -05:00
|
|
|
for _, v := range matrix {
|
|
|
|
name := formatLegend(v.Metric, query)
|
|
|
|
tags := make(map[string]string, len(v.Metric))
|
|
|
|
timeVector := make([]time.Time, 0, len(v.Values))
|
|
|
|
values := make([]float64, 0, len(v.Values))
|
2021-03-02 07:55:16 -06:00
|
|
|
|
|
|
|
for k, v := range v.Metric {
|
2021-03-26 10:47:46 -05:00
|
|
|
tags[string(k)] = string(v)
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range v.Values {
|
2021-03-26 10:47:46 -05:00
|
|
|
timeVector = append(timeVector, time.Unix(k.Timestamp.Unix(), 0).UTC())
|
|
|
|
values = append(values, float64(k.Value))
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
2021-03-26 10:47:46 -05:00
|
|
|
frames = append(frames, data.NewFrame(name,
|
|
|
|
data.NewField("time", nil, timeVector),
|
|
|
|
data.NewField("value", tags, values).SetConfig(&data.FieldConfig{DisplayNameFromDS: name})))
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|
|
|
|
|
2021-08-05 03:25:31 -05:00
|
|
|
return frames, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getDSInfo(pluginCtx backend.PluginContext) (*datasourceInfo, error) {
|
|
|
|
i, err := s.im.Get(pluginCtx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
instance, ok := i.(*datasourceInfo)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed to cast datsource info")
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance, nil
|
2021-03-02 07:55:16 -06:00
|
|
|
}
|