logotel/add log field prefix;

develop logotel/v0.14.0
Ge Song 11 months ago
parent 5a5fe14ea5
commit bef1a8cdc0

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

@ -0,0 +1,6 @@
package logotel
const (
tracerName = "git.blauwelle.com/go/crate/logotel"
version = "0.14.0"
)
Loading…
Cancel
Save