slimy 0.0.3 → 0.0.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eaf9b13b8237cd4762f4c47604094655839f8a3038325e82f09c7f80332e8323
4
- data.tar.gz: 1b0265a125ed7d39f19b82950a0e2b0ce26a42f6eed340125069366321fcf0f7
3
+ metadata.gz: 162b8ea4228cdc927a683c3ddaaa17e5795f7b48b1c895d5aa2648d3496173d2
4
+ data.tar.gz: 820932ee306a3d5dad370d446b7bf81b45fd4e1a8d42cb86130e1bddbf373c49
5
5
  SHA512:
6
- metadata.gz: 7bb3891e0c988132baa3a795cef5405e9e458f1d2d423948d01a9aa6f199caa0f12a9470a09f4764eb8144d44e61c87f8185e88939ce5efe94c6ec81924fccfb
7
- data.tar.gz: e305fa2a3d37ae07567e34aad79c40a606c507006d018a7a0179acc27b40842541b2e5c9dbc7e12fa31c0b34b6ba987a69d844f5f60038e50ca7d9b379b7b4a8
6
+ metadata.gz: 52fe4d288cf644d54308f4dd9286ec21e79c41498258f874a7b5a19afac025fae4f89c16692e2cae8e5b26706c180f42ab0b0e0a2126b7118e31434a2e79b916
7
+ data.tar.gz: 35ce82e426ea6d87d5e21eb4cc38cfc4f39778b7eb0c6232fada3d40fe2be488de510fc0262ffbca0820e6a1c35de236e0f18f1ec6e7796abbe94e65108060e4
@@ -5,4 +5,17 @@ AllCops:
5
5
 
6
6
  Style/StringLiterals:
7
7
  Enabled: true
8
- EnforcedStyle: double_quotes
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/ClassVars:
11
+ Enabled: false
12
+
13
+ # Configuration parameters: CountComments.
14
+ Metrics/MethodLength:
15
+ Max: 20
16
+ Severity: refactor
17
+
18
+ Metrics/BlockLength:
19
+ Enabled: true
20
+ CountComments: false
21
+ Max: 50
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.6
@@ -1,4 +1,12 @@
1
+ ### v0.0.5 - 2020-12-07
2
+
3
+ - Added sli_tags to allow multiple tags on one line
4
+ - Added more tests included for rake
5
+
1
6
  ### v0.0.3 - 2020-12-04
7
+
2
8
  - Tested changelog
9
+
3
10
  ### v0.0.2 - 2020-12-04
11
+
4
12
  - Added changelog
data/Gemfile CHANGED
@@ -3,4 +3,3 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
-
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- slimy (0.0.3)
5
- rack (~> 2.0)
4
+ slimy (0.0.5)
6
5
 
7
6
  GEM
8
7
  remote: https://rubygems.org/
@@ -11,19 +10,52 @@ GEM
11
10
  ast (2.4.1)
12
11
  builder (3.2.4)
13
12
  bump (0.10.0)
13
+ coderay (1.1.2)
14
+ ffi (1.9.25)
15
+ formatador (0.2.5)
16
+ guard (2.14.2)
17
+ formatador (>= 0.2.4)
18
+ listen (>= 2.7, < 4.0)
19
+ lumberjack (>= 1.0.12, < 2.0)
20
+ nenv (~> 0.1)
21
+ notiffany (~> 0.0)
22
+ pry (>= 0.9.12)
23
+ shellany (~> 0.0)
24
+ thor (>= 0.18.1)
25
+ guard-compat (1.2.1)
26
+ guard-minitest (2.4.6)
27
+ guard-compat (~> 1.2)
28
+ minitest (>= 3.0)
14
29
  jaro_winkler (1.5.3)
30
+ listen (3.1.5)
31
+ rb-fsevent (~> 0.9, >= 0.9.4)
32
+ rb-inotify (~> 0.9, >= 0.9.7)
33
+ ruby_dep (~> 1.2)
34
+ lumberjack (1.0.12)
35
+ method_source (0.8.2)
15
36
  minitest (5.14.2)
16
37
  minitest-reporters (1.4.2)
17
38
  ansi
