ctx

package
v0.0.0-...-556d222 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package ctx extends standard context to support logging. For context detail, see https://golang.org/pkg/context/

Index

Constants

View Source
const (

	// TraceHeader is request header for tracing, export so we can use it without http.Request
	TraceHeader = `X-Cloud-Trace-Context`
)

Variables

This section is empty.

Functions

func AddTraceToRequest

func AddTraceToRequest(context CTX, req *http.Request)

AddTraceToRequest add `X-Cloud-Trace-Context` header to request

func GetLogLevel

func GetLogLevel() logrus.Level

GetLogLevel returns log set level

func NewHook

func NewHook(conf *Config) (logrus.Hook, error)

NewHook to new a logrus hook

func SetDebugLevel

func SetDebugLevel()

SetDebugLevel for developer debug use case

func SetLogger

func SetLogger(configs ...*Config) error

SetLogger handle setup log level, and given hook config

func SpanContextToString

func SpanContextToString(sc trace.SpanContext) string

SpanContextToString convert SpanContext to `X-Cloud-Trace-Context` header propagation format used by Google Cloud products.

func StringToSpanContext

func StringToSpanContext(h string) (sc trace.SpanContext, ok bool)

StringToSpanContext convert `X-Cloud-Trace-Context` header value back to SpanContext.

Types

type CTX

type CTX struct {
	context.Context
	logrus.FieldLogger
}

CTX extends Google's context to support logging methods.

func Background

func Background() CTX

Background returns a non-nil, empty Context. It is never canceled, has no values, and has no deadline. It is typically used by the main function, initialization, and tests, and as the top-level Context for incoming requests

func InjectContext

func InjectContext(parent context.Context, ctx CTX) CTX

InjectContext return a copy of native context

func StartSpan

func StartSpan(parent CTX, name string, o ...trace.StartOption) (CTX, *trace.Span)

StartSpan returns a copy of parent with trace span append

func StartSpanWithRemoteParent

func StartSpanWithRemoteParent(parent CTX, name string, parentSpanContext trace.SpanContext, o ...trace.StartOption) (CTX, *trace.Span)

StartSpanWithRemoteParent returns a copy of parent ctx with parent trace span append

func WithCancel

func WithCancel(parent CTX) (CTX, context.CancelFunc)

WithCancel returns a copy of parent with added cancel function

func WithTimeout

func WithTimeout(parent CTX, d time.Duration) (CTX, context.CancelFunc)

WithTimeout returns a copy of parent with timeout condition and cancel function

func WithValue

func WithValue(parent CTX, key string, val interface{}) CTX

WithValue returns a copy of parent in which the value associated with key is val.

func WithValues

func WithValues(parent CTX, kvs map[string]interface{}) CTX

WithValues returns a copy of parent in which the values associated with keys are vals.

type Config

type Config struct {
	Environment    string
	ServiceName    string
	AdapterName    string
	ProjectFolder  string
	LogFormat      logrus.Formatter
	ExportLevel    []logrus.Level
	ExportToFile   *string
	SetAsStdOutput bool
}

Config struct define logrus hook setup

type HTTPFormat

type HTTPFormat struct{}

HTTPFormat implements propagation.HTTPFormat to propagate traces in HTTP headers for Google Cloud Platform and Stackdriver Trace. copied from "contrib.go.opencensus.io/exporter/stackdriver/propagation" and modified.

func (*HTTPFormat) SpanContextFromRequest

func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool)

SpanContextFromRequest extracts a Stackdriver Trace span context from incoming requests.

func (*HTTPFormat) SpanContextToRequest

func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request)

SpanContextToRequest modifies the given request to include a Stackdriver Trace header.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL