g.go 581 字节
Newer Older
S
stormgbs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package log

import (
	"context"

	"github.com/sirupsen/logrus"
	"go.opencensus.io/trace"
)

// G returns a `logrus.Entry` with the `TraceID, SpanID` from `ctx` if `ctx`
// contains an OpenCensus `trace.Span`.
func G(ctx context.Context) *logrus.Entry {
	span := trace.FromContext(ctx)
	if span != nil {
		sctx := span.SpanContext()
		return logrus.WithFields(logrus.Fields{
			"traceID": sctx.TraceID.String(),
			"spanID":  sctx.SpanID.String(),
			// "parentSpanID": TODO: JTERRY75 - Try to convince OC to export this?
		})
	}
	return logrus.NewEntry(logrus.StandardLogger())
}