18
39
  builder
19
40
  minitest (>= 5.0)
20
41
  ruby-progressbar
42
+ nenv (0.3.0)
43
+ notiffany (0.1.1)
44
+ nenv (~> 0.1)
45
+ shellany (~> 0.0)
21
46
  parallel (1.18.0)
22
47
  parser (2.6.5.0)
23
48
  ast (~> 2.4.0)
49
+ pry (0.10.4)
50
+ coderay (~> 1.1.0)
51
+ method_source (~> 0.8.1)
52
+ slop (~> 3.4)
24
53
  rack (2.2.3)
25
54
  rainbow (3.0.0)
26
55
  rake (10.5.0)
56
+ rb-fsevent (0.10.3)
57
+ rb-inotify (0.10.0)
58
+ ffi (~> 1.0)
27
59
  rubocop (0.75.1)
28
60
  jaro_winkler (~> 1.5.1)
29
61
  parallel (~> 1.10)
@@ -32,6 +64,10 @@ GEM
32
64
  ruby-progressbar (~> 1.7)
33
65
  unicode-display_width (>= 1.4.0, < 1.7)
34
66
  ruby-progressbar (1.10.1)
67
+ ruby_dep (1.5.0)
68
+ shellany (0.0.1)
69
+ slop (3.6.0)
70
+ thor (0.20.3)
35
71
  timecop (0.9.2)
36
72
  unicode-display_width (1.6.0)
37
73
 
@@ -41,12 +77,15 @@ PLATFORMS
41
77
  DEPENDENCIES
42
78
  bump
43
79
  bundler (>= 1.0, < 3)
80
+ guard
81
+ guard-minitest
44
82
  minitest (~> 5.14)
45
83
  minitest-reporters (~> 1.4)
84
+ rack (~> 2.0)
46
85
  rake (~> 10.4, >= 10.4.2)
47
86
  rubocop
48
87
  slimy!
49
88
  timecop (~> 0.9)
50
89
 
51
90
  BUNDLED WITH
52
- 2.0.2
91
+ 2.1.4
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :minitest do
4
+ # with Minitest::Unit
5
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
6
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
7
+ watch(%r{^test/test_helper\.rb$}) { "test" }
8
+ end
data/Rakefile CHANGED
@@ -1,17 +1,18 @@
1
- require 'bundler'
2
- Bundler::GemHelper.install_tasks
3
- require 'rake/testtask'
4
- require 'bump/tasks'
5
- require 'rubocop/rake_task'
1
+ # frozen_string_literal: true
6
2
 
3
+ require "bundler"
4
+ Bundler::GemHelper.install_tasks
5
+ require "rake/testtask"
6
+ require "bump/tasks"
7
+ require "rubocop/rake_task"
7
8
 
8
9
  RuboCop::RakeTask.new
9
10
 
10
11
  Rake::TestTask.new do |t|
11
12
  t.libs << "test"
12
13
  t.libs << "lib"
13
- t.test_files = FileList['test/**/*_test.rb']
14
+ t.test_files = FileList["test/**/*_test.rb"]
14
15
  end
15
16
  desc "Run tests"
16
17
 
17
- task default: [:test, :rubocop]
18
+ task default: %i[test rubocop]
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "slimy/version"
2
4
  require "slimy/context"
3
5
  require "slimy/config"
@@ -5,7 +7,7 @@ require "slimy/rack"
5
7
  require "slimy/rails"
6
8
  require "slimy/reporters"
7
9
 
8
-
10
+ # Slimy module for SLI Instrumentation
9
11
  module Slimy
10
12
  class << self
11
13
  def configure
@@ -16,4 +18,4 @@ module Slimy
16
18
  end
17
19
  end
18
20
  end
19
- end
21
+ end
@@ -1,13 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Slimy
4
+ # Class for configration of Slimy tooling
2
5
  class Configuration
3
-
4
6
  def initialize
5
7
  @reporter = nil
6
8
  end
7
9
 
8
10
  attr_accessor :reporter
9
11
 
10
-
11
12
  def self.default
12
13
  @@default ||= Configuration.new
13
14
  end
