* * Copyright 2018 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http:www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

package binarylog

import (
	
	
	
	

	
	
	pb 
	
	
)

type callIDGenerator struct {
	id uint64
}

func ( *callIDGenerator) () uint64 {
	 := atomic.AddUint64(&.id, 1)
	return 
}
reset is for testing only, and doesn't need to be thread safe.
func ( *callIDGenerator) () {
	.id = 0
}

var idGen callIDGenerator
MethodLogger is the sub-logger for each method.
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.
	}
}
Log creates a proto binary log entry, and logs it to the sink.
At the end of the loop, index will be the first entry where the total size is greater than the limit: len(entry[:index]) <= ml.hdr && len(entry[:index+1]) > ml.hdr.
	for ;  < len(.Entry); ++ {
		 := .Entry[]
"grpc-trace-bin" is a special key. It's kept in the log entry, but not counted towards the size limit.
			continue
		}
		 := uint64(len(.Value))
		if  >  {
			break
		}
		 -= 
	}
	 =  < len(.Entry)
	.Entry = .Entry[:]
	return 
}

func ( *MethodLogger) ( *pb.Message) ( bool) {
	if .messageMaxLen == maxUInt {
		return false
	}
	if .messageMaxLen >= uint64(len(.Data)) {
		return false
	}
	.Data = .Data[:.messageMaxLen]
	return true
}
LogEntryConfig represents the configuration for binary log entry.
type LogEntryConfig interface {
	toProto() *pb.GrpcLogEntry
}
ClientHeader configs the binary log entry to be a ClientHeader entry.
PeerAddr is required only when it's on server side.
This function doesn't need to set all the fields (e.g. seq ID). The Log function will set the fields when necessary.
ServerHeader configs the binary log entry to be a ServerHeader entry.
ClientMessage configs the binary log entry to be a ClientMessage entry.
type ClientMessage struct {
Message can be a proto.Message or []byte. Other messages formats are not supported.
	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 
}
ServerMessage configs the binary log entry to be a ServerMessage entry.
type ServerMessage struct {
Message can be a proto.Message or []byte. Other messages formats are not supported.
	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 
}
ClientHalfClose configs the binary log entry to be a ClientHalfClose entry.
ServerTrailer configs the binary log entry to be a ServerTrailer entry.
Err is the status error.
PeerAddr is required only when it's on client side and the RPC is trailer only.
	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 
}
Cancel configs the binary log entry to be a Cancel entry.
metadataKeyOmit returns whether the metadata entry with this key should be omitted.
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 {
Do not set address and port fields.
			break
		}
		.Address = .IP.String()
		.IpPort = uint32(.Port)
	case *net.UnixAddr:
		.Type = pb.Address_TYPE_UNIX
		.Address = .String()
	default:
		.Type = pb.Address_TYPE_UNKNOWN
	}
	return