lenjador-tracer 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a2163f7497b6b7ef5199299d1d79c48b69eee3d
4
+ data.tar.gz: bfa8078fc0a7789bf6c2c67b80eb1987e262a29d
5
+ SHA512:
6
+ metadata.gz: bd0fcd0c29ddf0801907398c61bb1f322af6765237cd3401828f6ef753c95e5632c8984ea8c5d8388324dbdaa8de793b725d48bb1d9342777cd333514420661e
7
+ data.tar.gz: e15d12956704b896efc394e710febad10a4305199bc6ba89ec55c79bbb5eca775befaed47f35ab9c25c7a01c05121202ab0bcb0c8ca4bc85c508db3460a83e27
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.1
4
+ - jruby-9.1.5.0
5
+ before_install: gem install bundler
6
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lenjador-tracer.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 SaleMove
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Lenjador::Tracer
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'lenjador-tracer'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lenjador-tracer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/salemove/lenjador-tracer.
32
+
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
37
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lenjador/tracer"
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__)
@@ -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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lenjador-tracer"
7
+ spec.version = "0.3.0"
8
+ spec.authors = ["SaleMove TechMovers"]
9
+ spec.email = ["techmovers@salemove.com"]
10
+
11
+ spec.summary = %q{}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.14"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,94 @@
1
+ require 'lenjador/tracer/span'
2
+ require 'lenjador/tracer/span_context'
3
+ require 'lenjador/tracer/trace_id'
4
+
5
+ class Lenjador
6
+ # Implements OpenTracing API
7
+ #
8
+ # See https://github.com/opentracing/opentracing-ruby/blob/master/lib/opentracing.rb
9
+ # and https://github.com/opentracing/opentracing-ruby/blob/master/lib/opentracing/tracer.rb
10
+ class Tracer
11
+ FORMAT_TEXT_MAP = 1
12
+ FORMAT_BINARY = 2
13
+ FORMAT_RACK = 3
14
+
15
+ def initialize(logger)
16
+ @logger = logger
17
+ end
18
+
19
+ # Starts a new span.
20
+ #
21
+ # @param operation_name [String] The operation name for the Span
22
+ # @param child_of [SpanContext, Span] SpanContext that acts as a parent to
23
+ # the newly-started Span. If a Span instance is provided, its
24
+ # context is automatically substituted.
25
+ # @param start_time [Time] When the Span started, if not now
26
+ # @param tags [Hash] Tags to assign to the Span at start time
27
+ #
28
+ # @return [Span] The newly-started Span
29
+ def start_span(operation_name, child_of: nil, start_time: Time.now, tags: {}, **)
30
+ context =
31
+ if child_of
32
+ parent_context = child_of.respond_to?(:context) ? child_of.context : child_of
33
+ SpanContext.create_from_parent_context(parent_context)
34
+ else
35
+ SpanContext.create_parent_context
36
+ end
37
+
38
+ Span.new(context, operation_name, @logger, start_time: start_time, tags: tags)
39
+ end
40
+
41
+ # Inject a SpanContext into the given carrier
42
+ #
43
+ # @param span_context [SpanContext]
44
+ # @param format [OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_BINARY, OpenTracing::FORMAT_RACK]
45
+ # @param carrier [Carrier] A carrier object of the type dictated by the specified `format`
46
+ def inject(span_context, format, carrier)
47
+ case format
48
+ when FORMAT_TEXT_MAP
49
+ carrier['trace-id'] = span_context.trace_id
50
+ carrier['parent-id'] = span_context.parent_id
51
+ carrier['span-id'] = span_context.span_id
52
+ when FORMAT_RACK
53
+ carrier['X-Trace-Id'] = span_context.trace_id
54
+ carrier['X-Trace-Parent-Id'] = span_context.parent_id
55
+ carrier['X-Trace-Span-Id'] = span_context.span_id
56
+ else
57
+ @logger.error "Lenjador::Tracer with format #{format} is not supported yet"
58
+ end
59
+ end
60
+
61
+ # Extract a SpanContext in the given format from the given carrier.
62
+ #
63
+ # @param format [OpenTracing::FORMAT_TEXT_MAP, OpenTracing::FORMAT_BINARY, OpenTracing::FORMAT_RACK]
64
+ # @param carrier [Carrier] A carrier object of the type dictated by the specified `format`
65
+ # @return [SpanContext] the extracted SpanContext or nil if none could be found
66
+ def extract(format, carrier)
67
+ case format
68
+ when FORMAT_TEXT_MAP
69
+ trace_id = carrier['trace-id']
70
+ parent_id = carrier['parent-id']
71
+ span_id = carrier['span-id']
72
+
73
+ if trace_id && span_id
74
+ SpanContext.new(trace_id: trace_id, parent_id: parent_id, span_id: span_id)
75
+ else
76
+ nil
77
+ end
78
+ when FORMAT_RACK
79
+ trace_id = carrier['HTTP_X_TRACE_ID']
80
+ parent_id = carrier['HTTP_X_TRACE_PARENT_ID']
81
+ span_id = carrier['HTTP_X_TRACE_SPAN_ID']
82
+
83
+ if trace_id && span_id
84
+ SpanContext.new(trace_id: trace_id, parent_id: parent_id, span_id: span_id)
85
+ else
86
+ nil
87
+ end
88
+ else
89
+ @logger.error "Lenjador::Tracer with format #{format} is not supported yet"
90
+ nil
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,90 @@
1
+ class Lenjador
2
+ class Tracer
3
+ # https://github.com/opentracing/opentracing-ruby/blob/master/lib/opentracing/span.rb
4
+ class Span
5
+ attr_reader :context
6
+
7
+ attr_writer :operation_name
8
+
9
+ def initialize(context, operation_name, logger, start_time: Time.now, tags: {})
10
+ @context = context
11
+ @operation_name = operation_name
12
+ @logger = logger
13
+ @start_time = start_time
14
+ @tags = tags
15
+ @logger.info "Span [#{@operation_name}] started", trace_information
16
+ end
17
+
18
+ # Set a tag value on this span
19
+ #
20
+ # @param key [String] the key of the tag
21
+ # @param value [String, Numeric, Boolean] the value of the tag. If it's not
22
+ # a String, Numeric, or Boolean it will be encoded with to_s
23
+ def set_tag(key, value)
24
+ @tags = @tags.merge(key => value)
25
+ end
26
+
27
+ # Set a baggage item on the span
28
+ #
29
+ # @param key [String] the key of the baggage item
30
+ # @param value [String] the value of the baggage item
31
+ def set_baggage_item(key, value)
32
+ self
33
+ end
34
+
35
+ # Get a baggage item
36
+ #
37
+ # @param key [String] the key of the baggage item
38
+ #
39
+ # @return Value of the baggage item
40
+ def get_baggage_item(key)
41
+ nil
42
+ end
43
+
44
+ # Add a log entry to this span
45
+ #
46
+ # @param event [String] event name for the log
47
+ # @param timestamp [Time] time of the log
48
+ # @param fields [Hash] Additional information to log
49
+ def log(event: nil, timestamp: Time.now, **fields)
50
+ @logger.info "Span [#{@operation_name}] #{event}", trace_information.merge(fields)
51
+ end
52
+
53
+ # Add a log entry to this span
54
+ #
55
+ # @param timestamp [Time] time of the log
56
+ # @param fields [Hash] Additional information to log
57
+ def log_kv(timestamp: Time.now, **fields)
58
+ event = fields.fetch(:event, 'unknown')
59
+ @logger.info "Span [#{@operation_name}] #{event}", trace_information.merge(fields)
60
+ end
61
+
62
+ # Finish the {Span}
63
+ #
64
+ # @param end_time [Time] custom end time, if not now
65
+ def finish(end_time: Time.now)
66
+ @logger.info "Span [#{@operation_name}] finished", trace_information(
67
+ execution_time: end_time - @start_time
68
+ )
69
+ end
70
+
71
+ # @deprecated This is not in OpenTracing API. It will be removed soon.
72
+ def to_h
73
+ trace_information
74
+ end
75
+
76
+ private
77
+
78
+ def trace_information(additional_fields = {})
79
+ {
80
+ trace: {
81
+ id: @context.trace_id,
82
+ parent_id: @context.parent_id,
83
+ span_id: @context.span_id,
84
+ operation_name: @operation_name
85
+ }.merge(additional_fields).merge(@tags)
86
+ }
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,28 @@
1
+ class Lenjador
2
+ class Tracer
3
+ # SpanContext holds the data for a span that gets inherited to child spans
4
+ class SpanContext
5
+ def self.create_parent_context
6
+ trace_id = TraceId.generate
7
+ span_id = TraceId.generate
8
+ new(trace_id: trace_id, span_id: span_id)
9
+ end
10
+
11
+ def self.create_from_parent_context(span_context)
12
+ trace_id = span_context.trace_id
13
+ parent_id = span_context.span_id
14
+ span_id = TraceId.generate
15
+ new(span_id: span_id, parent_id: parent_id, trace_id: trace_id)
16
+ end
17
+
18
+ attr_reader :span_id, :parent_id, :trace_id, :baggage
19
+
20
+ def initialize(trace_id:, parent_id: nil, span_id:, baggage: {})
21
+ @span_id = span_id
22
+ @parent_id = parent_id
23
+ @trace_id = trace_id
24
+ @baggage = baggage
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ class Lenjador
2
+ class Tracer
3
+ module TraceId
4
+ TRACE_ID_UPPER_BOUND = 2 ** 64
5
+
6
+ # Generates 64-bit lower-hex encoded ID. This was chosen to be compatible
7
+ # with tracing frameworks like zipkin.
8
+ def self.generate
9
+ rand(TRACE_ID_UPPER_BOUND).to_s(16)
10
+ end
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lenjador-tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - SaleMove TechMovers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: ''
56
+ email:
57
+ - techmovers@salemove.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lenjador-tracer.gemspec
72
+ - lib/lenjador/tracer.rb
73
+ - lib/lenjador/tracer/span.rb
74
+ - lib/lenjador/tracer/span_context.rb
75
+ - lib/lenjador/tracer/trace_id.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.8
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: ''
100
+ test_files: []