-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
helper function to execute jobs with tracing and sentry monitoring
- Loading branch information
1 parent
9388096
commit 8b2e5ba
Showing
4 changed files
with
108 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package jobs | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/checkmarble/marble-backend/tracing" | ||
"github.com/checkmarble/marble-backend/usecases" | ||
"github.com/checkmarble/marble-backend/utils" | ||
"github.com/getsentry/sentry-go" | ||
) | ||
|
||
func executeWithMonitoring( | ||
ctx context.Context, | ||
uc usecases.Usecases, | ||
config tracing.Configuration, | ||
jobName string, | ||
fn func(context.Context, usecases.Usecases) error, | ||
) error { | ||
telemetryRessources, err := tracing.Init(config) | ||
if err != nil { | ||
return fmt.Errorf("error initializing tracing: %w", err) | ||
} | ||
ctx = utils.StoreOpenTelemetryTracerInContext(ctx, telemetryRessources.Tracer) | ||
|
||
logger := utils.LoggerFromContext(ctx) | ||
logger.InfoContext(ctx, fmt.Sprintf("Start job %s", jobName)) | ||
|
||
checkinId := sentry.CaptureCheckIn( | ||
&sentry.CheckIn{ | ||
MonitorSlug: jobName, | ||
Status: sentry.CheckInStatusInProgress, | ||
}, | ||
nil, | ||
) | ||
|
||
err = fn(ctx, uc) | ||
if err != nil { | ||
// Known issue where Cloud Run will sometimes fail to create the unix socket to connect to CloudSQL. In this case, we don't log the error in Sentry. | ||
if strings.Contains(err.Error(), "failed to connect to `host=/cloudsql/") { | ||
logger.WarnContext(ctx, "Failed to create unix socket to connect to CloudSQL. Wait for the next execution of the job.") | ||
return nil | ||
} | ||
sentry.CaptureCheckIn( | ||
&sentry.CheckIn{ | ||
ID: *checkinId, | ||
MonitorSlug: jobName, | ||
Status: sentry.CheckInStatusError, | ||
}, | ||
nil, | ||
) | ||
if hub := sentry.GetHubFromContext(ctx); hub != nil { | ||
hub.CaptureException(err) | ||
} else { | ||
sentry.CaptureException(err) | ||
} | ||
return fmt.Errorf("error executing job %s: %w", jobName, err) | ||
} | ||
|
||
sentry.CaptureCheckIn( | ||
&sentry.CheckIn{ | ||
ID: *checkinId, | ||
MonitorSlug: jobName, | ||
Status: sentry.CheckInStatusOK, | ||
}, | ||
nil, | ||
) | ||
|
||
logger.InfoContext(ctx, fmt.Sprintf("Done executing job %s", jobName)) | ||
return nil | ||
} |
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