message.go 891 B

12345678910111213141516171819202122232425262728293031323334
  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 = messageType(RPCMessageTypeSent)
  10. MessageReceived = messageType(RPCMessageTypeReceived)
  11. )
  12. type messageType attribute.KeyValue
  13. // Event adds an event of the messageType to the span associated with the
  14. // passed context with id and size (if message is a proto message).
  15. func (m messageType) Event(ctx context.Context, id int, message interface{}) {
  16. span := trace.SpanFromContext(ctx)
  17. if p, ok := message.(proto.Message); ok {
  18. span.AddEvent("message", trace.WithAttributes(
  19. attribute.KeyValue(m),
  20. RPCMessageIDKey.Int(id),
  21. RPCMessageUncompressedSizeKey.Int(proto.Size(p)),
  22. ))
  23. } else {
  24. span.AddEvent("message", trace.WithAttributes(
  25. attribute.KeyValue(m),
  26. RPCMessageIDKey.Int(id),
  27. ))
  28. }
  29. }