Class: OpenCensus::Trace::Exporters::JaegerExporter

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/opencensus/trace/exporters/jaeger_exporter.rb

Constant Summary collapse

JAEGER_OPENCENSUS_EXPORTER_VERSION_TAG_KEY =
'opencensus.exporter.jaeger.version'
TRACER_HOSTNAME_TAG_KEY =
'opencensus.exporter.jaeger.hostname'
PROCESS_IP =
'ip'
DEFAULT_MAX_LENGTH =

Jaeger agent only accepts up to 65_000

65_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#default_logger, included, logger

Constructor Details

#initialize(logger: default_logger, service_name: 'UNCONFIGURED_SERVICE_NAME', host: 'localhost', port: 6831, tags: {}, max_packet_length: DEFAULT_MAX_LENGTH, protocol_class: ::Thrift::CompactProtocol) ⇒ JaegerExporter



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 17

def initialize(
  logger: default_logger,
  service_name: 'UNCONFIGURED_SERVICE_NAME',
  host: 'localhost',
  port: 6831,
  tags: {},
  max_packet_length: DEFAULT_MAX_LENGTH,
  protocol_class: ::Thrift::CompactProtocol
)
  @logger = logger
  @service_name = service_name
  @host = host
  @port = port

  default_tags = {}
  default_tags[JAEGER_OPENCENSUS_EXPORTER_VERSION_TAG_KEY] = \
    "opencensus-exporter-jaeger-#{OpenCensus::Jaeger::VERSION}"
  default_tags[TRACER_HOSTNAME_TAG_KEY] = Socket.gethostname
  default_tags[PROCESS_IP] = JaegerDriver.ip_v4
  @tags = default_tags.merge(tags)
  @client = JaegerDriver::UDPSender.new(host, port, logger, protocol_class)
  @process = ::Jaeger::Thrift::Process.new(
    'serviceName': @service_name,
    'tags': JaegerDriver.build_thrift_tags(@tags)
  )
  @protocol_class = protocol_class
  @max_span_size = max_packet_length - process_size - 8 - 20 # 8 is UDP header , 20 is IP header
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 15

def client
  @client
end

#span_batchesObject (readonly)

Returns the value of attribute span_batches.



15
16
17
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 15

def span_batches
  @span_batches
end

Instance Method Details

#encode_batch(encoded_spans) ⇒ Object



79
80
81
82
83
84
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 79

def encode_batch(encoded_spans)
  ::Jaeger::Thrift::Batch.new(
    'process' => @process,
    'spans' => encoded_spans
  )
end

#encode_within_limit(spans) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 60

def encode_within_limit(spans)
  batches = []
  current_batch = []
  transport.flush

  spans.each do |span|
    encoded_span = JaegerDriver.encode_span(span)
    if aggregated_span_size(encoded_span) >= @max_span_size && !current_batch.empty?
      batches << encode_batch(current_batch)
      current_batch = []
      transport.flush
    end
    current_batch << encoded_span
  end

  batches << encode_batch(current_batch) unless current_batch.empty?
  batches
end

#export(spans) ⇒ Object



46
47
48
49
50
51
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 46

def export(spans)
  return nil if spans.nil? || spans.empty?
  export_as_batch(spans)
rescue StandardError => e
  @logger.error("Fail to export spans due to #{e.message}")
end

#export_as_batch(spans) ⇒ Object



53
54
55
56
57
58
# File 'lib/opencensus/trace/exporters/jaeger_exporter.rb', line 53

def export_as_batch(spans)
  @span_batches = encode_within_limit(spans)
  @span_batches.each do |span_batch|
    @client.send_spans(span_batch)
  end
end