instruments 0.0.1 → 0.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.
- data/lib/instruments.rb +89 -0
- metadata +1 -1
    
        data/lib/instruments.rb
    CHANGED
    
    | @@ -1,2 +1,91 @@ | |
| 1 1 | 
             
            module Instruments
         | 
| 2 | 
            +
              def self.defaults=(args)
         | 
| 3 | 
            +
                @logger = args[:logger] || Kernel
         | 
| 4 | 
            +
                @method = args[:method] || :puts
         | 
| 5 | 
            +
                @default_data = args[:data] || {}
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def self.write(data)
         | 
| 9 | 
            +
                @logger.send(@method, data.merge(@default_data))
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              if defined?(::Sinatra)
         | 
| 13 | 
            +
                module ::Sinatra
         | 
| 14 | 
            +
                  module Instrumentation
         | 
| 15 | 
            +
                    def route(verb, action, *)
         | 
| 16 | 
            +
                      condition {@instrumented_route = action}
         | 
| 17 | 
            +
                      super
         | 
| 18 | 
            +
                    end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                    def instrument_routes
         | 
| 21 | 
            +
                      before do
         | 
| 22 | 
            +
                        @start_request = Time.now
         | 
| 23 | 
            +
                      end
         | 
| 24 | 
            +
                      after do
         | 
| 25 | 
            +
                        t = Integer((Time.now - @start_request)*1000)
         | 
| 26 | 
            +
                        Instruments.write({
         | 
| 27 | 
            +
                          :lib => "sinatra",
         | 
| 28 | 
            +
                          :action => "http-request",
         | 
| 29 | 
            +
                          :route => @instrumented_route,
         | 
| 30 | 
            +
                          :elapsed => t,
         | 
| 31 | 
            +
                          :method => env["REQUEST_METHOD"].downcase,
         | 
| 32 | 
            +
                          :status => response.status
         | 
| 33 | 
            +
                        }.merge(params))
         | 
| 34 | 
            +
                      end
         | 
| 35 | 
            +
                    end
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
                  register Instrumentation
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
              if defined?(::Sequel)
         | 
| 42 | 
            +
                module ::Sequel
         | 
| 43 | 
            +
                  class Database
         | 
| 44 | 
            +
                    def log_yield(sql, args=nil)
         | 
| 45 | 
            +
                      sql = "#{sql}; #{args.inspect}" if args
         | 
| 46 | 
            +
                      t0 = Time.now
         | 
| 47 | 
            +
                      begin
         | 
| 48 | 
            +
                        yield
         | 
| 49 | 
            +
                      rescue => e
         | 
| 50 | 
            +
                        log_exception(e, sql)
         | 
| 51 | 
            +
                        raise
         | 
| 52 | 
            +
                      ensure
         | 
| 53 | 
            +
                        t1 = Time.now
         | 
| 54 | 
            +
                        log_duration(Integer((t1-t0)*1000), sql) unless e
         | 
| 55 | 
            +
                      end
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                    def log_duration(t, sql)
         | 
| 59 | 
            +
                      Instruments.write(:info => true, :action => action(sql), :elapsed => t, :sql => sql)
         | 
| 60 | 
            +
                    end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                    def log_exception(e, sql)
         | 
| 63 | 
            +
                      Instruments.write(:error => true, :exception => e.class, :sql => sql)
         | 
| 64 | 
            +
                    end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                    def action(sql)
         | 
| 67 | 
            +
                      sql[/(\w+){1}/].downcase
         | 
| 68 | 
            +
                    end
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
              if defined?(::Excon)
         | 
| 74 | 
            +
                module ::Excon
         | 
| 75 | 
            +
                  module Instrumentation
         | 
| 76 | 
            +
                    def self.instrument(name, params={}, &blk)
         | 
| 77 | 
            +
                      t0 = Time.now
         | 
| 78 | 
            +
                      res = yield if block_given?
         | 
| 79 | 
            +
                      t1 = Time.now
         | 
| 80 | 
            +
                      Instruments.write(
         | 
| 81 | 
            +
                        :lib => "excon",
         | 
| 82 | 
            +
                        :action => "http-request",
         | 
| 83 | 
            +
                        :elapsed => Integer((t1-t0)*1000)
         | 
| 84 | 
            +
                      )
         | 
| 85 | 
            +
                    end
         | 
| 86 | 
            +
                  end
         | 
| 87 | 
            +
                end
         | 
| 88 | 
            +
                Excon.defaults[:instrumentor] = ::Excon::Instrumentation
         | 
| 89 | 
            +
              end
         | 
| 90 | 
            +
             | 
| 2 91 | 
             
            end
         |