Skip to content

Save metrics to cloud to preserve metrics history #1940

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 6 commits into from
Mar 10, 2021
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
2 changes: 2 additions & 0 deletions pkg/operator/resources/job/batchapi/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,12 @@ func handleJobSubmissionError(jobKey spec.JobKey, jobErr error) {
}
}

// delete k8s job, queue and save batch metrics from prometheus to cloud
func deleteJobRuntimeResources(jobKey spec.JobKey) error {
err := errors.FirstError(
deleteK8sJob(jobKey),
deleteQueueByJobKeyIfExists(jobKey),
saveMetricsToCloud(jobKey),
)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/operator/resources/job/batchapi/job_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func getJobStatusFromJobState(jobState *job.State, k8sJob *kbatch.Job, pods []kc
}
}

if jobState.Status.IsCompleted() && jobState.EndTime != nil {
metrics, err := getBatchMetrics(jobKey, *jobState.EndTime)
if _, ok := jobState.LastUpdatedMap[_completedMetricsFileKey]; ok && jobState.Status.IsCompleted() {
metrics, err := readMetricsFromCloud(jobKey)
if err != nil {
return nil, err
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/operator/resources/job/batchapi/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package batchapi
import (
"context"
"fmt"
"path"
"time"

"github.com/cortexlabs/cortex/pkg/lib/errors"
Expand All @@ -32,6 +33,7 @@ import (

const (
_metricsRequestTimeoutSeconds = 10
_completedMetricsFileKey = "metrics.json"
)

func getBatchMetrics(jobKey spec.JobKey, t time.Time) (metrics.BatchMetrics, error) {
Expand Down Expand Up @@ -143,3 +145,28 @@ func queryPrometheusVec(promAPIv1 promv1.API, query string, t time.Time) (model.

return values, nil
}

func saveMetricsToCloud(jobKey spec.JobKey) error {
t := time.Now()
batchMetrics, err := getBatchMetrics(jobKey, t)
if err != nil {
return err
}

s3Key := path.Join(jobKey.Prefix(config.ClusterName()), _completedMetricsFileKey)
err = config.UploadJSONToBucket(batchMetrics, s3Key)
if err != nil {
return err
}
return nil
}

func readMetricsFromCloud(jobKey spec.JobKey) (metrics.BatchMetrics, error) {
s3Key := path.Join(jobKey.Prefix(config.ClusterName()), _completedMetricsFileKey)
batchMetrics := metrics.BatchMetrics{}
err := config.ReadJSONFromBucket(&batchMetrics, s3Key)
if err != nil {
return batchMetrics, err
}
return batchMetrics, nil
}