-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from LerianStudio/feature/MZ-625
Feature/MZ-625
- Loading branch information
Showing
86 changed files
with
1,656 additions
and
415 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
"github.com/LerianStudio/midaz/common/mlog" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type customContextKey string | ||
|
||
var CustomContextKey = customContextKey("custom_context") | ||
|
||
type CustomContextKeyValue struct { | ||
Tracer trace.Tracer | ||
Logger mlog.Logger | ||
} | ||
|
||
// NewLoggerFromContext extract the Logger from "logger" value inside context | ||
// | ||
//nolint:ireturn | ||
func NewLoggerFromContext(ctx context.Context) mlog.Logger { | ||
if customContext, ok := ctx.Value(CustomContextKey).(*CustomContextKeyValue); ok && | ||
customContext.Logger != nil { | ||
return customContext.Logger | ||
} | ||
|
||
return &mlog.NoneLogger{} | ||
} | ||
|
||
// ContextWithLogger returns a context within a Logger in "logger" value. | ||
func ContextWithLogger(ctx context.Context, logger mlog.Logger) context.Context { | ||
values, _ := ctx.Value(CustomContextKey).(*CustomContextKeyValue) | ||
if values == nil { | ||
values = &CustomContextKeyValue{} | ||
} | ||
|
||
values.Logger = logger | ||
|
||
return context.WithValue(ctx, CustomContextKey, values) | ||
} | ||
|
||
// NewTracerFromContext returns a new tracer from the context. | ||
// | ||
//nolint:ireturn | ||
func NewTracerFromContext(ctx context.Context) trace.Tracer { | ||
if customContext, ok := ctx.Value(CustomContextKey).(*CustomContextKeyValue); ok && | ||
customContext.Tracer != nil { | ||
return customContext.Tracer | ||
} | ||
|
||
return otel.Tracer("default") | ||
} | ||
|
||
// ContextWithTracer returns a context within a trace.Tracer in "tracer" value. | ||
func ContextWithTracer(ctx context.Context, tracer trace.Tracer) context.Context { | ||
values, _ := ctx.Value(CustomContextKey).(*CustomContextKeyValue) | ||
if values == nil { | ||
values = &CustomContextKeyValue{} | ||
} | ||
|
||
values.Tracer = tracer | ||
|
||
return context.WithValue(ctx, CustomContextKey, values) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.