metrics 0.1.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47038b401644ef77b0316ac3b16340df8f7efe44ca7d5b696a1cb73615a52ef7
4
- data.tar.gz: '018de755d4db7d975b4b699b8fe67d7f19559816f1d970b595836609905c9a3c'
3
+ metadata.gz: 17744766f0b1ea11b5ff6d133cff9f17e3779c5ca364eb4d0ec6baff47a113b8
4
+ data.tar.gz: d375e8a6a550e480116af29e648c8e81c4a5639d1dc09dffdf0cfbfe8055f600
5
5
  SHA512:
6
- metadata.gz: 3a5e5475285bad1a0b9e1124e5988a06490cf3a0eaed388499b1deaf91410084713a797aee99cd7bed6cde2483d7fe9fa0db471de01c38f2cfc899bfec94b87d
7
- data.tar.gz: '019ed337389e7508fe8115e1587810a4eeee9d206bac126139718af8de82bb807928afca1588987cad5a964b8f510e9c4e50768f64eba3feb8f7c757f7acbdb9'
6
+ metadata.gz: 877a67b65d9c2b1ee8febb5a0899dd37dc07f2553d9b8a98a6c3c27c266c4c3a92c4345d717749110d7501e2eec0a9e9e5b70219c93fa6097c3d573c2a8f3e92
7
+ data.tar.gz: f0e2439306576fd0dcf20c1b6715da9534aa149252b7a6fb2aeb4d5bf8ec8844bf932a0d287b542ea23f03cf9d0ec41277c4d8bf6ff84557fdc4ba1dc4a138c5
checksums.yaml.gz.sig CHANGED
Binary file
@@ -21,21 +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
- private
28
-
29
- def metric_increment(name, amount = 1, attributes: nil)
30
- Console.logger.info(self, name, "+= #{amount}", attributes)
31
- end
32
-
33
- def metric_decrement(name, amount = 1, attributes: nil)
34
- Console.logger.info(self, name, "-= #{amount}", attributes)
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
48
+ end
35
49
  end
36
50
 
37
- def metric_measure(name, amount = 1, attributes: nil)
38
- Console.logger.info(self, name, "<< #{amount}", attributes)
39
- end
51
+ Interface = Console::Interface
40
52
  end
41
53
  end
54
+
@@ -21,18 +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
- private
28
-
29
- def metric_increment(name, amount = 1, attributes: nil)
30
- end
31
-
32
- def metric_decrement(name, amount = 1, attributes: nil)
28
+ module Test
29
+ VALID_METRIC_NAME = /\A[a-z0-9\-_\.]{1,128}\Z/i
30
+ VALID_TAG = /\A[a-z][a-z0-9\-_\.:\\]{0,127}\Z/i
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!"
36
+ end
37
+
38
+ tags&.each do |tag|
39
+ raise ArgumentError, "Invalid tag (must be String): #{tag.inspect}!" unless tag.is_a?(String)
40
+
41
+ unless tag =~ VALID_TAG
42
+ raise ArgumentError, "Invalid tag (must match #{VALID_TAG}): #{tag.inspect}!"
43
+ end
44
+ end
45
+ end
46
+ end
47
+
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): #{name.inspect}!"
52
+ end
53
+
54
+ unless name =~ VALID_METRIC_NAME
55
+ raise ArgumentError, "Invalid name (must match #{VALID_METRIC_NAME}): #{name.inspect}!"
56
+ end
57
+
58
+ unless type.is_a?(Symbol)
59
+ raise ArgumentError, "Invalid type (must be Symbol): #{type.inspect}!"
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): #{description.inspect}!"
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): #{unit.inspect}!"
73
+ end
74
+ end
75
+
76
+ return Metric.new(name, type, description, unit)
77
+ end
78
+ end
33
79
  end
34
80
 
35
- def metric_measure(name, amount = 1, attributes: nil)
36
- end
81
+ Interface = Test::Interface
37
82
  end
38
- 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,12 @@
23
23
  require_relative 'backend'
24
24
 
25
25
  module Metrics
26
+ # @returns [Boolean] Whether there is an active backend.
27
+ def self.enabled?
28
+ self.const_defined?(:Backend)
29
+ end
30
+
31
+ # A module which contains tracing specific wrappers.
26
32
  module Provider
27
33
  def metrics_provider
28
34
  @metrics_provider ||= Module.new
@@ -30,13 +36,13 @@ module Metrics
30
36
  end
31
37
 
32
38
  # Bail out if there is no backend configured.
33
- if Metrics.const_defined?(:Backend)
39
+ if self.enabled?
40
+ # Extend the specified class in order to emit traces.
34
41
  def self.Provider(klass, &block)
35
42
  klass.extend(Provider)
36
- klass.prepend(Backend)
37
43
 
38
44
  provider = klass.metrics_provider
39
- provider.prepend(Backend)
45
+ provider.extend(Backend::Interface)
40
46
 
41
47
  klass.prepend(provider)
42
48
 
@@ -44,7 +50,7 @@ module Metrics
44
50
  end
45
51
  else
46
52
  def self.Provider(klass, &block)
47
- # Tracing disabled.
53
+ # Metrics disabled.
48
54
  end
49
55
  end
50
56
  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.1.0"
24
+ VERSION = "0.3.2"
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.1.0
4
+ version: 0.3.2
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-09 00:00:00.000000000 Z
39
+ date: 2022-01-27 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:
@@ -83,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
85
  - !ruby/object:Gem::Version
84
86
  version: '0'
85
87
  requirements: []
86
- rubygems_version: 3.2.22
88
+ rubygems_version: 3.1.6
87
89
  signing_key:
88
90
  specification_version: 4
89
91
  summary: Application metrics and instrumentation.
metadata.gz.sig CHANGED
Binary file