jaeger-client 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ac7f048b966685822762e51884cf5122cc437d6
4
- data.tar.gz: 7986fccb2c4c683e74777803639e4da546227dac
3
+ metadata.gz: dc47584b45bef088c9871ca1a40a73ebbc04bad8
4
+ data.tar.gz: '03990b88b5c512ef7278be81980e1ba8e23b9b8d'
5
5
  SHA512:
6
- metadata.gz: 7978af583df71b1c50b76b60e9208d558f12f7a1f21876e9f55982a7d9e6f7ef14757457fb8a41a1a59a4865e7583e3f4b54c7b442e17135b1fc902dfd9e5206
7
- data.tar.gz: b75781a93c93856c017d06b78e9c7119416752ab66a38b992b0de14e224823e74c7baff484bc8553d67c8722500dc70b98e10943e91088238d5c8c5b6ac70804
6
+ metadata.gz: 8c8b9cd13c3fd71a6b83ce793ae55d4ddfc59e0169c2c1f0ef75390df37eedd4fc5f9c34c24654d2f4c2bd86e13aee6f4eca3c043f0ad6b17bbca898a771c3bd
7
+ data.tar.gz: 26098a8331acd51cbbcf345ae510158cd96ab4e37c55857cf5db4b327bef6634eba46f7ae35b59a03e45e34093b16db0692c1d0698d5d2da32189e981c04d51f
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'jaeger-client'
7
- spec.version = '0.1.0'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ['SaleMove TechMovers']
9
9
  spec.email = ['techmovers@salemove.com']
10
10
 
@@ -8,12 +8,23 @@ require_relative 'client/span_context'
8
8
  require_relative 'client/carrier'
9
9
  require_relative 'client/trace_id'
10
10
  require_relative 'client/udp_sender'
11
+ require_relative 'client/collector'
11
12
 
12
13
  module Jaeger
13
14
  module Client
14
- def self.build(host: '127.0.0.1', port: 6831, service_name:)
15
- client = UdpSender.new(service_name, host, port)
16
- Tracer.new(client, service_name)
15
+ DEFAULT_FLUSH_INTERVAL = 10
16
+
17
+ def self.build(host: '127.0.0.1', port: 6831, service_name:, flush_interval: DEFAULT_FLUSH_INTERVAL)
18
+ collector = Collector.new
19
+ sender = UdpSender.new(
20
+ service_name: service_name,
21
+ host: host,
22
+ port: port,
23
+ collector: collector,
24
+ flush_interval: flush_interval
25
+ )
26
+ sender.start
27
+ Tracer.new(collector, sender)
17
28
  end
18
29
  end
19
30
  end
@@ -0,0 +1,86 @@
1
+ require 'thread'
2
+
3
+ module Jaeger
4
+ module Client
5
+ class Collector
6
+ def initialize
7
+ @buffer = Buffer.new
8
+ end
9
+
10
+ def send_span(span, end_time)
11
+ context = span.context
12
+ start_ts, duration = build_timestamps(span, end_time)
13
+
14
+ @buffer << Jaeger::Thrift::Span.new(
15
+ 'traceIdLow' => context.trace_id,
16
+ 'traceIdHigh' => context.trace_id,
17
+ 'spanId' => context.span_id,
18
+ 'parentSpanId' => context.parent_id || 0,
19
+ 'operationName' => span.operation_name,
20
+ 'references' => [],
21
+ 'flags' => context.flags,
22
+ 'startTime' => start_ts,
23
+ 'duration' => duration,
24
+ 'tags' => build_tags(span.tags),
25
+ 'logs' => build_logs(span.logs)
26
+ )
27
+ end
28
+
29
+ def retrieve
30
+ @buffer.retrieve
31
+ end
32
+
33
+ private
34
+
35
+ def build_tags(tags)
36
+ tags.map {|name, value| build_tag(name, value)}
37
+ end
38
+
39
+ def build_logs(logs)
40
+ logs.map do |timestamp:, fields:|
41
+ Jaeger::Thrift::Log.new(
42
+ 'timestamp' => (timestamp.to_f * 1_000_000).to_i,
43
+ 'fields' => fields.map {|name, value| build_tag(name, value)}
44
+ )
45
+ end
46
+ end
47
+
48
+ def build_tag(name, value)
49
+ Jaeger::Thrift::Tag.new(
50
+ 'key' => name.to_s,
51
+ 'vType' => Jaeger::Thrift::TagType::STRING,
52
+ 'vStr' => value.to_s
53
+ )
54
+ end
55
+
56
+ def build_timestamps(span, end_time)
57
+ start_ts = (span.start_time.to_f * 1_000_000).to_i
58
+ end_ts = (end_time.to_f * 1_000_000).to_i
59
+ duration = end_ts - start_ts
60
+ [start_ts, duration]
61
+ end
62
+
63
+ class Buffer
64
+ def initialize
65
+ @buffer = []
66
+ @mutex = Mutex.new
67
+ end
68
+
69
+ def <<(element)
70
+ @mutex.synchronize do
71
+ @buffer << element
72
+ true
73
+ end
74
+ end
75
+
76
+ def retrieve
77
+ @mutex.synchronize do
78
+ elements = @buffer.dup
79
+ @buffer.clear
80
+ elements
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -9,13 +9,13 @@ module Jaeger
9
9
  #
10
10
  # @param context [SpanContext] the context of the span
11
11
  # @param context [String] the operation name
12
- # @param client [UdpSender] span emitter
12
+ # @param collector [Collector] span collector
13
13
  #
14
14
  # @return [Span] a new Span
15
- def initialize(context, operation_name, client, start_time: Time.now, tags: {})
15
+ def initialize(context, operation_name, collector, start_time: Time.now, tags: {})
16
16
  @context = context
17
17
  @operation_name = operation_name
18
- @client = client
18
+ @collector = collector
19
19
  @start_time = start_time
20
20
  @tags = tags
21
21
  @logs = []
@@ -59,7 +59,7 @@ module Jaeger
59
59
  #
60
60
  # @param end_time [Time] custom end time, if not now
61
61
  def finish(end_time: Time.now)
62
- @client.send_span(self, end_time)
62
+ @collector.send_span(self, end_time)
63
63
  end
64
64
 
65
65
  private
@@ -1,8 +1,13 @@
1
1
  module Jaeger
2
2
  module Client
3
3
  class Tracer
4
- def initialize(client, service_name)
5
- @client = client
4
+ def initialize(collector, sender)
5
+ @collector = collector
6
+ @sender = sender
7
+ end
8
+
9
+ def stop
10
+ @sender.stop
6
11
  end
7
12
 
8
13
  # Starts a new span.
@@ -23,7 +28,7 @@ module Jaeger
23
28
  else
24
29
  SpanContext.create_parent_context
25
30
  end
26
- Span.new(context, operation_name, @client, start_time: start_time, tags: tags)
31
+ Span.new(context, operation_name, @collector, start_time: start_time, tags: tags)
27
32
  end
28
33
 
29
34
  # Inject a SpanContext into the given carrier
@@ -68,13 +73,10 @@ module Jaeger
68
73
  return nil if !trace || trace == ''
69
74
  trace_id, span_id, parent_id, _flags = trace.split(':')
70
75
 
71
- parent_id = parent_id.to_i
72
- parent_id = nil if parent_id == 0
73
-
74
- trace_id = trace_id.to_i
75
- span_id = span_id.to_i
76
+ parent_id = parent_id
77
+ parent_id = nil if parent_id == ''
76
78
 
77
- if trace_id != 0 && span_id != 0
79
+ if trace_id != '' && span_id != ''
78
80
  SpanContext.new(trace_id: trace_id, parent_id: parent_id, span_id: span_id)
79
81
  else
80
82
  nil
@@ -1,130 +1,50 @@
1
+ require_relative './udp_sender/transport'
1
2
  require 'jaeger/thrift/agent'
2
3
  require 'thread'
3
4
 
4
5
  module Jaeger
5
6
  module Client
6
7
  class UdpSender
