
Product
Secure Your AI-Generated Code with Socket MCP
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Listen to Rails ActiveSupport::Notifications
and deliver to a
Statsd backend. This gem also supports
writing your own custom collectors.
Add this line to your application's Gemfile:
gem "nsa"
And then execute:
$ bundle
Or install it yourself as:
$ gem install nsa
NSA comes packaged with collectors for ActionController, ActiveRecord, ActiveSupport Caching, and Sidekiq.
To use this gem, simply get a reference to a statsd backend, then indicate which
collectors you'd like to run. Each collect
method specifies a Collector to use
and the additional key namespace.
statsd = ::Statsd.new(ENV["STATSD_HOST"], ENV["STATSD_PORT"])
application_name = ::Rails.application.class.parent_name.underscore
application_env = ENV["PLATFORM_ENV"] || ::Rails.env
statsd.namespace = [ application_name, application_env ].join(".")
::NSA.inform_statsd(statsd) do |informant|
# Load :action_controller collector with a key prefix of :web
informant.collect(:action_controller, :web)
informant.collect(:active_record, :db)
informant.collect(:cache, :cache)
informant.collect(:sidekiq, :sidekiq)
end
:action_controller
Listens to: process_action.action_controller
Metrics recorded:
{ns}.{prefix}.{controller}.{action}.{format}.total_duration
{ns}.{prefix}.{controller}.{action}.{format}.db_time
{ns}.{prefix}.{controller}.{action}.{format}.view_time
{ns}.{prefix}.{controller}.{action}.{format}.status.{status_code}
:active_record
Listens to: sql.active_record
Metrics recorded:
{ns}.{prefix}.tables.{table_name}.queries.delete.duration
{ns}.{prefix}.tables.{table_name}.queries.insert.duration
{ns}.{prefix}.tables.{table_name}.queries.select.duration
{ns}.{prefix}.tables.{table_name}.queries.update.duration
:active_support_cache
Listens to: cache_*.active_suppport
Metrics recorded:
{ns}.{prefix}.delete.duration
{ns}.{prefix}.exist?.duration
{ns}.{prefix}.fetch_hit.duration
{ns}.{prefix}.generate.duration
{ns}.{prefix}.read_hit.duration
{ns}.{prefix}.read_miss.duration
{ns}.{prefix}.read_miss.duration
:sidekiq
Listens to: Sidekiq middleware, run before each job that is processed
Metrics recorded:
{ns}.{prefix}.{WorkerName}.processing_time
{ns}.{prefix}.{WorkerName}.success
{ns}.{prefix}.{WorkerName}.failure
{ns}.{prefix}.queues.{queue_name}.enqueued
{ns}.{prefix}.queues.{queue_name}.latency
{ns}.{prefix}.dead_size
{ns}.{prefix}.enqueued
{ns}.{prefix}.failed
{ns}.{prefix}.processed
{ns}.{prefix}.processes_size
{ns}.{prefix}.retry_size
{ns}.{prefix}.scheduled_size
{ns}.{prefix}.workers_size
Writing your own collector is very simple. To take advantage of the keyspace handling you must:
collect
, taking the key_prefix
as its only argument.NSA::Statsd::Publisher
or
NSA::Statsd::Publisher
.statsd_*
prefixed methods provided by the included
Publisher:Publisher
methods:
statsd_count(key, value = 1, sample_rate = nil)
statsd_decrement(key, sample_rate = nil)
statsd_gauge(key, value = 1, sample_rate = nil)
statsd_increment(key, sample_rate = nil)
statsd_set(key, value = 1, sample_rate = nil)
statsd_time(key, sample_rate = nil, &block)
statsd_timing(key, value = 1, sample_rate = nil)
AsyncPublisher
methods:
async_statsd_count(key, sample_rate = nil, &block)
async_statsd_gauge(key, sample_rate = nil, &block)
async_statsd_set(key, sample_rate = nil, &block)
async_statsd_time(key, sample_rate = nil, &block)
async_statsd_timing(key, sample_rate = nil, &block)
Note: When using the AsyncPublisher
, the value is derived from the
block. This is useful when the value is not near at hand and has a relatively
high cost to compute (e.g. db query) and you don't want your current thread to
wait.
For example, first define your collector. Our (very naive) example will write a gauge metric every 10 seconds of the User count in the db.
# Publishing User.count gauge using a collector
module UsersCollector
extend ::NSA::Statsd::Publisher
def self.collect(key_prefix)
loop do
statsd_gauge("count", ::User.count)
sleep 10 # don't do this, obvi
end
end
end
Then let the informant know about it in some initializer:
# file: config/initializers/statsd.rb
# $statsd =
NSA.inform_statsd($statsd) do |informant|
# ...
informant.collect(UserCollector, :users)
end
You could also implement the provided example not as a Collector, but using
AsyncPublisher
directly in your ActiveRecord model:
# Publishing User.count gauge using AsyncPublisher methods
class User < ActiveRecord::Base
include NSA::Statsd::AsyncPublisher
after_commit :write_count_gauge, :on => [ :create, :destroy ]
# ...
private
def write_count_gauge
async_statsd_gauge("models.User.all.count") { ::User.count }
end
end
Using this technique, publishing the User.count
stat gauge will not hold up
the thread responsible for creating the record (and processing more callbacks).
After checking out the repo, run bin/setup
to install dependencies. Then, run
rake test
to run the tests. You can also run bin/console
for an interactive
prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To
release a new version, update the version number in version.rb
, and then run
bundle exec rake release
, which will create a git tag for the version, push
git commits and tags, and push the .gem
file to
rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/localshred/nsa.
FAQs
Unknown package
We found that nsa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket MCP brings real-time security checks to AI-generated code, helping developers catch risky dependencies before they enter the codebase.
Security News
As vulnerability data bottlenecks grow, the federal government is formally investigating NIST’s handling of the National Vulnerability Database.
Research
Security News
Socket’s Threat Research Team has uncovered 60 npm packages using post-install scripts to silently exfiltrate hostnames, IP addresses, DNS servers, and user directories to a Discord-controlled endpoint.