Sentry Tracing Identifier

This commit is contained in:
Maximilian Paß
2023-02-03 00:36:13 +00:00
parent a9581ac1d9
commit 2650efbb38
6 changed files with 55 additions and 41 deletions

View File

@ -1,14 +1,15 @@
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/gorilla/mux"
"github.com/openHPI/poseidon/internal/environment"
"github.com/openHPI/poseidon/internal/runner"
"github.com/openHPI/poseidon/pkg/dto"
"github.com/openHPI/poseidon/pkg/logging"
"net/http"
"strconv"
)
@ -119,9 +120,10 @@ func (e *EnvironmentController) createOrUpdate(writer http.ResponseWriter, reque
return
}
span := sentry.StartSpan(request.Context(), "Create Environment")
created, err := e.manager.CreateOrUpdate(environmentID, *req, request.Context())
span.Finish()
var created bool
logging.StartSpan("api.env.update", "Create Environment", request.Context(), func(ctx context.Context) {
created, err = e.manager.CreateOrUpdate(environmentID, *req, ctx)
})
if err != nil {
writeInternalServerError(writer, err, dto.ErrorUnknown)
}

View File

@ -1,9 +1,9 @@
package api
import (
"context"
"errors"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/openHPI/poseidon/internal/config"
@ -66,9 +66,11 @@ func (r *RunnerController) provide(writer http.ResponseWriter, request *http.Req
}
environmentID := dto.EnvironmentID(runnerRequest.ExecutionEnvironmentID)
span := sentry.StartSpan(request.Context(), "Claim Runner")
nextRunner, err := r.manager.Claim(environmentID, runnerRequest.InactivityTimeout)
span.Finish()
var nextRunner runner.Runner
var err error
logging.StartSpan("api.runner.claim", "Claim Runner", request.Context(), func(_ context.Context) {
nextRunner, err = r.manager.Claim(environmentID, runnerRequest.InactivityTimeout)
})
if err != nil {
switch {
case errors.Is(err, runner.ErrUnknownExecutionEnvironment):
@ -106,9 +108,9 @@ func (r *RunnerController) listFileSystem(writer http.ResponseWriter, request *h
}
writer.Header().Set("Content-Type", "application/json")
span := sentry.StartSpan(request.Context(), "List File System")
err = targetRunner.ListFileSystem(path, recursive, writer, privilegedExecution, request.Context())
span.Finish()
logging.StartSpan("api.fs.list", "List File System", request.Context(), func(ctx context.Context) {
err = targetRunner.ListFileSystem(path, recursive, writer, privilegedExecution, ctx)
})
if errors.Is(err, runner.ErrFileNotFound) {
writeClientError(writer, err, http.StatusFailedDependency)
return
@ -131,9 +133,10 @@ func (r *RunnerController) updateFileSystem(writer http.ResponseWriter, request
targetRunner, _ := runner.FromContext(request.Context())
monitoring.AddRunnerMonitoringData(request, targetRunner.ID(), targetRunner.Environment())
span := sentry.StartSpan(request.Context(), "Update File System")
err := targetRunner.UpdateFileSystem(fileCopyRequest, request.Context())
span.Finish()
var err error
logging.StartSpan("api.fs.update", "Update File System", request.Context(), func(ctx context.Context) {
err = targetRunner.UpdateFileSystem(fileCopyRequest, ctx)
})
if err != nil {
log.WithError(err).Error("Could not perform the requested updateFileSystem.")
writeInternalServerError(writer, err, dto.ErrorUnknown)
@ -153,9 +156,9 @@ func (r *RunnerController) fileContent(writer http.ResponseWriter, request *http
}
writer.Header().Set("Content-Disposition", "attachment; filename=\""+path+"\"")
span := sentry.StartSpan(request.Context(), "File Content")
err = targetRunner.GetFileContent(path, writer, privilegedExecution, request.Context())
span.Finish()
logging.StartSpan("api.fs.read", "File Content", request.Context(), func(ctx context.Context) {
err = targetRunner.GetFileContent(path, writer, privilegedExecution, ctx)
})
if errors.Is(err, runner.ErrFileNotFound) {
writeClientError(writer, err, http.StatusFailedDependency)
return
@ -202,9 +205,10 @@ func (r *RunnerController) execute(writer http.ResponseWriter, request *http.Req
return
}
id := newUUID.String()
span := sentry.StartSpan(request.Context(), "Store Execution")
logging.StartSpan("api.runner.exec", "Store Execution", request.Context(), func(ctx context.Context) {
targetRunner.StoreExecution(id, executionRequest)
span.Finish()
})
webSocketURL := url.URL{
Scheme: scheme,
Host: request.Host,
@ -243,9 +247,10 @@ func (r *RunnerController) delete(writer http.ResponseWriter, request *http.Requ
targetRunner, _ := runner.FromContext(request.Context())
monitoring.AddRunnerMonitoringData(request, targetRunner.ID(), targetRunner.Environment())
span := sentry.StartSpan(request.Context(), "Return Runner")
err := r.manager.Return(targetRunner)
span.Finish()
var err error
logging.StartSpan("api.runner.delete", "Return Runner", request.Context(), func(ctx context.Context) {
err = r.manager.Return(targetRunner)
})
if err != nil {
writeInternalServerError(writer, err, dto.ErrorNomadInternalServerError)
return

View File

@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"github.com/getsentry/sentry-go"
"github.com/gorilla/websocket"
"github.com/openHPI/poseidon/internal/api/ws"
"github.com/openHPI/poseidon/internal/runner"
@ -97,14 +96,14 @@ func (r *RunnerController) connectToRunner(writer http.ResponseWriter, request *
log.WithField("runnerId", targetRunner.ID()).
WithField("executionID", logging.RemoveNewlineSymbol(executionID)).
Info("Running execution")
span := sentry.StartSpan(request.Context(), "Execute Interactively")
defer span.Finish()
logging.StartSpan("api.runner.connect", "Execute Interactively", request.Context(), func(_ context.Context) {
exit, cancel, err := targetRunner.ExecuteInteractively(executionID,
proxy.Input, proxy.Output.StdOut(), proxy.Output.StdErr())
if err != nil {
log.WithError(err).Warn("Cannot execute request.")
return // The proxy is stopped by the defered cancel.
return // The proxy is stopped by the deferred cancel.
}
proxy.waitForExit(exit, cancel)
})
}

View File

@ -4,7 +4,6 @@ import (
"context"
_ "embed"
"fmt"
"github.com/getsentry/sentry-go"
nomadApi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/openHPI/poseidon/internal/nomad"
@ -113,17 +112,17 @@ func (m *NomadEnvironmentManager) CreateOrUpdate(
m.runnerManager.StoreEnvironment(environment)
// Register template Job with Nomad.
span := sentry.StartSpan(ctx, "Register Environment")
logging.StartSpan("env.update.register", "Register Environment", ctx, func(_ context.Context) {
err = environment.Register()
span.Finish()
})
if err != nil {
return false, fmt.Errorf("error registering template job in API: %w", err)
}
// Launch idle runners based on the template job.
span = sentry.StartSpan(ctx, "Apply Prewarming Pool Size")
logging.StartSpan("env.update.poolsize", "Apply Prewarming Pool Size", ctx, func(_ context.Context) {
err = environment.ApplyPrewarmingPoolSize()
span.Finish()
})
if err != nil {
return false, fmt.Errorf("error scaling template job in API: %w", err)
}

View File

@ -1,6 +1,7 @@
package logging
import (
"context"
"github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus"
)
@ -36,3 +37,10 @@ func (hook *SentryHook) Levels() []logrus.Level {
logrus.WarnLevel,
}
}
func StartSpan(op, description string, ctx context.Context, callback func(context.Context)) {
span := sentry.StartSpan(ctx, op)
span.Description = description
defer span.Finish()
callback(span.Context())
}

View File

@ -91,7 +91,8 @@ func (w *Ls2JsonWriter) initializeJSONObject() (count int, err error) {
err = fmt.Errorf("could not write to target: %w", err)
} else {
w.jsonStartSent = true
w.sentrySpan = sentry.StartSpan(w.Ctx, "Forwarding")
w.sentrySpan = sentry.StartSpan(w.Ctx, "nullio.init")
w.sentrySpan.Description = "Forwarding"
}
}
return count, err