7
- def initialize(service_name, host, port)
8
+ def initialize(service_name:, host:, port:, collector:, flush_interval:)
8
9
  @service_name = service_name
10
+ @collector = collector
11
+ @flush_interval = flush_interval
9
12
 
10
- emitter = SocketEmitter.new(host, port)
11
- emitter.start
12
- transport = Transport.new(emitter)
13
+ transport = Transport.new(host, port)
13
14
  protocol = ::Thrift::CompactProtocol.new(transport)
14
-
15
15
  @client = Jaeger::Thrift::Agent::Client.new(protocol)
16
16
  end
17
17
 
18
- def send_span(span, end_time)
19
- context = span.context
20
- start_ts, duration = build_timestamps(span, end_time)
18
+ def start
19
+ # Sending spans in a separate thread to avoid blocking the main thread.
20
+ @thread = Thread.new do
21
+ loop do
22
+ emit_batch(@collector.retrieve)
23
+ sleep @flush_interval
24
+ end
25
+ end
26
+ end
27
+
28
+ def stop
29
+ @thread.terminate if @thread
30
+ emit_batch(@collector.retrieve)
31
+ end
32
+
33
+ private
34
+
35
+ def emit_batch(thrift_spans)
36
+ return if thrift_spans.empty?
21
37
 
22
- thrift_span = Jaeger::Thrift::Span.new(
23
- 'traceIdLow' => context.trace_id,
24
- 'traceIdHigh' => context.trace_id,
25
- 'spanId' => context.span_id,
26
- 'parentSpanId' => context.parent_id || 0,
27
- 'operationName' => span.operation_name,
28
- 'references' => [],
29
- 'flags' => context.flags,
30
- 'startTime' => start_ts,
31
- 'duration' => duration,
32
- 'tags' => build_tags(span.tags),
33
- 'logs' => build_logs(span.logs)
34
- )
35
38
  batch = Jaeger::Thrift::Batch.new(
36
39
  'process' => Jaeger::Thrift::Process.new(
37
40
  'serviceName' => @service_name,
38
41
  'tags' => [],
39
42
  ),
40
- 'spans' => [thrift_span]
43
+ 'spans' => thrift_spans
41
44
  )
42
45
 
43
46
  @client.emitBatch(batch)
44
47
  end
45
-
46
- private
47
-
48
- def build_tags(tags)
49
- tags.map {|name, value| build_tag(name, value)}
50
- end
51
-
52
- def build_logs(logs)
53
- logs.map do |timestamp:, fields:|
54
- Jaeger::Thrift::Log.new(
55
- 'timestamp' => (timestamp.to_f * 1_000_000).to_i,
56
- 'fields' => fields.map {|name, value| build_tag(name, value)}
57
- )
58
- end
59
- end
60
-
61
- def build_tag(name, value)
62
- Jaeger::Thrift::Tag.new(
63
- 'key' => name.to_s,
64
- 'vType' => Jaeger::Thrift::TagType::STRING,
65
- 'vStr' => value.to_s
66
- )
67
- end
68
-
69
- def build_timestamps(span, end_time)
70
- start_ts = (span.start_time.to_f * 1_000_000).to_i
71
- end_ts = (end_time.to_f * 1_000_000).to_i
72
- duration = end_ts - start_ts
73
- [start_ts, duration]
74
- end
75
-
76
- class Transport
77
- def initialize(emitter)
78
- @emitter = emitter
79
- @buffer = ::Thrift::MemoryBufferTransport.new
80
- end
81
-
82
- def write(str)
83
- @buffer.write(str)
84
- end
85
-
86
- def flush
87
- @emitter.emit(@buffer.read(@buffer.available))
88
- @buffer.reset_buffer
89
- end
90
-
91
- def open; end
92
- def close; end
93
- end
94
-
95
- class SocketEmitter
96
- FLAGS = 0
97
-
98
- def initialize(host, port)
99
- @socket = UDPSocket.new
100
- @socket.connect(host, port)
101
- @encoded_spans = Queue.new
102
- end
103
-
104
- def emit(encoded_spans)
105
- @encoded_spans << encoded_spans
106
- end
107
-
108
- def start
109
- # Sending spans in a separate thread to avoid blocking the main thread.
110
- Thread.new do
111
- while encoded_span = @encoded_spans.pop
112
- send_bytes(encoded_span)
113
- end
114
- end
115
- end
116
-
117
- private
118
-
119
- def send_bytes(bytes)
120
- @socket.send(bytes, FLAGS)
121
- @socket.flush
122
- rescue Errno::ECONNREFUSED
123
- warn 'Unable to connect to Jaeger Agent'
124
- rescue => e
125
- warn "Unable to send spans: #{e.message}"
126
- end
127
- end
128
48
  end
