WithClientInfoFromContext returns an option that extracts the UserAgent and the RemoteIP from the request context.
()
| 88 | |
| 89 | // WithClientInfoFromContext returns an option that extracts the UserAgent and the RemoteIP from the request context. |
| 90 | func WithClientInfoFromContext() Option { |
| 91 | return optionFunc(func(e *event) { |
| 92 | if p, ok := peer.FromContext(e.ctx); ok && p.Addr != nil && p.Addr.String() != "pipe" { |
| 93 | if host, _, err := net.SplitHostPort(p.Addr.String()); err == nil { |
| 94 | e.innerEvent.RemoteIp = host |
| 95 | } |
| 96 | } |
| 97 | md := rpcmetadata.FromIncomingContext(e.ctx) |
| 98 | if md.XForwardedFor != "" { |
| 99 | xff := strings.Split(md.XForwardedFor, ",") |
| 100 | e.innerEvent.RemoteIp = strings.Trim(xff[0], " ") |
| 101 | } |
| 102 | if md := rpcmetadata.FromIncomingContext(e.ctx); md.UserAgent != "" { |
| 103 | e.innerEvent.UserAgent = md.UserAgent |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | // DefinitionOption is like Option, but applies to the definition instead. |
| 109 | type DefinitionOption interface { |