2018-11-26 17:43:09 +00:00
|
|
|
// Package control provides a query controller.
|
2018-09-11 22:56:51 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/influxdata/flux"
|
|
|
|
"github.com/influxdata/flux/control"
|
2019-03-05 20:54:32 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
2019-01-08 00:37:16 +00:00
|
|
|
platform "github.com/influxdata/influxdb"
|
2019-03-06 00:18:04 +00:00
|
|
|
"github.com/influxdata/influxdb/kit/tracing"
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/query"
|
2018-09-11 22:56:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// orgLabel is the metric label to use in the controller
|
|
|
|
const orgLabel = "org"
|
|
|
|
|
|
|
|
// Controller implements AsyncQueryService by consuming a control.Controller.
|
|
|
|
type Controller struct {
|
|
|
|
c *control.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewController creates a new Controller specific to platform.
|
2019-04-22 19:11:32 +00:00
|
|
|
func New(config control.Config) (*Controller, error) {
|
2018-09-11 22:56:51 +00:00
|
|
|
config.MetricLabelKeys = append(config.MetricLabelKeys, orgLabel)
|
2019-04-22 19:11:32 +00:00
|
|
|
c, err := control.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Controller{c: c}, nil
|
2018-09-11 22:56:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 15:57:32 +00:00
|
|
|
// Query satisfies the AsyncQueryService while ensuring the request is propagated on the context.
|
2018-09-11 22:56:51 +00:00
|
|
|
func (c *Controller) Query(ctx context.Context, req *query.Request) (flux.Query, error) {
|
2019-03-06 00:18:04 +00:00
|
|
|
span, ctx := tracing.StartSpanFromContext(ctx)
|
2019-03-05 00:38:10 +00:00
|
|
|
defer span.Finish()
|
|
|
|
|
2018-09-11 22:56:51 +00:00
|
|
|
// Set the request on the context so platform specific Flux operations can retrieve it later.
|
|
|
|
ctx = query.ContextWithRequest(ctx, req)
|
|
|
|
// Set the org label value for controller metrics
|
|
|
|
ctx = context.WithValue(ctx, orgLabel, req.OrganizationID.String())
|
2018-12-11 15:57:32 +00:00
|
|
|
q, err := c.c.Query(ctx, req.Compiler)
|
|
|
|
if err != nil {
|
|
|
|
// If the controller reports an error, it's usually because of a syntax error
|
|
|
|
// or other problem that the client must fix.
|
|
|
|
return q, &platform.Error{
|
|
|
|
Code: platform.EInvalid,
|
2018-12-13 22:11:17 +00:00
|
|
|
Msg: err.Error(),
|
2018-12-11 15:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return q, nil
|
2018-09-11 22:56:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrometheusCollectors satisifies the prom.PrometheusCollector interface.
|
|
|
|
func (c *Controller) PrometheusCollectors() []prometheus.Collector {
|
|
|
|
return c.c.PrometheusCollectors()
|
|
|
|
}
|
2018-11-26 17:43:09 +00:00
|
|
|
|
|
|
|
// Shutdown shuts down the underlying Controller.
|
|
|
|
func (c *Controller) Shutdown(ctx context.Context) error {
|
|
|
|
return c.c.Shutdown(ctx)
|
|
|
|
}
|