aws-embedded-metrics-customink 0.1.0 → 0.6.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76477125adcfa5d534ff63eda7c6b3469e5ca3c5c778ce40b9b23bce906d1da5
4
- data.tar.gz: 0bee3bd7ea8e7d2d1ed0a72f928c29741caf17c9d3af2e83901896bfcb40f39a
3
+ metadata.gz: b07c505a8d7790b89bff615c824567402f3e6b613287753ea5b7fe930614ce8b
4
+ data.tar.gz: cd42f99bccbd143a40acdd8f7ec91e442d3fc2c6475397179561c78482e9a2de
5
5
  SHA512:
6
- metadata.gz: 5a73a2ab9be29c38ab9dd833642d22877481d46b909740820cfa2420d9fa655f9dc348c9dbf82aa73f61621a00ce1263e4eef4e9b4edc6bb380e67410cfce558
7
- data.tar.gz: 5ae92d80a90c497a264775a1a5db28ea7614a922cb33865bc7f102e0b65303f80dad0d6f18d485a665100b3a73988a4e387a74dad8e53fe1e267b861df076276
6
+ metadata.gz: 8de84739de64092a2998196e7a5348ad2a8339282eb7fdc7dfbb0773096dd70fd8ef77e853e8a812075c3d6ef2cddd74fdb9cb629643bb0efabd9fe9a8168582
7
+ data.tar.gz: 9892885a4debc5335e8c43cecc687e4107c8ac52efb046cb82b12c3d12c0529a40c23d73e4b9539ca51b94b6de6dfd805f417136f68a6a3551ddc684e85f72a9
@@ -0,0 +1,23 @@
1
+ name: Lint
2
+ on: [push]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ ruby:
9
+ - '2.5'
10
+ - '2.7'
11
+ steps:
12
+ - name: Checkout
13
+ uses: actions/checkout@v1
14
+ - name: Setup Ruby
15
+ uses: actions/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+ - name: Bundle
19
+ run: |
20
+ ./bin/setup
21
+ - name: Rubocop
22
+ run: |
23
+ ./bin/bundle exec rubocop --parallel
@@ -0,0 +1,89 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - "test/**/*"
6
+ - "bin/**/*"
7
+ - "*.gemspec"
8
+ - Rakefile
9
+
10
+ Style/Documentation:
11
+ Enabled: false
12
+
13
+ Style/FrozenStringLiteralComment: # PENDING: [Ruby3.x] Remove this, autofix, test, conform.
14
+ Enabled: false
15
+
16
+ Layout/EmptyLinesAroundAttributeAccessor:
17
+ Enabled: true
18
+
19
+ Layout/EmptyLinesAroundClassBody:
20
+ Enabled: false
21
+
22
+ Layout/EmptyLinesAroundModuleBody:
23
+ Enabled: false
24
+
25
+ Layout/SpaceAroundMethodCallOperator:
26
+ Enabled: true
27
+
28
+ Lint/DeprecatedOpenSSLConstant:
29
+ Enabled: true
30
+
31
+ Lint/DuplicateElsifCondition:
32
+ Enabled: true
33
+
34
+ Lint/MixedRegexpCaptureTypes:
35
+ Enabled: true
36
+
37
+ Lint/RaiseException:
38
+ Enabled: true
39
+
40
+ Lint/StructNewOverride:
41
+ Enabled: true
42
+
43
+ Style/AccessorGrouping:
44
+ Enabled: true
45
+
46
+ Style/ArrayCoercion:
47
+ Enabled: true
48
+
49
+ Style/BisectedAttrAccessor:
50
+ Enabled: true
51
+
52
+ Style/CaseLikeIf:
53
+ Enabled: true
54
+
55
+ Style/ExponentialNotation:
56
+ Enabled: true
57
+
58
+ Style/HashAsLastArrayItem:
59
+ Enabled: true
60
+
61
+ Style/HashEachMethods:
62
+ Enabled: true
63
+
64
+ Style/HashLikeCase:
65
+ Enabled: true
66
+
67
+ Style/HashTransformKeys:
68
+ Enabled: true
69
+
70
+ Style/HashTransformValues:
71
+ Enabled: true
72
+
73
+ Style/RedundantAssignment:
74
+ Enabled: true
75
+
76
+ Style/RedundantFetchBlock:
77
+ Enabled: true
78
+
79
+ Style/RedundantFileExtensionInRequire:
80
+ Enabled: true
81
+
82
+ Style/RedundantRegexpCharacterClass:
83
+ Enabled: true
84
+
85
+ Style/RedundantRegexpEscape:
86
+ Enabled: true
87
+
88
+ Style/SlicingWithRange:
89
+ Enabled: true
@@ -0,0 +1,19 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2020-07-30 18:19:25 UTC using RuboCop version 0.88.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
11
+ Metrics/MethodLength:
12
+ Max: 15
13
+
14
+ # Offense count: 1
15
+ # Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, Regex, IgnoreExecutableScripts, AllowedAcronyms.
16
+ # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
17
+ Naming/FileName:
18
+ Exclude:
19
+ - 'lib/aws-embedded-metrics-customink.rb'
@@ -3,6 +3,42 @@
3
3
 
