udp_client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package utils
  21. import (
  22. "errors"
  23. "fmt"
  24. "io"
  25. "net"
  26. "github.com/apache/thrift/lib/go/thrift"
  27. "github.com/uber/jaeger-client-go/thrift-gen/agent"
  28. "github.com/uber/jaeger-client-go/thrift-gen/jaeger"
  29. "github.com/uber/jaeger-client-go/thrift-gen/zipkincore"
  30. )
  31. // UDPPacketMaxLength is the max size of UDP packet we want to send, synced with jaeger-agent
  32. const UDPPacketMaxLength = 65000
  33. // AgentClientUDP is a UDP client to Jaeger agent that implements agent.Agent interface.
  34. type AgentClientUDP struct {
  35. agent.Agent
  36. io.Closer
  37. connUDP *net.UDPConn
  38. client *agent.AgentClient
  39. maxPacketSize int // max size of datagram in bytes
  40. thriftBuffer *thrift.TMemoryBuffer // buffer used to calculate byte size of a span
  41. }
  42. // NewAgentClientUDP creates a client that sends spans to Jaeger Agent over UDP.
  43. func NewAgentClientUDP(hostPort string, maxPacketSize int) (*AgentClientUDP, error) {
  44. if maxPacketSize == 0 {
  45. maxPacketSize = UDPPacketMaxLength
  46. }
  47. thriftBuffer := thrift.NewTMemoryBufferLen(maxPacketSize)
  48. protocolFactory := thrift.NewTCompactProtocolFactory()
  49. client := agent.NewAgentClientFactory(thriftBuffer, protocolFactory)
  50. destAddr, err := net.ResolveUDPAddr("udp", hostPort)
  51. if err != nil {
  52. return nil, err
  53. }
  54. connUDP, err := net.DialUDP(destAddr.Network(), nil, destAddr)
  55. if err != nil {
  56. return nil, err
  57. }
  58. if err := connUDP.SetWriteBuffer(maxPacketSize); err != nil {
  59. return nil, err
  60. }
  61. clientUDP := &AgentClientUDP{
  62. connUDP: connUDP,
  63. client: client,
  64. maxPacketSize: maxPacketSize,
  65. thriftBuffer: thriftBuffer}
  66. return clientUDP, nil
  67. }
  68. // EmitZipkinBatch implements EmitZipkinBatch() of Agent interface
  69. func (a *AgentClientUDP) EmitZipkinBatch(spans []*zipkincore.Span) error {
  70. return errors.New("Not implemented")
  71. }
  72. // EmitBatch implements EmitBatch() of Agent interface
  73. func (a *AgentClientUDP) EmitBatch(batch *jaeger.Batch) error {
  74. a.thriftBuffer.Reset()
  75. a.client.SeqId = 0 // we have no need for distinct SeqIds for our one-way UDP messages
  76. if err := a.client.EmitBatch(batch); err != nil {
  77. return err
  78. }
  79. if a.thriftBuffer.Len() > a.maxPacketSize {
  80. return fmt.Errorf("Data does not fit within one UDP packet; size %d, max %d, spans %d",
  81. a.thriftBuffer.Len(), a.maxPacketSize, len(batch.Spans))
  82. }
  83. _, err := a.connUDP.Write(a.thriftBuffer.Bytes())
  84. return err
  85. }
  86. // Close implements Close() of io.Closer and closes the underlying UDP connection.
  87. func (a *AgentClientUDP) Close() error {
  88. return a.connUDP.Close()
  89. }