Skip to content

Commit

Permalink
fix: wire up cron metrics (#3401)
Browse files Browse the repository at this point in the history
There were a couple of metrics that no longer made sense, and we don't
have the deployment key available in the cron module, which might be a
problem.
  • Loading branch information
alecthomas authored Nov 15, 2024
1 parent 52d9358 commit dc6ba23
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
17 changes: 1 addition & 16 deletions backend/cron/observability/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const (
cronMeterName = "ftl.cron"
cronJobFullNameAttribute = "ftl.cron.job.full_name"

cronJobKilledStatus = "killed"
cronJobFailedStartStatus = "failed_start"

deploymentMeterName = "ftl.deployments.cron"
)

Expand Down Expand Up @@ -84,24 +81,12 @@ func (m *CronMetrics) JobSuccess(ctx context.Context, job model.CronJob) {
m.jobCompleted(ctx, job, observability.SuccessStatus)
}

func (m *CronMetrics) JobKilled(ctx context.Context, job model.CronJob) {
m.jobCompleted(ctx, job, cronJobKilledStatus)
}

func (m *CronMetrics) JobFailedStart(ctx context.Context, job model.CronJob) {
completionAttributes := cronAttributes(job, optional.Some(cronJobFailedStartStatus))

elapsed := timeSinceMS(job.NextExecution)
m.jobLatency.Record(ctx, elapsed, completionAttributes)
m.jobsCompleted.Add(ctx, 1, completionAttributes)
}

func (m *CronMetrics) JobFailed(ctx context.Context, job model.CronJob) {
m.jobCompleted(ctx, job, observability.FailureStatus)
}

func (m *CronMetrics) jobCompleted(ctx context.Context, job model.CronJob, status string) {
elapsed := timeSinceMS(job.NextExecution)
elapsed := timeSinceMS(job.StartTime)

m.jobsActive.Add(ctx, -1, cronAttributes(job, optional.None[string]()))

Expand Down
14 changes: 14 additions & 0 deletions backend/cron/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import (
"github.com/jpillora/backoff"
"golang.org/x/sync/errgroup"

"github.com/TBD54566975/ftl/backend/cron/observability"
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
schemapb "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/schema"
"github.com/TBD54566975/ftl/internal/cron"
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/model"
"github.com/TBD54566975/ftl/internal/rpc"
"github.com/TBD54566975/ftl/internal/schema"
"github.com/TBD54566975/ftl/internal/slices"
Expand Down Expand Up @@ -108,8 +110,20 @@ func run(ctx context.Context, verbClient CallClient, changes chan *ftlv1.PullSch
cronQueue[0] = job
orderQueue(cronQueue)

cronModel := model.CronJob{
// TODO: We don't have the runner key available here.
Key: model.NewCronJobKey(job.module, job.verb.Name),
Verb: schema.Ref{Module: job.module, Name: job.verb.Name},
Schedule: job.pattern.String(),
StartTime: time.Now(),
NextExecution: job.next,
}
observability.Cron.JobStarted(ctx, cronModel)
if err := callCronJob(ctx, verbClient, job); err != nil {
observability.Cron.JobFailed(ctx, cronModel)
logger.Errorf(err, "Failed to execute cron job")
} else {
observability.Cron.JobSuccess(ctx, cronModel)
}
}
}
Expand Down

0 comments on commit dc6ba23

Please sign in to comment.