message.go 1009 B

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