github.com/lingyao2333/mo-zero@v1.4.1/core/trace/message.go (about) 1 package trace 2 3 import ( 4 "context" 5 6 "go.opentelemetry.io/otel/attribute" 7 "go.opentelemetry.io/otel/trace" 8 "google.golang.org/protobuf/proto" 9 ) 10 11 const messageEvent = "message" 12 13 var ( 14 // MessageSent is the type of sent messages. 15 MessageSent = messageType(RPCMessageTypeSent) 16 // MessageReceived is the type of received messages. 17 MessageReceived = messageType(RPCMessageTypeReceived) 18 ) 19 20 type messageType attribute.KeyValue 21 22 // Event adds an event of the messageType to the span associated with the 23 // passed context with id and size (if message is a proto message). 24 func (m messageType) Event(ctx context.Context, id int, message interface{}) { 25 span := trace.SpanFromContext(ctx) 26 if p, ok := message.(proto.Message); ok { 27 span.AddEvent(messageEvent, trace.WithAttributes( 28 attribute.KeyValue(m), 29 RPCMessageIDKey.Int(id), 30 RPCMessageUncompressedSizeKey.Int(proto.Size(p)), 31 )) 32 } else { 33 span.AddEvent(messageEvent, trace.WithAttributes( 34 attribute.KeyValue(m), 35 RPCMessageIDKey.Int(id), 36 )) 37 } 38 }