Commit e2e4769d authored by Paul Asmuth's avatar Paul Asmuth
Browse files

push events via http-post + version bump to 0.5.5, fixes #7

parent a81d3bfe
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
0.5.4
0.5.5
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@

Gem::Specification.new do |s|
  s.name = "fnordmetric"
  s.version = "0.5.4"
  s.version = "0.5.5"

  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
  s.authors = ["Paul Asmuth"]
+10 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ class FnordMetric::App < Sinatra::Base
  def initialize(namespaces, opts)
    @namespaces = {}
    @redis = Redis.new
    @opts = opts
    namespaces.each do |key, block|
      @namespaces[key] = FnordMetric::Namespace.new(key, opts.clone)
      @namespaces[key].instance_eval(&block)
@@ -114,9 +115,8 @@ class FnordMetric::App < Sinatra::Base
  end

  post '/events' do
    halt 400, 'please specify the event_type' unless params["type"]       
    event_type = params.delete("type")
    FnordMetric.track(event_type, parse_params(params))
    halt 400, 'please specify the event_type (_type)' unless params["_type"]           
    track_event((8**32).to_s(36), parse_params(params))
  end

private
@@ -134,5 +134,12 @@ private
    object
  end

  def track_event(event_id, event_data)
    @redis.hincrby "#{@opts[:redis_prefix]}-stats",             "events_received", 1
    @redis.set     "#{@opts[:redis_prefix]}-event-#{event_id}", event_data.to_json
    @redis.lpush   "#{@opts[:redis_prefix]}-queue",             event_id
    @redis.expire  "#{@opts[:redis_prefix]}-event-#{event_id}", @opts[:event_queue_ttl]
  end

end
+5 −5
Original line number Diff line number Diff line
@@ -80,18 +80,18 @@ or in your Gemfile:

The slow way: HTTP-Post the json event to the fnordmetric webinterface

  POST http://localhost:2323/events _type=_set_name&name=Horst(..)
  POST http://localhost:2323/events _type=unicorn_seen

  curl -X POST -d "_type=_set_name&name=Horst(..)" http://localhost:2323/events
  curl -X POST -d "_type=unicorn_seen" http://localhost:4242/events 

The easy way: Stream one ore more newline-seperated json encoded events through a tcp connection.

  echo "\{\"_type\": \"foobar\"\}\n" | nc localhost 2323
  echo "\{\"_type\": \"unicorn_seen\"\}\n" | nc localhost 2323

The fast way: Add your event directly to the redis-based queue:

  uuid = "sobv67a9v73sba74"
  event = { :_type => "foobar" }.to_json
  uuid = (8**32).to_s(36)
  event = { :_type => "unicorn_seen" }.to_json

  redis.lpush("fnordmetric-queue", uuid) 
  redis.set("fnordmetric-event-#{my_uuid}", event)
+6 −34
Original line number Diff line number Diff line
@@ -372,43 +372,15 @@ describe "app" do
  describe "events api: creating events" do

    it "should track an event without auth" do      
      pending("fix this")
      #post "/events", :type => "myevent", :fnord => "foobar"
      #last_response.status.should == 200
      #FnordMetric::Event.last.type.should == "myevent"
      #FnordMetric::Event.last.fnord.should == "foobar"
      post "/events", :_type => "myevent", :fnord => "foobar"
      last_response.status.should == 200
    end

    it "should return 400 if no type is provided" do
      pending("fix this")
    	#post "/events", :fnord => "foobar"
    	#last_response.status.should == 400  	
    	#last_response.body.should == "please specify the event_type"
    end

    it "should track an event in the past" do
      pending("fix this")
    	#my_time = (Time.now-3.years).to_i
    	#post "/events", :type => "myevent", :time => my_time
    	#last_response.status.should == 200
    	#FnordMetric::Event.last.type.should == "myevent"
    	#FnordMetric::Event.last.time.should == my_time
    end

    it "should track an event with integer data" do
      pending("fix this")
    	#post "/events", :type => "myevent", :blubb => "123"
    	#last_response.status.should == 200
    	#FnordMetric::Event.last.type.should == "myevent"
    	#FnordMetric::Event.last.blubb.should == 123
    end
    
    it "should track an event with float data" do
      pending("fix this")
    	#post "/events", :type => "myevent", :blubb => "42.23"
    	#last_response.status.should == 200
    	#FnordMetric::Event.last.type.should == "myevent"
    	#FnordMetric::Event.last.blubb.should == 42.23
    	post "/events", :fnord => "foobar"
    	last_response.status.should == 400  	
    	last_response.body.should == "please specify the event_type"
    end

  end