feat service: introduce labels for logs/recorder (#104)

This commit is contained in:
Denis Galeev
2026-06-13 08:05:14 +03:00
committed by GitHub
parent eaeac2763e
commit 9f610fd163
6 changed files with 81 additions and 10 deletions
+13
View File
@@ -43,6 +43,7 @@ type options struct {
observer observer.Observer
observerPeriod time.Duration
logger logger.Logger
labels map[string]string
}
// Option is a functional option for configuring a service.
@@ -113,6 +114,14 @@ func ObserverPeriodOption(period time.Duration) Option {
}
}
// LabelsOption sets the static labels attached to the service's records
// and logs.
func LabelsOption(labels map[string]string) Option {
return func(opts *options) {
opts.labels = labels
}
}
// LoggerOption sets the logger for the service. If not provided, a no-op
// logger is used.
func LoggerOption(logger logger.Logger) Option {
@@ -245,6 +254,10 @@ func (s *defaultService) Serve() error {
sid := xid.New().String()
ctx = xctx.ContextWithSid(ctx, xctx.Sid(sid))
if len(s.options.labels) > 0 {
ctx = xctx.ContextWithLabels(ctx, s.options.labels)
}
log := s.options.logger.WithFields(map[string]any{
"sid": sid,
})