The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/pkg/aggregator/apiserver/dataplaneservice_handler.go

68 lines
1.8 KiB

package apiserver
import (
"net/http"
"sync/atomic"
"time"
grafanasemconv "github.com/grafana/grafana/pkg/semconv"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/component-base/tracing"
aggregationv0alpha1 "github.com/grafana/grafana/pkg/aggregator/apis/aggregation/v0alpha1"
"github.com/grafana/grafana/pkg/aggregator/apiserver/plugin"
)
// dataPlaneServiceHandler provides a http.Handler which will proxy traffic to a plugin client.
type dataPlaneServiceHandler struct {
localDelegate http.Handler
client plugin.PluginClient
pluginContextProvider plugin.PluginContextProvider
handlingInfo atomic.Value
}
type handlingInfo struct {
name string
handler http.Handler
}
func (r *dataPlaneServiceHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
value := r.handlingInfo.Load()
if value == nil {
r.localDelegate.ServeHTTP(w, req)
return
}
handlingInfo := value.(handlingInfo)
namespace, _ := request.NamespaceFrom(req.Context())
ctx, span := tracing.Start(
req.Context(),
"grafana-aggregator",
grafanasemconv.K8sDataplaneserviceName(handlingInfo.name),
semconv.K8SNamespaceName(namespace),
semconv.HTTPMethod(req.Method),
semconv.HTTPURL(req.URL.String()),
)
// log if the span has not ended after a minute
defer span.End(time.Minute)
handlingInfo.handler.ServeHTTP(w, req.WithContext(ctx))
}
func (r *dataPlaneServiceHandler) updateDataPlaneService(dataplaneService *aggregationv0alpha1.DataPlaneService) {
newInfo := handlingInfo{
name: dataplaneService.Name,
}
// currently only plugin handlers are supported
newInfo.handler = plugin.NewPluginHandler(
r.client,
*dataplaneService,
r.pluginContextProvider,
r.localDelegate,
)
r.handlingInfo.Store(newInfo)
}