adds basic traces using open traces
This commit is contained in:
@@ -170,6 +170,7 @@ func (hs *HttpServer) newMacaron() *macaron.Macaron {
|
||||
m.Use(hs.metricsEndpoint)
|
||||
m.Use(middleware.GetContextHandler())
|
||||
m.Use(middleware.Sessioner(&setting.SessionOptions))
|
||||
m.Use(middleware.RequestTracing())
|
||||
m.Use(middleware.OrgRedirect())
|
||||
|
||||
// needs to be after context handler
|
||||
|
||||
@@ -24,6 +24,10 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/search"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/social"
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
jaeger "github.com/uber/jaeger-client-go"
|
||||
jaegercfg "github.com/uber/jaeger-client-go/config"
|
||||
jaegerlog "github.com/uber/jaeger-client-go/log"
|
||||
)
|
||||
|
||||
func NewGrafanaServer() models.GrafanaServer {
|
||||
@@ -61,6 +65,35 @@ func (g *GrafanaServerImpl) Start() {
|
||||
eventpublisher.Init()
|
||||
plugins.Init()
|
||||
|
||||
//localhost:5775
|
||||
|
||||
cfg := jaegercfg.Configuration{
|
||||
Disabled: false,
|
||||
Sampler: &jaegercfg.SamplerConfig{
|
||||
Type: jaeger.SamplerTypeConst,
|
||||
Param: 1,
|
||||
},
|
||||
Reporter: &jaegercfg.ReporterConfig{
|
||||
LogSpans: false,
|
||||
LocalAgentHostPort: "localhost:5775",
|
||||
},
|
||||
}
|
||||
|
||||
jLogger := jaegerlog.StdLogger
|
||||
|
||||
tracer, closer, err := cfg.New(
|
||||
"grafana",
|
||||
jaegercfg.Logger(jLogger),
|
||||
)
|
||||
if err != nil {
|
||||
g.log.Error("tracing", "error", err)
|
||||
g.Shutdown(1, "Startup failed")
|
||||
return
|
||||
}
|
||||
|
||||
opentracing.InitGlobalTracer(tracer)
|
||||
defer closer.Close()
|
||||
|
||||
// init alerting
|
||||
if setting.AlertingEnabled && setting.ExecuteAlerts {
|
||||
engine := alerting.NewEngine()
|
||||
@@ -72,7 +105,7 @@ func (g *GrafanaServerImpl) Start() {
|
||||
g.childRoutines.Go(func() error { return cleanUpService.Run(g.context) })
|
||||
|
||||
if err := notifications.Init(); err != nil {
|
||||
g.log.Error("Notification service failed to initialize", "erro", err)
|
||||
g.log.Error("Notification service failed to initialize", "error", err)
|
||||
g.Shutdown(1, "Startup failed")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
opentracing "github.com/opentracing/opentracing-go"
|
||||
tlog "github.com/opentracing/opentracing-go/log"
|
||||
|
||||
"gopkg.in/macaron.v1"
|
||||
)
|
||||
|
||||
func RequestTracing() macaron.Handler {
|
||||
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
|
||||
rw := res.(macaron.ResponseWriter)
|
||||
|
||||
var span opentracing.Span
|
||||
opName := req.URL.Path
|
||||
carrier := opentracing.HTTPHeadersCarrier(req.Header)
|
||||
|
||||
wireContext, err := opentracing.GlobalTracer().Extract(
|
||||
opentracing.HTTPHeaders, carrier)
|
||||
if err != nil {
|
||||
span = opentracing.StartSpan(opName)
|
||||
} else {
|
||||
span = opentracing.StartSpan(opName, opentracing.ChildOf(wireContext))
|
||||
}
|
||||
defer span.Finish()
|
||||
|
||||
ctx := opentracing.ContextWithSpan(req.Context(), span)
|
||||
req = req.WithContext(ctx)
|
||||
start := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
status := rw.Status()
|
||||
|
||||
span.LogFields(
|
||||
tlog.Int("http.status_code", status),
|
||||
tlog.Float64("waited.millis", float64(time.Since(start)/time.Millisecond)))
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/opentracing/opentracing-go"
|
||||
tlog "github.com/opentracing/opentracing-go/log"
|
||||
|
||||
"github.com/benbjohnson/clock"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -105,6 +108,10 @@ func (e *Engine) processJob(grafanaCtx context.Context, job *Job) error {
|
||||
|
||||
done := make(chan struct{})
|
||||
|
||||
span := opentracing.StartSpan("alerting")
|
||||
alertCtx = opentracing.ContextWithSpan(alertCtx, span)
|
||||
evalContext.Ctx = alertCtx
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
@@ -115,7 +122,14 @@ func (e *Engine) processJob(grafanaCtx context.Context, job *Job) error {
|
||||
|
||||
e.evalHandler.Eval(evalContext)
|
||||
e.resultHandler.Handle(evalContext)
|
||||
span.LogFields(
|
||||
tlog.Int64("alertId", evalContext.Rule.Id),
|
||||
tlog.Int64("dashboardId", evalContext.Rule.DashboardId),
|
||||
tlog.Bool("firing", evalContext.Firing),
|
||||
)
|
||||
|
||||
close(done)
|
||||
span.Finish()
|
||||
}()
|
||||
|
||||
var err error = nil
|
||||
|
||||
Reference in New Issue
Block a user