microscope_tracer 0.0.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: 7643c5e1b0005e105386c53d615cbbfdd9fc31cf
4
+ data.tar.gz: fb317d4e561db12dd7decdef4e2c99a46a3202d2
5
+ SHA512:
6
+ metadata.gz: 1ede11290a7d81ef89830609109b7a513c24fb4cdf3167d002269f1f11cbf84c93c5acb8d286da85cbf53286a8e068be1a3ed9a2c99b5f4e581221b7d944d9f4
7
+ data.tar.gz: 41765f463a2183041991376d8d5912a582d36e080e04dcd4e2e3137b098e768088a0743715aa475911925f04b5c29117e285ca2f0d36042e62334596d86bc2c6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in microscope_tracer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Pete Hodgson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # MicroscopeTracer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'microscope_tracer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install microscope_tracer
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/moredip/microscope_tracer/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,29 @@
1
+ require 'faraday'
2
+
3
+ require 'microscope_tracer/trace_logger'
4
+
5
+ module MicroscopeTracer
6
+ class FaradayMiddleware < Faraday::Middleware
7
+ def initialize(app,logger = Logger.new($stdout))
8
+ @trace_logger = TraceLogger.new(logger)
9
+ super(app)
10
+ end
11
+
12
+ def call(env)
13
+ span = Span.lookup_for_this_request
14
+ child_span_id = Span.generate_unique_id
15
+
16
+ Headers.add_client_request_headers(env.request_headers,span,child_span_id)
17
+
18
+ started_at = Time.now
19
+
20
+ @trace_logger.log_client_start(span)
21
+
22
+
23
+ @app.call(env).on_complete do |response_env|
24
+ duration = Time.now - started_at
25
+ @trace_logger.log_client_end(span,duration)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ require 'microscope_tracer/span'
2
+
3
+ module MicroscopeTracer
4
+ module Headers
5
+ def self.add_client_request_headers(headers,span,child_span_id)
6
+ headers["Microscope-Trace-Id"] = span.trace_id if span.trace_id
7
+ headers["Microscope-Parent-Span-Id"] = span.span_id if span.span_id
8
+ headers["Microscope-Span-Id"] = child_span_id if child_span_id
9
+ end
10
+
11
+ def self.span_from_server_request_headers(env)
12
+ trace_id = env.fetch("HTTP_MICROSCOPE_TRACE_ID",false)
13
+ parent_span_id = env.fetch("HTTP_MICROSCOPE_PARENT_SPAN_ID",false)
14
+ span_id = env.fetch("HTTP_MICROSCOPE_SPAN_ID",false)
15
+
16
+ sanity_checks(!!trace_id,!!span_id,!!parent_span_id)
17
+
18
+ if trace_id && !parent_span_id
19
+ return false
20
+ end
21
+
22
+ Span.new(trace_id,parent_span_id,span_id)
23
+ end
24
+
25
+ def self.sanity_checks(trace_id_provided, span_id_provided, parent_span_id_provided)
26
+ if !trace_id_provided && span_id_provided
27
+ puts "WARNING: no TraceId provided but a SpanId *was* provided"
28
+ end
29
+
30
+ if !trace_id_provided && parent_span_id_provided
31
+ puts "WARNING: no TraceId provided but a ParentSpanId *was* provided"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ require 'rack/body_proxy'
2
+ require 'request_store'
3
+
4
+ require 'microscope_tracer/headers'
5
+ require 'microscope_tracer/trace_logger'
6
+
7
+ module MicroscopeTracer
8
+
9
+ class RackMiddleware
10
+ def initialize(app,logger = Logger.new($stdout))
11
+ @app = app
12
+ @trace_logger = TraceLogger.new(logger)
13
+ end
14
+
15
+ def call(env)
16
+ span = (env["SPAN"] ||= Headers.span_from_server_request_headers(env))
17
+ span.store_for_this_request
18
+
19
+ started_at = Time.now
20
+
21
+ @trace_logger.log_server_start(span)
22
+
23
+ status, header, body = @app.call(env)
24
+
25
+ body = Rack::BodyProxy.new(body) do
26
+ duration = Time.now - started_at
27
+ @trace_logger.log_server_end(span,duration)
28
+ end
29
+
30
+ [status, header, body]
31
+ ensure
32
+ RequestStore.clear! # make certain that any span info is cleared out before the next request comes in
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,26 @@
1
+ require 'securerandom'
2
+ require 'request_store'
3
+
4
+ module MicroscopeTracer
5
+ class Span
6
+ attr_reader :trace_id, :span_id, :parent_span_id
7
+
8
+ def self.generate_unique_id
9
+ SecureRandom.uuid
10
+ end
11
+
12
+ def self.lookup_for_this_request
13
+ RequestStore.store[:microscope_span]
14
+ end
15
+
16
+ def store_for_this_request
17
+ RequestStore.store[:microscope_span] = self
18
+ end
19
+
20
+ def initialize(trace_id,parent_span_id,span_id)
21
+ trace_id ||= Span.generate_unique_id
22
+ span_id ||= Span.generate_unique_id
23
+ @trace_id, @parent_span_id, @span_id = trace_id, parent_span_id, span_id
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ module MicroscopeTracer
2
+ class TraceLogger
3
+ def initialize(logger)
4
+ @logger = logger
5
+ end
6
+
7
+ def log_server_start(span)
8
+ log(:server_start,span)
9
+ end
10
+
11
+ def log_server_end(span,duration_in_seconds)
12
+ millis = duration_in_seconds * 1000
13
+ log(:server_end,span,{elapsedMillis:millis})
14
+ end
15
+
16
+ def log_client_start(span)
17
+ log(:client_start,span)
18
+ end
19
+
20
+ def log_client_end(span,duration_in_seconds)
21
+ millis = duration_in_seconds * 1000
22
+ log(:client_end,span,{elapsedMillis:millis})
23
+ end
24
+
25
+ def log(type,span,extras={})
26
+ fields = {type:type,traceId:span.trace_id,spanId:span.span_id,pspanId:span.parent_span_id}.merge(extras)
27
+ line = fields.map{ |k,v| if v then "#{k}=\"#{v}\"" else nil end }.compact.join(" ")
28
+ @logger.info(line)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module MicroscopeTracer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "microscope_tracer/version"
2
+
3
+ module MicroscopeTracer
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'microscope_tracer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "microscope_tracer"
8
+ spec.version = MicroscopeTracer::VERSION
9
+ spec.authors = ["Pete Hodgson"]
10
+ spec.email = ["rubygems@thepete.net"]
11
+ spec.summary = %q{Logs Microscope-compliant trace entries and wrangles Microscope- HTTP headers.}
12
+ #spec.description = %q{TODO: Write a longer description. Optional.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "request_store"
25
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: microscope_tracer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pete Hodgson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-05 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: request_store
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - rubygems@thepete.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/microscope_tracer.rb
68
+ - lib/microscope_tracer/faraday_middleware.rb
69
+ - lib/microscope_tracer/headers.rb
70
+ - lib/microscope_tracer/rack_middleware.rb
71
+ - lib/microscope_tracer/span.rb
72
+ - lib/microscope_tracer/trace_logger.rb
73
+ - lib/microscope_tracer/version.rb
74
+ - microscope_tracer.gemspec
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Logs Microscope-compliant trace entries and wrangles Microscope- HTTP headers.
99
+ test_files: []