diff --git a/packages/api/internal/auth/middleware.go b/packages/api/internal/auth/middleware.go index d1e7c770a4..1093b1b576 100644 --- a/packages/api/internal/auth/middleware.go +++ b/packages/api/internal/auth/middleware.go @@ -78,7 +78,7 @@ func (a *commonAuthenticator[T]) Authenticate(ctx context.Context, input *openap // Now, we need to get the API key from the request headerKey, err := a.getHeaderKeysFromRequest(input.RequestValidationInput.Request) if err != nil { - telemetry.ReportCriticalError(ctx, fmt.Errorf("%v %w", a.errorMessage, err)) + telemetry.ReportCriticalError(ctx, a.errorMessage, err) return fmt.Errorf("%v %w", a.errorMessage, err) } @@ -89,7 +89,7 @@ func (a *commonAuthenticator[T]) Authenticate(ctx context.Context, input *openap result, validationError := a.validationFunction(ctx, headerKey) if validationError != nil { zap.L().Info("validation error", zap.Error(validationError.Err)) - telemetry.ReportError(ctx, fmt.Errorf("%s %w", a.errorMessage, validationError.Err)) + telemetry.ReportError(ctx, a.errorMessage, validationError.Err) var forbiddenError *db.TeamForbiddenError if errors.As(validationError.Err, &forbiddenError) { diff --git a/packages/api/internal/cache/instance/instance.go b/packages/api/internal/cache/instance/instance.go index a33ee0a1ba..3899ebfad4 100644 --- a/packages/api/internal/cache/instance/instance.go +++ b/packages/api/internal/cache/instance/instance.go @@ -203,7 +203,7 @@ func (c *InstanceCache) Set(key string, value *InstanceInfo) { go func() { err := c.insertInstance(value) if err != nil { - fmt.Printf("error inserting instance: %v", err) + zap.L().Error("error inserting instance", zap.Error(err)) } }() } diff --git a/packages/api/internal/handlers/accesstoken.go b/packages/api/internal/handlers/accesstoken.go index 85a40b9ece..968cf3beaf 100644 --- a/packages/api/internal/handlers/accesstoken.go +++ b/packages/api/internal/handlers/accesstoken.go @@ -24,8 +24,7 @@ func (a *APIStore) PostAccessTokens(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -34,8 +33,7 @@ func (a *APIStore) PostAccessTokens(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when generating access token: %s", err)) - errMsg := fmt.Errorf("error when generating access token: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when generating access token", err) return } @@ -53,8 +51,7 @@ func (a *APIStore) PostAccessTokens(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when creating access token: %s", err)) - errMsg := fmt.Errorf("error when creating access token: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when creating access token", err) return } @@ -64,9 +61,7 @@ func (a *APIStore) PostAccessTokens(c *gin.Context) { maskedToken, err := keys.GetMaskedIdentifierProperties(keys.AccessTokenPrefix, valueWithoutPrefix) if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when masking access token: %s", err)) - - errMsg := fmt.Errorf("error when masking access token: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when masking access token", err) return } @@ -94,8 +89,8 @@ func (a *APIStore) DeleteAccessTokensAccessTokenID(c *gin.Context, accessTokenID if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing access token ID: %s", err)) - errMsg := fmt.Errorf("error when parsing access token ID: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing access token ID", err) + return } @@ -108,8 +103,8 @@ func (a *APIStore) DeleteAccessTokensAccessTokenID(c *gin.Context, accessTokenID } else if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when deleting access token: %s", err)) - errMsg := fmt.Errorf("error when deleting access token: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when deleting access token", err) + return } diff --git a/packages/api/internal/handlers/admin.go b/packages/api/internal/handlers/admin.go index 8e5afd6a20..c0a029f924 100644 --- a/packages/api/internal/handlers/admin.go +++ b/packages/api/internal/handlers/admin.go @@ -41,8 +41,7 @@ func (a *APIStore) PostNodesNodeID(c *gin.Context, nodeId api.NodeID) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -57,8 +56,7 @@ func (a *APIStore) PostNodesNodeID(c *gin.Context, nodeId api.NodeID) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when sending status change: %s", err)) - errMsg := fmt.Errorf("error when sending status change: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when sending status change", err) return } diff --git a/packages/api/internal/handlers/apikey.go b/packages/api/internal/handlers/apikey.go index fc3cbd404f..df8bb3f389 100644 --- a/packages/api/internal/handlers/apikey.go +++ b/packages/api/internal/handlers/apikey.go @@ -2,13 +2,13 @@ package handlers import ( "fmt" - "log" "net/http" "strings" "time" "github.com/gin-gonic/gin" "github.com/google/uuid" + "go.uber.org/zap" "github.com/e2b-dev/infra/packages/api/internal/api" "github.com/e2b-dev/infra/packages/api/internal/team" @@ -26,8 +26,7 @@ func (a *APIStore) PatchApiKeysApiKeyID(c *gin.Context, apiKeyID string) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -35,8 +34,7 @@ func (a *APIStore) PatchApiKeysApiKeyID(c *gin.Context, apiKeyID string) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing API key ID: %s", err)) - errMsg := fmt.Errorf("error when parsing API key ID: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing API key ID", err) return } @@ -47,8 +45,7 @@ func (a *APIStore) PatchApiKeysApiKeyID(c *gin.Context, apiKeyID string) { } else if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when updating team API key name: %s", err)) - errMsg := fmt.Errorf("error when updating team API key name: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when updating team API key name", err) return } @@ -66,7 +63,7 @@ func (a *APIStore) GetApiKeys(c *gin.Context) { WithCreator(). All(ctx) if err != nil { - log.Println("Error when getting team API keys: ", err) + zap.L().Warn("error when getting team API keys", zap.Error(err)) c.String(http.StatusInternalServerError, "Error when getting team API keys") return @@ -115,8 +112,7 @@ func (a *APIStore) DeleteApiKeysApiKeyID(c *gin.Context, apiKeyID string) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing API key ID: %s", err)) - errMsg := fmt.Errorf("error when parsing API key ID: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing API key ID", err) return } @@ -127,8 +123,7 @@ func (a *APIStore) DeleteApiKeysApiKeyID(c *gin.Context, apiKeyID string) { } else if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when deleting API key: %s", err)) - errMsg := fmt.Errorf("error when deleting API key: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when deleting API key", err) return } @@ -145,8 +140,7 @@ func (a *APIStore) PostApiKeys(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -155,8 +149,7 @@ func (a *APIStore) PostApiKeys(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when creating team API key: %s", err)) - errMsg := fmt.Errorf("error when creating team API key: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when creating team API key", err) return } @@ -165,8 +158,7 @@ func (a *APIStore) PostApiKeys(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when getting user: %s", err)) - errMsg := fmt.Errorf("error when getting user: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when getting user", err) return } @@ -177,8 +169,7 @@ func (a *APIStore) PostApiKeys(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when creating response key mask: %s", err)) - errMsg := fmt.Errorf("error when masking response key for creating API key %d: %w", apiKey.ID, err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, fmt.Sprintf("error when masking response key for creating API key %d", apiKey.ID), err) return } diff --git a/packages/api/internal/handlers/sandbox.go b/packages/api/internal/handlers/sandbox.go index 5dd206390a..344a8bd3ae 100644 --- a/packages/api/internal/handlers/sandbox.go +++ b/packages/api/internal/handlers/sandbox.go @@ -2,7 +2,6 @@ package handlers import ( "context" - "fmt" "net/http" "time" @@ -57,8 +56,7 @@ func (a *APIStore) startSandbox( envdAccessToken, ) if instanceErr != nil { - errMsg := fmt.Errorf("error when creating instance: %w", instanceErr.Err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when creating instance", instanceErr.Err) return nil, instanceErr } diff --git a/packages/api/internal/handlers/sandbox_create.go b/packages/api/internal/handlers/sandbox_create.go index 0270b21dbe..30f16f3073 100644 --- a/packages/api/internal/handlers/sandbox_create.go +++ b/packages/api/internal/handlers/sandbox_create.go @@ -55,8 +55,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -67,8 +66,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid environment ID: %s", err)) - errMsg := fmt.Errorf("error when cleaning env ID: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when cleaning env ID", err) return } @@ -81,7 +79,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) { // Check if team has access to the environment env, build, checkErr := a.templateCache.Get(ctx, cleanedAliasOrEnvID, teamInfo.Team.ID, true) if checkErr != nil { - telemetry.ReportCriticalError(ctx, checkErr.Err) + telemetry.ReportCriticalError(ctx, "error when getting template", checkErr.Err) a.sendAPIStoreError(c, checkErr.Code, checkErr.ClientMsg) return } diff --git a/packages/api/internal/handlers/sandbox_kill.go b/packages/api/internal/handlers/sandbox_kill.go index 023415ccfa..783487eb34 100644 --- a/packages/api/internal/handlers/sandbox_kill.go +++ b/packages/api/internal/handlers/sandbox_kill.go @@ -9,7 +9,6 @@ import ( "github.com/gin-gonic/gin" "github.com/google/uuid" "go.opentelemetry.io/otel/attribute" - "go.uber.org/zap" "github.com/e2b-dev/infra/packages/api/internal/auth" authcache "github.com/e2b-dev/infra/packages/api/internal/cache/auth" @@ -58,8 +57,7 @@ func (a *APIStore) deleteSnapshot( deleteJobErr := a.templateManager.DeleteBuilds(deleteCtx, envBuildIDs) if deleteJobErr != nil { - zap.L().Warn("Error deleting snapshot builds", zap.Error(deleteJobErr), zap.String("sandboxID", sandboxID)) - telemetry.ReportError(deleteCtx, deleteJobErr) + telemetry.ReportError(deleteCtx, "error deleting snapshot builds", deleteJobErr, attribute.String("sandboxID", sandboxID)) } }() @@ -87,8 +85,7 @@ func (a *APIStore) DeleteSandboxesSandboxID( sbx, err := a.orchestrator.GetSandbox(sandboxID) if err == nil { if *sbx.TeamID != teamID { - errMsg := fmt.Errorf("sandbox '%s' does not belong to team '%s'", sandboxID, teamID.String()) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "sandbox does not belong to team", fmt.Errorf("sandbox '%s' does not belong to team '%s'", sandboxID, teamID.String())) a.sendAPIStoreError(c, http.StatusUnauthorized, fmt.Sprintf("Error deleting sandbox - sandbox '%s' does not belong to your team '%s'", sandboxID, teamID.String())) @@ -98,7 +95,7 @@ func (a *APIStore) DeleteSandboxesSandboxID( // remove running sandbox from the orchestrator sandboxExists := a.orchestrator.DeleteInstance(ctx, sandboxID, false) if !sandboxExists { - telemetry.ReportError(ctx, fmt.Errorf("sandbox '%s' not found", sandboxID)) + telemetry.ReportError(ctx, "sandbox not found", fmt.Errorf("sandbox '%s' not found", sandboxID), attribute.String("sandboxID", sandboxID)) a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Error deleting sandbox - sandbox '%s' was not found", sandboxID)) return @@ -107,7 +104,7 @@ func (a *APIStore) DeleteSandboxesSandboxID( // remove any snapshots of the sandbox err := a.deleteSnapshot(ctx, sandboxID, teamID) if err != nil && !errors.Is(err, db.EnvNotFound{}) { - telemetry.ReportError(ctx, err) + telemetry.ReportError(ctx, "error deleting sandbox", err) a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error deleting sandbox: %s", err)) return @@ -123,14 +120,14 @@ func (a *APIStore) DeleteSandboxesSandboxID( // remove any snapshots when the sandbox is not running deleteSnapshotErr := a.deleteSnapshot(ctx, sandboxID, teamID) if errors.Is(deleteSnapshotErr, db.EnvNotFound{}) { - telemetry.ReportError(ctx, fmt.Errorf("snapshot for sandbox '%s' not found", sandboxID)) + telemetry.ReportError(ctx, "snapshot for sandbox not found", fmt.Errorf("snapshot for sandbox '%s' not found", sandboxID), attribute.String("sandboxID", sandboxID)) a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Error deleting sandbox - sandbox '%s' not found", sandboxID)) return } if deleteSnapshotErr != nil { - telemetry.ReportError(ctx, deleteSnapshotErr) + telemetry.ReportError(ctx, "error deleting sandbox", deleteSnapshotErr) a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error deleting sandbox: %s", deleteSnapshotErr)) return diff --git a/packages/api/internal/handlers/sandbox_logs.go b/packages/api/internal/handlers/sandbox_logs.go index 7c21e1d7e2..52f26749ce 100644 --- a/packages/api/internal/handlers/sandbox_logs.go +++ b/packages/api/internal/handlers/sandbox_logs.go @@ -55,8 +55,7 @@ func (a *APIStore) GetSandboxesSandboxIDLogs( res, err := a.lokiClient.QueryRange(query, int(*params.Limit), start, end, logproto.FORWARD, time.Duration(0), time.Duration(0), true) if err != nil { - errMsg := fmt.Errorf("error when returning logs for sandbox: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when returning logs for sandbox", err) a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Error returning logs for sandbox '%s'", sandboxID)) return @@ -87,8 +86,7 @@ func (a *APIStore) GetSandboxesSandboxIDLogs( }) default: - errMsg := fmt.Errorf("unexpected value type %T", res.Data.Result.Type()) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "unexpected value type", fmt.Errorf("unexpected value type %T", res.Data.Result.Type())) a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error returning logs for sandbox '%s", sandboxID)) return diff --git a/packages/api/internal/handlers/sandbox_metrics.go b/packages/api/internal/handlers/sandbox_metrics.go index 0e696e025d..072b32f3f8 100644 --- a/packages/api/internal/handlers/sandbox_metrics.go +++ b/packages/api/internal/handlers/sandbox_metrics.go @@ -13,7 +13,6 @@ import ( "github.com/grafana/loki/pkg/loghttp" "github.com/grafana/loki/pkg/logproto" "go.opentelemetry.io/otel/attribute" - "go.uber.org/zap" "github.com/e2b-dev/infra/packages/api/internal/api" "github.com/e2b-dev/infra/packages/api/internal/auth" @@ -70,8 +69,7 @@ func (a *APIStore) LegacyGetSandboxIDMetrics( err := json.Unmarshal([]byte(entry.Line), &metric) if err != nil { - zap.L().Error("Failed to unmarshal metric", zap.String("sandbox_id", sandboxID), zap.Error(err)) - telemetry.ReportCriticalError(ctx, fmt.Errorf("failed to unmarshal metric: %w", err)) + telemetry.ReportCriticalError(ctx, "failed to unmarshal metric", err, attribute.String("sandbox_id", sandboxID)) continue } @@ -174,11 +172,7 @@ func (a *APIStore) GetSandboxesSandboxIDMetrics( metrics, err := a.readMetricsBasedOnConfig(ctx, sandboxID, teamID, a) if err != nil { - zap.L().Error("Error returning metrics for sandbox", - zap.Error(err), - zap.String("sandboxID", sandboxID), - ) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error returning metrics for sandbox", err, attribute.String("sandboxID", sandboxID)) a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error returning metrics for sandbox '%s'", sandboxID)) return diff --git a/packages/api/internal/handlers/sandbox_pause.go b/packages/api/internal/handlers/sandbox_pause.go index af6fdb22a7..cf1faa3607 100644 --- a/packages/api/internal/handlers/sandbox_pause.go +++ b/packages/api/internal/handlers/sandbox_pause.go @@ -50,8 +50,7 @@ func (a *APIStore) PostSandboxesSandboxIDPause(c *gin.Context, sandboxID api.San } if *sbx.TeamID != teamID { - errMsg := fmt.Errorf("sandbox '%s' does not belong to team '%s'", sandboxID, teamID.String()) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "sandbox does not belong to team", fmt.Errorf("sandbox '%s' does not belong to team '%s'", sandboxID, teamID.String())) a.sendAPIStoreError(c, http.StatusUnauthorized, fmt.Sprintf("Error pausing sandbox - sandbox '%s' does not belong to your team '%s'", sandboxID, teamID.String())) diff --git a/packages/api/internal/handlers/sandbox_refresh.go b/packages/api/internal/handlers/sandbox_refresh.go index b15a6b8b39..9e5e9b897b 100644 --- a/packages/api/internal/handlers/sandbox_refresh.go +++ b/packages/api/internal/handlers/sandbox_refresh.go @@ -26,8 +26,7 @@ func (a *APIStore) PostSandboxesSandboxIDRefreshes( if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -44,7 +43,7 @@ func (a *APIStore) PostSandboxesSandboxIDRefreshes( apiErr := a.orchestrator.KeepAliveFor(ctx, sandboxID, duration, false) if apiErr != nil { - telemetry.ReportCriticalError(ctx, apiErr.Err) + telemetry.ReportCriticalError(ctx, "error when refreshing sandbox", apiErr.Err) a.sendAPIStoreError(c, apiErr.Code, apiErr.ClientMsg) return diff --git a/packages/api/internal/handlers/sandbox_resume.go b/packages/api/internal/handlers/sandbox_resume.go index 88d5641329..0fdbb5afc9 100644 --- a/packages/api/internal/handlers/sandbox_resume.go +++ b/packages/api/internal/handlers/sandbox_resume.go @@ -47,8 +47,7 @@ func (a *APIStore) PostSandboxesSandboxIDResume(c *gin.Context, sandboxID api.Sa if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } diff --git a/packages/api/internal/handlers/sandbox_timeout.go b/packages/api/internal/handlers/sandbox_timeout.go index f3beace7ac..a28f4ffea4 100644 --- a/packages/api/internal/handlers/sandbox_timeout.go +++ b/packages/api/internal/handlers/sandbox_timeout.go @@ -25,8 +25,7 @@ func (a *APIStore) PostSandboxesSandboxIDTimeout( if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Error when parsing request: %s", err)) - errMsg := fmt.Errorf("error when parsing request: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) return } @@ -39,7 +38,7 @@ func (a *APIStore) PostSandboxesSandboxIDTimeout( apiErr := a.orchestrator.KeepAliveFor(ctx, sandboxID, duration, true) if apiErr != nil { - telemetry.ReportCriticalError(ctx, apiErr.Err) + telemetry.ReportCriticalError(ctx, "error when setting timeout", apiErr.Err) a.sendAPIStoreError(c, apiErr.Code, apiErr.ClientMsg) return diff --git a/packages/api/internal/handlers/sandboxes_list_metrics.go b/packages/api/internal/handlers/sandboxes_list_metrics.go index 862a5d6acf..afd6acef2f 100644 --- a/packages/api/internal/handlers/sandboxes_list_metrics.go +++ b/packages/api/internal/handlers/sandboxes_list_metrics.go @@ -74,7 +74,7 @@ func (a *APIStore) getSandboxesMetrics( if err != nil { timeoutCount.Add(1) err := fmt.Errorf("context cancelled while waiting for rate limiter: %w", ctx.Err()) - telemetry.ReportError(ctx, err) + telemetry.ReportError(ctx, "context cancelled while waiting for rate limiter", err) results <- metricsResult{ sandbox: s, err: err, @@ -95,7 +95,7 @@ func (a *APIStore) getSandboxesMetrics( if err != nil { errorCount.Add(1) - telemetry.ReportError(ctx, fmt.Errorf("failed to fetch metrics for sandbox %s: %w", s.SandboxID, err)) + telemetry.ReportError(ctx, "failed to fetch metrics for sandbox", err, attribute.String("sandboxID", s.SandboxID)) } else { successCount.Add(1) } @@ -205,8 +205,7 @@ func (a *APIStore) GetSandboxesMetrics(c *gin.Context, params api.GetSandboxesMe sandboxesWithMetrics, err := a.getSandboxesMetrics(ctx, team.ID, sandboxes) if err != nil { - zap.L().Error("Error fetching metrics for sandboxes", zap.Error(err)) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error fetching metrics for sandboxes", err) a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error returning metrics for sandboxes for team '%s'", team.ID)) return diff --git a/packages/api/internal/handlers/teams.go b/packages/api/internal/handlers/teams.go index 67086caaec..9cfbb8a05c 100644 --- a/packages/api/internal/handlers/teams.go +++ b/packages/api/internal/handlers/teams.go @@ -1,10 +1,10 @@ package handlers import ( - "log" "net/http" "github.com/gin-gonic/gin" + "go.uber.org/zap" "github.com/e2b-dev/infra/packages/api/internal/api" "github.com/e2b-dev/infra/packages/api/internal/team" @@ -17,7 +17,7 @@ func (a *APIStore) GetTeams(c *gin.Context) { results, err := a.sqlcDB.GetTeamsWithUsersTeams(ctx, userID) if err != nil { - log.Println("Error when starting transaction: ", err) + zap.L().Error("error when starting transaction", zap.Error(err)) c.JSON(http.StatusInternalServerError, "Error when starting transaction") return @@ -28,7 +28,7 @@ func (a *APIStore) GetTeams(c *gin.Context) { apiKey, err := team.CreateAPIKey(ctx, a.db, row.Team.ID, userID, "CLI login/configure") if err != nil { - log.Println("Error when creating API key: ", err) + zap.L().Error("error when creating API key", zap.Error(err)) c.JSON(http.StatusInternalServerError, "Error when creating API key") return diff --git a/packages/api/internal/handlers/template_build_status.go b/packages/api/internal/handlers/template_build_status.go index e90c78329d..b020ca522f 100644 --- a/packages/api/internal/handlers/template_build_status.go +++ b/packages/api/internal/handlers/template_build_status.go @@ -12,6 +12,7 @@ import ( "github.com/google/uuid" "github.com/grafana/loki/pkg/loghttp" "github.com/grafana/loki/pkg/logproto" + "go.opentelemetry.io/otel/attribute" "go.uber.org/zap" "github.com/e2b-dev/infra/packages/api/internal/api" @@ -34,19 +35,16 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem userID := c.Value(auth.UserIDContextKey).(uuid.UUID) teams, err := a.sqlcDB.GetTeamsWithUsersTeams(ctx, userID) if err != nil { - errMsg := fmt.Errorf("error when getting teams: %w", err) - a.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to get the default team") - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when getting teams", err) return } buildUUID, err := uuid.Parse(buildID) if err != nil { - errMsg := fmt.Errorf("error when parsing build id: %w", err) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "error when parsing build id", err) a.sendAPIStoreError(c, http.StatusBadRequest, "Invalid build id") return } @@ -63,8 +61,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem return } - errMsg := fmt.Errorf("error when getting template: %w", err) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "error when getting template", err) a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting template") return } @@ -78,8 +75,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem } if team == nil { - msg := fmt.Errorf("user doesn't have access to env '%s'", templateID) - telemetry.ReportError(ctx, msg) + telemetry.ReportError(ctx, "user doesn't have access to env", fmt.Errorf("user doesn't have access to env '%s'", templateID), attribute.String("templateID", templateID)) a.sendAPIStoreError(c, http.StatusForbidden, fmt.Sprintf("You don't have access to this sandbox template (%s)", templateID)) return } @@ -140,8 +136,7 @@ func (a *APIStore) GetTemplatesTemplateIDBuildsBuildIDStatus(c *gin.Context, tem } } } else { - errMsg := fmt.Errorf("error when returning logs for template build: %w", err) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "error when returning logs for template build", err) zap.L().Error("error when returning logs for template build", zap.Error(err), zap.String("buildID", buildID)) } diff --git a/packages/api/internal/handlers/template_delete.go b/packages/api/internal/handlers/template_delete.go index 83b380fedb..48be93eac6 100644 --- a/packages/api/internal/handlers/template_delete.go +++ b/packages/api/internal/handlers/template_delete.go @@ -26,8 +26,7 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid env ID: %s", aliasOrTemplateID)) - err = fmt.Errorf("invalid env ID: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "invalid env ID", err) return } @@ -37,8 +36,7 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when getting default team: %s", err)) - err = fmt.Errorf("error when getting default team: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting default team", err) return } @@ -58,13 +56,13 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a notFound := models.IsNotFound(err) if notFound { - telemetry.ReportError(ctx, fmt.Errorf("template '%s' not found", aliasOrTemplateID)) + telemetry.ReportError(ctx, "template not found", nil, attribute.String("templateID", aliasOrTemplateID)) a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("the sandbox template '%s' wasn't found", cleanedAliasOrEnvID)) return } else if err != nil { - telemetry.ReportError(ctx, fmt.Errorf("failed to get env '%s': %w", aliasOrTemplateID, err)) - a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting env") + telemetry.ReportError(ctx, "failed to get template", fmt.Errorf("failed to get template: %w", err), attribute.String("templateID", aliasOrTemplateID)) + a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting template") return } @@ -78,8 +76,7 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a } if team == nil { - errMsg := fmt.Errorf("user '%s' doesn't have access to the sandbox template '%s'", userID, cleanedAliasOrEnvID) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "user doesn't have access to the sandbox template", nil, attribute.String("templateID", cleanedAliasOrEnvID), attribute.String("userID", userID.String())) a.sendAPIStoreError(c, http.StatusForbidden, fmt.Sprintf("You (%s) don't have access to sandbox template '%s'", userID, cleanedAliasOrEnvID)) @@ -96,14 +93,14 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a // check if base env has snapshots hasSnapshots, err := a.db.CheckBaseEnvHasSnapshots(ctx, template.ID) if err != nil { - telemetry.ReportError(ctx, fmt.Errorf("error when checking if base env has snapshots: %w", err)) + telemetry.ReportError(ctx, "error when checking if base env has snapshots", err) a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when checking if base env has snapshots") return } if hasSnapshots { - telemetry.ReportError(ctx, fmt.Errorf("base template '%s' has paused sandboxes", template.ID)) + telemetry.ReportError(ctx, "base template has paused sandboxes", nil, attribute.String("templateID", template.ID)) a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("cannot delete template '%s' because there are paused sandboxes using it", template.ID)) return @@ -111,8 +108,7 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a dbErr := a.db.DeleteEnv(ctx, template.ID) if dbErr != nil { - errMsg := fmt.Errorf("error when deleting env from db: %w", dbErr) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when deleting env from db", dbErr) a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when deleting env") @@ -131,8 +127,7 @@ func (a *APIStore) DeleteTemplatesTemplateID(c *gin.Context, aliasOrTemplateID a // delete all builds deleteJobErr := a.templateManager.DeleteBuilds(ctx, buildIds) if deleteJobErr != nil { - errMsg := fmt.Errorf("error when deleting env files from storage: %w", deleteJobErr) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when deleting env files from storage", deleteJobErr) } else { telemetry.ReportEvent(ctx, "deleted env from storage") } diff --git a/packages/api/internal/handlers/template_request_build.go b/packages/api/internal/handlers/template_request_build.go index 681a758b08..d197ebad28 100644 --- a/packages/api/internal/handlers/template_request_build.go +++ b/packages/api/internal/handlers/template_request_build.go @@ -40,8 +40,7 @@ func (a *APIStore) PostTemplatesTemplateID(c *gin.Context, templateID api.Templa if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid template ID: %s", cleanedTemplateID)) - err = fmt.Errorf("invalid template ID: %w", err) - telemetry.ReportCriticalError(c.Request.Context(), err) + telemetry.ReportCriticalError(c.Request.Context(), "invalid template ID", err) return } @@ -59,9 +58,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI body, err := utils.ParseBody[api.TemplateBuildRequest](ctx, c) if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid request body: %s", err)) - - err = fmt.Errorf("invalid request body: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "invalid request body", err) return nil } @@ -75,8 +72,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when getting user: %s", err)) - err = fmt.Errorf("error when getting default team: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting user", err) return nil } @@ -86,8 +82,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid team ID: %s", *body.TeamID)) - err = fmt.Errorf("invalid team ID: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "invalid team ID", err) return nil } @@ -103,8 +98,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if team == nil { a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Team '%s' not found", *body.TeamID)) - err = fmt.Errorf("team not found: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "team not found", err) return nil } @@ -120,8 +114,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if team == nil { a.sendAPIStoreError(c, http.StatusInternalServerError, "Default team not found") - err = fmt.Errorf("default team not found: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "default team not found", err) return nil } @@ -131,11 +124,9 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI // Check if the user has access to the template _, err = a.db.Client.Env.Query().Where(env.ID(templateID), env.TeamID(team.ID)).Only(ctx) if err != nil { - errMsg := fmt.Sprintf("Error when getting template '%s' for team '%s'", templateID, team.ID.String()) - a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("%s: %s", errMsg, err)) + a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Error when getting template '%s' for team '%s'", templateID, team.ID.String())) - err = fmt.Errorf("%s: %w", errMsg, err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting template", err, attribute.String("template_id", templateID), attribute.String("team_id", team.ID.String())) return nil } @@ -144,8 +135,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI // Generate a build id for the new build buildID, err := uuid.NewRandom() if err != nil { - err = fmt.Errorf("error when generating build id: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when generating build id", err) a.sendAPIStoreError(c, http.StatusInternalServerError, "Failed to generate build id") @@ -183,7 +173,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI cpuCount, ramMB, apiError := getCPUAndRAM(tier, body.CpuCount, body.MemoryMB) if apiError != nil { - telemetry.ReportCriticalError(ctx, apiError.Err) + telemetry.ReportCriticalError(ctx, "error when getting CPU and RAM", apiError.Err) a.sendAPIStoreError(c, apiError.Code, apiError.ClientMsg) return nil @@ -195,8 +185,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid alias: %s", alias)) - err = fmt.Errorf("invalid alias: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "invalid alias", err) return nil } @@ -207,8 +196,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when starting transaction: %s", err)) - err = fmt.Errorf("error when starting transaction: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when starting transaction", err) return nil } @@ -231,8 +219,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when updating template: %s", err)) - err = fmt.Errorf("error when updating env: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when updating env", err) return nil } @@ -245,8 +232,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when updating template: %s", err)) - err = fmt.Errorf("error when updating env: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when updating env", err) return nil } @@ -268,8 +254,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when inserting build: %s", err)) - err = fmt.Errorf("error when inserting build: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when inserting build", err) return nil } @@ -284,8 +269,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when querying alias '%s': %s", alias, err)) - err = fmt.Errorf("error when checking alias '%s': %w", alias, err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when checking alias", err, attribute.String("alias", alias)) return nil } @@ -293,8 +277,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if len(envs) > 0 { a.sendAPIStoreError(c, http.StatusConflict, fmt.Sprintf("Alias '%s' is already used", alias)) - err = fmt.Errorf("conflict of alias '%s' with template ID: %w", alias, err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "conflict of alias", err, attribute.String("alias", alias)) return nil } @@ -304,8 +287,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if !models.IsNotFound(err) { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when querying for alias: %s", err)) - err = fmt.Errorf("error when checking alias: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when checking alias", err, attribute.String("alias", alias)) return nil @@ -315,8 +297,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when deleting template alias: %s", err)) - err = fmt.Errorf("error when deleting template alias: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when deleting template alias", err, attribute.String("alias", alias)) return nil } @@ -333,8 +314,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when inserting alias '%s': %s", alias, err)) - err = fmt.Errorf("error when inserting alias '%s': %w", alias, err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when inserting alias", err, attribute.String("alias", alias)) return nil @@ -342,8 +322,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI } else if aliasDB.EnvID != templateID { a.sendAPIStoreError(c, http.StatusForbidden, fmt.Sprintf("Alias '%s' already used", alias)) - err = fmt.Errorf("alias '%s' already used: %w", alias, err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "alias already used", err, attribute.String("alias", alias)) return nil } @@ -356,8 +335,7 @@ func (a *APIStore) TemplateRequestBuild(c *gin.Context, templateID api.TemplateI if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when committing transaction: %s", err)) - err = fmt.Errorf("error when committing transaction: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when committing transaction", err) return nil } diff --git a/packages/api/internal/handlers/template_start_build.go b/packages/api/internal/handlers/template_start_build.go index 0c7b26e94a..7b6fccfdcd 100644 --- a/packages/api/internal/handlers/template_start_build.go +++ b/packages/api/internal/handlers/template_start_build.go @@ -11,7 +11,6 @@ import ( "github.com/posthog/posthog-go" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" - "go.uber.org/zap" "github.com/e2b-dev/infra/packages/api/internal/api" template_manager "github.com/e2b-dev/infra/packages/api/internal/template-manager" @@ -32,8 +31,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid build ID: %s", buildID)) - err = fmt.Errorf("invalid build ID: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "invalid build ID", err) return } @@ -42,8 +40,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when getting default team: %s", err)) - err = fmt.Errorf("error when getting default team: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting default team", err) return } @@ -61,8 +58,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template if err != nil { a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("Error when getting template: %s", err)) - err = fmt.Errorf("error when getting env: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting env", err, attribute.String("template_id", templateID)) return } @@ -79,8 +75,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template if team == nil { a.sendAPIStoreError(c, http.StatusForbidden, "User does not have access to the template") - err = fmt.Errorf("user '%s' does not have access to the template '%s'", userID, templateID) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "user does not have access to the template", err, attribute.String("template_id", templateID)) return } @@ -102,9 +97,8 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template ). All(ctx) if err != nil { - zap.L().Error("Error when getting running builds", zap.Error(err)) a.sendAPIStoreError(c, http.StatusInternalServerError, "Error during template build request") - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "Error when getting running builds", err) return } @@ -121,9 +115,8 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template }))) deleteJobErr := a.templateManager.DeleteBuilds(ctx, buildIDs) if deleteJobErr != nil { - errMsg := fmt.Errorf("error when canceling running build: %w", deleteJobErr) a.sendAPIStoreError(c, http.StatusInternalServerError, "Error during template build cancel request") - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when canceling running build", deleteJobErr) return } telemetry.ReportEvent(ctx, "canceled running builds") @@ -144,8 +137,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template // only waiting builds can be triggered if build.Status != envbuild.StatusWaiting { a.sendAPIStoreError(c, http.StatusBadRequest, "build is not in waiting state") - err = fmt.Errorf("build is not in waiting state: %s", build.Status) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "build is not in waiting state", fmt.Errorf("build is not in waiting state: %s", build.Status), attribute.String("template_id", templateID)) return } @@ -165,9 +157,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template ) if buildErr != nil { - buildErr = fmt.Errorf("error when building env: %w", buildErr) - zap.L().Error("build failed", zap.Error(buildErr)) - telemetry.ReportCriticalError(ctx, buildErr) + telemetry.ReportCriticalError(ctx, "build failed", buildErr, attribute.String("template_id", templateID)) err = a.templateManager.SetStatus( ctx, @@ -177,7 +167,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template fmt.Sprintf("error when building env: %s", buildErr), ) if err != nil { - telemetry.ReportCriticalError(ctx, fmt.Errorf("error when setting build status: %w", err)) + telemetry.ReportCriticalError(ctx, "error when setting build status", err) } return @@ -193,7 +183,7 @@ func (a *APIStore) PostTemplatesTemplateIDBuildsBuildID(c *gin.Context, template "starting build", ) if err != nil { - telemetry.ReportCriticalError(ctx, fmt.Errorf("error when setting build status: %w", err)) + telemetry.ReportCriticalError(ctx, "error when setting build status", err) return } diff --git a/packages/api/internal/handlers/template_update.go b/packages/api/internal/handlers/template_update.go index 99d6d83dd0..ec305f27cd 100644 --- a/packages/api/internal/handlers/template_update.go +++ b/packages/api/internal/handlers/template_update.go @@ -34,8 +34,7 @@ func (a *APIStore) PatchTemplatesTemplateID(c *gin.Context, aliasOrTemplateID ap if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, fmt.Sprintf("Invalid env ID: %s", aliasOrTemplateID)) - err = fmt.Errorf("invalid env ID: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "invalid env ID", err) return } @@ -45,8 +44,7 @@ func (a *APIStore) PatchTemplatesTemplateID(c *gin.Context, aliasOrTemplateID ap if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, fmt.Sprintf("Error when getting default team: %s", err)) - err = fmt.Errorf("error when getting default team: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting default team", err) return } @@ -64,12 +62,13 @@ func (a *APIStore) PatchTemplatesTemplateID(c *gin.Context, aliasOrTemplateID ap notFound := models.IsNotFound(err) if notFound { - telemetry.ReportError(ctx, fmt.Errorf("template '%s' not found", aliasOrTemplateID)) + telemetry.ReportError(ctx, "template not found", fmt.Errorf("template '%s' not found", aliasOrTemplateID)) a.sendAPIStoreError(c, http.StatusNotFound, fmt.Sprintf("the sandbox template '%s' wasn't found", cleanedAliasOrEnvID)) return } else if err != nil { - telemetry.ReportError(ctx, fmt.Errorf("failed to get env '%s': %w", aliasOrTemplateID, err)) + telemetry.ReportError(ctx, "failed to get env", err, attribute.String("env_id", aliasOrTemplateID)) + a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting env") return @@ -84,8 +83,7 @@ func (a *APIStore) PatchTemplatesTemplateID(c *gin.Context, aliasOrTemplateID ap } if team == nil { - errMsg := fmt.Errorf("user '%s' doesn't have access to the sandbox template '%s'", userID, cleanedAliasOrEnvID) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "user doesn't have access to the sandbox template", fmt.Errorf("user '%s' doesn't have access to the sandbox template '%s'", userID, cleanedAliasOrEnvID)) a.sendAPIStoreError(c, http.StatusForbidden, fmt.Sprintf("You (%s) don't have access to sandbox template '%s'", userID, cleanedAliasOrEnvID)) @@ -99,8 +97,7 @@ func (a *APIStore) PatchTemplatesTemplateID(c *gin.Context, aliasOrTemplateID ap }) if dbErr != nil { - errMsg := fmt.Errorf("error when updating env: %w", dbErr) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "error when updating env", dbErr) a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when updating env") return diff --git a/packages/api/internal/handlers/templates_list.go b/packages/api/internal/handlers/templates_list.go index e37bb40c88..7e972ec12d 100644 --- a/packages/api/internal/handlers/templates_list.go +++ b/packages/api/internal/handlers/templates_list.go @@ -1,7 +1,6 @@ package handlers import ( - "fmt" "net/http" "github.com/gin-gonic/gin" @@ -25,8 +24,7 @@ func (a *APIStore) GetTemplates(c *gin.Context, params api.GetTemplatesParams) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting teams") - err = fmt.Errorf("error when getting teams: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting teams", err) return } @@ -36,7 +34,7 @@ func (a *APIStore) GetTemplates(c *gin.Context, params api.GetTemplatesParams) { if err != nil { a.sendAPIStoreError(c, http.StatusBadRequest, "Invalid team ID") - telemetry.ReportError(ctx, err) + telemetry.ReportError(ctx, "invalid team ID", err) return } @@ -51,7 +49,7 @@ func (a *APIStore) GetTemplates(c *gin.Context, params api.GetTemplatesParams) { if team == nil { a.sendAPIStoreError(c, http.StatusNotFound, "Team not found") - telemetry.ReportError(ctx, fmt.Errorf("team not found")) + telemetry.ReportError(ctx, "team not found", err) return } @@ -66,7 +64,7 @@ func (a *APIStore) GetTemplates(c *gin.Context, params api.GetTemplatesParams) { if team == nil { a.sendAPIStoreError(c, http.StatusInternalServerError, "Default team not found") - telemetry.ReportError(ctx, fmt.Errorf("default team not found")) + telemetry.ReportError(ctx, "default team not found", err) return } @@ -81,8 +79,7 @@ func (a *APIStore) GetTemplates(c *gin.Context, params api.GetTemplatesParams) { if err != nil { a.sendAPIStoreError(c, http.StatusInternalServerError, "Error when getting sandbox templates") - err = fmt.Errorf("error when getting envs: %w", err) - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "error when getting envs", err) return } diff --git a/packages/api/internal/orchestrator/create_instance.go b/packages/api/internal/orchestrator/create_instance.go index a828017f9c..534efe2e92 100644 --- a/packages/api/internal/orchestrator/create_instance.go +++ b/packages/api/internal/orchestrator/create_instance.go @@ -61,7 +61,7 @@ func (o *Orchestrator) CreateSandbox( var limitErr *instance.ErrSandboxLimitExceeded var alreadyErr *instance.ErrAlreadyBeingStarted - telemetry.ReportCriticalError(ctx, err) + telemetry.ReportCriticalError(ctx, "failed to reserve sandbox for team", err) switch { case errors.As(err, &limitErr): @@ -167,13 +167,12 @@ func (o *Orchestrator) CreateSandbox( if node == nil { node, err = o.getLeastBusyNode(childCtx, nodesExcluded) if err != nil { - errMsg := fmt.Errorf("failed to get least busy node: %w", err) - telemetry.ReportError(childCtx, errMsg) + telemetry.ReportError(childCtx, "failed to get least busy node", err) return nil, &api.APIError{ Code: http.StatusInternalServerError, ClientMsg: "Failed to get node to place sandbox on.", - Err: errMsg, + Err: fmt.Errorf("failed to get least busy node: %w", err), } } } @@ -248,8 +247,7 @@ func (o *Orchestrator) CreateSandbox( cacheErr := o.instanceCache.Add(childCtx, instanceInfo, true) if cacheErr != nil { - errMsg := fmt.Errorf("error when adding instance to cache: %w", cacheErr) - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, "error when adding instance to cache", cacheErr) deleted := o.DeleteInstance(childCtx, sbx.SandboxID, false) if !deleted { @@ -259,7 +257,7 @@ func (o *Orchestrator) CreateSandbox( return nil, &api.APIError{ Code: http.StatusInternalServerError, ClientMsg: "Failed to create sandbox", - Err: errMsg, + Err: fmt.Errorf("error when adding instance to cache: %w", cacheErr), } } diff --git a/packages/api/internal/orchestrator/node.go b/packages/api/internal/orchestrator/node.go index 95f337bf57..cd74e8678f 100644 --- a/packages/api/internal/orchestrator/node.go +++ b/packages/api/internal/orchestrator/node.go @@ -3,7 +3,6 @@ package orchestrator import ( "context" "fmt" - "os" "sync" "sync/atomic" "time" @@ -139,7 +138,7 @@ func (o *Orchestrator) GetNodes() []*api.Node { for _, sbx := range o.instanceCache.Items() { n, ok := nodes[sbx.Instance.ClientID] if !ok { - fmt.Fprintf(os.Stderr, "node [%s] for sandbox [%s] wasn't found \n", sbx.Instance.ClientID, sbx.Instance.SandboxID) + zap.L().Error("node for sandbox wasn't found", zap.String("client_id", sbx.Instance.ClientID), zap.String("sandbox_id", sbx.Instance.SandboxID)) continue } diff --git a/packages/api/internal/orchestrator/pause_instance.go b/packages/api/internal/orchestrator/pause_instance.go index a720267ac4..f59ed0f4a4 100644 --- a/packages/api/internal/orchestrator/pause_instance.go +++ b/packages/api/internal/orchestrator/pause_instance.go @@ -52,33 +52,29 @@ func (o *Orchestrator) PauseInstance( teamID, ) if err != nil { - errMsg := fmt.Errorf("error pausing sandbox: %w", err) + telemetry.ReportCriticalError(ctx, "error pausing sandbox", err) - telemetry.ReportCriticalError(ctx, errMsg) - - return errMsg + return err } err = snapshotInstance(ctx, o, sbx, *envBuild.EnvID, envBuild.ID.String()) if errors.Is(err, ErrPauseQueueExhausted{}) { - telemetry.ReportCriticalError(ctx, fmt.Errorf("pause queue exhausted %w", err)) + telemetry.ReportCriticalError(ctx, "pause queue exhausted", err) return ErrPauseQueueExhausted{} } if err != nil && !errors.Is(err, ErrPauseQueueExhausted{}) { - errMsg := fmt.Errorf("error pausing sandbox: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error pausing sandbox", err) - return errMsg + return fmt.Errorf("error pausing sandbox: %w", err) } err = o.dbClient.EnvBuildSetStatus(ctx, *envBuild.EnvID, envBuild.ID, envbuild.StatusSuccess) if err != nil { - errMsg := fmt.Errorf("error pausing sandbox: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error pausing sandbox", err) - return errMsg + return fmt.Errorf("error pausing sandbox: %w", err) } return nil diff --git a/packages/api/internal/team/apikeys.go b/packages/api/internal/team/apikeys.go index 1c8a838345..f0fcd65051 100644 --- a/packages/api/internal/team/apikeys.go +++ b/packages/api/internal/team/apikeys.go @@ -16,10 +16,9 @@ import ( func CreateAPIKey(ctx context.Context, db *db.DB, teamID uuid.UUID, userID uuid.UUID, name string) (*models.TeamAPIKey, error) { teamApiKey, err := keys.GenerateKey(keys.ApiKeyPrefix) if err != nil { - errMsg := fmt.Errorf("error when generating team API key: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when generating team API key", err) - return nil, errMsg + return nil, fmt.Errorf("error when generating team API key: %w", err) } apiKey, err := db.Client.TeamAPIKey. @@ -34,8 +33,9 @@ func CreateAPIKey(ctx context.Context, db *db.DB, teamID uuid.UUID, userID uuid. SetName(name). Save(ctx) if err != nil { - errMsg := fmt.Errorf("error when creating API key: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error when creating API key", err) + + return nil, fmt.Errorf("error when creating API key: %w", err) } return apiKey, nil diff --git a/packages/api/internal/utils/body.go b/packages/api/internal/utils/body.go index 5561a09669..adbd674f66 100644 --- a/packages/api/internal/utils/body.go +++ b/packages/api/internal/utils/body.go @@ -14,11 +14,9 @@ import ( func ParseBody[B any](ctx context.Context, c *gin.Context) (body B, err error) { err = c.Bind(&body) if err != nil { - bodyErr := fmt.Errorf("error when parsing request: %w", err) + telemetry.ReportCriticalError(ctx, "error when parsing request", err) - telemetry.ReportCriticalError(ctx, bodyErr) - - return body, bodyErr + return body, fmt.Errorf("error when parsing request: %w", err) } return body, nil diff --git a/packages/api/internal/utils/counter.go b/packages/api/internal/utils/counter.go index b29e0cdb58..ab58c2524b 100644 --- a/packages/api/internal/utils/counter.go +++ b/packages/api/internal/utils/counter.go @@ -2,10 +2,11 @@ package utils import ( "context" - "log" "sync" "time" + "go.uber.org/zap" + "github.com/e2b-dev/infra/packages/shared/pkg/db" ) @@ -69,7 +70,7 @@ func (t *TemplateSpawnCounter) flushCounters(dbClient *db.DB) { for templateID, counter := range updates { err := dbClient.UpdateEnvLastUsed(context.Background(), int64(counter.count), counter.lastUpdate, templateID) if err != nil { - log.Println("Error updating template spawn count:", err) + zap.L().Error("error updating template spawn count", zap.Error(err)) } } } diff --git a/packages/api/internal/utils/error.go b/packages/api/internal/utils/error.go index b03bfe5477..3b5c6e7f93 100644 --- a/packages/api/internal/utils/error.go +++ b/packages/api/internal/utils/error.go @@ -41,7 +41,7 @@ func ErrorHandler(c *gin.Context, message string, statusCode int) { } } - telemetry.ReportError(ctx, errMsg) + telemetry.ReportError(ctx, message, errMsg) c.Error(errMsg) diff --git a/packages/orchestrator/internal/server/sandboxes.go b/packages/orchestrator/internal/server/sandboxes.go index 3bd9a1f30b..54fa5a8412 100644 --- a/packages/orchestrator/internal/server/sandboxes.go +++ b/packages/orchestrator/internal/server/sandboxes.go @@ -67,10 +67,10 @@ func (s *server) Create(ctxConn context.Context, req *orchestrator.SandboxCreate zap.L().Error("failed to create sandbox, cleaning up", zap.Error(err)) cleanupErr := cleanup.Run(ctx) - errMsg := fmt.Errorf("failed to cleanup sandbox: %w", errors.Join(err, context.Cause(ctx), cleanupErr)) - telemetry.ReportCriticalError(ctx, errMsg) + err := errors.Join(err, context.Cause(ctx), cleanupErr) + telemetry.ReportCriticalError(ctx, "failed to cleanup sandbox", err) - return nil, status.New(codes.Internal, errMsg.Error()).Err() + return nil, status.Errorf(codes.Internal, "failed to cleanup sandbox: %s", err) } s.sandboxes.Insert(req.Sandbox.SandboxId, sbx) @@ -125,10 +125,9 @@ func (s *server) Update(ctx context.Context, req *orchestrator.SandboxUpdateRequ item, ok := s.sandboxes.Get(req.SandboxId) if !ok { - errMsg := fmt.Errorf("sandbox not found") - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "sandbox not found", nil) - return nil, status.New(codes.NotFound, errMsg.Error()).Err() + return nil, status.Error(codes.NotFound, "sandbox not found") } item.EndAt = req.EndTime.AsTime() @@ -180,10 +179,9 @@ func (s *server) Delete(ctxConn context.Context, in *orchestrator.SandboxDeleteR sbx, ok := s.sandboxes.Get(in.SandboxId) if !ok { - errMsg := fmt.Errorf("sandbox '%s' not found", in.SandboxId) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "sandbox not found", nil, attribute.String("sandboxID", in.SandboxId)) - return nil, status.New(codes.NotFound, errMsg.Error()).Err() + return nil, status.Errorf(codes.NotFound, "sandbox '%s' not found", in.SandboxId) } // Remove the sandbox from the cache to prevent loading it again in API during the time the instance is stopping. @@ -218,10 +216,9 @@ func (s *server) Pause(ctx context.Context, in *orchestrator.SandboxPauseRequest if !ok { s.pauseMu.Unlock() - errMsg := fmt.Errorf("sandbox not found") - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "sandbox not found", nil) - return nil, status.New(codes.NotFound, errMsg.Error()).Err() + return nil, status.Error(codes.NotFound, "sandbox not found") } s.sandboxes.Remove(in.SandboxId) @@ -235,10 +232,9 @@ func (s *server) Pause(ctx context.Context, in *orchestrator.SandboxPauseRequest sbx.Config.FirecrackerVersion, ).NewTemplateCacheFiles() if err != nil { - errMsg := fmt.Errorf("error creating template files: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error creating template files", err) - return nil, status.New(codes.Internal, errMsg.Error()).Err() + return nil, status.Errorf(codes.Internal, "error creating template files: %s", err) } defer func() { @@ -257,10 +253,9 @@ func (s *server) Pause(ctx context.Context, in *orchestrator.SandboxPauseRequest snapshot, err := sbx.Pause(ctx, s.tracer, snapshotTemplateFiles) if err != nil { - errMsg := fmt.Errorf("error snapshotting sandbox '%s': %w", in.SandboxId, err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error snapshotting sandbox", err, attribute.String("sandboxID", in.SandboxId)) - return nil, status.New(codes.Internal, errMsg.Error()).Err() + return nil, status.Errorf(codes.Internal, "error snapshotting sandbox '%s': %s", in.SandboxId, err) } err = s.templateCache.AddSnapshot( @@ -275,10 +270,9 @@ func (s *server) Pause(ctx context.Context, in *orchestrator.SandboxPauseRequest snapshot.RootfsDiff, ) if err != nil { - errMsg := fmt.Errorf("error adding snapshot to template cache: %w", err) - telemetry.ReportCriticalError(ctx, errMsg) + telemetry.ReportCriticalError(ctx, "error adding snapshot to template cache", err) - return nil, status.New(codes.Internal, errMsg.Error()).Err() + return nil, status.Errorf(codes.Internal, "error adding snapshot to template cache: %s", err) } telemetry.ReportEvent(ctx, "added snapshot to template cache") diff --git a/packages/orchestrator/internal/template/build/oci/oci.go b/packages/orchestrator/internal/template/build/oci/oci.go index a0667cd07e..f1a257da73 100644 --- a/packages/orchestrator/internal/template/build/oci/oci.go +++ b/packages/orchestrator/internal/template/build/oci/oci.go @@ -65,7 +65,7 @@ func ToExt4(ctx context.Context, img v1.Image, rootfsPath string, sizeLimit int6 defer func() { rootfsErr := rootfsFile.Close() if rootfsErr != nil { - telemetry.ReportError(ctx, fmt.Errorf("error closing rootfs file: %w", rootfsErr)) + telemetry.ReportError(ctx, "error closing rootfs file", rootfsErr) } else { telemetry.ReportEvent(ctx, "closed rootfs file") } diff --git a/packages/orchestrator/internal/template/build/rootfs.go b/packages/orchestrator/internal/template/build/rootfs.go index 6e2a57b78f..7f61414bb0 100644 --- a/packages/orchestrator/internal/template/build/rootfs.go +++ b/packages/orchestrator/internal/template/build/rootfs.go @@ -72,7 +72,7 @@ func (r *Rootfs) createExt4Filesystem(ctx context.Context, tracer trace.Tracer, defer func() { if e != nil { - telemetry.ReportCriticalError(childCtx, e) + telemetry.ReportCriticalError(childCtx, "failed to create ext4 filesystem", e) } }() diff --git a/packages/orchestrator/internal/template/build/template_builder.go b/packages/orchestrator/internal/template/build/template_builder.go index 326616399d..d63acaf35b 100644 --- a/packages/orchestrator/internal/template/build/template_builder.go +++ b/packages/orchestrator/internal/template/build/template_builder.go @@ -360,8 +360,7 @@ func (b *TemplateBuilder) uploadTemplate( removeErr := b.templateStorage.Remove(removeCtx, templateFiles.BuildId) if removeErr != nil { - b.logger.Error("error while removing build files", zap.Error(removeErr)) - telemetry.ReportError(ctx, removeErr) + telemetry.ReportError(ctx, "error while removing build files", removeErr) } } }() diff --git a/packages/orchestrator/internal/template/server/create_template.go b/packages/orchestrator/internal/template/server/create_template.go index 920d701844..bdb914f399 100644 --- a/packages/orchestrator/internal/template/server/create_template.go +++ b/packages/orchestrator/internal/template/server/create_template.go @@ -83,14 +83,14 @@ func (s *ServerStore) TemplateCreate(ctx context.Context, templateRequest *templ // Ideally we should wait in the CLI for the last log message time.Sleep(8 * time.Second) if err != nil { - s.reportBuildFailed(buildContext, s.buildLogger, template, err) + s.reportBuildFailed(buildContext, template, err) return } buildMetadata := &templatemanager.TemplateBuildMetadata{RootfsSizeKey: int32(template.RootfsSizeMB()), EnvdVersionKey: res.EnvdVersion} err = s.buildCache.SetSucceeded(template.BuildId, buildMetadata) if err != nil { - s.reportBuildFailed(buildContext, s.buildLogger, template, fmt.Errorf("error while setting build state to succeeded: %w", err)) + s.reportBuildFailed(buildContext, template, fmt.Errorf("error while setting build state to succeeded: %w", err)) return } @@ -100,13 +100,12 @@ func (s *ServerStore) TemplateCreate(ctx context.Context, templateRequest *templ return nil, nil } -func (s *ServerStore) reportBuildFailed(ctx context.Context, logger *zap.Logger, config *build.TemplateConfig, err error) { - telemetry.ReportCriticalError(ctx, err) +func (s *ServerStore) reportBuildFailed(ctx context.Context, config *build.TemplateConfig, err error) { + telemetry.ReportCriticalError(ctx, "error while building template", err) cacheErr := s.buildCache.SetFailed(config.BuildId) if cacheErr != nil { s.logger.Error("Error while setting build state to failed", zap.Error(err)) } - logger.Error("Error while building template", zap.Error(err)) telemetry.ReportEvent(ctx, "Environment built failed") } diff --git a/packages/shared/pkg/models/accesstoken.go b/packages/shared/pkg/models/accesstoken.go index 7c9bf42382..02e964e4ae 100644 --- a/packages/shared/pkg/models/accesstoken.go +++ b/packages/shared/pkg/models/accesstoken.go @@ -20,7 +20,7 @@ type AccessToken struct { // ID of the ent. ID uuid.UUID `json:"id,omitempty"` // AccessToken holds the value of the "access_token" field. - AccessToken string `json:"access_token,omitempty"` + AccessToken string `json:"-"` // AccessTokenHash holds the value of the "access_token_hash" field. AccessTokenHash string `json:"-"` // AccessTokenMask holds the value of the "access_token_mask" field. @@ -168,8 +168,7 @@ func (at *AccessToken) String() string { var builder strings.Builder builder.WriteString("AccessToken(") builder.WriteString(fmt.Sprintf("id=%v, ", at.ID)) - builder.WriteString("access_token=") - builder.WriteString(at.AccessToken) + builder.WriteString("access_token=") builder.WriteString(", ") builder.WriteString("access_token_hash=") builder.WriteString(", ") diff --git a/packages/shared/pkg/schema/access_token.go b/packages/shared/pkg/schema/access_token.go index bfdafe18a8..078377aee4 100644 --- a/packages/shared/pkg/schema/access_token.go +++ b/packages/shared/pkg/schema/access_token.go @@ -16,7 +16,7 @@ type AccessToken struct { func (AccessToken) Fields() []ent.Field { return []ent.Field{ field.UUID("id", uuid.UUID{}).Immutable().Unique().Annotations(entsql.Default("gen_random_uuid()")), - field.String("access_token").Unique().Immutable().SchemaType(map[string]string{dialect.Postgres: "text"}), + field.String("access_token").Unique().Immutable().Sensitive().SchemaType(map[string]string{dialect.Postgres: "text"}), field.String("access_token_hash").Immutable().Unique().Sensitive().SchemaType(map[string]string{dialect.Postgres: "text"}), field.String("access_token_mask").Immutable().SchemaType(map[string]string{dialect.Postgres: "text"}), field.String("name").SchemaType(map[string]string{dialect.Postgres: "text"}).Default("Unnamed Access Token"), diff --git a/packages/shared/pkg/telemetry/tracing.go b/packages/shared/pkg/telemetry/tracing.go index df782ee72b..04c609a995 100644 --- a/packages/shared/pkg/telemetry/tracing.go +++ b/packages/shared/pkg/telemetry/tracing.go @@ -8,6 +8,7 @@ import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" + "go.uber.org/zap" ) var OTELTracingPrint = os.Getenv("OTEL_TRACING_PRINT") != "false" @@ -72,49 +73,31 @@ func ReportEvent(ctx context.Context, name string, attrs ...attribute.KeyValue) ) } -func ReportCriticalError(ctx context.Context, err error, attrs ...attribute.KeyValue) { +func ReportCriticalError(ctx context.Context, message string, err error, attrs ...attribute.KeyValue) { span := trace.SpanFromContext(ctx) - if OTELTracingPrint { - var msg string + debugID := getDebugID(ctx) + zap.L().With(attributesToZapFields(attrs...)...).Error(message, zap.Stringp("debug_id", debugID), zap.Error(err)) - if len(attrs) == 0 { - msg = fmt.Sprintf("Critical error: %v\n", err) - } else { - msg = fmt.Sprintf("Critical error: %v - %#v\n", err, attrs) - } + errorAttrs := append(attrs, attribute.String("error.message", message)) - debugID := getDebugID(ctx) - fmt.Fprint(os.Stderr, debugFormat(debugID, msg)) - } - - span.RecordError(err, + span.RecordError(fmt.Errorf("%s: %w", message, err), trace.WithStackTrace(true), trace.WithAttributes( - attrs..., + errorAttrs..., ), ) - span.SetStatus(codes.Error, "critical error") + span.SetStatus(codes.Error, message) } -func ReportError(ctx context.Context, err error, attrs ...attribute.KeyValue) { +func ReportError(ctx context.Context, message string, err error, attrs ...attribute.KeyValue) { span := trace.SpanFromContext(ctx) - if OTELTracingPrint { - var msg string - - if len(attrs) == 0 { - msg = fmt.Sprintf("Error: %v\n", err) - } else { - msg = fmt.Sprintf("Error: %v - %#v\n", err, attrs) - } + debugID := getDebugID(ctx) + zap.L().With(attributesToZapFields(attrs...)...).Warn(message, zap.Stringp("debug_id", debugID), zap.Error(err)) - debugID := getDebugID(ctx) - fmt.Fprint(os.Stderr, debugFormat(debugID, msg)) - } - - span.RecordError(err, + span.RecordError(fmt.Errorf("%s: %w", message, err), trace.WithStackTrace(true), trace.WithAttributes( attrs..., @@ -127,6 +110,7 @@ func GetContextFromRemote(ctx context.Context, tracer trace.Tracer, name, spanID if traceIDErr != nil { ReportError( ctx, + traceIDErr.Error(), traceIDErr, attribute.String("trace.id", traceID), attribute.Int("trace.id.length", len(traceID)), @@ -137,6 +121,7 @@ func GetContextFromRemote(ctx context.Context, tracer trace.Tracer, name, spanID if spanIDErr != nil { ReportError( ctx, + spanIDErr.Error(), spanIDErr, attribute.String("span.id", spanID), attribute.Int("span.id.length", len(spanID)), @@ -157,3 +142,31 @@ func GetContextFromRemote(ctx context.Context, tracer trace.Tracer, name, spanID ), ) } + +func attributesToZapFields(attrs ...attribute.KeyValue) []zap.Field { + fields := make([]zap.Field, 0, len(attrs)) + for _, attr := range attrs { + key := string(attr.Key) + switch attr.Value.Type() { + case attribute.STRING: + fields = append(fields, zap.String(key, attr.Value.AsString())) + case attribute.INT64: + fields = append(fields, zap.Int64(key, attr.Value.AsInt64())) + case attribute.FLOAT64: + fields = append(fields, zap.Float64(key, attr.Value.AsFloat64())) + case attribute.BOOL: + fields = append(fields, zap.Bool(key, attr.Value.AsBool())) + case attribute.BOOLSLICE: + fields = append(fields, zap.Bools(key, attr.Value.AsBoolSlice())) + case attribute.INT64SLICE: + fields = append(fields, zap.Int64s(key, attr.Value.AsInt64Slice())) + case attribute.FLOAT64SLICE: + fields = append(fields, zap.Float64s(key, attr.Value.AsFloat64Slice())) + case attribute.STRINGSLICE: + fields = append(fields, zap.Strings(key, attr.Value.AsStringSlice())) + default: + fields = append(fields, zap.Any(key, attr.Value.AsInterface())) + } + } + return fields +}