generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce cron job metrics (#2256)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
10f42d7
commit 005f605
Showing
7 changed files
with
144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package observability | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/alecthomas/types/optional" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/metric" | ||
|
||
"github.com/TBD54566975/ftl/internal/model" | ||
"github.com/TBD54566975/ftl/internal/observability" | ||
) | ||
|
||
const ( | ||
cronMeterName = "ftl.cron" | ||
cronJobFullNameAttribute = "ftl.cron.job.full_name" | ||
|
||
cronJobKilledStatus = "killed" | ||
cronJobFailedStartStatus = "failed_start" | ||
) | ||
|
||
type CronMetrics struct { | ||
jobsActive metric.Int64UpDownCounter | ||
jobsCompleted metric.Int64Counter | ||
jobLatency metric.Int64Histogram | ||
} | ||
|
||
func initCronMetrics() (*CronMetrics, error) { | ||
result := &CronMetrics{} | ||
|
||
var errs error | ||
var err error | ||
|
||
meter := otel.Meter(deploymentMeterName) | ||
|
||
counter := fmt.Sprintf("%s.jobs.completed", cronMeterName) | ||
if result.jobsCompleted, err = meter.Int64Counter( | ||
counter, | ||
metric.WithDescription("the number of cron jobs completed; successful or otherwise")); err != nil { | ||
result.jobsCompleted, errs = handleInt64CounterError(counter, err, errs) | ||
} | ||
|
||
counter = fmt.Sprintf("%s.jobs.active", cronMeterName) | ||
if result.jobsActive, err = meter.Int64UpDownCounter( | ||
counter, | ||
metric.WithDescription("the number of actively executing cron jobs")); err != nil { | ||
result.jobsActive, errs = handleInt64UpDownCounterError(counter, err, errs) | ||
} | ||
|
||
counter = fmt.Sprintf("%s.job.latency", cronMeterName) | ||
if result.jobLatency, err = meter.Int64Histogram( | ||
counter, | ||
metric.WithDescription("the latency between the scheduled execution time of a cron job"), | ||
metric.WithUnit("ms")); err != nil { | ||
result.jobLatency, errs = handleInt64HistogramCounterError(counter, err, errs) | ||
} | ||
|
||
return result, errs | ||
} | ||
|
||
func (m *CronMetrics) JobStarted(ctx context.Context, job model.CronJob) { | ||
m.jobsActive.Add(ctx, 1, cronAttributes(job, optional.None[string]())) | ||
} | ||
|
||
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) | ||
|
||
m.jobsActive.Add(ctx, -1, cronAttributes(job, optional.None[string]())) | ||
|
||
completionAttributes := cronAttributes(job, optional.Some(status)) | ||
m.jobLatency.Record(ctx, elapsed, completionAttributes) | ||
m.jobsCompleted.Add(ctx, 1, completionAttributes) | ||
} | ||
|
||
func cronAttributes(job model.CronJob, maybeStatus optional.Option[string]) metric.MeasurementOption { | ||
attributes := []attribute.KeyValue{ | ||
attribute.String(observability.ModuleNameAttribute, job.Key.Payload.Module), | ||
attribute.String(cronJobFullNameAttribute, job.Key.String()), | ||
attribute.String(observability.RunnerDeploymentKeyAttribute, job.DeploymentKey.String()), | ||
} | ||
if status, ok := maybeStatus.Get(); ok { | ||
attributes = append(attributes, attribute.String(observability.OutcomeStatusNameAttribute, status)) | ||
} | ||
return metric.WithAttributes(attributes...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
package observability | ||
|
||
import "go.opentelemetry.io/otel/attribute" | ||
|
||
const ( | ||
ModuleNameAttribute = "ftl.module.name" | ||
StatusSucceededAttribute = "ftl.status.succeeded" | ||
OutcomeStatusNameAttribute = "ftl.outcome.status" | ||
RunnerDeploymentKeyAttribute = "ftl.deployment.key" | ||
|
||
SuccessStatus = "success" | ||
FailureStatus = "failure" | ||
) | ||
|
||
func SuccessOrFailureStatusAttr(succeeded bool) attribute.KeyValue { | ||
if succeeded { | ||
return attribute.String(OutcomeStatusNameAttribute, SuccessStatus) | ||
} | ||
return attribute.String(OutcomeStatusNameAttribute, FailureStatus) | ||
} |