@@ -16,4 +17,4 @@ module Slimy
16
17
  yield(self) if block_given?
17
18
  end
18
19
  end
19
- end
20
+ end
@@ -1,16 +1,16 @@
1
- module Slimy
1
+ # frozen_string_literal: true
2
2
 
3
+ module Slimy
3
4
  # Slimy::Context
4
5
  #
5
- # This is a group of metadata that exists for the duration of some request to be measured
6
- # as part of an SLI.
6
+ # This is a group of metadata that exists for the duration of some request
7
+ # to be measured as part of an SLI.
7
8
  #
8
9
  class Context
9
-
10
10
  # Create a new context sets the `start_time` to now
11
11
  #
12
12
  # == Parameters
13
- # start_time::
13
+ # start_time:
14
14
  # An overriden Time at which the event started. Defaults to now.
15
15
  #
16
16
  # deadline::
@@ -28,7 +28,6 @@ module Slimy
28
28
 
29
29
  attr_reader :start_time, :end_time, :type
30
30
 
31
-
32
31
  attr_accessor :tags, :deadline
33
32
 
34
33
  # Set the end_time value for the context if not already set
@@ -76,12 +75,14 @@ module Slimy
76
75
  # duration in ms of the event
77
76
  def duration
78
77
  return -1 unless finished?
79
- [@end_time - @start_time,0].max * 1000.0
78
+
79
+ [@end_time - @start_time, 0].max * 1000.0
80
80
  end
81
81
 
82
82
  # Did the request finish before the deadline (or was there no deadline)
83
83
  def deadline_success?
84
- return true if @deadline.nil? || ! finished?
84
+ return true if @deadline.nil? || !finished?
85
+
85
86
  duration < @deadline
86
87
  end
87
88
 
@@ -94,11 +95,15 @@ module Slimy
94
95
  @reportable = false
95
96
  end
96
97
 
97
- # tool for debugging
98
+ # tool for debugging
98
99
  def debug_format
99
- "slimy_ctx:\n\tfinished: #{finished?}\n\tduration: #{duration}\n\tresult_success: #{result_success?}\n\tduration_success: #{deadline_success?}\n\tsuccess: #{success?}\n\tdeadline: #{deadline}\n\ttags: #{tags.inspect}"
100
+ "slimy_ctx:\n\tfinished: #{finished?}\n" \
101
+ + "\tduration: #{duration}\n" \
102
+ + "\tresult_success: #{result_success?}\n" \
103
+ + "\tduration_success: #{deadline_success?}\n" \
104
+ + "\tsuccess: #{success?}\n" \
105
+ + "\tdeadline: #{deadline}\n" \
106
+ + "\ttags: #{tags.inspect}\n"
100
107
  end
101
-
102
108
  end
103
-
104
- end
109
+ end
@@ -1,3 +1,8 @@
1
- module Slimy::Rack
2
- autoload :SLIMiddleware, "slimy/rack/middleware"
3
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Slimy
4
+ # rack integration for slimy
5
+ module Rack
6
+ autoload :SLIMiddleware, "slimy/rack/middleware"
7
+ end
8
+ end
@@ -1,31 +1,44 @@
1
- module Slimy::Rack
2
- class SLIMiddleware
1
+ # frozen_string_literal: true
3
2
 
4
- MIDDLEWARE_CONTEXT_KEY="slimy.milddeware.context"
3
+ module Slimy
4
+ module Rack
5
+ # rack middleware for tracking http request SLIs
6
+ class SLIMiddleware
7
+ MIDDLEWARE_CONTEXT_KEY = "slimy.milddeware.context"
5
8
 
6
- def initialize(app)
7
- @app = app
8
- @reporter = Slimy::Configuration.default.reporter
9
- end
9
+ def initialize(app, options = {})
10
+ @app = app
11
+ @reporter = if options.key? :reporter
12
+ options[:reporter]
13
+ else
14
+ Slimy::Configuration.default.reporter
15
+ end
16
+ end
10
17
 
