logotel/WithDefaultSpan 设置当 span 没有在记录时创建新 span;

develop logotel/v0.11.0
Ge Song 2 years ago
parent d83a012967
commit c4439e938b

@ -23,6 +23,7 @@ func newConfig(opts ...Option) *config {
func defaultConfig() *config { func defaultConfig() *config {
return &config{ return &config{
hasPool: false, hasPool: false,
defaultSpan: false,
} }
} }
@ -34,6 +35,13 @@ func WithBufferPool(pool logjson.BytesBufferPool) Option {
}) })
} }
// WithDefaultSpan 设置当 span 没有在记录时创建新 span
func WithDefaultSpan(defaultSpan bool) Option {
return optionFunc(func(cfg *config) {
cfg.defaultSpan = defaultSpan
})
}
// Option 配置 Processor // Option 配置 Processor
type Option interface { type Option interface {
apply(cfg *config) apply(cfg *config)
@ -42,6 +50,7 @@ type Option interface {
type config struct { type config struct {
bytesBufferPool logjson.BytesBufferPool bytesBufferPool logjson.BytesBufferPool
hasPool bool hasPool bool
defaultSpan bool
} }
type optionFunc func(cfg *config) type optionFunc func(cfg *config)

@ -9,6 +9,7 @@ import (
"git.blauwelle.com/go/crate/log/logsdk" "git.blauwelle.com/go/crate/log/logsdk"
"git.blauwelle.com/go/crate/log/logsdk/logjson" "git.blauwelle.com/go/crate/log/logsdk/logjson"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/codes"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0" semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
@ -20,6 +21,7 @@ func New(opts ...Option) *Processor {
cfg := newConfig(opts...) cfg := newConfig(opts...)
return &Processor{ return &Processor{
bufferPool: cfg.bytesBufferPool, bufferPool: cfg.bytesBufferPool,
defaultSpan: cfg.defaultSpan,
} }
} }
@ -28,10 +30,17 @@ var _ logsdk.EntryProcessor = &Processor{}
// Processor 用于把日志和 opentelemetry 对接 // Processor 用于把日志和 opentelemetry 对接
type Processor struct { type Processor struct {
bufferPool logjson.BytesBufferPool bufferPool logjson.BytesBufferPool
defaultSpan bool
} }
func (processor *Processor) Process(ctx context.Context, entry logsdk.ReadonlyEntry) { func (processor *Processor) Process(ctx context.Context, entry logsdk.ReadonlyEntry) {
span := trace.SpanFromContext(ctx) span := trace.SpanFromContext(ctx)
if !span.IsRecording() {
if processor.defaultSpan {
ctx, span = otel.Tracer("git.blauwelle.com/go/crate/logotel").Start(ctx, "default") //nolint:ineffassign,staticcheck,wastedassign
defer span.End()
}
}
if !span.IsRecording() { if !span.IsRecording() {
return return
} }

Loading…
Cancel
Save