otel_zap_logger

package module
v0.0.0-...-aab6500 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: AGPL-3.0 Imports: 27 Imported by: 1

README

logger

logger

基于loggeropentelemetry-go进行封装,降低opentelemetry-go的使用门槛。

架构
flowchart LR
logger -- openTelemetrySDK --> trace
trace -- http --> tempo
tempo-->grafana

Install
go get -u -v gitee.com/Himan000/otel_zap_logger
使用

可以参考otel_zero的使用方法

License

Use of logger is governed by the Mit License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string, attributes ...Field)

Debug record debug

func End

func End(ctx context.Context)

End end trace

func Error

func Error(ctx context.Context, msg string, attributes ...Field)

Error record error

func Fatal

func Fatal(ctx context.Context, msg string, attributes ...Field)

Fatal record fatal

func FieldsToKeyValues

func FieldsToKeyValues(fields ...Field) []attribute.KeyValue

FieldsToKeyValue

func FieldsToZapFields

func FieldsToZapFields(ctx context.Context, fields ...Field) []zapcore.Field

FieldsToZapFields

func Flush

func Flush(ctx context.Context)

func GenSpanID

func GenSpanID() string

GenSpanID gererate spanID

func GenTraceID

func GenTraceID() string

GenTraceID generate traceID current timestamp with 8 rand bytes

func GinMiddleware

func GinMiddleware(service string) gin.HandlerFunc

GinMiddleware extract spanContext

func HttpInject

func HttpInject(ctx context.Context, request *http.Request) error

HTTPInject inject spanContext

func Info

func Info(ctx context.Context, msg string, attributes ...Field)

Info record info

func Init

func Init(conf Config, applicationAttributes ...Field)

LoggerInit logger init applicationAttributes can use service.name,service.namesapce,service.instance.id,service.version, telemetry.sdk.name,telemetry.sdk.language,telemetry.sdk.version,telemetry.auto.version or other key that you need

example: Init(conf,String("sevice.name",service1))

func NewRootContext

func NewRootContext(traceID string, spanID string) (context.Context, error)

NewRootContext new root context with given traceID and spanID

func SetSpanAttr

func SetSpanAttr(ctx context.Context, attributes ...Field)

SetSpanAttr set attributes for current span

func SpanID

func SpanID(ctx context.Context) string

TraceID return traceID

func Start

func Start(ctx context.Context, spanName string, spanStartOption ...Field) context.Context

Start start span

example 1: parentCtx:=Start(context.Background(),"span1",String("spanAttr","attr")) Info(parentCtx,"msg",String("Attr","attr")) End(parentCtx) childCtx:=Start(parentCtx,"span2") Info(childCtx,"msg") End(childCtx)

example 2: ctx:= NewRootContext(traceID,spanID) ctx1:=Start(ctx,"span1",String("spanAttr","attr")) Info(ctx1,"msg") End(ctx1)

func TraceID

func TraceID(ctx context.Context) string

TraceID return traceID

func Warn

func Warn(ctx context.Context, msg string, attributes ...Field)

Warn record warn

Types

type Config

type Config struct {
	// Print info or error,default false
	Debug bool `yaml:"debug" mapstructure:"debug"`
	// Enable to save log to the disk,default false
	EnableLog bool `yaml:"enable_log" mapstructure:"enable_log"`
	// Enable to generate trace info,default false
	EnableTrace bool `yaml:"enable_trace" mapstructure:"enable_trace"`
	// Path to store the log,default ./run.log
	File string `yaml:"file" mapstructure:"file"`
	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `yaml:"max_size" mapstructure:"max_size"`
	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `yaml:"max_backups" mapstructure:"max_backups"`
	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `yaml:"max_age" mapstructure:"max_age"`
	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `yaml:"compress" mapstructure:"compress"`
	// "* * * * * *",The smallest unit is seconds,refer to linux crond format
	// Rotate causes Logger to close the existing log file and immediately create a new one.
	// This is a helper function for applications that want to initiate rotations outside of the normal rotation rules,
	// such as in response to SIGHUP. After rotating,
	// this initiates a cleanup of old log files according to the normal rules.
	Rotate string `yaml:"rotate" mapstructure:"rotate"`
	// TraceProviderType support file or jaeger
	TracerProviderType string `yaml:"tracer_provider_type" mapstructure:"tracer_provider_type"`
	// trace sampling, 0.0-1
	// 0,never trace
	// 1,always trace
	TraceSampleRatio float64 `yaml:"trace_sample_ratio" mapstructure:"trace_sample_ratio"`
	// Jaeger URI
	JaegerServer   string `yaml:"jaeger_server" mapstructure:"jaeger_server"`
	JaegerUsername string `yaml:"jaeger_username" mapstructure:"jaeger_username"`
	JaegerPassword string `yaml:"jaeger_password" mapstructure:"jaeger_password"`
}

Config

type Field

type Field struct {
	Key        string
	Type       FieldType
	Bool       bool
	Bools      []bool
	Integer    int
	Integers   []int
	String     string
	Float64    float64
	Integer64  int64
	Integer64s []int64
	Strings    []string
	Float64s   []float64
}

func Bool

func Bool(key string, val bool) Field

Bool

func BoolSlice

func BoolSlice(key string, val []bool) Field

BoolSlice

func Float64

func Float64(key string, val float64) Field

Float64

func Float64Slice

func Float64Slice(key string, val []float64) Field

Float64Slice

func Int

func Int(key string, val int) Field

Int

func Int64

func Int64(key string, val int64) Field

Int64

func Int64Slice

func Int64Slice(key string, val []int64) Field

Int64Slice

func IntSlice

func IntSlice(key string, val []int) Field

IntSlice

func String

func String(key string, val string) Field

String

func StringSlice

func StringSlice(key string, val []string) Field

StringSlice

func Stringer

func Stringer(key string, val fmt.Stringer) Field

Stringer

type FieldType

type FieldType int
const (
	UnknownType FieldType = iota
	BoolType
	BoolSliceType
	IntType
	IntSliceType
	Int64Type
	Int64SliceType
	Float64Type
	Float64SliceType
	StringType
	StringSliceType
	StringerType
)

type LoggerSpanContext

type LoggerSpanContext struct {
	// contains filtered or unexported fields
}

save context span

type Trace

type Trace struct{}

func (Trace) NewFileProvider

func (tx Trace) NewFileProvider(conf Config, attributes ...Field) (*trace.TracerProvider, error)

NewFileProvider

func (Trace) NewJaegerProvider

func (tx Trace) NewJaegerProvider(conf Config,
	attributes ...Field) (*trace.TracerProvider, error)

NewJaegerProvider

Directories

Path Synopsis
propagation

Jump to

Keyboard shortcuts

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