11
- def call(env)
12
- context = Slimy::Context.new(deadline: 200, type: 'rack')
13
- env[MIDDLEWARE_CONTEXT_KEY] = context
14
- begin
15
- response = @app.call(env)
16
- context.result_error! if response[0] >= 500
17
- rescue Exception => error
18
- context.result_error!
19
- raise error
20
- ensure
21
- context.finish
22
- report(context)
23
- return response
18
+ def init_context(env)
19
+ context = Slimy::Context.new(deadline: 200, type: "rack")
20
+ env[MIDDLEWARE_CONTEXT_KEY] = context
24
21
  end
25
- end
26
22
 
27
- def report(context)
28
- @reporter.report(context) unless @reporter.nil?
23
+ def call(env)
24
+ context = init_context(env)
25
+ response = nil
26
+ begin
27
+ response = @app.call(env)
28
+ context.result_error! if response[0] >= 500
29
+ rescue StandardError => e
30
+ context.result_error!
31
+ raise e
32
+ ensure
33
+ context.finish
34
+ report(context)
35
+ end
36
+ response
37
+ end
38
+
39
+ def report(context)
40
+ @reporter&.report(context)
41
+ end
29
42
  end
30
43
  end
31
- end
44
+ end
@@ -1,3 +1,8 @@
1
- module Slimy::Rails
2
- autoload :SLITools, "slimy/rails/sli_concern"
3
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Slimy
4
+ # rails integration for slimy
5
+ module Rails
6
+ autoload :SLITools, "slimy/rails/sli_concern"
7
+ end
8
+ end
@@ -1,52 +1,66 @@
1
- module Slimy::Rails
1
+ # frozen_string_literal: true
2
2
 
3
- module SLITools extend ActiveSupport::Concern
4
-
5
- included do
3
+ module Slimy
4
+ module Rails
5
+ # SLITools concern
6
+ #
7
+ # This module adds Controller helpers for instrumenting SLIs
8
+ #
9
+ module SLITools
10
+ extend ActiveSupport::Concern
11
+ included do
12
+ # class-level meta commands
13
+ def self.sli_tag(tag, value, except: nil, only: nil)
14
+ before_action only: only, except: except do
15
+ add_sli_tag(tag, value)
16
+ end
17
+ end
6
18
 
7
- # class-level meta commands
8
- def self.sli_tag(tag, value, except: nil, only: nil)
9
- self.before_action only: only, except: except do
10
- add_sli_tag(tag,value)
19
+ def self.sli_tags(tags, except: nil, only: nil)
20
+ before_action only: only, except: except do
21
+ tags.each_pair do |tag, value|
22
+ add_sli_tag(tag, value)
23
+ end
24
+ end
11
25
  end
12
- end
13
26
 
14
- def self.sli_ignore(except: nil, only: nil)
15
- self.before_action only: only, except: except do
16
- add_sli_ignore
27
+ def self.sli_ignore(except: nil, only: nil)
28
+ before_action only: only, except: except do
29
+ add_sli_ignore
30
+ end
17
31
  end
18
- end
19
32
 
20
- def self.sli_deadline(deadline, except: nil, only: nil)
21
- self.before_action only: only, except: except do
22
- add_sli_deadline(deadline)
33
+ def self.sli_deadline(deadline, except: nil, only: nil)
34
+ before_action only: only, except: except do
35
+ add_sli_deadline(deadline)
36
+ end
23
37
  end
24
- end
25
38
 
26
- before_action do
27
- add_sli_tag("controller", controller_name)
28
- add_sli_tag("action", action_name)
29
- end
39
+ before_action do
40
+ add_sli_tag("controller", controller_name)
41
+ add_sli_tag("action", action_name)
42
+ end
30
43
 
31
- # helpers
32
- def slimy_context
33
- request.env[Slimy::Rack::SLIMiddleware::MIDDLEWARE_CONTEXT_KEY]
34
- end
44
+ # helpers
45
+ def slimy_context
46
+ request.env[Slimy::Rack::SLIMiddleware::MIDDLEWARE_CONTEXT_KEY]
47
+ end
35
48
 
36
- def add_sli_tag(tag,value)
37
- ctx = slimy_context
38
- ctx.tags[tag] = value unless ctx.nil?
39
- end
49
+ def add_sli_tag(tag, value)
50
+ ctx = slimy_context
51
+ ctx.tags[tag] = value unless ctx.nil?
52
+ end
40
53
 
41
- def add_sli_ignore
42
- ctx = slimy_context
43
- ctx.do_not_report! unless ctx.nil?
44
- end
54
+ def add_sli_ignore
55
+ ctx = slimy_context
56
+ ctx&.do_not_report!
57
+ end
45
58
 
46
- def add_sli_deadline(deadline)
47
- ctx = slimy_context
48
- ctx.deadline = deadline unless ctx.nil?
59
+ def add_sli_deadline(deadline)
60
+ ctx = slimy_context
61
+ ctx.deadline = deadline unless ctx.nil?
62
+ end
49
63
  end
50
64
  end
51
65
  end
52
- end
66
+ end
@@ -1,6 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "slimy/reporters/reporter"
2
4
 
3
- module Slimy::Reporters
4
- autoload :RailsLogReporter, "slimy/reporters/rails_log_reporter"
5
- autoload :DatadogReporter, "slimy/reporters/datadog"
6
- end
5
+ module Slimy
6
+ # integrations for sending recorded data somewhere for long term storage
7
+ module Reporters
8
+ autoload :RailsLogReporter, "slimy/reporters/rails_log_reporter"
9
+ autoload :DatadogReporter, "slimy/reporters/datadog"
10
+ end
11
+ end
@@ -1,25 +1,38 @@
1
- module Slimy::Reporters
2
- class DatadogReporter < BaseReporter
1
+ # frozen_string_literal: true
3
2
 
4
- def initialize(dogstatsd)
5
- @dogstatsd = dogstatsd
6
- end
3
+ module Slimy
4
+ module Reporters
5
+ # Reporter for sending data to datadog
6
+ #
7
+ # this requires a DogstatsD instance to operate
8
+ #
9
+ class DatadogReporter < BaseReporter
10
+ def initialize(dogstatsd)
11
+ @dogstatsd = dogstatsd
12
+ end
13
+
14
+ # report the given context to datadog
15
+ def report(context)
16
+ return unless context.reportable?
7
17
 
8
- def report(context)
9
- return unless context.reportable?
18
+ sli_status = (context.success? ? "success" : "failure")
19
+ current_span = Datadog.tracer.active_span
20
+ if current_span.nil?
21
+ Rails.logger.debug("COULD NOT FIND SPAN")
22
+ else
23
+ set_tags_on_span(context, sli_status)
24
+ @dogstatsd.increment("sli.#{context.type}.#{sli_status}",
25
+ tags: context.tags)
26
+ end
27
+ end
10
28
 
11
- current_span = Datadog.tracer.active_span
12
- unless current_span.nil?
13
- sli_status =(context.success? ? "success" : "failure")
14
- current_span.set_tag('sli_status', sli_status)
15
- current_span.set_tag('sli_deadline', context.deadline)
29
+ def set_tags_on_span(context, sli_status)
30
+ current_span.set_tag("sli_status", sli_status)
31
+ current_span.set_tag("sli_deadline", context.deadline)
16
32
  context.tags.each_pair do |key, value|
17
- current_span.set_tag(key,value)
33
+ current_span.set_tag(key, value)
18
34
  end
19
- @dogstatsd.increment("sli.#{context.type}.#{sli_status}", tags: context.tags)
20
- else
21
- Rails.logger.debug("COULD NOT FIND SPAN")
22
35
  end
23
36
  end
24
37
  end
25
- end
38
+ end
@@ -1,16 +1,21 @@
1
- module Slimy::Reporters
1
+ # frozen_string_literal: true
2
2
 
3
- class RailsLogReporter
4
- def initialize(level: :debug)
5
- @level = level
6
- end
3
+ module Slimy
4
+ module Reporters
5
+ # reporter that logs to rails logs
6
+ # this will not send the data anywhere else
7
+ class RailsLogReporter
8
+ def initialize(level: :debug)
9
+ @level = level
10
+ end
7
11
 
8
- def report(context)
9
- log(context.debug_format)
10
- end
12
+ def report(context)
13
+ log(context.debug_format)
14
+ end
11
15
 
