You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 regels
2.7 KiB

  1. // Copyright 2018, OpenCensus Authors
  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 jaeger
  15. import (
  16. "fmt"
  17. "io"
  18. "net"
  19. "github.com/apache/thrift/lib/go/thrift"
  20. gen "go.opencensus.io/exporter/jaeger/internal/gen-go/jaeger"
  21. )
  22. // udpPacketMaxLength is the max size of UDP packet we want to send, synced with jaeger-agent
  23. const udpPacketMaxLength = 65000
  24. // agentClientUDP is a UDP client to Jaeger agent that implements gen.Agent interface.
  25. type agentClientUDP struct {
  26. gen.Agent
  27. io.Closer
  28. connUDP *net.UDPConn
  29. client *gen.AgentClient
  30. maxPacketSize int // max size of datagram in bytes
  31. thriftBuffer *thrift.TMemoryBuffer // buffer used to calculate byte size of a span
  32. }
  33. // newAgentClientUDP creates a client that sends spans to Jaeger Agent over UDP.
  34. func newAgentClientUDP(hostPort string, maxPacketSize int) (*agentClientUDP, error) {
  35. if maxPacketSize == 0 {
  36. maxPacketSize = udpPacketMaxLength
  37. }
  38. thriftBuffer := thrift.NewTMemoryBufferLen(maxPacketSize)
  39. protocolFactory := thrift.NewTCompactProtocolFactory()
  40. client := gen.NewAgentClientFactory(thriftBuffer, protocolFactory)
  41. destAddr, err := net.ResolveUDPAddr("udp", hostPort)
  42. if err != nil {
  43. return nil, err
  44. }
  45. connUDP, err := net.DialUDP(destAddr.Network(), nil, destAddr)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if err := connUDP.SetWriteBuffer(maxPacketSize); err != nil {
  50. return nil, err
  51. }
  52. clientUDP := &agentClientUDP{
  53. connUDP: connUDP,
  54. client: client,
  55. maxPacketSize: maxPacketSize,
  56. thriftBuffer: thriftBuffer}
  57. return clientUDP, nil
  58. }
  59. // EmitBatch implements EmitBatch() of Agent interface
  60. func (a *agentClientUDP) EmitBatch(batch *gen.Batch) error {
  61. a.thriftBuffer.Reset()
  62. a.client.SeqId = 0 // we have no need for distinct SeqIds for our one-way UDP messages
  63. if err := a.client.EmitBatch(batch); err != nil {
  64. return err
  65. }
  66. if a.thriftBuffer.Len() > a.maxPacketSize {
  67. return fmt.Errorf("Data does not fit within one UDP packet; size %d, max %d, spans %d",
  68. a.thriftBuffer.Len(), a.maxPacketSize, len(batch.Spans))
  69. }
  70. _, err := a.connUDP.Write(a.thriftBuffer.Bytes())
  71. return err
  72. }
  73. // Close implements Close() of io.Closer and closes the underlying UDP connection.
  74. func (a *agentClientUDP) Close() error {
  75. return a.connUDP.Close()
  76. }