metrics 0.2.0 → 0.3.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: ca751f7a0f0ce3c575014f236d8f34738723c6b64933c1fac259f9c19b13c460
4
- data.tar.gz: 0e9b3b2a695ec555416b20d969968079943730723411076081c6e5447af632aa
3
+ metadata.gz: 8db52b23e0e9f9fc5b7b1253667da5ee801eea1f2e0555908cb51f698c3e3ae7
4
+ data.tar.gz: b428e08aeb5d30604a19e69058567ba0be4bdd188ac6bad8d7a15c64766f9dc1
5
5
  SHA512:
6
- metadata.gz: ce75234031de1b6382ddb86311ea33afa4c1924f9ed14a233ff010a4a6acaf2de09a95aeda229462def77eb84d3cc831da1f3ca52e5c1d7a4376a65fb95485ad
7
- data.tar.gz: 1392e8e3f39e800dc33b63e9e30a1ca9c15da3b03d2ed04f01393db8a07582f32afd6e15c3c203d0d0cf9a35afabdc1ca966c0ee299be4d571b134b96b20b29f
6
+ metadata.gz: 397fa3e698ffbcabe00733a9c2370e9d4fb96320669fd546a547f01dae73c90f97932ee4eb19f9f5ce6579e123ad0931d62635b3539e6bc46cb5db5605e3073f
7
+ data.tar.gz: 9a60eb2350b9d7ee760636c45392d0aa3ee198c90ebc7d51ee3f9556f4b75c8c9af7aff4ffdc9edc5ac2a904d551d5b504818ff8a67cfcbf4b6b2078681c3d5b
checksums.yaml.gz.sig CHANGED
Binary file
@@ -21,34 +21,34 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'console'
24
+ require_relative '../metric'
24
25
 
25
26
  module Metrics
26
27
  module Backend
27
- module Register
28
- def metric_register(name, type, **options)
29
- Console.logger.debug(self) {"Registering metric #{name} (#{type}): #{options}."}
28
+ module Console
29
+ class Metric < Metrics::Metric
30
+ def emit(value, tags: nil, sample_rate: 1.0)
31
+ ::Console.logger.info(self, @name, value, tags)
32
+ end
33
+ end
34
+
35
+ module Interface
36
+ def metric(name, type, description: nil, unit: nil, &block)
37
+ return Metric.new(name, type, description, unit)
38
+ end
39
+
40
+ # def metric_call_counter(name, description: nil, tags: nil)
41
+ # metric = self.metric(...)
42
+ #
43
+ # self.define_method(name) do
44
+ # metric.emit(1)
45
+ # super
46
+ # end
47
+ # end
30
48
  end
31
49
  end
32
50
 
33
- def self.prepended(provider)
34
- provider.extend(Register)
35
- end
36
-
37
- private
38
-
39
- # Relative metric adjustment.
40
- def metric_adjust(name, amount = 1, attributes: nil)
41
- Console.logger.info(self, name, "+= #{amount}", attributes)
42
- end
43
-
44
- # Absolute metric assignment.
45
- def metric_count(name, value, attributes: nil)
46
- Console.logger.info(self, name, "= #{amount}", attributes)
47
- end
48
-
49
- # Relative metric measurement.
50
- def metric_measure(name, value, attributes: nil)
51
- Console.logger.info(self, name, "<< #{amount}", attributes)
52
- end
51
+ Interface = Console::Interface
53
52
  end
54
53
  end
54
+
@@ -21,70 +21,63 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'console'
24
+ require_relative '../metric'
24
25
 
25
26
  module Metrics
26
27
  module Backend
27
- module Register
28
- def metric_register(name, type, description: nil, unit: nil, &block)
29
- unless name.is_a?(String)
30
- raise ArgumentError, "Invalid name!"
31
- end
32
-
33
- unless type.is_a?(Symbol)
34
- raise ArgumentError, "Invalid type!"
35
- end
36
-
37
- if description
38
- unless description.is_a?(String)
39
- raise ArgumentError, "Invalid description!"
28
+ module Test
29
+ VALID_METRIC_NAME = /\A[a-z0-9\-_\.]{1,128}\Z/
30
+ VALID_TAG = /\A[a-z][a-z0-9\-_\.:\\]{0,127}\Z/
31
+
32
+ class Metric < Metrics::Metric
33
+ def emit(value, tags: nil, sample_rate: 1.0)
34
+ unless value.is_a?(Numeric)
35
+ raise ArgumentError, "Value must be numeric!"
40
36
  end
41
- end
42
-
43
- if unit
44
- unless unit.is_a?(String)
45
- raise ArgumentError, "Invalid unit!"
37
+
38
+ tags&.each do |tag|
39
+ raise ArgumentError, "Invalid tag, must be string!" unless tag.is_a?(String)
40
+
41
+ unless tag =~ VALID_TAG
42
+ raise ArgumentError, "Invalid tag, must match #{VALID_TAG}!"
43
+ end
46
44
  end
47
45
  end
48
46
  end
49
- end
50
-
51
- def self.prepended(provider)
52
- provider.extend(Register)
53
- end
54
-
55
- private
56
-
57
- # Relative metric adjustment.
58
- def metric_adjust(name, amount = 1, attributes: nil)
59
- unless name.is_a?(String)
60
- raise ArgumentError, "Invalid name!"
61
- end
62
47
 
