message.go 987 B

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