active_statsd 0.1.0 → 0.1.0.2

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: 7ba98bd3462d5bb72af512e20ed1a28889a1b8ace4d47478a429086e76007222
4
- data.tar.gz: 524bb2108f7b84aa9e13d06c22edbd088a65b8ce2cb0716b3355e191f7cf1ed8
3
+ metadata.gz: 2d6a863b23d647b528f8b3bd62c1f37537e389b9042674454012f8908878ec0c
4
+ data.tar.gz: b81303f0442c397650344875d61aee68738f30a6f5c87643b0886075076c423d
5
5
  SHA512:
6
- metadata.gz: a1269bae74e65c2b55a92720cc962abdb2e8794a145826332b87a0540caefb141faf0bb4c9a4d28a5a6ebca501b0356afab8f7b10125c6d725fe27407dad32e0
7
- data.tar.gz: 318929577949d3447672166ed61cbb8c1d81b689a814fa25b05c49b916f852ecb1f2f8707746bfca00a0074b25322c011c3fe8015447c73d4e792d2505e50d58
6
+ metadata.gz: 4bfc3764aaccf595f1391e46b3eae23a98ec56af933098f4d6f66673883f26077c03d34838e330b33c3f3f0d7f4790ebc1916bc9402dfc4d18b483eb0a1e1cdb
7
+ data.tar.gz: 723aff4bf12e0b4facc5a15fbb8b205ab767c6bb7e545d2f8277c94a4939c0a75792628b866a99b2970fe6555b027e1e7328a553cd46b17c4669f4278d1f3057
@@ -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
 
@@ -3,25 +3,43 @@ require 'socket'
3
3
 
4
4
  module ActiveStatsD
5
5
  class Server
6
- def initialize(host: '127.0.0.1', port: 8125)
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?
15
+ @running = false
11
16
  end
12
17
 
13
18
  def start
14
- start_aggregation_thread
19
+ return if @running # Don't attempt to start twice
15
20
 
16
21
  Thread.new do
17
- Rails.logger.info "[ActiveStatsD] UDP StatsD listener started on #{@host}:#{@port}"
18
22
  begin
19
- Socket.udp_server_loop(@host, @port) do |msg, _|
23
+ sockets = Socket.udp_server_sockets(@host, @port)
24
+ rescue Errno::EADDRINUSE
25
+ Rails.logger.warn "[ActiveStatsD] Server already running on #{@host}:#{@port}, skipping startup."
26
+ next
27
+ end
28
+
29
+ @running = true
30
+ Rails.logger.info "[ActiveStatsD] UDP StatsD listener started on #{@host}:#{@port} (aggregation=#{@aggregation})"
31
+ start_aggregation_thread if aggregation_enabled?
32
+
33
+ begin
34
+ Socket.udp_server_loop_on(sockets) do |msg, _|
20
35
  Rails.logger.debug "[ActiveStatsD] UDP packet received: #{msg.strip}"
21
36
  handle_message(msg.strip)
22
37
  end
23
38
  rescue => e
24
39
  Rails.logger.error "[ActiveStatsD] Server error: #{e.class.name} - #{e.message}\n#{e.backtrace.join("\n")}"
40
+ ensure
41
+ sockets.each(&:close)
42
+ @running = false
25
43
  end
26
44
  end
27
45
  end
@@ -30,19 +48,16 @@ module ActiveStatsD
30
48
 
31
49
  def handle_message(message)
32
50
  metric, value, type = parse_metric(message)
33
-
34
51
  return unless metric && type == 'c'
35
52
 
36
- @mutex.synchronize do
37
- @counters[metric] += value
38
- end
53
+ @mutex.synchronize { @counters[metric] += value } if aggregation_enabled?
54
+ forward_message(message) if forwarding_enabled?
55
+ log_message(metric, value, type) unless aggregation_enabled?
39
56
  end
40
57
 
41
58
  def parse_metric(message)
42
- # Example message: "rails_app.test.metric:1|c"
43
59
  metric_data, type = message.split('|')
44
60
  metric, value = metric_data.split(':')
45
-
46
61
  [metric, value.to_i, type]
47
62
  rescue
48
63
  Rails.logger.error "[ActiveStatsD] Failed to parse metric: #{message}"
@@ -52,7 +67,7 @@ module ActiveStatsD
52
67
  def start_aggregation_thread
53
68
  Thread.new do
54
69
  loop do
55
- sleep 10 # flush metrics every 10 seconds
70
+ sleep 10
56
71
  flush_metrics
57
72
  end
58
73
  end
@@ -69,5 +84,23 @@ module ActiveStatsD
69
84
  Rails.logger.info "[ActiveStatsD] Aggregated metric - #{metric}: #{count}"
70
85
  end
71
86
  end
87
+
88
+ def forwarding_enabled?
89
+ @forward_host && @forward_port
90
+ end
91
+
92
+ def aggregation_enabled?
93
+ @aggregation
94
+ end
95
+
96
+ def forward_message(message)
97
+ @forward_socket.send(message, 0, @forward_host, @forward_port)
98
+ rescue => e
99
+ Rails.logger.error "[ActiveStatsD] Forwarding error: #{e.message}"
100
+ end
101
+
102
+ def log_message(metric, value, type)
103
+ Rails.logger.info "[ActiveStatsD] Metric received (no aggregation) - #{metric}:#{value}|#{type}"
104
+ end
72
105
  end
73
106
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveStatsd
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.0.2"
5
5
  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
@@ -0,0 +1,5 @@
1
+ ActiveStatsD.configure do |config|
2
+ config.host = '127.0.0.1'
3
+ config.port = 8125
4
+ config.namespace = 'my_rails_app'
5
+ 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.2
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: