Added NSQ

Andrew Kane committed Jan 25, 2017
commit c538f56967daaa129525f87def40cb6682135457
Showing 5 changed files with 78 additions and 0 deletions
README.md +17 -0
@@ @@ -33,6 +33,7 @@ Ahoy supports a number of data stores out of the box. You can start with one of
- [Fluentd](#fluentd)
- [RabbitMQ](#rabbitmq)
- [NATS](#nats-master)
+ - [NSQ](#nsq-master)
- [Amazon Kinesis Firehose](#amazon-kinesis-firehose)
- [Logs](#logs)
- [Custom](#custom)
@@ @@ -116,6 +117,22 @@ rails generate ahoy:stores:nats
Use `ENV["NATS_URL"]` to configure.
+ ### NSQ [master]
+
+ Add [nsq-ruby](https://github.com/wistia/nsq-ruby) to your Gemfile.
+
+ ```ruby
+ gem 'nsq-ruby'
+ ```
+
+ And run:
+
+ ```sh
+ rails generate ahoy:stores:nsq
+ ```
+
+ Use `ENV["NSQ_URL"]` to configure.
+
### Amazon Kinesis Firehose
Add [aws-sdk](https://github.com/aws/aws-sdk-ruby) to your Gemfile.
ahoy.rb b/lib/ahoy.rb +1 -0
@@ @@ -28,6 +28,7 @@ require "ahoy/stores/fluentd_store"
require "ahoy/stores/mongoid_store"
require "ahoy/stores/kafka_store"
require "ahoy/stores/nats_store"
+ require "ahoy/stores/nsq_store"
require "ahoy/stores/kinesis_firehose_store"
require "ahoy/stores/bunny_store"
require "ahoy/engine" if defined?(Rails)
ahoy/stores/nsq_store.rb b/lib/ahoy/stores/nsq_store.rb +36 -0
@@ @@ -0,0 +1,36 @@
+ module Ahoy
+ module Stores
+ class NsqStore < LogStore
+ def log_visit(data)
+ post(visits_topic, data)
+ end
+
+ def log_event(data)
+ post(events_topic, data)
+ end
+
+ def client
+ @client ||= begin
+ require "nsq"
+ client = Nsq::Producer.new(
+ nsqd: ENV["NSQ_URL"] || "127.0.0.1:4150"
+ )
+ at_exit { client.terminate }
+ client
+ end
+ end
+
+ def post(topic, data)
+ client.write_to_topic(topic, data.to_json)
+ end
+
+ def visits_topic
+ "ahoy_visits"
+ end
+
+ def events_topic
+ "ahoy_events"
+ end
+ end
+ end
+ end
generators/ahoy/stores/nsq_generator.rb b/lib/generators/ahoy/stores/nsq_generator.rb +15 -0
@@ @@ -0,0 +1,15 @@
+ require "rails/generators"
+
+ module Ahoy
+ module Stores
+ module Generators
+ class NsqGenerator < Rails::Generators::Base
+ source_root File.expand_path("../templates", __FILE__)
+
+ def create_initializer
+ template "nsq_initializer.rb", "config/initializers/ahoy.rb"
+ end
+ end
+ end
+ end
+ end
generators/ahoy/stores/templates/nsq_initializer.rb b/lib/generators/ahoy/stores/templates/nsq_initializer.rb +9 -0
@@ @@ -0,0 +1,9 @@
+ class Ahoy::Store < Ahoy::Stores::NsqStore
+ def visits_topic
+ "ahoy_visits"
+ end
+
+ def events_topic
+ "ahoy_events"
+ end
+ end