exporter.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package oc
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "go.opencensus.io/trace"
  5. )
  6. var _ = (trace.Exporter)(&LogrusExporter{})
  7. // LogrusExporter is an OpenCensus `trace.Exporter` that exports
  8. // `trace.SpanData` to logrus output.
  9. type LogrusExporter struct {
  10. }
  11. // ExportSpan exports `s` based on the the following rules:
  12. //
  13. // 1. All output will contain `s.Attributes`, `s.TraceID`, `s.SpanID`,
  14. // `s.ParentSpanID` for correlation
  15. //
  16. // 2. Any calls to .Annotate will not be supported.
  17. //
  18. // 3. The span itself will be written at `logrus.InfoLevel` unless
  19. // `s.Status.Code != 0` in which case it will be written at `logrus.ErrorLevel`
  20. // providing `s.Status.Message` as the error value.
  21. func (le *LogrusExporter) ExportSpan(s *trace.SpanData) {
  22. // Combine all span annotations with traceID, spanID, parentSpanID
  23. baseEntry := logrus.WithFields(logrus.Fields(s.Attributes))
  24. baseEntry.Data["traceID"] = s.TraceID.String()
  25. baseEntry.Data["spanID"] = s.SpanID.String()
  26. baseEntry.Data["parentSpanID"] = s.ParentSpanID.String()
  27. baseEntry.Data["startTime"] = s.StartTime
  28. baseEntry.Data["endTime"] = s.EndTime
  29. baseEntry.Data["duration"] = s.EndTime.Sub(s.StartTime).String()
  30. baseEntry.Data["name"] = s.Name
  31. baseEntry.Time = s.StartTime
  32. level := logrus.InfoLevel
  33. if s.Status.Code != 0 {
  34. level = logrus.ErrorLevel
  35. baseEntry.Data[logrus.ErrorKey] = s.Status.Message
  36. }
  37. baseEntry.Log(level, "Span")
  38. }