metrics 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/metrics/backend/console.rb +23 -23
- data/lib/metrics/backend/test.rb +48 -55
- data/lib/metrics/backend.rb +3 -3
- data/lib/metrics/metric.rb +41 -0
- data/lib/metrics/provider.rb +9 -3
- data/lib/metrics/tags.rb +37 -0
- data/lib/metrics/version.rb +1 -1
- data.tar.gz.sig +0 -0
- metadata +4 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8db52b23e0e9f9fc5b7b1253667da5ee801eea1f2e0555908cb51f698c3e3ae7
|
4
|
+
data.tar.gz: b428e08aeb5d30604a19e69058567ba0be4bdd188ac6bad8d7a15c64766f9dc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
28
|
-
|
29
|
-
|
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
|
-
|
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
|
+
|
data/lib/metrics/backend/test.rb
CHANGED
@@ -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
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
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
|
-
|
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
|
data/lib/metrics/backend.rb
CHANGED
@@ -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
|
26
|
-
path
|
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
|
data/lib/metrics/provider.rb
CHANGED
@@ -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
|
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.
|
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
|
-
#
|
52
|
+
# Metrics disabled.
|
47
53
|
end
|
48
54
|
end
|
49
55
|
end
|
data/lib/metrics/tags.rb
ADDED
@@ -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
|
data/lib/metrics/version.rb
CHANGED
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.
|
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-
|
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
|