63
- unless amount.is_a?(Numeric)
64
- raise ArgumentError, "Invalid amount!"
65
- end
66
- end
67
-
68
- # Absolute metric assignment.
69
- def metric_count(name, value, attributes: nil)
70
- unless name.is_a?(String)
71
- raise ArgumentError, "Invalid name!"
72
- end
73
-
74
- unless value.is_a?(Numeric)
75
- raise ArgumentError, "Invalid value!"
48
+ module Interface
49
+ def metric(name, type, description: nil, unit: nil, &block)
50
+ unless name.is_a?(String)
51
+ raise ArgumentError, "Invalid name, must be string!"
52
+ end
53
+
54
+ unless name =~ VALID_METRIC_NAME
55
+ raise ArgumentError, "Invalid name, must match #{VALID_METRIC_NAME}!"
56
+ end
57
+
58
+ unless type.is_a?(Symbol)
59
+ raise ArgumentError, "Invalid type, must be symbol!"
60
+ end
61
+
62
+ # Description is optional but must be string if given:
63
+ if description
64
+ unless description.is_a?(String)
65
+ raise ArgumentError, "Invalid description, must be string!"
66
+ end
67
+ end
68
+
69
+ # Unit is optional but must be string if given:
70
+ if unit
71
+ unless unit.is_a?(String)
72
+ raise ArgumentError, "Invalid unit, must be string!"
73
+ end
74
+ end
75
+
76
+ return Metric.new(name, type, description, unit)
77
+ end
76
78
  end
77
79
  end
78
80
 
79
- # Relative metric measurement.
80
- def metric_measure(name, value, attributes: nil)
81
- unless name.is_a?(String)
82
- raise ArgumentError, "Invalid name!"
83
- end
84
-
85
- unless value.is_a?(Numeric)
86
- raise ArgumentError, "Invalid value!"
87
- end
88
- end
81
+ Interface = Test::Interface
89
82
  end
90
- end
83
+ end
@@ -21,10 +21,10 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Metrics
24
+ # Require a specific trace backend.
24
25
  def self.require_backend(env = ENV)
25
- if backend = env['METRICS_BACKEND']
26
- path = File.join('backend', backend)
27
- require_relative(path)
26
+ if path = env['METRICS_BACKEND']
27
+ require(path)
28
28
  end
29
29
  end
30
30
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ module Metrics
24
+ class Metric
25
+ def initialize(name, type, description, unit)
26
+ @name = name
27
+ @type = type
28
+ @description = description
29
+ @unit = unit
30
+ end
31
+
32
+ attr :name
33
+ attr :type
34
+ attr :description
35
+ attr :unit
36
+
37
+ def emit(value, tags: nil, sample_rate: 1.0)
38
+ raise NotImplementedError
39
+ end
40
+ end
41
+ end
@@ -23,6 +23,11 @@
23
23
  require_relative 'backend'
24
24
 
25
25
  module Metrics
26
+ def self.enabled?
27
+ self.const_defined?(:Backend)
28
+ end
29
+
30
+ # A module which contains tracing specific wrappers.
26
31
  module Provider
27
32
  def metrics_provider
28
33
  @metrics_provider ||= Module.new
@@ -30,12 +35,13 @@ module Metrics
30
35
  end
31
36
 
32
37
  # Bail out if there is no backend configured.
33
- if Metrics.const_defined?(:Backend)
38
+ if self.enabled?
39
+ # Extend the specified class in order to emit traces.
34
40
  def self.Provider(klass, &block)
35
41
  klass.extend(Provider)
36
42
 
37
43
  provider = klass.metrics_provider
38
- provider.prepend(Backend)
44
+ provider.extend(Backend::Interface)
39
45
 
40
46
  klass.prepend(provider)
41
47
 
@@ -43,7 +49,7 @@ module Metrics
43
49
  end
44
50
  else
45
51
  def self.Provider(klass, &block)
46
- # Tracing disabled.
52
+ # Metrics disabled.
47
53
  end
48
54
  end
49
55
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
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.
22
+
23
+ require_relative 'backend'
24
+
25
+ module Metrics
26
+ module Tags
27
+ def self.normalize(tags)
28
+ return nil unless tags&.any?
29
+
30
+ if tags.is_a?(Hash)
31
+ tags = tags.map{|key, value| "#{key}:#{value}"}
32
+ end
33
+
34
+ return Array(tags)
35
+ end
36
+ end
37
+ end
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Metrics
24
- VERSION = "0.2.0"
24
+ VERSION = "0.3.0"
25
25
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -36,7 +36,7 @@ cert_chain:
36
36
  RAOsIl+HOBTb252nx1kIRN5hqQx272AJCbCjKx8egcUQKffFVVCI0nye09v5CK+a
37
37
  HiLJ8VOFx6w=
38
38
  -----END CERTIFICATE-----
39
- date: 2021-10-15 00:00:00.000000000 Z
39
+ date: 2021-10-26 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
@@ -62,7 +62,9 @@ files:
62
62
  - lib/metrics/backend.rb
63
63
  - lib/metrics/backend/console.rb
64
64
  - lib/metrics/backend/test.rb
65
+ - lib/metrics/metric.rb
65
66
  - lib/metrics/provider.rb
67
+ - lib/metrics/tags.rb
66
68
  - lib/metrics/version.rb
67
69
  homepage: https://github.com/socketry/metrics
68
70
  licenses:
metadata.gz.sig CHANGED
Binary file