From bef1a8cdc0f6a4596802e2e4bca2abb5c014bb6b Mon Sep 17 00:00:00 2001 From: Ge Song Date: Mon, 8 Jan 2024 19:06:04 +0800 Subject: [PATCH] logotel/add log field prefix; --- logotel/processor.go | 30 +++++++++++++++++++----------- logotel/version.go | 6 ++++++ 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 logotel/version.go diff --git a/logotel/processor.go b/logotel/processor.go index 27fc66b..7db1d5a 100644 --- a/logotel/processor.go +++ b/logotel/processor.go @@ -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, "") + return attribute.String(fieldPrefix+field.Key, "") 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)) } diff --git a/logotel/version.go b/logotel/version.go new file mode 100644 index 0000000..184777a --- /dev/null +++ b/logotel/version.go @@ -0,0 +1,6 @@ +package logotel + +const ( + tracerName = "git.blauwelle.com/go/crate/logotel" + version = "0.14.0" +)