You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: export BaseMetrics for forward-compatible custom implementations (#2)
* feat: export BaseMetrics for forward-compatible custom implementations
Replace unexported noopMetrics with exported BaseMetrics. Custom
Metrics implementations embed BaseMetrics so that new methods added
to the interface get safe no-op defaults. Remove redundant NoopMetrics
var — use &BaseMetrics{} directly.
* fix: use BaseMetrics{} value type to avoid allocation, guard nil WithMetrics
funcRunWorker(ctxcontext.Context, w *Worker, opts ...RunOption)
@@ -478,10 +486,93 @@ done
478
486
</p>
479
487
</details>
480
488
489
+
<aname="BaseMetrics"></a>
490
+
## type [BaseMetrics](<https://github.com/go-coldbrew/workers/blob/main/metrics.go#L43>)
491
+
492
+
BaseMetrics provides no\-op implementations of all Metrics methods. Embed it in custom Metrics implementations so that new methods added to the Metrics interface in future versions get safe no\-op defaults instead of breaking your build:
## type [Metrics](<https://github.com/go-coldbrew/workers/blob/main/metrics.go#L20-L28>)
483
574
484
-
Metrics collects worker lifecycle metrics. Implement this interface to provide custom metrics \(e.g., Datadog, StatsD\). Use NoopMetrics to disable metrics, or NewPrometheusMetrics for the built\-in Prometheus implementation.
575
+
Metrics collects worker lifecycle metrics. Implement this interface to provide custom metrics \(e.g., Datadog, StatsD\). Use BaseMetrics\{\} to disable metrics, or NewPrometheusMetricsfor the built\-in Prometheus implementation.
485
576
486
577
```go
487
578
type Metrics interface {
@@ -495,14 +586,8 @@ type Metrics interface {
495
586
}
496
587
```
497
588
498
-
<aname="NoopMetrics"></a>NoopMetrics is a no\-op implementation of Metrics. Used as the default when no metrics are configured via WithMetrics.
NewPrometheusMetrics creates a Metrics implementation backed by Prometheus. The namespace is prepended to all metric names \(e.g., "myapp" → "myapp\_worker\_started\_total"\). Metrics are auto\-registered with the defaultPrometheus registry. Safe to call multiple times with the same namespace — returns the cached instance. The cache is process\-global; use a small number of static namespaces \(not per\-request/tenant values\).
WithMetrics sets the metrics implementation for all workers started by Run. Workers inherit this unless they override via Worker.WithMetrics. If not set, NoopMetrics is used.
614
+
WithMetrics sets the metrics implementation for all workers started by Run. Workers inherit this unless they override via Worker.WithMetrics. If not set, BaseMetrics\{\} is used.
@@ -741,7 +826,7 @@ func (w *Worker) WithTimeout(d time.Duration) *Worker
741
826
WithTimeout sets the maximum time to wait for the worker to stop during graceful shutdown. Suture default is 10 seconds.
742
827
743
828
<a name="WorkerContext"></a>
744
-
## type WorkerContext
829
+
## type [WorkerContext](<https://github.com/go-coldbrew/workers/blob/main/worker.go#L52-L66>)
745
830
746
831
WorkerContext extends context.Context with worker metadata and dynamic child worker management. The framework creates these — users never need to implement this interface.
0 commit comments