Documentation
¶
Overview ¶
Package service provides a standardized framework for building Go services with Sentry integration, structured logging, graceful shutdown, and signal handling.
The framework handles cross-cutting concerns including:
- Command-line argument parsing via github.com/bborbe/argument
- Sentry error reporting with configurable exclusions
- Concurrent function execution with proper error handling
- Signal-based context cancellation
- Panic recovery and logging
Example usage:
type app struct {
SentryDSN string `required:"true" arg:"sentry-dsn" env:"SENTRY_DSN"`
}
func (a *app) Run(ctx context.Context, sentryClient libsentry.Client) error {
return service.Run(ctx, a.createServer(), a.createWorker())
}
func main() {
app := &app{}
os.Exit(service.Main(context.Background(), app, &app.SentryDSN, nil))
}
Index ¶
- func FilterErrors(fn run.Func, filteredErrors ...error) run.Func
- func Main(ctx context.Context, app Application, sentryDSN *string, sentryProxy *string, ...) int
- func MainCmd(ctx context.Context, app run.Runnable) int
- func Run(ctx context.Context, funcs ...run.Func) error
- type Application
- type Options
- type OptionsFn
- type Service
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FilterErrors ¶
FilterErrors wraps a run.Func to suppress specified errors, returning nil instead of the error if it matches any of the provided filteredErrors using errors.Is. This is useful for filtering out expected errors like context.Canceled during graceful shutdown.
func Main ¶
func Main( ctx context.Context, app Application, sentryDSN *string, sentryProxy *string, fns ...OptionsFn, ) int
Main initializes and runs the service application with Sentry integration. It handles argument parsing, timezone configuration (UTC), Sentry setup, signal handling, and graceful shutdown. Returns an exit code: 0 for success, 1 for runtime error, 2 for Sentry setup failure, 3 for missing Sentry DSN, 4 for argument parsing failure.
func MainCmd ¶
MainCmd initializes and runs a command-line application without Sentry integration. Unlike Main, this function is designed for CLI tools that do not require error reporting to Sentry and uses reduced logging verbosity (V(3) instead of V(0)). Returns an exit code: 0 for success, 1 for runtime error, 4 for argument parsing failure.
func Run ¶
Run executes multiple functions concurrently with automatic error logging, panic recovery, and context.Canceled error filtering. Each function is wrapped with logging, panic recovery, and error filtering middleware. Returns on first function completion using the CancelOnFirstFinishWait strategy, which cancels all other functions when any one completes.
Types ¶
type Application ¶
Application defines the contract for services that can be executed with Sentry integration. Implementations receive a configured Sentry client for error reporting and should implement the Run method to contain the application's business logic.
type Options ¶
type Options struct {
ExcludeErrors sentry.ExcludeErrors
}
Options configures behavior for service execution, particularly Sentry error reporting. It allows customization of which errors should be excluded from Sentry reports.
func NewOptions ¶
NewOptions creates a new Options instance with sensible defaults and applies the given functional options. By default, context.Canceled and context.DeadlineExceeded errors are excluded from Sentry reporting as they represent expected shutdown conditions.
type OptionsFn ¶
type OptionsFn func(option *Options)
OptionsFn is a functional option pattern function for configuring Options. It allows callers to customize service behavior by modifying the Options struct.
type Service ¶
Service wraps application execution with Sentry error reporting and structured logging. It provides a layer between the main entry point and the application logic, handling error capture and reporting to Sentry automatically.
func NewService ¶
func NewService( sentryClient libsentry.Client, app Application, ) Service
NewService creates a new Service instance that wraps the given application with Sentry integration. The sentryClient will be used for error reporting, and the app will receive it for use in business logic.