opencensus-jaeger 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +77 -0
  3. data/.gitignore +15 -0
  4. data/.gitmodules +3 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +36 -0
  7. data/.travis.yml +7 -0
  8. data/CHANGELOG.md +6 -0
  9. data/CODEOWNERS +13 -0
  10. data/CODE_OF_CONDUCT.md +43 -0
  11. data/Gemfile +3 -0
  12. data/LICENSE +201 -0
  13. data/README.md +72 -0
  14. data/Rakefile +6 -0
  15. data/bin/console +14 -0
  16. data/bin/setup +8 -0
  17. data/examples/.byebug_history +97 -0
  18. data/examples/Gemfile +9 -0
  19. data/examples/Gemfile.lock +46 -0
  20. data/examples/config.ru +15 -0
  21. data/examples/example.rb +25 -0
  22. data/lib/opencensus-jaeger.rb +4 -0
  23. data/lib/opencensus/jaeger.rb +12 -0
  24. data/lib/opencensus/jaeger/version.rb +5 -0
  25. data/lib/opencensus/logging.rb +24 -0
  26. data/lib/opencensus/trace/exporters/jaeger_driver.rb +125 -0
  27. data/lib/opencensus/trace/exporters/jaeger_driver/intermediate_transport.rb +25 -0
  28. data/lib/opencensus/trace/exporters/jaeger_driver/udp_sender.rb +28 -0
  29. data/lib/opencensus/trace/exporters/jaeger_driver/udp_transport.rb +43 -0
  30. data/lib/opencensus/trace/exporters/jaeger_exporter.rb +108 -0
  31. data/opencensus-jaeger.gemspec +45 -0
  32. data/scripts/publish.rb +7 -0
  33. data/scripts/rename_collectors.rb +88 -0
  34. data/thrift/agent.thrift +27 -0
  35. data/thrift/gen-rb/jaeger/thrift/agent.rb +117 -0
  36. data/thrift/gen-rb/jaeger/thrift/agent_constants.rb +13 -0
  37. data/thrift/gen-rb/jaeger/thrift/agent_types.rb +14 -0
  38. data/thrift/gen-rb/jaeger/thrift/collector.rb +82 -0
  39. data/thrift/gen-rb/jaeger/thrift/jaeger_constants.rb +13 -0
  40. data/thrift/gen-rb/jaeger/thrift/jaeger_types.rb +213 -0
  41. data/thrift/gen-rb/zipkin/zipkin_collector.rb +80 -0
  42. data/thrift/gen-rb/zipkin/zipkincore_constants.rb +43 -0
  43. data/thrift/gen-rb/zipkin/zipkincore_types.rb +223 -0
  44. data/thrift/jaeger.thrift +86 -0
  45. data/thrift/zipkincore.thrift +345 -0
  46. metadata +161 -0
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "opencensus/jaeger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,97 @@
1
+ c
2
+ continue
3
+ next
4
+ step
5
+ next
6
+ a.running?
7
+ a=OpenCensus.config.trace.exporter.instance_variable_get('@executor')
8
+ a.running?
9
+ next
10
+ a.running?
11
+ a=OpenCensus.config.trace.exporter.instance_variable_get('@executor')
12
+ a = _
13
+ OpenCensus.config.trace.exporter.instance_variable_get('@executor')
14
+ OpenCensus.config.trace.exporter.instance_variable_get('executor')
15
+ OpenCensus.config.trace.exporter.instance_variable_get(:executor)
16
+ OpenCensus.config.trace.exporter.running?
17
+ OpenCensus.config.trace.exporter
18
+ OpenCensus.config.trace.methods
19
+ OpenCensus.config.trace
20
+ OpenCensus.config.trcae
21
+ OpenCensus.config.methods
22
+ OpenCensus.config
23
+ next
24
+ value
25
+ field.value
26
+ next
27
+ field
28
+ field.exporter
29
+ field
30
+ key
31
+ next
32
+ key
33
+ @fields
34
+ field
35
+ n
36
+ 'n
37
+ n
38
+ next
39
+ field
40
+ key
41
+ next
42
+ key
43
+ field.exporter.methods
44
+ field.exporter
45
+ field.methods
46
+ field
47
+ field.is_a? Config
48
+ field.value
49
+ @fields
50
+ field
51
+ fiel
52
+ next
53
+ backtrace
54
+ step
55
+ puts "#{c}"
56
+ step
57
+ next
58
+ step
59
+ c
60
+ step
61
+ next
62
+ step
63
+ continue
64
+ require 'jaeger/client'
65
+ continue
66
+ Jaeger.constants
67
+ Jaeger::Thrift
68
+ $LOAD_PATH
69
+ require_relative 'jaeger/client/../../thrift/gen-rb'
70
+ require 'jaeger/client/../../thrift/gen-rb'
71
+ require 'jaeger/client'
72
+ require 'jaeger/client/../../thrift/gen-rb'
73
+ require_relative 'jaeger/client/../../thrift/gen-rb'
74
+ require 'jaeger/client/../../thrift/gen-rb'
75
+ require 'jaeger/client'
76
+ require_relative 'jaeger/client/../../thrift/gen-rb'
77
+ __FILE__
78
+ File.dirname(__FILE__)
79
+ require 'jaeger'
80
+ $LOAD_PATH.push(File.dirname(__FILE__)+ '/../../thrift/gen-rb')
81
+ '
82
+ require 'jaeger/thrift/gen-rb/'
83
+ require 'jaeger/thrift/agent'
84
+ $LOAD_PATH.push(File.dirname(__FILE__)+ '/../../thrift/gen-rb')
85
+ require 'opentracing'
86
+ require 'jaeger/thrift/agent'
87
+ Jaeger::Thrift
88
+ Jaeger::Span.constants
89
+ Jaeger::Span.constant
90
+ Jaeger::Span.class
91
+ Jaeger::Span
92
+ Jaeger.constants
93
+ Jaeger.class
94
+ Jaeger.type
95
+ Jaeger.tyoe
96
+ Jaeger.methods
97
+ Jaeger
data/examples/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem 'opencensus'
6
+ gem 'sinatra'
7
+ gem 'thin'
8
+ gem 'faraday'
9
+ gem 'opencensus-jaeger', path: "../"
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ opencensus-jaeger (0.2.0)
5
+ opencensus (~> 0.4.0)
6
+ thrift (~> 0.11.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ daemons (1.3.1)
12
+ eventmachine (1.2.7)
13
+ faraday (0.15.4)
14
+ multipart-post (>= 1.2, < 3)
15
+ multipart-post (2.0.0)
16
+ mustermann (1.0.3)
17
+ opencensus (0.4.0)
18
+ rack (2.0.6)
19
+ rack-protection (2.0.5)
20
+ rack
21
+ sinatra (2.0.5)
22
+ mustermann (~> 1.0)
23
+ rack (~> 2.0)
24
+ rack-protection (= 2.0.5)
25
+ tilt (~> 2.0)
26
+ thin (1.7.2)
27
+ daemons (~> 1.0, >= 1.0.9)
28
+ eventmachine (~> 1.0, >= 1.0.4)
29
+ rack (>= 1, < 3)
30
+ thrift (0.11.0.0)
31
+ tilt (2.0.9)
32
+
33
+ PLATFORMS
34
+ ruby
35
+ x86_64-darwin-17
36
+ x86_64-darwin-18
37
+
38
+ DEPENDENCIES
39
+ faraday
40
+ opencensus
41
+ opencensus-jaeger!
42
+ sinatra
43
+ thin
44
+
45
+ BUNDLED WITH
46
+ 1.17.3
@@ -0,0 +1,15 @@
1
+ require 'opencensus/jaeger'
2
+ require 'opencensus/trace/integrations/rack_middleware'
3
+ require 'opencensus/trace/integrations/faraday_middleware'
4
+ require_relative './example.rb'
5
+
6
+ OpenCensus.configure do |c|
7
+ c.trace.exporter = OpenCensus::Trace::Exporters::JaegerExporter.new \
8
+ service_name: 'test_service',
9
+ host: 'localhost',
10
+ port: 6831,
11
+ tags: { 'example_tag': 'hello_world' }
12
+ end
13
+
14
+ use OpenCensus::Trace::Integrations::RackMiddleware
15
+ run Example
@@ -0,0 +1,25 @@
1
+ require 'sinatra/base'
2
+
3
+ class Example < Sinatra::Base
4
+ get '/' do
5
+ 'Hello world'
6
+ end
7
+
8
+ get "/lengthy" do
9
+ # Configure this Faraday connection with a middleware to trace outgoing
10
+ # requests.
11
+ conn = Faraday.new(url: "http://www.google.com") do |c|
12
+ c.use OpenCensus::Trace::Integrations::FaradayMiddleware
13
+ c.adapter Faraday.default_adapter
14
+ end
15
+ conn.get "/"
16
+
17
+ # You may instrument your code to create custom spans for long-running
18
+ # operations.
19
+ OpenCensus::Trace.in_span "long task" do
20
+ sleep rand
21
+ end
22
+
23
+ "Done!"
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module OpenCensus
2
+ end
3
+
4
+ require 'opencensus/jaeger'
@@ -0,0 +1,12 @@
1
+ require 'opencensus'
2
+
3
+ module OpenCensus
4
+ end
5
+
6
+ thrift_folder = File.expand_path(__dir__ + '../../../thrift/gen-rb')
7
+ $LOAD_PATH.unshift(thrift_folder) unless $LOAD_PATH.include?(thrift_folder)
8
+
9
+ require 'jaeger/thrift/agent'
10
+ require 'opencensus/jaeger/version'
11
+ require 'opencensus/logging'
12
+ require 'opencensus/trace/exporters/jaeger_exporter'
@@ -0,0 +1,5 @@
1
+ module OpenCensus
2
+ module Jaeger
3
+ VERSION = '0.2.1'.freeze
4
+ end
5
+ end
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+
3
+ module Logging
4
+ def default_logger
5
+ Logging.logger
6
+ end
7
+
8
+ def self.logger
9
+ @logger ||= Logger.new(STDOUT)
10
+ @logger.level = ENV['LOG_LEVEL'].to_i || Logger::INFO
11
+ at_exit do
12
+ @logger.close
13
+ end
14
+ @logger
15
+ end
16
+
17
+ def self.included(base)
18
+ class << base
19
+ def logger
20
+ Logging.logger
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,125 @@
1
+ require 'socket'
2
+ require 'thrift'
3
+ require 'opencensus/trace/exporters/jaeger_driver/intermediate_transport'
4
+ require 'opencensus/trace/exporters/jaeger_driver/udp_sender'
5
+
6
+ module OpenCensus
7
+ module Trace
8
+ module Exporters
9
+ module JaegerDriver
10
+ extend self
11
+
12
+ MAX_64BIT_SIGNED_INT = (1 << 63) - 1
13
+ MAX_64BIT_UNSIGNED_INT = (1 << 64) - 1
14
+ TRACE_ID_UPPER_BOUND = MAX_64BIT_UNSIGNED_INT + 1
15
+
16
+ FIELDS = ::Jaeger::Thrift::Tag::FIELDS
17
+ KEY = FIELDS[::Jaeger::Thrift::Tag::KEY].fetch(:name)
18
+ VTYPE = FIELDS[::Jaeger::Thrift::Tag::VTYPE].fetch(:name)
19
+ VLONG = FIELDS[::Jaeger::Thrift::Tag::VLONG].fetch(:name)
20
+ VDOUBLE = FIELDS[::Jaeger::Thrift::Tag::VDOUBLE].fetch(:name)
21
+ VBOOL = FIELDS[::Jaeger::Thrift::Tag::VBOOL].fetch(:name)
22
+ VSTR = FIELDS[::Jaeger::Thrift::Tag::VSTR].fetch(:name)
23
+
24
+ def encode_span(span)
25
+ tags = build_thrift_tags(span.attributes)
26
+ trace_id_high = base16_hex_to_int64(
27
+ span.trace_id.slice(0, 16)
28
+ )
29
+ trace_id_low = base16_hex_to_int64(
30
+ span.trace_id.slice(16)
31
+ )
32
+ span_id = base16_hex_to_int64(
33
+ span.span_id
34
+ )
35
+ parent_span_id = base16_hex_to_int64(
36
+ span.parent_span_id
37
+ )
38
+ operation_name = span.name.value
39
+ references = []
40
+ flags = 0x01
41
+ start_time = span.start_time.to_f * 1_000_000
42
+ end_time = span.end_time.to_f * 1_000_000
43
+ duration = end_time - start_time
44
+
45
+ ::Jaeger::Thrift::Span.new(
46
+ 'traceIdLow': trace_id_low,
47
+ 'traceIdHigh': trace_id_high,
48
+ 'spanId': span_id,
49
+ 'parentSpanId': parent_span_id,
50
+ 'operationName': operation_name,
51
+ 'references': references,
52
+ 'flags': flags,
53
+ 'startTime': start_time,
54
+ 'duration': duration,
55
+ 'tags': tags
56
+ )
57
+ rescue StandardError => e
58
+ puts "convert error #{e}"
59
+ end
60
+
61
+ def ip_v4
62
+ ip_v4 = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }
63
+ ip_v4.nil? ? nil : ip_v4.ip_address
64
+ end
65
+
66
+ def base16_hex_id_to_uint64(id)
67
+ return nil unless id
68
+ value = id.to_i(16)
69
+ value > MAX_64BIT_UNSIGNED_INT || value < 0 ? 0 : value
70
+ end
71
+
72
+ # Thrift defines ID fields as i64, which is signed, therefore we convert
73
+ # large IDs (> 2^63) to negative longs
74
+ def uint64_id_to_int64(id)
75
+ id > MAX_64BIT_SIGNED_INT ? id - MAX_64BIT_UNSIGNED_INT - 1 : id
76
+ end
77
+
78
+ def base16_hex_to_int64(id)
79
+ uint64_id = base16_hex_id_to_uint64(id)
80
+ uint64_id_to_int64(uint64_id)
81
+ end
82
+
83
+ def build_thrift_tags(attributes)
84
+ attributes.collect do |key, value|
85
+ begin
86
+ if value.is_a?(OpenCensus::Trace::TruncatableString)
87
+ ::Jaeger::Thrift::Tag.new(
88
+ KEY => key.to_s,
89
+ VTYPE => ::Jaeger::Thrift::TagType::STRING,
90
+ VSTR => value.value
91
+ )
92
+ elsif value.is_a?(Integer)
93
+ ::Jaeger::Thrift::Tag.new(
94
+ KEY => key.to_s,
95
+ VTYPE => ::Jaeger::Thrift::TagType::LONG,
96
+ VLONG => value
97
+ )
98
+ elsif value.is_a?(Float)
99
+ ::Jaeger::Thrift::Tag.new(
100
+ KEY => key.to_s,
101
+ VTYPE => ::Jaeger::Thrift::TagType::DOUBLE,
102
+ VDOUBLE => value
103
+ )
104
+ elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
105
+ ::Jaeger::Thrift::Tag.new(
106
+ KEY => key.to_s,
107
+ VTYPE => ::Jaeger::Thrift::TagType::BOOL,
108
+ VBOOL => value
109
+ )
110
+ else
111
+ ::Jaeger::Thrift::Tag.new(
112
+ KEY => key.to_s,
113
+ VTYPE => ::Jaeger::Thrift::TagType::STRING,
114
+ VSTR => value.to_s
115
+ )
116
+ end
117
+ rescue StandardError => error
118
+ logger.error "Cannot build thrift tags for #{key}:#{value}, error: #{error}"
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,25 @@
1
+ module OpenCensus
2
+ module Trace
3
+ module Exporters
4
+ module JaegerDriver
5
+ class IntermediateTransport
6
+ attr_accessor :size
7
+
8
+ def initialize
9
+ @size = 0
10
+ end
11
+
12
+ def write(buf)
13
+ @size += buf.size
14
+ end
15
+
16
+ def flush
17
+ @size = 0
18
+ end
19
+
20
+ def close; end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'opencensus/trace/exporters/jaeger_driver/udp_transport'
2
+
3
+ module OpenCensus
4
+ module Trace
5
+ module Exporters
6
+ module JaegerDriver
7
+ class UDPSender
8
+ include ::Logging
9
+
10
+ def initialize(host, port, logger, protocol_class)
11
+ @logger = logger || default_logger
12
+ @host = host
13
+ @port = port
14
+ @transport = UDPTransport.new(host, port, logger)
15
+ @protocol = protocol_class.new(@transport)
16
+ @client = ::Jaeger::Thrift::Agent::Client.new(@protocol)
17
+ end
18
+
19
+ def send_spans(spans)
20
+ @client.emitBatch(spans)
21
+ rescue StandardError => error
22
+ @logger.error "Failure while sending a batch of spans: #{error}"
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end