Class: Ahoy::Stores::KafkaStore

Inherits:
LogStore show all
Defined in:
lib/ahoy/stores/kafka_store.rb

Instance Method Summary collapse

Methods inherited from LogStore

#track_event, #track_visit

Methods inherited from BaseStore

#authenticate, #exclude?, #generate_id, #initialize, #report_exception, #track_event, #track_visit, #user, #visit

Constructor Details

This class inherits a constructor from Ahoy::Stores::BaseStore

Instance Method Details

#clientObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/ahoy/stores/kafka_store.rb', line 12

def client
  @client ||= begin
    client = Kafka.new(
      seed_brokers: ENV["KAFKA_URL"],
      logger: Rails.logger
    )
    at_exit { client.shutdown }
    client
  end
end

#events_topicObject



35
36
37
# File 'lib/ahoy/stores/kafka_store.rb', line 35

def events_topic
  "ahoy_events"
end

#log_event(data) ⇒ Object



8
9
10
# File 'lib/ahoy/stores/kafka_store.rb', line 8

def log_event(data)
  post(events_topic, data)
end

#log_visit(data) ⇒ Object



4
5
6
# File 'lib/ahoy/stores/kafka_store.rb', line 4

def log_visit(data)
  post(visits_topic, data)
end

#post(topic, data) ⇒ Object



27
28
29
# File 'lib/ahoy/stores/kafka_store.rb', line 27

def post(topic, data)
  producer.produce(data.to_json, topic: topic)
end

#producerObject



23
24
25
# File 'lib/ahoy/stores/kafka_store.rb', line 23

def producer
  @producer ||= client.async_producer(delivery_interval: 3)
end

#visits_topicObject



31
32
33
# File 'lib/ahoy/stores/kafka_store.rb', line 31

def visits_topic
  "ahoy_visits"
end