8000 fix: handle last alerted null case + some logging/cleanup improvements by abelanger5 · Pull Request #468 · hatchet-dev/hatchet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: handle last alerted null case + some logging/cleanup improvements #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api/v1/server/handlers/workflows/get_step_run_diff.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package workflows

import (
"errors"
"fmt"

"github.com/labstack/echo/v4"

"github.com/hatchet-dev/hatchet/api/v1/server/oas/apierrors"
"github.com/hatchet-dev/hatchet/api/v1/server/oas/gen"
"github.com/hatchet-dev/hatchet/internal/integrations/vcs/vcsutils"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
Expand All @@ -16,6 +18,12 @@ func (t *WorkflowService) StepRunGetDiff(ctx echo.Context, request gen.StepRunGe
diffs, originalValues, err := vcsutils.GetStepRunOverrideDiffs(t.config.APIRepository.StepRun(), stepRun)

if err != nil {
if errors.Is(err, vcsutils.ErrNoInput) {
return gen.StepRunGetDiff404JSONResponse(
apierrors.NewAPIErrors("step run does not have an input"),
), nil
}

return nil, fmt.Errorf("could not get diffs: %s", err)
}

Expand Down
47 changes: 46 additions & 1 deletion api/v1/server/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"

"github.com/hatchet-dev/hatchet/api/v1/server/authn"
"github.com/hatchet-dev/hatchet/api/v1/server/authz"
Expand Down Expand Up @@ -230,9 +231,53 @@ func (t *APIServer) Run() (func() error, error) {
return nil, err
}

loggerMiddleware := middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogURI: true,
LogStatus: true,
LogError: true,
LogLatency: true,
LogRemoteIP: true,
LogHost: true,
LogMethod: true,
LogURIPath: true,
LogUserAgent: true,
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
statusCode := v.Status

// note that the status code is not set yet as it gets picked up by the global err handler
// see here: https://github.com/labstack/echo/issues/2310#issuecomment-1288196898
if v.Error != nil {
statusCode = 500
}

var e *zerolog.Event

switch {
case statusCode >= 500:
e = t.config.Logger.Error().Err(v.Error)
case statusCode >= 400:
e = t.config.Logger.Warn()
default:
e = t.config.Logger.Info()
}

e.
Dur("latency", v.Latency).< 8000 /td>
Int("status", statusCode).
Str("method", v.Method).
Str("uri", v.URI).
Str("user_agent", v.UserAgent).
Str("remote_ip", v.RemoteIP).
Str("host", v.Host).
Msg("API")

return nil
},
})

// register echo middleware
e.Use(
middleware.Logger(),
loggerMiddleware,
middleware.Recover(),
allHatchetMiddleware,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function StepRunPlayground({

const stepRunDiffQuery = useQuery({
...queries.stepRuns.getDiff(stepRun?.metadata.id || ''),
enabled: !!stepRun,
enabled: !!stepRun && !!stepRun.input,
refetchInterval: () => {
if (stepRun?.status === StepRunStatus.RUNNING) {
return 1000;
Expand Down
9 changes: 8 additions & 1 deletion internal/integrations/alerting/alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ func (t *TenantAlertManager) HandleAlert(tenantId string) error {
return err
}

if lastAlertedAt.IsZero() || time.Since(lastAlertedAt) > maxFrequency {
isZero := lastAlertedAt.IsZero()

if isZero || time.Since(lastAlertedAt) > maxFrequency {
// update the lastAlertedAt
now := time.Now().UTC()

// if we're in the zero state, we don't want to alert since the very beginning of the interval
if isZero {
lastAlertedAt = now.Add(-1 * maxFrequency)
}

err = t.repo.TenantAlertingSettings().UpdateTenantAlertingSettings(ctx, tenantId, &repository.UpdateTenantAlertingSettingsOpts{
LastAlertedAt: &now,
})
Expand Down
12 changes: 11 additions & 1 deletion internal/integrations/vcs/vcsutils/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
)

var ErrNoInput = errors.New("no input found")

// GetStepRunOverrideDiffs returns a map of the override keys to the override values which have changed
// between the first step run and the latest step run.
func GetStepRunOverrideDiffs(repo repository.StepRunAPIRepository, stepRun *db.StepRunModel) (diffs map[string]string, original map[string]string, err error) {
Expand All @@ -32,12 +34,20 @@ func GetStepRunOverrideDiffs(repo repository.StepRunAPIRepository, stepRun *db.S
firstInput, err := getStepRunInput(archivedResult)

if err != nil {
if errors.Is(err, ErrNoInput) {
return nil, nil, ErrNoInput
}

return nil, nil, fmt.Errorf("could not get input from archived result: %w", err)
}

secondInput, err := getStepRunInput(stepRun)

if err != nil {
if errors.Is(err, ErrNoInput) {
return nil, nil, ErrNoInput
}

return nil, nil, fmt.Errorf("could not get input from step run: %w", err)
}

Expand Down Expand Up @@ -87,7 +97,7 @@ func getStepRunInput(in inputtable) (*datautils.StepRunData, error) {
input, ok := in.Input()

if !ok {
return nil, fmt.Errorf("could not get input from inputtable")
return nil, ErrNoInput
}

data := &datautils.StepRunData{}
Expand Down
8 changes: 4 additions & 4 deletions internal/repository/prisma/dbsqlc/tickers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,6 @@ WITH active_tenant_alerts AS (
alerts.*
FROM
"TenantAlertingSettings" as alerts
-- only return alerts which have a slack webhook enabled
JOIN
"SlackAppWebhook" as webhooks ON webhooks."tenantId" = alerts."tenantId"
WHERE
"lastAlertedAt" IS NULL OR
"lastAlertedAt" <= NOW() - convert_duration_to_interval(alerts."maxFrequency")
Expand All @@ -242,7 +239,10 @@ failed_run_count_by_tenant AS (
WHERE
"status" = 'FAILED'
AND (
"lastAlertedAt" IS NULL OR
(
"lastAlertedAt" IS NULL AND
workflowRun."finishedAt" >= NOW() - convert_duration_to_interval(active_tenant_alerts."maxFrequency")
) OR
workflowRun."finishedAt" >= "lastAlertedAt"
)
GROUP BY workflowRun."tenantId"
Expand Down
8 changes: 4 additions & 4 deletions internal/repository/prisma/dbsqlc/tickers.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
0