metrics-backend-statsd 0.1.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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data/lib/metrics/backend/statsd/interface.rb +104 -0
- data/lib/metrics/backend/statsd/version.rb +12 -0
- data/lib/metrics/backend/statsd.rb +7 -0
- data/license.md +21 -0
- data/readme.md +54 -0
- data.tar.gz.sig +0 -0
- metadata +87 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8f9b7c611b6af050e5a387907f38a4506c53e872b65991d8cb2037910180114a
|
4
|
+
data.tar.gz: ad44b521e73e76726ba68194a986c3f0393c32bd2a6755928b75fc9f90605553
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 267261b06c704edcad1be0a0311ffdd5271173f6061593b897e7c9c12798ced01fea27e7cf70c2e0b38f17a3cd6dda054e0d1e9c60167fc0a8005dc5f4a25d2c
|
7
|
+
data.tar.gz: 2623ba15b063aa175116131b5ae832c9e2e634207702e59d37c23593b0bcd4b02522f5f8cd28df3fad1625124085c6d21f807294cdbd3368816a9f6302bf9edd
|
checksums.yaml.gz.sig
ADDED
Binary file
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Released under the MIT License.
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
5
|
+
|
6
|
+
require "metrics/metric"
|
7
|
+
require "metrics/tags"
|
8
|
+
|
9
|
+
require "console"
|
10
|
+
require "io/endpoint/host_endpoint"
|
11
|
+
|
12
|
+
module Metrics
|
13
|
+
module Backend
|
14
|
+
module Statsd
|
15
|
+
::Thread.attr_accessor :metrics_backend_statsd_instance
|
16
|
+
|
17
|
+
class Client
|
18
|
+
DEFAULT_ENDPOINT = IO::Endpoint.udp("localhost", 8125)
|
19
|
+
|
20
|
+
def initialize(endpoint = DEFAULT_ENDPOINT)
|
21
|
+
@endpoint = endpoint
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_metric(name, value, type:, sample_rate: 1.0, tags: nil)
|
25
|
+
return if sample_rate < 1.0 && rand > sample_rate
|
26
|
+
|
27
|
+
parts = ["#{name}:#{value}|#{type}"]
|
28
|
+
parts << "@#{sample_rate}" if sample_rate < 1.0
|
29
|
+
parts << format_tags(tags) if tags
|
30
|
+
|
31
|
+
message = parts.compact.join("|")
|
32
|
+
|
33
|
+
# Basically, we don't want to block the application if the socket is not connected or otherwise unavailable:
|
34
|
+
self.socket.sendmsg_nonblock(message, 0, exception: false)
|
35
|
+
rescue Errno::ECONNREFUSED => error
|
36
|
+
Console.error(self, "Failed to send metric!", error)
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def socket
|
42
|
+
@socket ||= @endpoint.connect
|
43
|
+
end
|
44
|
+
|
45
|
+
def format_tags(tags)
|
46
|
+
"##{tags.join(',')}" if tags && !tags.empty?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.instance
|
51
|
+
Thread.current.metrics_backend_statsd_instance ||= Client.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.instance=(instance)
|
55
|
+
Thread.current.metrics_backend_statsd_instance = instance
|
56
|
+
end
|
57
|
+
|
58
|
+
class Metric < Metrics::Metric
|
59
|
+
def emit(value, tags: nil, sample_rate: 1.0)
|
60
|
+
Statsd.instance.send_metric(@name, value, type: "c", sample_rate: sample_rate, tags: Tags.normalize(tags))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class Gauge < Metric
|
65
|
+
def emit(value, tags: nil, sample_rate: 1.0)
|
66
|
+
Statsd.instance.send_metric(@name, value, type: "g", sample_rate: sample_rate, tags: Tags.normalize(tags))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class Histogram < Metric
|
71
|
+
def emit(value, tags: nil, sample_rate: 1.0)
|
72
|
+
Statsd.instance.send_metric(@name, value, type: "h", sample_rate: sample_rate, tags: Tags.normalize(tags))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class Distribution < Metric
|
77
|
+
def emit(value, tags: nil, sample_rate: 1.0)
|
78
|
+
Statsd.instance.send_metric(@name, value, type: "d", sample_rate: sample_rate, tags: Tags.normalize(tags))
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
module Interface
|
83
|
+
def metric(name, type, description: nil, unit: nil)
|
84
|
+
klass = Metric
|
85
|
+
|
86
|
+
case type
|
87
|
+
when :counter
|
88
|
+
# klass = Metric
|
89
|
+
when :guage
|
90
|
+
klass = Gauge
|
91
|
+
when :histogram
|
92
|
+
klass = Histogram
|
93
|
+
when :distribution
|
94
|
+
klass = Distribution
|
95
|
+
end
|
96
|
+
|
97
|
+
klass.new(name, type, description, unit)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
Interface = Statsd::Interface
|
103
|
+
end
|
104
|
+
end
|
data/license.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MIT License
|
2
|
+
|
3
|
+
Copyright, 2025, by Samuel Williams.
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/readme.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Metrics::Backend::Statsd
|
2
|
+
|
3
|
+
A metrics backend for Statsd.
|
4
|
+
|
5
|
+
[](https://github.com/socketry/metrics-backend-statsd/actions?workflow=Test)
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
If you need to specify custom options, you can override the default statsd client instance.
|
10
|
+
|
11
|
+
``` ruby
|
12
|
+
# config/initializers/metrics/backend/statsd.rb
|
13
|
+
|
14
|
+
require 'metrics/backend/statsd'
|
15
|
+
|
16
|
+
module Metrics
|
17
|
+
module Backend
|
18
|
+
module Statsd
|
19
|
+
# Override the default instance setup:
|
20
|
+
def self.new
|
21
|
+
instance = ::Statsd::Statsd.new('localhost', 8125, logger: Console.logger)
|
22
|
+
|
23
|
+
at_exit do
|
24
|
+
self.close
|
25
|
+
end
|
26
|
+
|
27
|
+
return instance
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
We welcome contributions to this project.
|
37
|
+
|
38
|
+
1. Fork it.
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`).
|
42
|
+
5. Create new Pull Request.
|
43
|
+
|
44
|
+
### Developer Certificate of Origin
|
45
|
+
|
46
|
+
In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
|
47
|
+
|
48
|
+
### Community Guidelines
|
49
|
+
|
50
|
+
This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
|
51
|
+
|
52
|
+
## See Also
|
53
|
+
|
54
|
+
- [metrics](https://github.com/socketry/metrics) — Capture metrics about code execution in a vendor agnostic way.
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metrics-backend-statsd
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Williams
|
8
|
+
bindir: bin
|
9
|
+
cert_chain:
|
10
|
+
- |
|
11
|
+
-----BEGIN CERTIFICATE-----
|
12
|
+
MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
|
13
|
+
ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
|
14
|
+
CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
|
15
|
+
MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
|
16
|
+
MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
|
17
|
+
bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
|
18
|
+
igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
|
19
|
+
9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YD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QWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP
|
35
|
+
eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt
|
36
|
+
Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
|
37
|
+
voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
|
38
|
+
-----END CERTIFICATE-----
|
39
|
+
date: 2025-03-07 00:00:00.000000000 Z
|
40
|
+
dependencies:
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: metrics
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
executables: []
|
56
|
+
extensions: []
|
57
|
+
extra_rdoc_files: []
|
58
|
+
files:
|
59
|
+
- lib/metrics/backend/statsd.rb
|
60
|
+
- lib/metrics/backend/statsd/interface.rb
|
61
|
+
- lib/metrics/backend/statsd/version.rb
|
62
|
+
- license.md
|
63
|
+
- readme.md
|
64
|
+
homepage: https://github.com/socketry/metrics-backend-statsd
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata:
|
68
|
+
documentation_uri: https://socketry.github.io/metrics-backend-statsd/
|
69
|
+
source_code_uri: https://github.com/socketry/metrics-backend-statsd.git
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '3.1'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubygems_version: 3.6.2
|
85
|
+
specification_version: 4
|
86
|
+
summary: Application metrics and instrumentation.
|
87
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|