feat service: introduce labels for logs/recorder (#104)
This commit is contained in:
@@ -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,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user