hubstep 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 12049128293bd8dd6ebfd5a63eb7bab3d0d07307
4
+ data.tar.gz: 335afba8aa1d6a9f4376ce9af4309fe410491e5e
5
+ SHA512:
6
+ metadata.gz: fad83ef49dc17ffb0a16ed7244ce3723d396cc5248832f0ddbb95efa5a4df207645c93fea20e1902639dcb3a70e9bc8ff028f71f419a344db19fe206479f366e
7
+ data.tar.gz: 2babb2d26a745b96661f5c65833dfb316228f23ef812ec7d918d17259e687c3a2c2553d678caad9b8cae0632e6649c4e3235b9e2cf7bc49fbd52372c3df1748c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /bin/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ /vendor/gems/
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ Exclude:
4
+ - "bin/**/*"
5
+ - "vendor/**/*"
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - "hubstep.gemspec"
10
+
11
+ Metrics/ClassLength:
12
+ Exclude:
13
+ - "test/**/*"
14
+
15
+ Metrics/LineLength:
16
+ Max: 100
17
+
18
+ Metrics/MethodLength:
19
+ Max: 15
20
+
21
+ Style/DoubleNegation:
22
+ Enabled: false
23
+
24
+ Style/EmptyMethod:
25
+ EnforcedStyle: expanded
26
+
27
+ Style/HashSyntax:
28
+ EnforcedStyle: ruby19_no_mixed_keys
29
+
30
+ Style/IfUnlessModifier:
31
+ Enabled: false
32
+
33
+ Style/SpaceInsideBlockBraces:
34
+ EnforcedStyleForEmptyBraces: space
35
+
36
+ Style/StringLiterals:
37
+ EnforcedStyle: double_quotes
38
+ ConsistentQuotesInMultiline: true
39
+
40
+ Style/StringLiteralsInInterpolation:
41
+ EnforcedStyle: double_quotes
42
+
43
+ Style/TrailingCommaInLiteral:
44
+ EnforcedStyleForMultiline: comma
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.3
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ notifications:
3
+ email: false
4
+ language: ruby
5
+ rvm:
6
+ - 2.3.3
7
+ before_install: gem install bundler -v 1.13.6
8
+ script:
9
+ - bundle exec rake test
10
+ - bundle exec rubocop
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at adam@roben.org. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+
4
+ # Specify your gem's dependencies in hubstep.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Adam Roben
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,131 @@
1
+ # HubStep
2
+
3
+ HubStep provides standard tracing via [LightStep][] for GitHub-style Ruby apps.
4
+
5
+ [LightStep]: http://lightstep.com/
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hubstep'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hubstep
22
+
23
+ ## Usage
24
+
25
+ The core of HubStep is [`HubStep::Tracer`][tracer]. This wraps
26
+ `LightStep::Tracer` and provides a block-based API for tracing, and the ability
27
+ to enable/disable tracing at runtime. Typical usage looks something like:
28
+
29
+ ```ruby
30
+ require "hubstep"
31
+
32
+ module MyApp
33
+ def self.tracer
34
+ @tracer ||= HubStep::Tracer.new(tags: { "environment" => MyApp.environment })
35
+ end
36
+
37
+ class Fribble
38
+ def frob
39
+ MyApp.tracer.span("fribble.frob") do |span|
40
+ # real work goes here
41
+ span.set_tag("result", "success")
42
+ end
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ [tracer]: /lib/hubstep/tracer.rb
49
+
50
+ In addition to explicit tracing like in the above example, HubStep provides
51
+ automatic instrumentation for various libraries.
52
+
53
+ ### ActiveSupport::Notifications
54
+
55
+ [`HubStep::Instrumenter`][instrumenter] wraps `ActiveSupport::Notifications`
56
+ and automatically creates spans for any blocks passed to `#instrument`:
57
+
58
+ ```ruby
59
+ require "hubstep/instrumenter"
60
+
61
+ module MyApp
62
+ def self.instrumenter
63
+ @instrumenter ||= HubStep::Instrumenter.new(self.tracer, ActiveSupport::Notifications)
64
+ end
65
+
66
+ class Fribble
67
+ def frob
68
+ MyApp.instrumenter.instrument("fribble.frob") do |payload, span|
69
+ # real work goes here
70
+ span.set_tag("result", "success")
71
+ end
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
77
+ [instrumenter]: /lib/hubstep/instrumenter.rb
78
+
79
+ ### Faraday
80
+
81
+ [`HubStep::Faraday::Middlware`][fm] is a [Faraday][] middleware that wraps each
82
+ request in a span.
83
+
84
+ ```ruby
85
+ require "hubstep/faraday/middleware"
86
+
87
+ module MyApp
88
+ def self.faraday
89
+ @faraday ||= Faraday.new do |b|
90
+ b.request(:hubstep)
91
+ b.adapter(:typhoeus)
92
+ end
93
+ end
94
+ end
95
+ ```
96
+
97
+ [fm]: /lib/hubstep/faraday/middleware.rb
98
+ [Faraday]: https://github.com/lostisland/faraday
99
+
100
+ ### Rack/Sinatra
101
+
102
+ [`HubStep::Rack::Middleware`][rm] is a [Rack][]/[Sinatra][] middleware that wraps
103
+ each request in a span. This middleware also enables or disables tracing for
104
+ the duration of a request based on the return value of the proc passed to it.
105
+
106
+ ```ruby
107
+ # config.ru
108
+
109
+ require "hubstep/rack/middleware"
110
+
111
+ use HubStep::Rack::Middleware, MyApp.tracer, ->(env) { MyApp.tracing_enabled?(env) }
112
+ ```
113
+
114
+ [rm]: /lib/hubstep/rack/middleware.rb
115
+ [Rack]: http://rack.github.io/
116
+ [Sinatra]: http://www.sinatrarb.com/
117
+
118
+ ## Development
119
+
120
+ After checking out the repo, run `script/bootstrap` to install dependencies. Then, run `rake test` to run the tests. You can also run `script/console` for an interactive prompt that will allow you to experiment.
121
+
122
+ 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).
123
+
124
+ ## Contributing
125
+
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/github/hubstep. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
127
+
128
+
129
+ ## License
130
+
131
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task default: :test
data/hubstep.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "hubstep/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "hubstep"
9
+ spec.version = HubStep::VERSION
10
+ spec.authors = ["Adam Roben"]
11
+ spec.email = ["adam@roben.org"]
12
+
13
+ spec.summary = "Standard LightStep tracing of GitHub Ruby apps"
14
+ spec.description = "Makes it easy to trace Sinatra and Ruby apps that use GitHub conventions."
15
+ spec.homepage = "https://github.com/github/hubstep"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "lightstep", "~> 0.10.0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_development_dependency "rubocop", "~> 0.46.0"
31
+ spec.add_development_dependency "rack-test", "~> 0.6"
32
+ spec.add_development_dependency "activesupport", "~> 4.0"
33
+ spec.add_development_dependency "faraday", "~> 0.10"
34
+ spec.add_development_dependency "pry-byebug"
35
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require "failbot"
5
+
6
+ module LightStep
7
+ module Transport
8
+ class HTTPJSON # rubocop:disable Style/Documentation
9
+ # This reimplementation of LightStep::Transport::HTTPJSON#report reports
10
+ # network errors and other exceptions to Failbot.
11
+ module Failbot
12
+ class HTTPError < StandardError; end
13
+
14
+ # There's no way to call through to the normal implementation while getting
15
+ # access to the response object, so we just copy all the code here.
16
+ def report(report) # rubocop:disable Metrics/AbcSize
17
+ p report if @verbose >= 3
18
+
19
+ https = Net::HTTP.new(@host, @port)
20
+ https.use_ssl = @encryption == ENCRYPTION_TLS
21
+ req = Net::HTTP::Post.new("/api/v0/reports")
22
+ req["LightStep-Access-Token"] = @access_token
23
+ req["Content-Type"] = "application/json"
24
+ req["Connection"] = "keep-alive"
25
+ req.body = report.to_json
26
+ res = https.request(req)
27
+
28
+ puts res.to_s, res.body if @verbose >= 3
29
+
30
+ track_error(res)
31
+
32
+ nil
33
+ ensure
34
+ ::Failbot.report!($ERROR_INFO) if $ERROR_INFO
35
+ end
36
+
37
+ def track_error(res)
38
+ return unless res.is_a?(Net::HTTPClientError) || res.is_a?(Net::HTTPServerError)
39
+ exception = HTTPError.new("#{res.code} #{res.message}")
40
+ exception.set_backtrace(caller)
41
+ ::Failbot.report!(exception, response_body: res.body, response_uri: res.uri)
42
+ end
43
+ end
44
+
45
+ prepend Failbot
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require "faraday"
5
+
6
+ module HubStep
7
+ module Faraday
8
+ # Faraday middlware for wrapping a request in a span.
9
+ #
10
+ # tracer = HubStep::Tracer.new
11
+ # Faraday.new do |b|
12
+ # b.request(:hubstep, tracer)
13
+ # b.adapter(:typhoeus)
14
+ # end
15
+ class Middleware < ::Faraday::Middleware
16
+ def initialize(app, tracer)
17
+ super(app)
18
+ @tracer = tracer
19
+ end
20
+
21
+ def call(request_env)
22
+ # We pass `finish: false` so that the span won't have its end time
23
+ # recorded until #on_complete runs (which could be after #call returns
24
+ # if the request is asynchronous).
25
+ @tracer.span("faraday", finish: false) do |span|
26
+ begin
27
+ span.configure { record_request(span, request_env) }
28
+ @app.call(request_env).on_complete do |response_env|
29
+ span.configure do
30
+ record_response(span, response_env)
31
+ span.finish if span.end_micros.nil?
32
+ end
33
+ end
34
+ ensure
35
+ span.configure { record_exception(span, $ERROR_INFO) }
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def record_request(span, request_env)
43
+ method = request_env[:method].to_s.upcase
44
+ span.operation_name = "Faraday #{method}"
45
+ span.set_tag("component", "faraday")
46
+ span.set_tag("http.url", request_env[:url])
47
+ span.set_tag("http.method", method)
48
+ end
49
+
50
+ def record_response(span, response_env)
51
+ span.set_tag("http.status_code", response_env[:status])
52
+ end
53
+
54
+ def record_exception(span, exception)
55
+ return unless exception
56
+
57
+ # The on_complete block may not be called if an exception is
58
+ # thrown while processing the request, so we need to finish the
59
+ # span here.
60
+ @tracer.record_exception(span, exception)
61
+ span.finish if span.end_micros.nil?
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ Faraday::Request.register_middleware hubstep: HubStep::Faraday::Middleware
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HubStep
4
+ # Wrapper around ActiveSupport::Notifications (or any object with the same
5
+ # API) that traces instrumented blocks. Behaves identically to the underlying
6
+ # object except that blocks passed to #instrument will be passed two
7
+ # arguments instead of one: the payload Hash (as usual) and the Span that
8
+ # will represent this block of code in the trace. The Span can be used to set
9
+ # tags etc.
10
+ #
11
+ # require "hubstep/instrumenter"
12
+ #
13
+ # instrumenter = HubStep::Instrumenter.new(tracer, ActiveSupport::Notifications)
14
+ #
15
+ # def deflate(data)
16
+ # # This block will be recorded as a span by the tracer and will also
17
+ # # generate a notification as usual.
18
+ # instrumenter.instrument("deflate.zlib") do |payload, span|
19
+ # span.set_tag("bytesize", data.bytesize)
20
+ # Zlib::Deflate.deflate(data)
21
+ # end
22
+ # end
23
+ class Instrumenter
24
+ # Creates an Instrumenter
25
+ #
26
+ # tracer - HubStep::Tracer instance
27
+ # service - Object that provides ActiveSupport::Notifications' API (i.e.,
28
+ # you could just pass ActiveSupport::Notifications here, or wrap
29
+ # it in some other object).
30
+ def initialize(tracer, service)
31
+ @tracer = tracer
32
+ @service = service
33
+ end
34
+
35
+ def publish(name, *args)
36
+ service.publish(name, *args)
37
+ end
38
+
39
+ def instrument(name, payload = {})
40
+ @tracer.span(name) do |span|
41
+ service.instrument(name, payload) do |inner_payload|
42
+ yield inner_payload, span if block_given?
43
+ end
44
+ end
45
+ end
46
+
47
+ def subscribe(*args, &block)
48
+ service.subscribe(*args, &block)
49
+ end
50
+
51
+ def subscribed(callback, *args, &block)
52
+ service.subscribed(callback, *args, &block)
53
+ end
54
+
55
+ def unsubscribe(args)
56
+ service.unsubscribe(args)
57
+ end
58
+
59
+ private
60
+
61
+ attr_reader :service
62
+ end
63
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack"
4
+
5
+ module HubStep
6
+ module Rack
7
+ # Rack middleware for wrapping a request in a span.
8
+ class Middleware
9
+ # Create a Middleware
10
+ #
11
+ # tracer - a HubStep::Tracer instance
12
+ # enable_if - Proc that is passed the env for each request. If the Proc
13
+ # returns true, the tracer will be enabled for the duration
14
+ # of the request. If the Proc returns false, the tracer will
15
+ # be disabled for the duration of the request.
16
+ def initialize(app, tracer, enable_if)
17
+ @app = app
18
+ @tracer = tracer
19
+ @enable_if = enable_if
20
+ end
21
+
22
+ def call(env)
23
+ @tracer.with_enabled(@enable_if.call(env)) do
24
+ trace(env) do
25
+ @app.call(env)
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def trace(env)
33
+ @tracer.span("Rack #{env["REQUEST_METHOD"]}") do |span|
34
+ span.configure do
35
+ add_tags(span, ::Rack::Request.new(env))
36
+ end
37
+
38
+ result = yield
39
+
40
+ span.set_tag("http.status_code", result[0].to_s)
41
+
42
+ result
43
+ end
44
+ end
45
+
46
+ def add_tags(span, request)
47
+ tags(request).each do |key, value|
48
+ span.set_tag(key, value)
49
+ end
50
+ end
51
+
52
+ def tags(request)
53
+ tags = {
54
+ "component" => "rack",
55
+ "span.kind" => "server",
56
+ "http.url" => request.url,
57
+ "http.method" => request.request_method,
58
+ }
59
+ id = request_id(request)
60
+ if id
61
+ tags["guid:github_request_id"] = id
62
+ end
63
+
64
+ tags.freeze
65
+ end
66
+
67
+ def request_id(request)
68
+ request.env["HTTP_X_GITHUB_REQUEST_ID"]
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,201 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "English"
4
+ require "lightstep"
5
+ require "singleton"
6
+
7
+ module HubStep
8
+ # Tracer wraps LightStep::Tracer. It provides a block-based API for creating
9
+ # and configuring spans and support for enabling and disabling tracing at
10
+ # runtime.
11
+ class Tracer
12
+ # Create a Tracer.
13
+ #
14
+ # tags - Hash of tags to assign to the tracer. These will be
15
+ # associated with every span the tracer creates.
16
+ # transport - instance of a LightStep::Transport::Base subclass
17
+ def initialize(transport: default_transport, tags: {})
18
+ name = HubStep.server_metadata.values_at("app", "role").join("-")
19
+
20
+ default_tags = {
21
+ "hostname" => HubStep.hostname,
22
+ }
23
+
24
+ @tracer = LightStep::Tracer.new(component_name: name,
25
+ transport: transport,
26
+ tags: default_tags.merge(tags))
27
+ @spans = []
28
+ self.enabled = false
29
+ end
30
+
31
+ def enabled?
32
+ !!@enabled
33
+ end
34
+
35
+ # Enable/disable the tracer at runtime
36
+ #
37
+ # When disabled, all #span blocks will be passed InertSpans instead of real
38
+ # spans. Operations on InertSpan are no-ops.
39
+ attr_writer :enabled
40
+
41
+ # Enable/disable the tracer within a block
42
+ def with_enabled(value)
43
+ original = enabled?
44
+ self.enabled = value
45
+ yield
46
+ ensure
47
+ self.enabled = original
48
+ end
49
+
50
+ # Get the topmost span in the stack
51
+ #
52
+ # This is the span that has no parent span; the rest of the spans in the
53
+ # stack descend from it.
54
+ #
55
+ # Returns a LightStep::Span or InertSpan.
56
+ def top_span
57
+ span = @spans.first if enabled?
58
+ span || InertSpan.instance
59
+ end
60
+
61
+ # Get the bottommost span in the stack
62
+ #
63
+ # This is the span that has no children.
64
+ #
65
+ # Returns a LightStep::Span or InertSpan.
66
+ def bottom_span
67
+ span = @spans.last if enabled?
68
+ span || InertSpan.instance
69
+ end
70
+
71
+ # Record a span representing the execution of the given block
72
+ #
73
+ # operation_name - short human-readable String identifying the work done by the span
74
+ # start_time - Time instance representing when the span began
75
+ # tags - Hash of String => String tags to add to the span
76
+ # finish - Boolean indicating whether to "finish" (i.e., record the
77
+ # span's end time and submit it to the collector).
78
+ # Defaults to true.
79
+ #
80
+ # Yields a LightStep::Span or InertSpan to the block. Returns the block's
81
+ # return value.
82
+ def span(operation_name, start_time: nil, tags: nil, finish: true)
83
+ unless enabled?
84
+ return yield InertSpan.instance
85
+ end
86
+
87
+ span = @tracer.start_span(operation_name,
88
+ child_of: @spans.last,
89
+ start_time: start_time,
90
+ tags: tags)
91
+ @spans << span
92
+
93
+ begin
94
+ yield span
95
+ ensure
96
+ record_exception(span, $ERROR_INFO) if $ERROR_INFO
97
+
98
+ remove(span)
99
+
100
+ span.finish if finish && span.end_micros.nil?
101
+ end
102
+ end
103
+
104
+ # Record an exception that happened during a span
105
+ #
106
+ # span - Span or InertSpan instance
107
+ # exception - Exception instance
108
+ #
109
+ # Returns nothing.
110
+ def record_exception(span, exception)
111
+ span.configure do
112
+ span.set_tag("error", true)
113
+ span.set_tag("error.class", exception.class.name)
114
+ span.set_tag("error.message", exception.message)
115
+ end
116
+ end
117
+
118
+ # Submit all buffered spans to the collector
119
+ #
120
+ # This happens automatically so you probably don't need to call this
121
+ # outside of tests.
122
+ #
123
+ # Returns nothing.
124
+ def flush
125
+ @tracer.flush if enabled?
126
+ end
127
+
128
+ private
129
+
130
+ def default_transport
131
+ host = ENV["LIGHTSTEP_COLLECTOR_HOST"]
132
+ port = ENV["LIGHTSTEP_COLLECTOR_PORT"]
133
+ encryption = ENV["LIGHTSTEP_COLLECTOR_ENCRYPTION"]
134
+ access_token = ENV["LIGHTSTEP_ACCESS_TOKEN"]
135
+
136
+ if host && port && encryption && access_token
137
+ LightStep::Transport::HTTPJSON.new(host: host,
138
+ port: port.to_i,
139
+ encryption: encryption,
140
+ access_token: access_token)
141
+ else
142
+ LightStep::Transport::Nil.new
143
+ end
144
+ end
145
+
146
+ def remove(span)
147
+ if span == @spans.last
148
+ # Common case
149
+ @spans.pop
150
+ else
151
+ @spans.delete(span)
152
+ end
153
+ end
154
+
155
+ # Mimics the interface and no-op behavior of OpenTracing::Span. This is
156
+ # used when tracing is disabled.
157
+ class InertSpan
158
+ include Singleton
159
+ instance.freeze
160
+
161
+ def configure
162
+ end
163
+
164
+ def operation_name=(name)
165
+ end
166
+
167
+ def set_tag(_key, _value)
168
+ self
169
+ end
170
+
171
+ def set_baggage_item(_key, _value)
172
+ self
173
+ end
174
+
175
+ def get_baggage_item(_key, _value)
176
+ nil
177
+ end
178
+
179
+ def log(event: nil, timestamp: nil, **fields) # rubocop:disable Lint/UnusedMethodArgument
180
+ nil
181
+ end
182
+
183
+ def finish(end_time: nil)
184
+ end
185
+ end
186
+ end
187
+ end
188
+
189
+ # rubocop:disable Style/Documentation
190
+ module LightStep
191
+ class Span
192
+ module Configurable
193
+ def configure
194
+ yield self
195
+ end
196
+ end
197
+
198
+ include Configurable
199
+ end
200
+ end
201
+ # rubocop:enable Style/Documentation
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module HubStep
3
+ VERSION = "0.1.1"
4
+ end
data/lib/hubstep.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "hubstep/tracer"
4
+ require "hubstep/version"
5
+
6
+ require "socket"
7
+
8
+ #:nodoc:
9
+ module HubStep
10
+ # Internal: Get this machine's hostname.
11
+ #
12
+ # Returns a String.
13
+ def self.hostname
14
+ @hostname ||= Socket.gethostname.freeze
15
+ end
16
+
17
+ # Internal: Reads server data written during provisioning.
18
+ #
19
+ # Returns a Hash.
20
+ def self.server_metadata
21
+ return @server_metadata if defined?(@server_metadata)
22
+ @server_metadata =
23
+ begin
24
+ JSON.parse(File.read("/etc/github/metadata.json")).freeze
25
+ rescue
26
+ {}.freeze
27
+ end
28
+ end
29
+ end
data/script/bootstrap ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ cd "$(dirname "$0")/.."
6
+
7
+ bundle install --path vendor/gems --binstubs bin
data/script/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "hubstep"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start
metadata ADDED
@@ -0,0 +1,189 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hubstep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Roben
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lightstep
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.46.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.46.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack-test
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.6'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: faraday
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.10'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.10'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Makes it easy to trace Sinatra and Ruby apps that use GitHub conventions.
140
+ email:
141
+ - adam@roben.org
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rubocop.yml"
148
+ - ".ruby-version"
149
+ - ".travis.yml"
150
+ - CODE_OF_CONDUCT.md
151
+ - Gemfile
152
+ - LICENSE.txt
153
+ - README.md
154
+ - Rakefile
155
+ - hubstep.gemspec
156
+ - lib/hubstep.rb
157
+ - lib/hubstep/failbot.rb
158
+ - lib/hubstep/faraday/middleware.rb
159
+ - lib/hubstep/instrumenter.rb
160
+ - lib/hubstep/rack/middleware.rb
161
+ - lib/hubstep/tracer.rb
162
+ - lib/hubstep/version.rb
163
+ - script/bootstrap
164
+ - script/console
165
+ homepage: https://github.com/github/hubstep
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubyforge_project:
185
+ rubygems_version: 2.5.2
186
+ signing_key:
187
+ specification_version: 4
188
+ summary: Standard LightStep tracing of GitHub Ruby apps
189
+ test_files: []