|
| 1 | +// Copyright 2025 The etcd Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package traceutil |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "slices" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "go.opentelemetry.io/otel/attribute" |
| 24 | + "go.opentelemetry.io/otel/sdk/trace" |
| 25 | + "go.uber.org/zap" |
| 26 | +) |
| 27 | + |
| 28 | +// LogExporter writes Span to specified Logger. |
| 29 | +type LogExporter struct { |
| 30 | + // Log is usually zap.Logger.Info. |
| 31 | + Log func(msg string, fields ...zap.Field) |
| 32 | +} |
| 33 | + |
| 34 | +var _ trace.SpanExporter = (*LogExporter)(nil) |
| 35 | + |
| 36 | +// NewLogExporter creates a new LogExporter which will write Spans as Log messages. |
| 37 | +func NewLogExporter(logger *zap.Logger) *LogExporter { |
| 38 | + if logger == nil { |
| 39 | + logger = zap.NewNop() |
| 40 | + } |
| 41 | + return &LogExporter{Log: logger.Info} |
| 42 | +} |
| 43 | + |
| 44 | +func (e *LogExporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error { |
| 45 | + for _, span := range spans { |
| 46 | + msg, fields := logSpan(span) |
| 47 | + e.Log(msg, fields...) |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (e *LogExporter) Shutdown(ctx context.Context) error { |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func logSpan(s trace.ReadOnlySpan) (string, []zap.Field) { |
| 57 | + start := s.StartTime() |
| 58 | + end := s.EndTime() |
| 59 | + duration := end.Sub(start) |
| 60 | + events := s.Events() |
| 61 | + steps := make([]string, 0, len(events)) |
| 62 | + slices.SortFunc(events, func(a, b trace.Event) int { |
| 63 | + return a.Time.Compare(b.Time) |
| 64 | + }) |
| 65 | + for _, event := range events { |
| 66 | + step := fmt.Sprintf("%s %s [+%dms]", |
| 67 | + event.Name, writeAttrs(event.Attributes), event.Time.Sub(start).Milliseconds()) |
| 68 | + steps = append(steps, step) |
| 69 | + } |
| 70 | + msg := fmt.Sprintf("trace[%s] %s", s.SpanContext().SpanID().String(), s.Name()) |
| 71 | + |
| 72 | + return msg, []zap.Field{ |
| 73 | + zap.String("detail", writeAttrs(s.Attributes())), |
| 74 | + zap.Duration("duration", duration), |
| 75 | + zap.Time("start", s.StartTime()), |
| 76 | + zap.Time("end", s.EndTime()), |
| 77 | + zap.Strings("steps", steps), |
| 78 | + zap.Int("step_count", len(steps)), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func writeAttrs(attrs []attribute.KeyValue) string { |
| 83 | + if len(attrs) == 0 { |
| 84 | + return "" |
| 85 | + } |
| 86 | + var buf strings.Builder |
| 87 | + buf.WriteString("{") |
| 88 | + for _, attr := range attrs { |
| 89 | + buf.WriteString(string(attr.Key)) |
| 90 | + buf.WriteString(":") |
| 91 | + buf.WriteString(attr.Value.Emit()) |
| 92 | + buf.WriteString("; ") |
| 93 | + } |
| 94 | + buf.WriteString("}") |
| 95 | + return buf.String() |
| 96 | +} |
0 commit comments