Documentation ¶
Index ¶
- func AttachTraceID(ctx context.Context) (traceID string, result context.Context)
- func Caller(skip int) (caller string)
- func Context(ctx context.Context, traceID string) context.Context
- func ContextKey() string
- func ForkContext(ctx context.Context) (forked context.Context)
- func ForkContextWithOpts(ctx context.Context, fields ...string) (forked context.Context)
- func ForkContextWithoutCancel(ctx context.Context) (forked context.Context)
- func FromContext(ctx context.Context) (traced context.Context)
- func FunctionLocation(fn any) (location string)
- func GetClientIPFromPeer(ctx context.Context) (ip string)
- func GetTid(ctx context.Context) string
- func NewContext() context.Context
- func NewContextWithTid(traceID string) context.Context
- func SetTraceIDKey(key string)
- func Stack(skip int) []byte
- func TransformContext(ctx context.Context) (traceID string, result context.Context)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AttachTraceID ¶
AttachTraceID attaches a newly generated trace ID to the given context, returning the new trace ID and the updated context.
func Caller ¶ added in v1.2.17
Caller returns a string representing the file and line number of the caller of the function that called Caller, adjusted by the skip parameter. The skip parameter is used to ascend the call stack by a number of frames, allowing the retrieval of callers further up the call stack. The resulting string is in the format "file:line", where "file" is the full path to the source file containing the caller and "line" is the line number within that file.
Parameters:
skip (int): The number of stack frames to ascend, with 0 identifying the caller of Caller, 1 identifying the caller's caller, and so on.
Returns:
caller (string): A string in the format "file:line" indicating the source file and line number of the caller adjusted by skip.
func Context ¶ added in v1.2.14
Context creates a new context with the specified trace ID, overwriting any existing trace ID.
func ContextKey ¶ added in v1.2.14
func ContextKey() string
ContextKey returns the current key used to store the trace ID in the context.
func ForkContext ¶ added in v1.2.12
ForkContext creates a new traced context from an existing context, copying only the trace ID. If the original context does not contain a trace ID, a new trace ID is generated.
func ForkContextWithOpts ¶ added in v1.2.12
ForkContextWithOpts creates a new traced context from an existing context, copying specified fields along with the trace ID.
func ForkContextWithoutCancel ¶ added in v1.2.17
ForkContextWithoutCancel creates a new context that is derived from the given context but without the ability to be canceled. This function is useful when you want to create a context that won't be affected by the cancellation of its parent context.
Parameters:
ctx (context.Context): The parent context from which the new context is derived.
Returns:
forked (context.Context): A new context that is derived from the given context without the ability to be canceled.
func FromContext ¶ added in v1.2.12
FromContext checks if the context contains a trace ID. If it does, it returns the context unchanged. If the context does not contain a trace ID, it returns a new context with a generated trace ID.
func FunctionLocation ¶ added in v1.2.17
FunctionLocation returns a string representing the file and line number of the location where the provided function is defined. The resulting string is in the format "file:line", where "file" is the full path to the source file containing the function definition and "line" is the line number within that file.
Parameters:
fn (any): The function whose location is to be determined.
Returns:
location (string): A string in the format "file:line" indicating the source file and line number of the function definition.
func GetClientIPFromPeer ¶ added in v1.2.11
GetClientIPFromPeer extracts the client IP address from a gRPC request context. If the context does not originate from a gRPC request or the client IP cannot be determined, it returns an empty string.
func GetTid ¶ added in v1.2.12
GetTid retrieves the trace ID from a traced context. If the context does not contain a trace ID, it returns an empty string.
func NewContext ¶ added in v1.2.11
NewContext creates a new context with a generated trace ID.
func NewContextWithTid ¶ added in v1.2.11
NewContextWithTid creates a new context with the specified trace ID.
func SetTraceIDKey ¶ added in v1.2.12
func SetTraceIDKey(key string)
SetTraceIDKey sets the key used to store the trace ID in the context globally. This function should be called only once, before any other operations that use the trace ID.
func Stack ¶
Stack generates a formatted stack trace of the calling goroutine. The stack trace starts from the function that calls Stack and ascends up the call stack. Each entry in the stack trace includes the file name, line number, and the address of the call. If the function can read the source file, it also includes the line of source code at the call site. The skip parameter allows skipping a number of stack frames to start the trace at a higher level in the call stack.
Parameters:
skip (int): The number of stack frames to skip before starting the stack trace.
Returns:
[]byte: A byte slice containing the formatted stack trace.
func TransformContext ¶ added in v1.2.12
TransformContext transforms a context into a traced context, returning the traced context and its trace ID. If the context already contains a trace ID, it returns the context unchanged along with the existing trace ID.
Types ¶
This section is empty.