Skip to content

Commit

Permalink
chore: add context.WithTrace/Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Dec 27, 2023
1 parent d431a85 commit 9ad60d1
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func NewContext(basectx gocontext.Context, opts ...ContextOptions) Context {
type Context struct {
gocontext.Context
logger logger.Logger
debug *bool
trace *bool
isDebugFn func(Context) bool
isTraceFn func(Context) bool
tracer trace.Tracer
Expand All @@ -96,6 +98,32 @@ func (c Context) GetTracer() trace.Tracer {
return c.tracer
}

func (c Context) WithDebug() Context {
t := true
c.debug = &t
c.logger.SetLogLevel(1)
return c
}

func (c Context) WithTrace() Context {
t := true
c.trace = &t
c.logger.SetLogLevel(2)
return c
}

func (c Context) Clone() Context {
return Context{
Context: c.Context,
isDebugFn: c.isDebugFn,
trace: c.trace,
debug: c.debug,
logger: c.logger,
isTraceFn: c.isTraceFn,
tracer: c.tracer,
}
}

func (c Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFunc) {
ctx, cancelFunc := gocontext.WithTimeout(c, timeout)
return Context{
Expand All @@ -108,22 +136,22 @@ func (c Context) WithTimeout(timeout time.Duration) (Context, gocontext.CancelFu
}

func (c Context) IsDebug() bool {
return c.isDebugFn(c)
return (c.debug != nil && *c.debug) || c.isDebugFn(c)
}

func (c Context) IsTrace() bool {
return c.isTraceFn(c)
return (c.trace != nil && *c.trace) || c.isTraceFn(c)
}

func (c Context) Debugf(format string, args ...interface{}) {
if c.isDebugFn(c) {
if c.IsDebug() {
c.GetSpan().AddEvent(fmt.Sprintf(format, args...), trace.WithAttributes(attribute.String("level", "debug")))
c.logger.Debugf(format, args...)
}
}

func (c Context) Tracef(format string, args ...interface{}) {
if c.isTraceFn(c) {
if c.IsTrace() {
c.GetSpan().AddEvent(fmt.Sprintf(format, args...), trace.WithAttributes(attribute.String("level", "trace")))
c.logger.Tracef(format, args...)
}
Expand All @@ -132,11 +160,13 @@ func (c Context) Tracef(format string, args ...interface{}) {
func (c Context) Error(err error) {
c.GetSpan().RecordError(err)
c.GetSpan().SetStatus(codes.Error, err.Error())
c.logger.Errorf(err.Error())
}

func (c Context) Errorf(err error, format string, args ...interface{}) {
c.GetSpan().RecordError(err)
c.GetSpan().SetStatus(codes.Error, fmt.Sprintf(format, args...))
c.logger.Errorf(fmt.Sprintf(format, args...))
}

func (c Context) GetSpan() trace.Span {
Expand Down

0 comments on commit 9ad60d1

Please sign in to comment.