|
|
|
@ -16,6 +16,14 @@ import (
|
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
fieldPrefix = "log.field."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
tracer = otel.Tracer(tracerName, trace.WithInstrumentationVersion("semver:"+version))
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// New 创建 log/opentelemetry 处理器
|
|
|
|
|
func New(opts ...Option) *Processor {
|
|
|
|
|
cfg := newConfig(opts...)
|
|
|
|
@ -41,7 +49,7 @@ func (processor *Processor) Process(ctx context.Context, entry logsdk.ReadonlyEn
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = "default"
|
|
|
|
|
}
|
|
|
|
|
ctx, span = otel.Tracer("git.blauwelle.com/go/crate/logotel").Start(ctx, name) //nolint:ineffassign,staticcheck,wastedassign
|
|
|
|
|
ctx, span = tracer.Start(ctx, name) //nolint:ineffassign,staticcheck,wastedassign
|
|
|
|
|
defer span.End()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -70,7 +78,7 @@ func (processor *Processor) Process(ctx context.Context, entry logsdk.ReadonlyEn
|
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
|
}
|
|
|
|
|
processor.bufferPool.Put(buf)
|
|
|
|
|
attrs = append(attrs, attribute.String("zz.stack", buf.String()))
|
|
|
|
|
attrs = append(attrs, attribute.String("log.stack", buf.String()))
|
|
|
|
|
}
|
|
|
|
|
for _, field := range entry.Fields {
|
|
|
|
|
attrs = append(attrs, fieldToKV(field))
|
|
|
|
@ -84,22 +92,22 @@ func (processor *Processor) Process(ctx context.Context, entry logsdk.ReadonlyEn
|
|
|
|
|
func fieldToKV(field logsdk.KV) attribute.KeyValue {
|
|
|
|
|
switch value := field.Value.(type) {
|
|
|
|
|
case nil:
|
|
|
|
|
return attribute.String(field.Key, "<nil>")
|
|
|
|
|
return attribute.String(fieldPrefix+field.Key, "<nil>")
|
|
|
|
|
case string:
|
|
|
|
|
return attribute.String(field.Key, value)
|
|
|
|
|
return attribute.String(fieldPrefix+field.Key, value)
|
|
|
|
|
case int:
|
|
|
|
|
return attribute.Int(field.Key, value)
|
|
|
|
|
return attribute.Int(fieldPrefix+field.Key, value)
|
|
|
|
|
case int64:
|
|
|
|
|
return attribute.Int64(field.Key, value)
|
|
|
|
|
return attribute.Int64(fieldPrefix+field.Key, value)
|
|
|
|
|
case float64:
|
|
|
|
|
return attribute.Float64(field.Key, value)
|
|
|
|
|
return attribute.Float64(fieldPrefix+field.Key, value)
|
|
|
|
|
case bool:
|
|
|
|
|
return attribute.Bool(field.Key, value)
|
|
|
|
|
return attribute.Bool(fieldPrefix+field.Key, value)
|
|
|
|
|
case error:
|
|
|
|
|
return attribute.String(field.Key, value.Error())
|
|
|
|
|
return attribute.String(fieldPrefix+field.Key, value.Error())
|
|
|
|
|
case fmt.Stringer:
|
|
|
|
|
return attribute.String(field.Key, value.String())
|
|
|
|
|
return attribute.String(fieldPrefix+field.Key, value.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attribute.String(field.Key, fmt.Sprint(field.Value))
|
|
|
|
|
return attribute.String(fieldPrefix+field.Key, fmt.Sprint(field.Value))
|
|
|
|
|
}
|
|
|
|
|