active_statsd 0.1.0 → 0.1.0.1
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 +4 -4
- data/lib/active_statsd/configuration.rb +5 -1
- data/lib/active_statsd/railtie.rb +4 -4
- data/lib/active_statsd/server.rb +34 -8
- data/lib/active_statsd/version.rb +1 -1
- data/lib/generators/active_statsd/active_statsd_generator.rb +12 -0
- data/lib/generators/active_statsd/templates/active_statsd_initializer.rb +5 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65a1d95ad8e6bb84ddaa3ebfdbdebf0f2e22461690f7600c8ddffdfd4e285d49
|
4
|
+
data.tar.gz: 6678ff948cb67c06d9d577186de57ebfc06b2d6cbe46ef42a7d35daea5e2e803
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32d76aaf7d39d8f9f89c7097da9a6ed184388044ab9f087e0ab5882b4df125f879a18d7dda19f53b12840f50d7daf18285f0753ece6c679d93a18a1865c85cd2
|
7
|
+
data.tar.gz: f4fae172b244f3913fb1a380993848396d91b3b7e625a78d25b4b944d573dd2faf20b320bc8a74025f11eaad620bb2904113a1916f9d6a223b1c0358d6a80466
|
@@ -1,11 +1,15 @@
|
|
1
|
+
# lib/active_statsd/configuration.rb
|
1
2
|
module ActiveStatsD
|
2
3
|
class Configuration
|
3
|
-
attr_accessor :host, :port, :namespace
|
4
|
+
attr_accessor :host, :port, :namespace, :aggregation, :forward_host, :forward_port
|
4
5
|
|
5
6
|
def initialize
|
6
7
|
@host = '127.0.0.1'
|
7
8
|
@port = 8125
|
8
9
|
@namespace = 'rails_app'
|
10
|
+
@aggregation = true # Aggregation enabled by default
|
11
|
+
@forward_host = nil # Set if forwarding
|
12
|
+
@forward_port = nil
|
9
13
|
end
|
10
14
|
end
|
11
15
|
end
|
@@ -7,12 +7,12 @@ module ActiveStatsD
|
|
7
7
|
ActiveSupport.on_load(:after_initialize) do
|
8
8
|
server = ActiveStatsD::Server.new(
|
9
9
|
host: ActiveStatsD.configuration.host,
|
10
|
-
port: ActiveStatsD.configuration.port
|
10
|
+
port: ActiveStatsD.configuration.port,
|
11
|
+
aggregation: ActiveStatsD.configuration.aggregation,
|
12
|
+
forward_host: ActiveStatsD.configuration.forward_host,
|
13
|
+
forward_port: ActiveStatsD.configuration.forward_port
|
11
14
|
)
|
12
|
-
|
13
15
|
server.start
|
14
|
-
|
15
|
-
Rails.logger.info "[ActiveStatsD] Embedded StatsD server started on #{ActiveStatsD.configuration.host}:#{ActiveStatsD.configuration.port}"
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
data/lib/active_statsd/server.rb
CHANGED
@@ -3,18 +3,22 @@ require 'socket'
|
|
3
3
|
|
4
4
|
module ActiveStatsD
|
5
5
|
class Server
|
6
|
-
def initialize(host
|
6
|
+
def initialize(host:, port:, aggregation:, forward_host:, forward_port:)
|
7
7
|
@host = host
|
8
8
|
@port = port
|
9
|
+
@aggregation = aggregation
|
10
|
+
@forward_host = forward_host
|
11
|
+
@forward_port = forward_port
|
9
12
|
@counters = Hash.new(0)
|
10
13
|
@mutex = Mutex.new
|
14
|
+
@forward_socket = UDPSocket.new if forwarding_enabled?
|
11
15
|
end
|
12
16
|
|
13
17
|
def start
|
14
|
-
start_aggregation_thread
|
18
|
+
start_aggregation_thread if aggregation_enabled?
|
15
19
|
|
16
20
|
Thread.new do
|
17
|
-
Rails.logger.info "[ActiveStatsD] UDP StatsD listener started on #{@host}:#{@port}"
|
21
|
+
Rails.logger.info "[ActiveStatsD] UDP StatsD listener started on #{@host}:#{@port} (aggregation=#{@aggregation})"
|
18
22
|
begin
|
19
23
|
Socket.udp_server_loop(@host, @port) do |msg, _|
|
20
24
|
Rails.logger.debug "[ActiveStatsD] UDP packet received: #{msg.strip}"
|
@@ -33,16 +37,20 @@ module ActiveStatsD
|
|
33
37
|
|
34
38
|
return unless metric && type == 'c'
|
35
39
|
|
36
|
-
|
37
|
-
@counters[metric] += value
|
40
|
+
if aggregation_enabled?
|
41
|
+
@mutex.synchronize { @counters[metric] += value }
|
42
|
+
end
|
43
|
+
|
44
|
+
if forwarding_enabled?
|
45
|
+
forward_message(message)
|
38
46
|
end
|
47
|
+
|
48
|
+
log_message(metric, value, type) unless aggregation_enabled?
|
39
49
|
end
|
40
50
|
|
41
51
|
def parse_metric(message)
|
42
|
-
# Example message: "rails_app.test.metric:1|c"
|
43
52
|
metric_data, type = message.split('|')
|
44
53
|
metric, value = metric_data.split(':')
|
45
|
-
|
46
54
|
[metric, value.to_i, type]
|
47
55
|
rescue
|
48
56
|
Rails.logger.error "[ActiveStatsD] Failed to parse metric: #{message}"
|
@@ -52,7 +60,7 @@ module ActiveStatsD
|
|
52
60
|
def start_aggregation_thread
|
53
61
|
Thread.new do
|
54
62
|
loop do
|
55
|
-
sleep 10
|
63
|
+
sleep 10
|
56
64
|
flush_metrics
|
57
65
|
end
|
58
66
|
end
|
@@ -69,5 +77,23 @@ module ActiveStatsD
|
|
69
77
|
Rails.logger.info "[ActiveStatsD] Aggregated metric - #{metric}: #{count}"
|
70
78
|
end
|
71
79
|
end
|
80
|
+
|
81
|
+
def forwarding_enabled?
|
82
|
+
@forward_host && @forward_port
|
83
|
+
end
|
84
|
+
|
85
|
+
def aggregation_enabled?
|
86
|
+
@aggregation
|
87
|
+
end
|
88
|
+
|
89
|
+
def forward_message(message)
|
90
|
+
@forward_socket.send(message, 0, @forward_host, @forward_port)
|
91
|
+
rescue => e
|
92
|
+
Rails.logger.error "[ActiveStatsD] Forwarding error: #{e.message}"
|
93
|
+
end
|
94
|
+
|
95
|
+
def log_message(metric, value, type)
|
96
|
+
Rails.logger.info "[ActiveStatsD] Metric received (no aggregation) - #{metric}:#{value}|#{type}"
|
97
|
+
end
|
72
98
|
end
|
73
99
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# lib/generators/active_statsd/active_statsd_generator.rb
|
2
|
+
require 'rails/generators'
|
3
|
+
|
4
|
+
module ActiveStatsd
|
5
|
+
class ActiveStatsdGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('templates', __dir__)
|
7
|
+
|
8
|
+
def copy_initializer_file
|
9
|
+
copy_file 'active_statsd_initializer.rb', 'config/initializers/active_statsd.rb'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_statsd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0
|
4
|
+
version: 0.1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Poage
|
@@ -74,6 +74,8 @@ files:
|
|
74
74
|
- lib/active_statsd/railtie.rb
|
75
75
|
- lib/active_statsd/server.rb
|
76
76
|
- lib/active_statsd/version.rb
|
77
|
+
- lib/generators/active_statsd/active_statsd_generator.rb
|
78
|
+
- lib/generators/active_statsd/templates/active_statsd_initializer.rb
|
77
79
|
- sig/active_statsd.rbs
|
78
80
|
homepage: https://github.com/RubyBrewsday/active_statsd
|
79
81
|
licenses:
|