Skip to content

Commit

Permalink
Merge pull request #126 from erez-rabih/slow-log
Browse files Browse the repository at this point in the history
Slow query logging for HTTP queries
  • Loading branch information
maxdemarzi committed Dec 6, 2013
2 parents d3e1902 + 235ab7c commit 29eb16e
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 111 deletions.
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,22 @@ Configure Neography as follows:
```ruby
# these are the default values:
Neography.configure do |config|
config.protocol = "http://"
config.server = "localhost"
config.port = 7474
config.directory = "" # prefix this path with '/'
config.cypher_path = "/cypher"
config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script"
config.log_file = "neography.log"
config.log_enabled = false
config.max_threads = 20
config.authentication = nil # 'basic' or 'digest'
config.username = nil
config.password = nil
config.parser = MultiJsonParser
end
```
config.protocol = "http://"
config.server = "localhost"
config.port = 7474
config.directory = "" # prefix this path with '/'
config.cypher_path = "/cypher"
config.gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script"
config.log_file = "neography.log"
config.log_enabled = false
config.slow_log_threshold = 0 # time in ms for query logging
config.max_threads = 20
config.authentication = nil # 'basic' or 'digest'
config.username = nil
config.password = nil
config.parser = MultiJsonParser
end
```

Then initialize a `Rest` instance:

Expand Down
58 changes: 30 additions & 28 deletions lib/neography/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Config

attr_accessor :protocol, :server, :port, :directory,
:cypher_path, :gremlin_path,
:log_file, :log_enabled,
:log_file, :log_enabled, :slow_log_threshold,
:max_threads,
:authentication, :username, :password,
:parser
Expand All @@ -14,39 +14,41 @@ def initialize

def to_hash
{
:protocol => @protocol,
:server => @server,
:port => @port,
:directory => @directory,
:cypher_path => @cypher_path,
:gremlin_path => @gremlin_path,
:log_file => @log_file,
:log_enabled => @log_enabled,
:max_threads => @max_threads,
:authentication => @authentication,
:username => @username,
:password => @password,
:parser => @parser
:protocol => @protocol,
:server => @server,
:port => @port,
:directory => @directory,
:cypher_path => @cypher_path,
:gremlin_path => @gremlin_path,
:log_file => @log_file,
:log_enabled => @log_enabled,
:slow_log_threshold => @slow_log_threshold,
:max_threads => @max_threads,
:authentication => @authentication,
:username => @username,
:password => @password,
:parser => @parser
}
end

private

def set_defaults
@protocol = "http://"
@server = "localhost"
@port = 7474
@directory = ""
@cypher_path = "/cypher"
@gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script"
@log_file = "neography.log"
@log_enabled = false
@max_threads = 20
@authentication = nil
@username = nil
@password = nil
@parser = MultiJsonParser
end
@protocol = "http://"
@server = "localhost"
@port = 7474
@directory = ""
@cypher_path = "/cypher"
@gremlin_path = "/ext/GremlinPlugin/graphdb/execute_script"
@log_file = "neography.log"
@log_enabled = false
@slow_log_threshold = 0
@max_threads = 20
@authentication = nil
@username = nil
@password = nil
@parser = MultiJsonParser
end

end
end
25 changes: 13 additions & 12 deletions lib/neography/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Connection

attr_accessor :protocol, :server, :port, :directory,
:cypher_path, :gremlin_path,
:log_file, :log_enabled, :logger,
:log_file, :log_enabled, :logger, :slow_log_threshold,
:max_threads,
:authentication, :username, :password,
:parser, :client
Expand Down Expand Up @@ -54,7 +54,7 @@ def log(path, body)
start_time = Time.now
response = yield
time = ((Time.now - start_time) * 1000).round(2)
@logger.info "[Neography::Query] #{path} #{body} [#{time}ms]"
@logger.info "[Neography::Query] #{path} #{body} [#{time}ms]" if time >= slow_log_threshold
response
else
yield
Expand All @@ -76,16 +76,17 @@ def merge_configuration(options)
end

def save_local_configuration(config)
@protocol = config[:protocol]
@server = config[:server]
@port = config[:port]
@directory = config[:directory]
@cypher_path = config[:cypher_path]
@gremlin_path = config[:gremlin_path]
@log_file = config[:log_file]
@log_enabled = config[:log_enabled]
@max_threads = config[:max_threads]
@parser = config[:parser]
@protocol = config[:protocol]
@server = config[:server]
@port = config[:port]
@directory = config[:directory]
@cypher_path = config[:cypher_path]
@gremlin_path = config[:gremlin_path]
@log_file = config[:log_file]
@log_enabled = config[:log_enabled]
@slow_log_threshold = config[:slow_log_threshold]
@max_threads = config[:max_threads]
@parser = config[:parser]

@user_agent = { "User-Agent" => USER_AGENT }

Expand Down
54 changes: 28 additions & 26 deletions spec/unit/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ module Neography

context "defaults" do

its(:protocol) { should == 'http://' }
its(:server) { should == 'localhost' }
its(:port) { should == 7474 }
its(:directory) { should == '' }
its(:cypher_path) { should == '/cypher' }
its(:gremlin_path) { should == '/ext/GremlinPlugin/graphdb/execute_script' }
its(:log_file) { should == 'neography.log' }
its(:log_enabled) { should == false }
its(:max_threads) { should == 20 }
its(:authentication) { should == nil }
its(:username) { should == nil }
its(:password) { should == nil }
its(:parser) { should == MultiJsonParser}
its(:protocol) { should == 'http://' }
its(:server) { should == 'localhost' }
its(:port) { should == 7474 }
its(:directory) { should == '' }
its(:cypher_path) { should == '/cypher' }
its(:gremlin_path) { should == '/ext/GremlinPlugin/graphdb/execute_script' }
its(:log_file) { should == 'neography.log' }
its(:log_enabled) { should == false }
its(:slow_log_threshold) { should == 0 }
its(:max_threads) { should == 20 }
its(:authentication) { should == nil }
its(:username) { should == nil }
its(:password) { should == nil }
its(:parser) { should == MultiJsonParser}

it "has a hash representation" do
expected_hash = {
:protocol => 'http://',
:server => 'localhost',
:port => 7474,
:directory => '',
:cypher_path => '/cypher',
:gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script',
:log_file => 'neography.log',
:log_enabled => false,
:max_threads => 20,
:authentication => nil,
:username => nil,
:password => nil,
:parser => MultiJsonParser
:protocol => 'http://',
:server => 'localhost',
:port => 7474,
:directory => '',
:cypher_path => '/cypher',
:gremlin_path => '/ext/GremlinPlugin/graphdb/execute_script',
:log_file => 'neography.log',
:log_enabled => false,
:slow_log_threshold => 0,
:max_threads => 20,
:authentication => nil,
:username => nil,
:password => nil,
:parser => MultiJsonParser
}
config.to_hash.should == expected_hash
end
Expand Down
83 changes: 53 additions & 30 deletions spec/unit/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,37 @@ module Neography
context "hash options" do
let(:options) do
{
:protocol => "https://",
:server => "foobar",
:port => 4242,
:directory => "/dir",
:cypher_path => "/cyph",
:gremlin_path => "/grem",
:log_file => "neo.log",
:log_enabled => false,
:max_threads => 10,
:parser => Foo,
:authentication => "foo",
:username => "bar",
:password => "baz"
:protocol => "https://",
:server => "foobar",
:port => 4242,
:directory => "/dir",
:cypher_path => "/cyph",
:gremlin_path => "/grem",
:log_file => "neo.log",
:log_enabled => false,
:slow_log_threshold => 0,
:max_threads => 10,
:parser => Foo,
:authentication => "foo",
:username => "bar",
:password => "baz"
}
end

it "accepts all options in a hash" do
connection.configuration.should == "https://foobar:4242/dir/db/data"

connection.protocol.should == "https://"
connection.server.should == "foobar"
connection.port.should == 4242
connection.directory.should == "/dir"
connection.cypher_path.should == "/cyph"
connection.gremlin_path.should == "/grem"
connection.log_file.should == "neo.log"
connection.log_enabled.should == false
connection.max_threads.should == 10
connection.parser.should == Foo
connection.protocol.should == "https://"
connection.server.should == "foobar"
connection.port.should == 4242
connection.directory.should == "/dir"
connection.cypher_path.should == "/cyph"
connection.gremlin_path.should == "/grem"
connection.log_file.should == "neo.log"
connection.log_enabled.should == false
connection.slow_log_threshold.should == 0
connection.max_threads.should == 10
connection.parser.should == Foo

connection.authentication.should == {
:foo_auth => {
Expand Down Expand Up @@ -207,17 +209,14 @@ module Neography

context "query logging" do
before do
connection.logger = Logger.new(nil)
@logger = Logger.new(nil)
connection.logger = @logger
connection.log_enabled = true
end

let :expected_response do
"expected_response"
end
let(:expected_response) {"expected_response"}

let :request_body do
{key1: :val1}
end
let(:request_body) { {key1: :val1} }

it "should log query" do
connection.should_receive(:log).with("/foo/bar", request_body).once
Expand All @@ -228,6 +227,30 @@ module Neography
connection.stub(:evaluate_response).and_return expected_response
connection.get("/foo/bar").should eq expected_response
end

describe "slow_log_threshold" do
before do
connection.stub(:evaluate_response).and_return expected_response
end

context "default value" do
it "should have output" do
@logger.should_receive(:info).once
end
end

context "high value" do
before { connection.slow_log_threshold = 100_000 }
it "should not have output" do
@logger.should_not_receive(:info)
end
end

after do
connection.get("/foo/bar", {body: request_body})
end
end

end
end
end
Expand Down

0 comments on commit 29eb16e

Please sign in to comment.