4
4
  See this http://keepachangelog.com link for information on how we want this documented formatted.
5
5
 
6
+ ## v0.6.0
7
+
8
+ ### Fixed
9
+
10
+ - Moved concurrent-ruby to runtime dep.
11
+
12
+ ## v0.5.0
13
+
14
+ ### Added
15
+
16
+ - Simple singleton/delegator for metrics instance in Rails.
17
+
18
+ ## v0.4.0
19
+
20
+ ### Changed
21
+
22
+ - Use Concurrent Ruby for Logger data.
23
+
24
+ ## v0.3.0
25
+
26
+ ### Added
27
+
28
+ - New `benchmark` helper.
29
+
30
+ ## v0.2.0
31
+
32
+ ### Changed
33
+
34
+ - `Lambda` sink renamed to `Stdout` to reflect its destination rather than its intended use
35
+ - The currently configured sink is now accessed through the `Config` object rather than from the root namespace
36
+ - Improved test coverage
37
+
38
+ ### Added
39
+
40
+ - A `Logger` sink to emit to a Ruby Logger instance, for logfile output
41
+
6
42
  ## v0.1.0
7
43
 
8
44
  ### Added
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
- gem 'rake'
5
4
  gem 'minitest'
5
+ gem 'rake'
6
+ gem 'rubocop'
6
7
 
7
8
  group :test do
8
9
  gem 'mocha'
@@ -1,19 +1,41 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- aws-embedded-metrics-customink (0.1.0)
4
+ aws-embedded-metrics-customink (0.6.0)
5
+ concurrent-ruby
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
10
+ ast (2.4.1)
9
11
  coderay (1.1.3)
12
+ concurrent-ruby (1.1.7)
10
13
  method_source (1.0.0)
11
14
  minitest (5.14.1)
12
15
  mocha (1.11.2)
16
+ parallel (1.19.2)
17
+ parser (2.7.1.4)
18
+ ast (~> 2.4.1)
13
19
  pry (0.13.1)
14
20
  coderay (~> 1.1)
15
21
  method_source (~> 1.0)
22
+ rainbow (3.0.0)
16
23
  rake (13.0.1)
24
+ regexp_parser (1.7.1)
25
+ rexml (3.2.3)
26
+ rubocop (0.88.0)
27
+ parallel (~> 1.10)
28
+ parser (>= 2.7.1.1)
29
+ rainbow (>= 2.2.2, < 4.0)
30
+ regexp_parser (>= 1.7)
31
+ rexml
32
+ rubocop-ast (>= 0.1.0, < 1.0)
33
+ ruby-progressbar (~> 1.7)
34
+ unicode-display_width (>= 1.4.0, < 2.0)
35
+ rubocop-ast (0.2.0)
36
+ parser (>= 2.7.0.1)
37
+ ruby-progressbar (1.10.1)
38
+ unicode-display_width (1.7.0)
17
39
 
18
40
  PLATFORMS
19
41
  ruby
@@ -24,6 +46,7 @@ DEPENDENCIES
24
46
  mocha
25
47
  pry
26
48
  rake
49
+ rubocop
27
50
 
28
51
  BUNDLED WITH
29
52
  2.1.2
data/README.md CHANGED
@@ -29,7 +29,7 @@ gem 'aws-embedded-metrics-customink'
29
29
  ```
30
30
  ## Usage
31
31
 
32
- Simple configuration.
32
+ Simple configuration:
33
33
 
34
34
  ```ruby
35
35
  Aws::Embedded::Metrics.configure do |c|
@@ -37,6 +37,14 @@ Aws::Embedded::Metrics.configure do |c|
37
37
  end
38
38
  ```
39
39
 
40
+ Using the `Logger` sink to write to a log file:
41
+
42
+ ```ruby
43
+ Aws::Embedded::Metrics.configure do |c|
44
+ c.sink = Aws::Embedded::Metrics::Sinks::Logger.new(Rails.logger)
45
+ end
46
+ ```
47
+
40
48
  Usage is in a scope block. All metrics are flushed afterward
41
49
 
42
50
  ```ruby