12
- def log(msg)
13
- Rails.logger.send(@level, msg)
16
+ def log(msg)
17
+ Rails.logger.send(@level, msg)
18
+ end
14
19
  end
15
20
  end
16
- end
21
+ end
@@ -1,9 +1,14 @@
1
- module Slimy::Reporters
1
+ # frozen_string_literal: true
2
2
 
3
- class BaseReporter
4
-
5
- def report(context)
6
- raise NotImplementedError
3
+ module Slimy
4
+ # module for housing integrations that send data somewhere
5
+ # for storage/analysis
6
+ module Reporters
7
+ # This is mostly a template for actualy implementations
8
+ class BaseReporter
9
+ def report(_context)
10
+ raise NotImplementedError
11
+ end
7
12
  end
8
13
  end
9
- end
14
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Slimy
2
- VERSION = "0.0.3"
3
- end
4
+ VERSION = "0.0.5"
5
+ end
@@ -1,4 +1,6 @@
1
- $:.push File.expand_path("../lib", __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path("lib", __dir__)
2
4
  require "slimy/version"
3
5
 
4
6
  Gem::Specification.new do |s|
@@ -10,19 +12,20 @@ Gem::Specification.new do |s|
10
12
  + "type metrics for Rack, Sidekiq, and maybe more."
11
13
  s.authors = ["Tyler Henrichs", "Stephen Gregory"]
12
14
  s.email = "devops+slimy@lessonly.com"
13
- s.homepage = "https://rubygems.org/gems/slimy"
15
+ s.homepage = "https://github.com/lessonly/slimy"
14
16
  s.license = "MIT"
15
17
  s.files = `git ls-files`.split("\n")
16
18
 
17
19
  s.require_paths = ["lib"]
18
20
 
19
- s.add_runtime_dependency "rack", "~> 2.0"
20
-
21
21
  s.add_development_dependency "bump"
22
22
  s.add_development_dependency "bundler", ">= 1.0", "< 3"
23
+ s.add_development_dependency "guard"
24
+ s.add_development_dependency "guard-minitest"
23
25
  s.add_development_dependency "minitest", "~> 5.14"
24
26
  s.add_development_dependency "minitest-reporters", "~> 1.4"
27
+ s.add_development_dependency "rack", "~> 2.0"
25
28
  s.add_development_dependency "rake", "~> 10.4", ">= 10.4.2"
26
29
  s.add_development_dependency "rubocop"
27
30
  s.add_development_dependency "timecop", "~> 0.9"
28
- end
31
+ end
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "test_helper"
2
4
 
3
5
  class ContextTest < Minitest::Test
4
-
5
6
  def test_that_context_new_works
6
7
  context = Slimy::Context.new
7
8
  refute_nil context
@@ -14,7 +15,8 @@ class ContextTest < Minitest::Test
14
15
  freeze_ms(30) do
15
16
  context.finish
16
17
  assert context.finished?
17
- refute context.deadline_success?, "duration: #{context.duration} should be less than 20"
18
+ refute context.deadline_success?,
19
+ "duration: #{context.duration} should be less than 20"
18
20
  assert context.result_success?
19
21
  refute context.success?
20
22
  end
@@ -44,5 +46,4 @@ class ContextTest < Minitest::Test
44
46
  refute context.success?
45
47
  end
46
48
  end
47
-
48
- end
49
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/mock"
4
+ require "test_helper"
5
+
6
+ class ContextTest < Minitest::Test
7
+ def setup
8
+ @reporter = DummyReporter.new
9
+ @app = ->(_env) { [200, { "Content-Type" => "text/plain" }, ["hello"]] }
10
+ @app_slow = lambda do |_env|
11
+ Timecop.freeze(Time.now + 1990)
12
+ [200, { "Content-Type" => "text/plain" }, ["hello"]]
13
+ end
14
+ @app_error = ->(_env) { raise ArgumentError }
15
+ @app500 = ->(_env) { [500, { "Content-Type" => "text/plain" }, ["error"]] }
16
+ end
17
+
18
+ def teardown
19
+ Timecop.return
20
+ end
21
+
22
+ def middleware(app)
23
+ mw = Slimy::Rack::SLIMiddleware.new(app, reporter: @reporter)
24
+ Rack::Lint.new(mw)
25
+ end
26
+
27
+ def request(app)
28
+ req = Rack::MockRequest.new(middleware(app))
29
+ req.get("/", "REMOTE_ADDR" => "127.0.0.1")
30
+ end
31
+
32
+ def test_rack_request
33
+ res = request(@app)
34
+ assert_equal(res.status, 200)
35
+ assert_equal(res.body, "hello")
36
+ assert(@reporter.ctx.success?, "SLI metric should be successful")
37
+ end
38
+
39
+ def test_slow_rack_request
40
+ res = request(@app_slow)
41
+ assert_equal(res.status, 200)
42
+ assert_equal(res.body, "hello")
43
+ refute(@reporter.ctx.success?, "SLI metric should be a failure")
44
+ end
45
+
46
+ def test_5xx_rack_request
47
+ res = request(@app500)
48
+ assert_equal(res.status, 500)
49
+ assert_equal(res.body, "error")
50
+ refute(@reporter.ctx.success?, "SLI metric should be a failure")
51
+ end
52
+
53
+ def test_error_rack_request
54
+ assert_raises ArgumentError do
55
+ request(@app_error)
56
+ end
57
+ refute(@reporter.ctx.success?, "SLI metric should be a failure")
58
+ end
59
+ end
60
+
61
+ class DummyReporter
62
+ def initialize
63
+ @ctx = nil
64
+ end
65
+
66
+ def report(ctx)
67
+ @ctx = ctx
68
+ end
69
+
70
+ attr_reader :ctx
71
+ end
@@ -1,10 +1,11 @@
1
- require 'minitest/autorun'
2
- require 'slimy'
3
- require 'timecop'
1
+ # frozen_string_literal: true
4
2
 
3
+ require "minitest/autorun"
4
+ require "slimy"
5
+ require "timecop"
5
6
 
6
7
  def freeze_ms(duration_ms)
7
- Timecop.freeze(Time.now+(duration_ms/1000.0)) do
8
+ Timecop.freeze(Time.now + (duration_ms / 1000.0)) do
8
9
  yield
9
10
  end
10
- end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slimy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Henrichs
@@ -11,20 +11,6 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2020-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rack
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '2.0'
21
- type: :runtime
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '2.0'
28
14
  - !ruby/object:Gem::Dependency
29
15
  name: bump
30
16
  requirement: !ruby/object:Gem::Requirement
@@ -59,6 +45,34 @@ dependencies:
59
45
  - - "<"
60
46
  - !ruby/object:Gem::Version
61
47
  version: '3'
48
+ - !ruby/object:Gem::Dependency
49
+ name: guard
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
62
76
  - !ruby/object:Gem::Dependency
63
77
  name: minitest
64
78
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +101,20 @@ dependencies:
87
101
  - - "~>"
88
102
  - !ruby/object:Gem::Version
89
103
  version: '1.4'
104
+ - !ruby/object:Gem::Dependency
105
+ name: rack
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.0'
90
118
  - !ruby/object:Gem::Dependency
91
119
  name: rake
92
120
  requirement: !ruby/object:Gem::Requirement
@@ -145,9 +173,11 @@ files:
145
173
  - ".gitignore"
146
174
  - ".rubocop.yml"
147
175
  - ".tool-versions"
176
+ - ".travis.yml"
148
177
  - CHANGELOG.md
149
178
  - Gemfile
150
179
  - Gemfile.lock
180
+ - Guardfile
151
181
  - LICENSE
152
182
  - README.md
153
183
  - Rakefile
@@ -165,8 +195,9 @@ files:
165
195
  - lib/slimy/version.rb
166
196
  - slimy.gemspec
167
197
  - test/slimy/context_test.rb
198
+ - test/slimy/rack_middleware_test.rb
168
199
  - test/test_helper.rb
169
- homepage: https://rubygems.org/gems/slimy
200
+ homepage: https://github.com/lessonly/slimy
170
201
  licenses:
171
202
  - MIT
172
203
  metadata: {}