129
49
  end
130
50
  end
@@ -0,0 +1,38 @@
1
+ module Jaeger
2
+ module Client
3
+ class UdpSender
4
+ class Transport
5
+ FLAGS = 0
6
+
7
+ def initialize(host, port)
8
+ @socket = UDPSocket.new
9
+ @socket.connect(host, port)
10
+ @buffer = ::Thrift::MemoryBufferTransport.new
11
+ end
12
+
13
+ def write(str)
14
+ @buffer.write(str)
15
+ end
16
+
17
+ def flush
18
+ data = @buffer.read(@buffer.available)
19
+ send_bytes(data)
20
+ end
21
+
22
+ def open; end
23
+ def close; end
24
+
25
+ private
26
+
27
+ def send_bytes(bytes)
28
+ @socket.send(bytes, FLAGS)
29
+ @socket.flush
30
+ rescue Errno::ECONNREFUSED
31
+ warn 'Unable to connect to Jaeger Agent'
32
+ rescue => e
33
+ warn "Unable to send spans: #{e.message}"
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -8,8 +8,8 @@ require 'jaeger/client'
8
8
  host = ENV['JAEGER_HOST'] || '127.0.0.1'
9
9
  port = ENV['JAEGER_HOST'] || 6831
10
10
 
11
- tracer1 = Jaeger::Client.build(host: host, port: port.to_i, service_name: 'test-service')
12
- tracer2 = Jaeger::Client.build(host: host, port: port.to_i, service_name: 'downstream-service')
11
+ tracer1 = Jaeger::Client.build(host: host, port: port.to_i, service_name: 'test-service', flush_interval: 1)
12
+ tracer2 = Jaeger::Client.build(host: host, port: port.to_i, service_name: 'downstream-service', flush_interval: 1)
13
13
 
14
14
  outer_span = tracer1.start_span('receive request', tags: {
15
15
  'span.kind' => 'server'
@@ -39,5 +39,8 @@ inner_span.finish
39
39
  sleep 0.1 # doing something with fetched info
40
40
  outer_span.finish
41
41
 
42
- puts "Finishing..."
43
- sleep 3
42
+ tracer1.stop
43
+ tracer2.stop
44
+
45
+ puts "Finished"
46
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jaeger-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SaleMove TechMovers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-22 00:00:00.000000000 Z
11
+ date: 2017-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,11 +99,13 @@ files:
99
99
  - jaeger-client.gemspec
100
100
  - lib/jaeger/client.rb
101
101
  - lib/jaeger/client/carrier.rb
102
+ - lib/jaeger/client/collector.rb
102
103
  - lib/jaeger/client/span.rb
103
104
  - lib/jaeger/client/span_context.rb
104
105
  - lib/jaeger/client/trace_id.rb
105
106
  - lib/jaeger/client/tracer.rb
106
107
  - lib/jaeger/client/udp_sender.rb
108
+ - lib/jaeger/client/udp_sender/transport.rb
107
109
  - script/create_trace
108
110
  - thrift/agent.thrift
109
111
  - thrift/gen-rb/jaeger/thrift/agent.rb
@@ -140,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
142
  version: '0'
141
143
  requirements: []
142
144
  rubyforge_project:
143
- rubygems_version: 2.6.8
145
+ rubygems_version: 2.6.11
144
146
  signing_key:
145
147
  specification_version: 4
146
148
  summary: OpenTracing Tracer implementation for Jaeger in Ruby