@@ -48,6 +56,38 @@ Aws::Embedded::Metrics.logger do |metrics|
48
56
  end
49
57
  ```
50
58
 
59
+ ## Using Rails?
60
+
61
+ And want to instrument metrics deep in your code during the request/response lifecycle? Consider creating a PORO like this `Metrics` example.
62
+
63
+ ```ruby
64
+ class MyMetrics < Aws::Embedded::Metrics::Instance
65
+ end
66
+ ```
67
+
68
+ This object is ready to use as a per-request singleton that acts as a simple delegator to all metrics/logger methods. A great way to hook it up for your application is in ApplicationController.
69
+
70
+ ```ruby
71
+ class ApplicationController < ActionController::Base
72
+ around_action :embedded_metrics
73
+ private
74
+ def embedded_metrics
75
+ Aws::Embedded::Metrics.logger do |metrics|
76
+ MyMetrics.instance = MyMetrics.new(metrics)
77
+ yield
78
+ end
79
+ end
80
+ end
81
+ ```
82
+
83
+ Now you can happily instrument your code.
84
+
85
+ ```ruby
86
+ proof, time = MyMetrics.benchmark { @imagebuilder.data }
87
+ MyMetrics.put_metric 'ImageBuilderTime', time, 'Milliseconds'
88
+ MyMetrics.set_property 'ImageId', params[:image_id]
89
+ ```
90
+
51
91
  ## Development
52
92
 
53
93
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -19,4 +19,5 @@ Gem::Specification.new do |spec|
19
19
  spec.bindir = "exe"
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
+ spec.add_dependency 'concurrent-ruby'
22
23
  end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -1,14 +1,21 @@
1
1
  require 'json'
2
2
  require 'benchmark'
3
+ require 'concurrent/array'
4
+ require 'concurrent/hash'
3
5
  require 'aws-embedded-metrics-customink/version'
4
- require 'aws-embedded-metrics-customink/config'
5
6
  require 'aws-embedded-metrics-customink/sinks'
7
+ require 'aws-embedded-metrics-customink/config'
6
8
  require 'aws-embedded-metrics-customink/logger'
9
+ require 'aws-embedded-metrics-customink/instance' if defined?(Rails)
7
10
 
8
11
  module Aws
9
12
  module Embedded
10
13
  module Metrics
11
14
 
15
+ def config
16
+ Config.config
17
+ end
18
+
12
19
  def configure
13
20
  Config.configure { |c| yield(c) }
14
21
  end
@@ -23,11 +30,7 @@ module Aws
23
30
  end
24
31
  end
25
32
 
26
- def sink
27
- @sink ||= Sinks::Lambda.new
28
- end
29
-
30
- extend self
33
+ module_function :config, :configure, :reconfigure, :logger
31
34
 
32
35
  end
33
36
  end
@@ -3,6 +3,8 @@ module Aws
3
3
  module Metrics
4
4
  module Config
5
5
 
6
+ DEFAULT_SINK = Sinks::Stdout
7
+
6
8
  def configure
7
9
  yield(config)
8
10
  config
@@ -16,11 +18,11 @@ module Aws
16
18
  @config ||= Configuration.new
17
19
  end
18
20
 
19
- extend self
21
+ module_function :configure, :reconfigure, :config
20
22
 
21
23
  class Configuration
22
24
 
23
- attr_writer :namespace
25
+ attr_writer :namespace, :sink
24
26
 
25
27
  def reconfigure
26
28
  instance_variables.each { |var| instance_variable_set var, nil }
@@ -30,9 +32,14 @@ module Aws
30
32
 
31
33
  def namespace
32
34
  return @namespace if defined?(@namespace)
35
+
33
36
  ENV['AWS_EMF_NAMESPACE'] || 'aws-embedded-metrics'
34
37
  end
35
38
 
39
+ def sink
40
+ @sink ||= DEFAULT_SINK.new
41
+ end
42
+
36
43
  end
37
44
  end
38
45
  end
@@ -0,0 +1,18 @@
1
+ module Aws
2
+ module Embedded
3
+ module Metrics
4
+ class Instance < SimpleDelegator
5
+ mattr_accessor :instance
6
+
7
+ extend SingleForwardable
8
+
9
+ def_delegators :instance,
10
+ :flush,
11
+ :benchmark,
12
+ :put_dimension,
13
+ :put_metric,
14
+ :set_property
15
+ end
16
+ end
17
+ end
18
+ end
@@ -3,12 +3,12 @@ module Aws
3
3
  module Metrics
4
4
  class Logger
5
5
 
6
- def initialize(sink = Sinks::Lambda)
7
- @sink = sink.new
6
+ def initialize(sink = Config.config.sink)
7
+ @sink = sink
8
8
  @namespace = Config.config.namespace
9
- @dimensions = []
10
- @metrics = []
11
- @properties = {}
9
+ @dimensions = Concurrent::Array.new
10
+ @metrics = Concurrent::Array.new
11
+ @properties = Concurrent::Hash.new
12
12
  end
13
13
 
14
14
  def metrics
@@ -18,7 +18,14 @@ module Aws
18
18
  end
19
19
 
20
20
  def flush
21
- Metrics.sink.accept(message) unless empty?
21
+ @sink.accept(message) unless empty?
22
+ end
23
+
24
+ def benchmark
25
+ value = nil
26
+ seconds = Benchmark.realtime { value = yield }
27
+ milliseconds = (seconds * 1000).to_i
28
+ [value, milliseconds]
22
29
  end
23
30
 
24
31
  def put_dimension(name, value)
@@ -27,9 +34,9 @@ module Aws
27
34
  end
28
35
 
29
36
  def put_metric(name, value, unit = nil)
30
- @metrics << { 'Name' => name }.tap { |m|
37
+ @metrics << { 'Name' => name }.tap do |m|
31
38
  m['Unit'] = unit if unit
32
- }
39
+ end
33
40
  set_property name, value
34
41
  end
35
42
 
@@ -39,7 +46,7 @@ module Aws
39
46
  end
40
47
 
41
48
  def empty?
42
- [@dimensions, @metrics, @properties].all? { |x| x.empty? }
49
+ [@dimensions, @metrics, @properties].all?(&:empty?)
43
50
  end
44
51
 
45
52
  def message
@@ -1 +1,2 @@
1
- require 'aws-embedded-metrics-customink/sinks/lambda'
1
+ require 'aws-embedded-metrics-customink/sinks/logger'
2
+ require 'aws-embedded-metrics-customink/sinks/stdout'
@@ -0,0 +1,22 @@
1
+ module Aws
2
+ module Embedded
3
+ module Metrics
4
+ module Sinks
5
+ class Logger
6
+
7
+ attr_reader :logger, :level
8
+
9
+ def initialize(logger, level: :info)
10
+ @logger = logger
11
+ @level = level.to_sym
12
+ end
13
+
14
+ def accept(message)
15
+ logger.public_send(level, JSON.dump(message))
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,7 +2,7 @@ module Aws
2
2
  module Embedded
3
3
  module Metrics
4
4
  module Sinks
5
- class Lambda
5
+ class Stdout
6
6
 
7
7
  def accept(message)
8
8
  puts JSON.dump(message)
@@ -1,7 +1,7 @@
1
1
  module Aws
2
2
  module Embedded
3
3
  module Metrics
4
- VERSION = '0.1.0'.freeze
4
+ VERSION = '0.6.0'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-embedded-metrics-customink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Collins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-29 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
13
27
  description: Amazon CloudWatch Embedded Metric Format Client Library for Ruby.
14
28
  email:
15
29
  - ken@metaskills.net
@@ -18,7 +32,10 @@ extensions: []
18
32
  extra_rdoc_files: []
19
33
  files:
20
34
  - ".github/workflows/ci.yml"
35
+ - ".github/workflows/lint.yml"
21
36
  - ".gitignore"
37
+ - ".rubocop.yml"
38
+ - ".rubocop_todo.yml"
22
39
  - CHANGELOG.md
23
40
  - CODE_OF_CONDUCT.md
24
41
  - Gemfile
@@ -27,14 +44,17 @@ files:
27
44
  - README.md
28
45
  - Rakefile
29
46
  - aws-embedded-metrics-customink.gemspec
47
+ - bin/bundle
30
48
  - bin/console
31
49
  - bin/setup
32
50
  - bin/test
33
51
  - lib/aws-embedded-metrics-customink.rb
34
52
  - lib/aws-embedded-metrics-customink/config.rb
53
+ - lib/aws-embedded-metrics-customink/instance.rb
35
54
  - lib/aws-embedded-metrics-customink/logger.rb
36
55
  - lib/aws-embedded-metrics-customink/sinks.rb
37
- - lib/aws-embedded-metrics-customink/sinks/lambda.rb
56
+ - lib/aws-embedded-metrics-customink/sinks/logger.rb
57
+ - lib/aws-embedded-metrics-customink/sinks/stdout.rb
38
58
  - lib/aws-embedded-metrics-customink/version.rb
39
59
  homepage: https://github.com/customink/aws-embedded-metrics-customink
40
60
  licenses: