Source File
method_logger.go
Belonging Package
google.golang.org/grpc/internal/binarylog
package binarylog
import (
pb
)
type callIDGenerator struct {
id uint64
}
func ( *callIDGenerator) () uint64 {
:= atomic.AddUint64(&.id, 1)
return
}
func ( *callIDGenerator) () {
.id = 0
}
var idGen callIDGenerator
type MethodLogger struct {
headerMaxLen, messageMaxLen uint64
callID uint64
idWithinCallGen *callIDGenerator
sink Sink // TODO(blog): make this plugable.
}
func (, uint64) *MethodLogger {
return &MethodLogger{
headerMaxLen: ,
messageMaxLen: ,
callID: idGen.next(),
idWithinCallGen: &callIDGenerator{},
sink: defaultSink, // TODO(blog): make it plugable.
}
}
func ( *MethodLogger) ( LogEntryConfig) {
:= .toProto()
, := ptypes.TimestampProto(time.Now())
.Timestamp =
.CallId = .callID
.SequenceIdWithinCall = .idWithinCallGen.next()
switch pay := .Payload.(type) {
case *pb.GrpcLogEntry_ClientHeader:
.PayloadTruncated = .truncateMetadata(.ClientHeader.GetMetadata())
case *pb.GrpcLogEntry_ServerHeader:
.PayloadTruncated = .truncateMetadata(.ServerHeader.GetMetadata())
case *pb.GrpcLogEntry_Message:
.PayloadTruncated = .truncateMessage(.Message)
}
.sink.Write()
}
func ( *MethodLogger) ( *pb.Metadata) ( bool) {
if .headerMaxLen == maxUInt {
return false
}
var (
= .headerMaxLen
int
type LogEntryConfig interface {
toProto() *pb.GrpcLogEntry
}
type ClientHeader struct {
OnClientSide bool
Header metadata.MD
MethodName string
Authority string
:= &pb.ClientHeader{
Metadata: mdToMetadataProto(.Header),
MethodName: .MethodName,
Authority: .Authority,
}
if .Timeout > 0 {
.Timeout = ptypes.DurationProto(.Timeout)
}
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_CLIENT_HEADER,
Payload: &pb.GrpcLogEntry_ClientHeader{
ClientHeader: ,
},
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
if .PeerAddr != nil {
.Peer = addrToProto(.PeerAddr)
}
return
}
type ServerHeader struct {
OnClientSide bool
PeerAddr net.Addr
}
func ( *ServerHeader) () *pb.GrpcLogEntry {
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_SERVER_HEADER,
Payload: &pb.GrpcLogEntry_ServerHeader{
ServerHeader: &pb.ServerHeader{
Metadata: mdToMetadataProto(.Header),
},
},
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
if .PeerAddr != nil {
.Peer = addrToProto(.PeerAddr)
}
return
}
type ClientMessage struct {
Message interface{}
}
func ( *ClientMessage) () *pb.GrpcLogEntry {
var (
[]byte
error
)
if , := .Message.(proto.Message); {
, = proto.Marshal()
if != nil {
grpclogLogger.Infof("binarylogging: failed to marshal proto message: %v", )
}
} else if , := .Message.([]byte); {
=
} else {
grpclogLogger.Infof("binarylogging: message to log is neither proto.message nor []byte")
}
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_CLIENT_MESSAGE,
Payload: &pb.GrpcLogEntry_Message{
Message: &pb.Message{
Length: uint32(len()),
Data: ,
},
},
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
return
}
type ServerMessage struct {
Message interface{}
}
func ( *ServerMessage) () *pb.GrpcLogEntry {
var (
[]byte
error
)
if , := .Message.(proto.Message); {
, = proto.Marshal()
if != nil {
grpclogLogger.Infof("binarylogging: failed to marshal proto message: %v", )
}
} else if , := .Message.([]byte); {
=
} else {
grpclogLogger.Infof("binarylogging: message to log is neither proto.message nor []byte")
}
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_SERVER_MESSAGE,
Payload: &pb.GrpcLogEntry_Message{
Message: &pb.Message{
Length: uint32(len()),
Data: ,
},
},
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
return
}
type ClientHalfClose struct {
OnClientSide bool
}
func ( *ClientHalfClose) () *pb.GrpcLogEntry {
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_CLIENT_HALF_CLOSE,
Payload: nil, // No payload here.
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
return
}
type ServerTrailer struct {
OnClientSide bool
PeerAddr net.Addr
}
func ( *ServerTrailer) () *pb.GrpcLogEntry {
, := status.FromError(.Err)
if ! {
grpclogLogger.Info("binarylogging: error in trailer is not a status error")
}
var (
[]byte
error
)
:= .Proto()
if != nil && len(.Details) != 0 {
, = proto.Marshal()
if != nil {
grpclogLogger.Infof("binarylogging: failed to marshal status proto: %v", )
}
}
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_SERVER_TRAILER,
Payload: &pb.GrpcLogEntry_Trailer{
Trailer: &pb.Trailer{
Metadata: mdToMetadataProto(.Trailer),
StatusCode: uint32(.Code()),
StatusMessage: .Message(),
StatusDetails: ,
},
},
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
if .PeerAddr != nil {
.Peer = addrToProto(.PeerAddr)
}
return
}
type Cancel struct {
OnClientSide bool
}
func ( *Cancel) () *pb.GrpcLogEntry {
:= &pb.GrpcLogEntry{
Type: pb.GrpcLogEntry_EVENT_TYPE_CANCEL,
Payload: nil,
}
if .OnClientSide {
.Logger = pb.GrpcLogEntry_LOGGER_CLIENT
} else {
.Logger = pb.GrpcLogEntry_LOGGER_SERVER
}
return
}
func ( string) bool {
switch {
case "lb-token", ":path", ":authority", "content-encoding", "content-type", "user-agent", "te":
return true
case "grpc-trace-bin": // grpc-trace-bin is special because it's visiable to users.
return false
}
return strings.HasPrefix(, "grpc-")
}
func ( metadata.MD) *pb.Metadata {
:= &pb.Metadata{}
for , := range {
if metadataKeyOmit() {
continue
}
for , := range {
.Entry = append(.Entry,
&pb.MetadataEntry{
Key: ,
Value: []byte(),
},
)
}
}
return
}
func ( net.Addr) *pb.Address {
:= &pb.Address{}
switch a := .(type) {
case *net.TCPAddr:
if .IP.To4() != nil {
.Type = pb.Address_TYPE_IPV4
} else if .IP.To16() != nil {
.Type = pb.Address_TYPE_IPV6
} else {
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |