-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapmlog.go
39 lines (31 loc) · 928 Bytes
/
apmlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package apmlog
import (
"context"
"github.com/sirupsen/logrus"
"go.elastic.co/apm/v2"
)
const (
// FieldKeyTraceID is the field key for the trace ID.
FieldKeyTraceID = "traceid"
// FieldKeyTransactionID is the field key for the transaction ID.
FieldKeyTransactionID = "transactionid"
// FieldKeySpanID is the field key for the span ID.
FieldKeySpanID = "spanid"
)
// this function create insert field logrus
// TODO test failed data context
func TraceContext(ctx context.Context) logrus.Fields {
tx := apm.TransactionFromContext(ctx)
if tx == nil {
return nil
}
traceContext := tx.TraceContext()
fields := logrus.Fields{
FieldKeyTraceID: traceContext.Trace.String(),
FieldKeyTransactionID: traceContext.Span.String(),
}
if span := apm.SpanFromContext(ctx); span != nil {
fields[FieldKeySpanID] = span.TraceContext().Span.String()
}
return fields
}