logasm-tracer 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/logasm/tracer.rb +86 -0
- data/lib/logasm/tracer/span.rb +78 -0
- data/lib/logasm/tracer/span_context.rb +28 -0
- data/lib/logasm/tracer/trace_id.rb +13 -0
- data/lib/logasm/tracer/version.rb +5 -0
- data/logasm-tracer.gemspec +27 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b83a1b19cff46ec4102bdcad4135e3c9ea161a3
|
4
|
+
data.tar.gz: b15354fc51d177e3472deec5ebce7bbc9c0d256a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9300ace2170c9951c74c28d97e3ecd0ab5e9414621896c8c27fd09459ede42cb71584bd697826bbf4adff5b844f8d4dbcd7745251522f876a6c254c16212a2a0
|
7
|
+
data.tar.gz: cec4b7f0baddd5aede0aa60bdfc9cd7802ddd41e82772693a0d68230fa66ed1d3ce7f8e3b4988b2a8fce52ce5f4796c011e7fd18ee228691d50692d2bea2cbeb
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Logasm::Tracer
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'logasm-tracer'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install logasm-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/logasm-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
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "logasm/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__)
|
data/bin/setup
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'logasm/tracer/version'
|
2
|
+
require 'logasm/tracer/span'
|
3
|
+
require 'logasm/tracer/span_context'
|
4
|
+
require 'logasm/tracer/trace_id'
|
5
|
+
|
6
|
+
class Logasm
|
7
|
+
# Implements OpenTracing API
|
8
|
+
#
|
9
|
+
# See https://github.com/opentracing/opentracing-ruby/blob/master/lib/opentracing.rb
|
10
|
+
# and https://github.com/opentracing/opentracing-ruby/blob/master/lib/opentracing/tracer.rb
|
11
|
+
class Tracer
|
12
|
+
FORMAT_TEXT_MAP = 1
|
13
|
+
FORMAT_BINARY = 2
|
14
|
+
FORMAT_RACK = 3
|
15
|
+
|
16
|
+
def initialize(logger)
|
17
|
+
@logger = logger
|
18
|
+
end
|
19
|
+
|
20
|
+
# Starts a new span.
|
21
|
+
#
|
22
|
+
# @param operation_name [String] The operation name for the Span
|
23
|
+
# @param child_of [SpanContext] SpanContext that acts as a parent to
|
24
|
+
# the newly-started Span. If a Span instance is provided, its
|
25
|
+
# .span_context is automatically substituted.
|
26
|
+
# @param start_time [Time] When the Span started, if not now
|
27
|
+
# @param tags [Hash] Tags to assign to the Span at start time
|
28
|
+
#
|
29
|
+
# @return [Span] The newly-started Span
|
30
|
+
def start_span(operation_name, child_of: nil, start_time: Time.now, **)
|
31
|
+
context =
|
32
|
+
if child_of
|
33
|
+
SpanContext.create_from_parent_context(child_of)
|
34
|
+
else
|
35
|
+
SpanContext.create_parent_context
|
36
|
+
end
|
37
|
+
|
38
|
+
Span.new(context, operation_name, @logger, start_time: start_time)
|
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 "Logasm::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
|
+
SpanContext.new(
|
70
|
+
trace_id: carrier['trace-id'],
|
71
|
+
parent_id: carrier['parent-id'],
|
72
|
+
span_id: carrier['span-id']
|
73
|
+
)
|
74
|
+
when FORMAT_RACK
|
75
|
+
SpanContext.new(
|
76
|
+
trace_id: carrier['X-Trace-Id'],
|
77
|
+
parent_id: carrier['X-Trace-Parent-Id'],
|
78
|
+
span_id: carrier['X-Trace-Span-Id']
|
79
|
+
)
|
80
|
+
else
|
81
|
+
@logger.error "Logasm::Tracer with format #{format} is not supported yet"
|
82
|
+
nil
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
class Logasm
|
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
|
+
def initialize(context, operation_name, logger, start_time: Time.now)
|
8
|
+
@context = context
|
9
|
+
@operation_name = operation_name
|
10
|
+
@logger = logger
|
11
|
+
@start_time = start_time
|
12
|
+
@logger.info "Span [#{@operation_name}] started", trace_information
|
13
|
+
end
|
14
|
+
|
15
|
+
# Set a tag value on this span
|
16
|
+
#
|
17
|
+
# @param key [String] the key of the tag
|
18
|
+
# @param value [String, Numeric, Boolean] the value of the tag. If it's not
|
19
|
+
# a String, Numeric, or Boolean it will be encoded with to_s
|
20
|
+
def set_tag(key, value)
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set a baggage item on the span
|
25
|
+
#
|
26
|
+
# @param key [String] the key of the baggage item
|
27
|
+
# @param value [String] the value of the baggage item
|
28
|
+
def set_baggage_item(key, value)
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
# Get a baggage item
|
33
|
+
#
|
34
|
+
# @param key [String] the key of the baggage item
|
35
|
+
#
|
36
|
+
# @return Value of the baggage item
|
37
|
+
def get_baggage_item(key)
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
|
41
|
+
# Add a log entry to this span
|
42
|
+
#
|
43
|
+
# @param event [String] event name for the log
|
44
|
+
# @param timestamp [Time] time of the log
|
45
|
+
# @param fields [Hash] Additional information to log
|
46
|
+
def log(event: nil, timestamp: Time.now, **fields)
|
47
|
+
@logger.info "Span [#{@operation_name}] #{event}", trace_information.merge(fields)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Finish the {Span}
|
51
|
+
#
|
52
|
+
# @param end_time [Time] custom end time, if not now
|
53
|
+
def finish(end_time: Time.now)
|
54
|
+
@logger.info "Span [#{@operation_name}] finished", trace_information(
|
55
|
+
execution_time: end_time - @start_time
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
# @deprecated This is not in OpenTracing API. It will be removed soon.
|
60
|
+
def to_h
|
61
|
+
trace_information
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def trace_information(additional_fields = {})
|
67
|
+
{
|
68
|
+
trace: {
|
69
|
+
id: @context.trace_id,
|
70
|
+
parent_id: @context.parent_id,
|
71
|
+
span_id: @context.span_id,
|
72
|
+
operation_name: @operation_name
|
73
|
+
}.merge(additional_fields)
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Logasm
|
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 Logasm
|
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
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'logasm/tracer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "logasm-tracer"
|
8
|
+
spec.version = Logasm::Tracer::VERSION
|
9
|
+
spec.authors = ["SaleMove TechMovers"]
|
10
|
+
spec.email = ["techmovers@salemove.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: logasm-tracer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SaleMove TechMovers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-29 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
|
+
- lib/logasm/tracer.rb
|
72
|
+
- lib/logasm/tracer/span.rb
|
73
|
+
- lib/logasm/tracer/span_context.rb
|
74
|
+
- lib/logasm/tracer/trace_id.rb
|
75
|
+
- lib/logasm/tracer/version.rb
|
76
|
+
- logasm-tracer.gemspec
|
77
|
+
homepage: ''
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.8
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: ''
|
101
|
+
test_files: []
|