Skip to content

Commit

Permalink
Merge pull request #22 from rogerogers/fix/zap-status
Browse files Browse the repository at this point in the history
fix(logging/zap): fix span status error
  • Loading branch information
GuangmingLuo committed Aug 21, 2023
2 parents d9e483e + 07ce9f8 commit 6a9c6ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion logging/zap/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (l *Logger) CtxLogf(level hlog.Level, ctx context.Context, format string, k
span.AddEvent(logEventKey, trace.WithAttributes(attrs...))

// set span status
if zlevel <= l.config.traceConfig.errorSpanLevel {
if zlevel >= l.config.traceConfig.errorSpanLevel {
span.SetStatus(codes.Error, msg)
span.RecordError(errors.New(msg), trace.WithStackTrace(l.config.traceConfig.recordStackTraceInSpan))
}
Expand Down
13 changes: 9 additions & 4 deletions logging/zap/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -110,18 +111,22 @@ func TestLogger(t *testing.T) {

span.End()

ctx, child := tracer.Start(ctx, "child")

hlog.CtxWarnf(ctx, "foo %s", "bar")
ctx, child1 := tracer.Start(ctx, "child1")

hlog.CtxTracef(ctx, "trace %s", "this is a trace log")
hlog.CtxDebugf(ctx, "debug %s", "this is a debug log")
hlog.CtxInfof(ctx, "info %s", "this is a info log")

child1.End()
assert.Equal(t, codes.Unset, child1.(sdktrace.ReadOnlySpan).Status().Code)

ctx, child2 := tracer.Start(ctx, "child2")
hlog.CtxNoticef(ctx, "notice %s", "this is a notice log")
hlog.CtxWarnf(ctx, "warn %s", "this is a warn log")
hlog.CtxErrorf(ctx, "error %s", "this is a error log")

child.End()
child2.End()
assert.Equal(t, codes.Error, child2.(sdktrace.ReadOnlySpan).Status().Code)

_, errSpan := tracer.Start(ctx, "error")

Expand Down

0 comments on commit 6a9c6ca

Please sign in to comment.