Health Checks
Overview
Section titled “Overview”samber/do v2 does not expose health-check hooks in InjectorOpts. do-auditlog wraps injector.HealthCheckWithContext() to record EventTypeHealthCheck events per service.
// Instead of: err := injector.HealthCheckWithContext(ctx)// Use the plugin wrapper:err := plugin.RecordHealthCheckWithContext(ctx, injector)if err != nil { log.Printf("health checks failed: %v", err)}
report := plugin.Report()if !report.HealthCheckSucceeded { for _, svc := range report.UnhealthyServices() { if svc.HealthCheckError != nil { log.Printf("unhealthy: %s — %s", svc.ServiceName, *svc.HealthCheckError) } }}How It Works
Section titled “How It Works”- The plugin calls
injector.HealthCheckWithContext(ctx)internally. - For each service that implements
do.Healthcheckerordo.HealthcheckerWithContext, aPhaseAfterhealth check event is recorded. - The event captures the service name, scope, and any error from the health check.
ServiceInfo.HealthCheckCountis incremented per check.ServiceInfo.IsHealthcheckeris set totrueviado.ExplainInjectorat report time.
Important Details
Section titled “Important Details”- PhaseAfter only: There is no interception point before the bulk health check runs, so there are no
PhaseBeforeevents. - No per-service timing: The bulk
HealthCheckWithContext()API does not provide per-service duration.DurationMsisnilfor health check events. - HealthCheckSucceeded is
falsewhen no health checks ran: The report requires at least one health-checked service to reporttrue.
Disabled Mode
Section titled “Disabled Mode”When the plugin is disabled, RecordHealthCheck delegates directly to the injector without recording events:
plugin, _ := auditlog.New(auditlog.Config{ Enabled: false,})
// Still works — delegates to injector.HealthCheckWithContext(ctx)err := plugin.RecordHealthCheckWithContext(ctx, injector)Scope Resolution
Section titled “Scope Resolution”The plugin resolves scope metadata from its internal serviceRecord map:
// Plugin.ResolveServiceScope resolves scope metadata by service name// Handles both *do.RootScope and *do.ScopescopeID, scopeName, found := plugin.ResolveServiceScope(scope, "*main.Database")