udp_client.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package utils
  15. import (
  16. "errors"
  17. "fmt"
  18. "io"
  19. "net"
  20. "github.com/uber/jaeger-client-go/thrift"
  21. "github.com/uber/jaeger-client-go/thrift-gen/agent"
  22. "github.com/uber/jaeger-client-go/thrift-gen/jaeger"
  23. "github.com/uber/jaeger-client-go/thrift-gen/zipkincore"
  24. )
  25. // UDPPacketMaxLength is the max size of UDP packet we want to send, synced with jaeger-agent
  26. const UDPPacketMaxLength = 65000
  27. // AgentClientUDP is a UDP client to Jaeger agent that implements agent.Agent interface.
  28. type AgentClientUDP struct {
  29. agent.Agent
  30. io.Closer
  31. connUDP *net.UDPConn
  32. client *agent.AgentClient
  33. maxPacketSize int // max size of datagram in bytes
  34. thriftBuffer *thrift.TMemoryBuffer // buffer used to calculate byte size of a span
  35. }
  36. // NewAgentClientUDP creates a client that sends spans to Jaeger Agent over UDP.
  37. func NewAgentClientUDP(hostPort string, maxPacketSize int) (*AgentClientUDP, error) {
  38. if maxPacketSize == 0 {
  39. maxPacketSize = UDPPacketMaxLength
  40. }
  41. thriftBuffer := thrift.NewTMemoryBufferLen(maxPacketSize)
  42. protocolFactory := thrift.NewTCompactProtocolFactory()
  43. client := agent.NewAgentClientFactory(thriftBuffer, protocolFactory)
  44. destAddr, err := net.ResolveUDPAddr("udp", hostPort)
  45. if err != nil {
  46. return nil, err
  47. }
  48. connUDP, err := net.DialUDP(destAddr.Network(), nil, destAddr)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if err := connUDP.SetWriteBuffer(maxPacketSize); err != nil {
  53. return nil, err
  54. }
  55. clientUDP := &AgentClientUDP{
  56. connUDP: connUDP,
  57. client: client,
  58. maxPacketSize: maxPacketSize,
  59. thriftBuffer: thriftBuffer}
  60. return clientUDP, nil
  61. }
  62. // EmitZipkinBatch implements EmitZipkinBatch() of Agent interface
  63. func (a *AgentClientUDP) EmitZipkinBatch(spans []*zipkincore.Span) error {
  64. return errors.New("Not implemented")
  65. }
  66. // EmitBatch implements EmitBatch() of Agent interface
  67. func (a *AgentClientUDP) EmitBatch(batch *jaeger.Batch) error {
  68. a.thriftBuffer.Reset()
  69. a.client.SeqId = 0 // we have no need for distinct SeqIds for our one-way UDP messages
  70. if err := a.client.EmitBatch(batch); err != nil {
  71. return err
  72. }
  73. if a.thriftBuffer.Len() > a.maxPacketSize {
  74. return fmt.Errorf("Data does not fit within one UDP packet; size %d, max %d, spans %d",
  75. a.thriftBuffer.Len(), a.maxPacketSize, len(batch.Spans))
  76. }
  77. _, err := a.connUDP.Write(a.thriftBuffer.Bytes())
  78. return err
  79. }
  80. // Close implements Close() of io.Closer and closes the underlying UDP connection.
  81. func (a *AgentClientUDP) Close() error {
  82. return a.connUDP.Close()
  83. }