Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-05-09 16:41:22 +03:00
parent 9c8fbb2202
commit 62074965ee
5 changed files with 35 additions and 12 deletions

View File

@ -4,6 +4,17 @@ import "context"
type loggerKey struct{} type loggerKey struct{}
// MustContext returns logger from passed context or DefaultLogger if empty
func MustContext(ctx context.Context) Logger {
if ctx == nil {
return DefaultLogger
}
if l, ok := ctx.Value(loggerKey{}).(Logger); ok && l != nil {
return l
}
return DefaultLogger
}
// FromContext returns logger from passed context // FromContext returns logger from passed context
func FromContext(ctx context.Context) (Logger, bool) { func FromContext(ctx context.Context) (Logger, bool) {
if ctx == nil { if ctx == nil {

View File

@ -6,6 +6,8 @@ import (
"log/slog" "log/slog"
"os" "os"
"time" "time"
"go.unistack.org/micro/v3/meter"
) )
// Option func signature // Option func signature
@ -45,6 +47,8 @@ type Options struct {
Level Level Level Level
// TimeFunc used to obtain current time // TimeFunc used to obtain current time
TimeFunc func() time.Time TimeFunc func() time.Time
// Meter used to count logs for specific level
Meter meter.Meter
} }
// NewOptions creates new options struct // NewOptions creates new options struct
@ -58,6 +62,7 @@ func NewOptions(opts ...Option) Options {
ContextAttrFuncs: DefaultContextAttrFuncs, ContextAttrFuncs: DefaultContextAttrFuncs,
AddSource: true, AddSource: true,
TimeFunc: time.Now, TimeFunc: time.Now,
Meter: meter.DefaultMeter,
} }
WithMicroKeys()(&options) WithMicroKeys()(&options)

View File

@ -11,6 +11,7 @@ import (
"sync" "sync"
"go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/semconv"
"go.unistack.org/micro/v3/tracer" "go.unistack.org/micro/v3/tracer"
) )
@ -150,6 +151,7 @@ func (s *slogLogger) Init(opts ...logger.Option) error {
} }
func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interface{}) { func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
if !s.V(lvl) { if !s.V(lvl) {
return return
} }
@ -189,6 +191,7 @@ func (s *slogLogger) Log(ctx context.Context, lvl logger.Level, attrs ...interfa
} }
func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, attrs ...interface{}) { func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", lvl.String()).Inc()
if !s.V(lvl) { if !s.V(lvl) {
return return
} }
@ -228,6 +231,7 @@ func (s *slogLogger) Logf(ctx context.Context, lvl logger.Level, msg string, att
} }
func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) { func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.InfoLevel.String()).Inc()
if !s.V(logger.InfoLevel) { if !s.V(logger.InfoLevel) {
return return
} }
@ -249,6 +253,7 @@ func (s *slogLogger) Info(ctx context.Context, attrs ...interface{}) {
} }
func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}) { func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.InfoLevel.String()).Inc()
if !s.V(logger.InfoLevel) { if !s.V(logger.InfoLevel) {
return return
} }
@ -270,6 +275,7 @@ func (s *slogLogger) Infof(ctx context.Context, msg string, attrs ...interface{}
} }
func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) { func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.DebugLevel.String()).Inc()
if !s.V(logger.DebugLevel) { if !s.V(logger.DebugLevel) {
return return
} }
@ -291,6 +297,7 @@ func (s *slogLogger) Debug(ctx context.Context, attrs ...interface{}) {
} }
func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{}) { func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.DebugLevel.String()).Inc()
if !s.V(logger.DebugLevel) { if !s.V(logger.DebugLevel) {
return return
} }
@ -312,6 +319,7 @@ func (s *slogLogger) Debugf(ctx context.Context, msg string, attrs ...interface{
} }
func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) { func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.TraceLevel.String()).Inc()
if !s.V(logger.TraceLevel) { if !s.V(logger.TraceLevel) {
return return
} }
@ -333,6 +341,7 @@ func (s *slogLogger) Trace(ctx context.Context, attrs ...interface{}) {
} }
func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{}) { func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.TraceLevel.String()).Inc()
if !s.V(logger.TraceLevel) { if !s.V(logger.TraceLevel) {
return return
} }
@ -354,6 +363,7 @@ func (s *slogLogger) Tracef(ctx context.Context, msg string, attrs ...interface{
} }
func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) { func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.ErrorLevel.String()).Inc()
if !s.V(logger.ErrorLevel) { if !s.V(logger.ErrorLevel) {
return return
} }
@ -393,6 +403,7 @@ func (s *slogLogger) Error(ctx context.Context, attrs ...interface{}) {
} }
func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{}) { func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.ErrorLevel.String()).Inc()
if !s.V(logger.ErrorLevel) { if !s.V(logger.ErrorLevel) {
return return
} }
@ -432,6 +443,7 @@ func (s *slogLogger) Errorf(ctx context.Context, msg string, attrs ...interface{
} }
func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) { func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.FatalLevel.String()).Inc()
if !s.V(logger.FatalLevel) { if !s.V(logger.FatalLevel) {
return return
} }
@ -454,6 +466,7 @@ func (s *slogLogger) Fatal(ctx context.Context, attrs ...interface{}) {
} }
func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{}) { func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.FatalLevel.String()).Inc()
if !s.V(logger.FatalLevel) { if !s.V(logger.FatalLevel) {
return return
} }
@ -476,6 +489,7 @@ func (s *slogLogger) Fatalf(ctx context.Context, msg string, attrs ...interface{
} }
func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) { func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.WarnLevel.String()).Inc()
if !s.V(logger.WarnLevel) { if !s.V(logger.WarnLevel) {
return return
} }
@ -497,6 +511,7 @@ func (s *slogLogger) Warn(ctx context.Context, attrs ...interface{}) {
} }
func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}) { func (s *slogLogger) Warnf(ctx context.Context, msg string, attrs ...interface{}) {
s.opts.Meter.Counter(semconv.LoggerMessageTotal, "level", logger.WarnLevel.String()).Inc()
if !s.V(logger.WarnLevel) { if !s.V(logger.WarnLevel) {
return return
} }

View File

@ -2,8 +2,6 @@ package meter
import ( import (
"context" "context"
"go.unistack.org/micro/v3/logger"
) )
// Option powers the configuration for metrics implementations: // Option powers the configuration for metrics implementations:
@ -11,8 +9,6 @@ type Option func(*Options)
// Options for metrics implementations // Options for metrics implementations
type Options struct { type Options struct {
// Logger used for logging
Logger logger.Logger
// Context holds external options // Context holds external options
Context context.Context Context context.Context
// Name holds the meter name // Name holds the meter name
@ -39,7 +35,6 @@ func NewOptions(opt ...Option) Options {
Address: DefaultAddress, Address: DefaultAddress,
Path: DefaultPath, Path: DefaultPath,
Context: context.Background(), Context: context.Background(),
Logger: logger.DefaultLogger,
MetricPrefix: DefaultMetricPrefix, MetricPrefix: DefaultMetricPrefix,
LabelPrefix: DefaultLabelPrefix, LabelPrefix: DefaultLabelPrefix,
} }
@ -95,13 +90,6 @@ func TimingObjectives(value map[float64]float64) Option {
} }
*/ */
// Logger sets the logger
func Logger(l logger.Logger) Option {
return func(o *Options) {
o.Logger = l
}
}
// Labels sets the meter labels // Labels sets the meter labels
func Labels(ls ...string) Option { func Labels(ls ...string) Option {
return func(o *Options) { return func(o *Options) {

4
semconv/logger.go Normal file
View File

@ -0,0 +1,4 @@
package semconv
// LoggerMessageTotal specifies meter metric name for logger messages
var LoggerMessageTotal = "logger_message_total"