Skip to content

Commit

Permalink
Merge pull request #44 from CoderPoet/fix/log-message
Browse files Browse the repository at this point in the history
fix(logger): not record log message in span
  • Loading branch information
CoderPoet authored Jan 17, 2024
2 parents 7d3e01e + 55d7710 commit e7127e1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 31 deletions.
16 changes: 1 addition & 15 deletions logging/logrus/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"

"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)
Expand All @@ -28,12 +27,6 @@ const (
traceIDKey = "trace_id"
spanIDKey = "span_id"
traceFlagsKey = "trace_flags"
logEventKey = "log"
)

var (
logSeverityTextKey = attribute.Key("otel.log.severity.text")
logMessageKey = attribute.Key("otel.log.message")
)

var _ logrus.Hook = (*TraceHook)(nil)
Expand Down Expand Up @@ -78,16 +71,9 @@ func (h *TraceHook) Fire(entry *logrus.Entry) error {
return nil
}

// attach log to span event attributes
attrs := []attribute.KeyValue{
logMessageKey.String(entry.Message),
logSeverityTextKey.String(OtelSeverityText(entry.Level)),
}
span.AddEvent(logEventKey, trace.WithAttributes(attrs...))

// set span status
if entry.Level <= h.cfg.errorSpanLevel {
span.SetStatus(codes.Error, entry.Message)
span.SetStatus(codes.Error, "")
span.RecordError(errors.New(entry.Message), trace.WithStackTrace(h.cfg.recordStackTraceInSpan))
}

Expand Down
19 changes: 3 additions & 16 deletions logging/zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/cloudwego/hertz/pkg/common/hlog"
hertzzap "github.com/hertz-contrib/logger/zap"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
Expand All @@ -37,14 +36,9 @@ const (
traceIDKey = "trace_id"
spanIDKey = "span_id"
traceFlagsKey = "trace_flags"
logEventKey = "log"
)

var (
logSeverityTextKey = attribute.Key("otel.log.severity.text")
logMessageKey = attribute.Key("otel.log.message")
extraKeys = []hertzzap.ExtraKey{traceIDKey, spanIDKey, traceFlagsKey}
)
var extraKeys = []hertzzap.ExtraKey{traceIDKey, spanIDKey, traceFlagsKey}

func NewLogger(opts ...Option) *Logger {
config := defaultConfig()
Expand Down Expand Up @@ -96,17 +90,10 @@ func (l *Logger) CtxLogf(level hlog.Level, ctx context.Context, format string, k
return
}

msg := getMessage(format, kvs)

attrs := []attribute.KeyValue{
logMessageKey.String(msg),
logSeverityTextKey.String(OtelSeverityText(zlevel)),
}
span.AddEvent(logEventKey, trace.WithAttributes(attrs...))

// set span status
if zlevel >= l.config.traceConfig.errorSpanLevel {
span.SetStatus(codes.Error, msg)
msg := getMessage(format, kvs)
span.SetStatus(codes.Error, "")
span.RecordError(errors.New(msg), trace.WithStackTrace(l.config.traceConfig.recordStackTraceInSpan))
}
}
Expand Down

0 comments on commit e7127e1

Please sign in to comment.