LogEntry implements slog.Sink. All entries are added as events to the span in the context. If no span is present, the entry is dropped.
(ctx context.Context, e slog.SinkEntry)
| 19 | // LogEntry implements slog.Sink. All entries are added as events to the span |
| 20 | // in the context. If no span is present, the entry is dropped. |
| 21 | func (SlogSink) LogEntry(ctx context.Context, e slog.SinkEntry) { |
| 22 | span := trace.SpanFromContext(ctx) |
| 23 | if !span.IsRecording() { |
| 24 | // If the span is a noopSpan or isn't recording, we don't want to |
| 25 | // compute the attributes (which is expensive) below. |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | attributes := []attribute.KeyValue{ |
| 30 | attribute.String("slog.time", e.Time.Format(time.RFC3339Nano)), |
| 31 | attribute.String("slog.logger", strings.Join(e.LoggerNames, ".")), |
| 32 | attribute.String("slog.level", e.Level.String()), |
| 33 | attribute.String("slog.message", e.Message), |
| 34 | attribute.String("slog.func", e.Func), |
| 35 | attribute.String("slog.file", e.File), |
| 36 | attribute.Int64("slog.line", int64(e.Line)), |
| 37 | } |
| 38 | attributes = append(attributes, slogFieldsToAttributes(e.Fields)...) |
| 39 | |
| 40 | name := fmt.Sprintf("log: %s: %s", e.Level, e.Message) |
| 41 | span.AddEvent(name, trace.WithAttributes(attributes...)) |
| 42 | } |
| 43 | |
| 44 | // Sync implements slog.Sink. No-op as syncing is handled externally by otel. |
| 45 | func (SlogSink) Sync() {} |