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

View File

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

View File

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

View File

@ -4,7 +4,6 @@ import (
"context" "context"
_ "embed" _ "embed"
"fmt" "fmt"
"github.com/getsentry/sentry-go"
nomadApi "github.com/hashicorp/nomad/api" nomadApi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/nomad/structs" "github.com/hashicorp/nomad/nomad/structs"
"github.com/openHPI/poseidon/internal/nomad" "github.com/openHPI/poseidon/internal/nomad"
@ -113,17 +112,17 @@ func (m *NomadEnvironmentManager) CreateOrUpdate(
m.runnerManager.StoreEnvironment(environment) m.runnerManager.StoreEnvironment(environment)
// Register template Job with Nomad. // 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() err = environment.Register()
span.Finish() })
if err != nil { if err != nil {
return false, fmt.Errorf("error registering template job in API: %w", err) return false, fmt.Errorf("error registering template job in API: %w", err)
} }
// Launch idle runners based on the template job. // 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() err = environment.ApplyPrewarmingPoolSize()
span.Finish() })
if err != nil { if err != nil {
return false, fmt.Errorf("error scaling template job in API: %w", err) return false, fmt.Errorf("error scaling template job in API: %w", err)
} }

View File

@ -1,6 +1,7 @@
package logging package logging
import ( import (
"context"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -36,3 +37,10 @@ func (hook *SentryHook) Levels() []logrus.Level {
logrus.WarnLevel, 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) err = fmt.Errorf("could not write to target: %w", err)
} else { } else {
w.jsonStartSent = true 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 return count, err