neo4jrb / neo4j-core

A simple unified API that can access both the server and embedded Neo4j database. Used by the neo4j gem
MIT License
99 stars 80 forks source link

Unable to use Config properties #299

Closed rushilagr closed 6 years ago

rushilagr commented 6 years ago

I am using the neo4j core gem as part of a rack app.

I have set the following options-

require 'neo4j' require 'neo4j/core/cypher_session/adaptors/http' require 'neo4j/core/cypher_session/adaptors/bolt' require 'logger' Neo4j::Config[:logger] = Logger.new(STDOUT) Neo4j::Config[:wait_for_connection] = true

I am using the following - fairly straightforward setup to make queries to a locally running Neo4j server.

neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new(NEO4J_URL) session = Neo4j::Core::CypherSession.new(neo4j_adaptor) session.query(query)

Although everything else works great and I am able to hit the DB just fine, I am unable to get the logger the print out any queries or have the client wait for connections while the server spins up.

Runtime information:

Neo4j database version: 3.2.2 neo4j gem version: 8.1.3 neo4j-core gem version: 7.2.2

cheerfulstoic commented 6 years ago

This is an issue with changes in the gems (which maybe aren't fully baked) and a failure of documentation. The logger and wait_for_connection configuration options are for the neo4j gem which provides the ActiveNode and ActiveRel modules for creating models. In your examples, you are only using APIs from the neo4j-core gem.

For the logger, you should be able to provide a logger option to the adaptor like so:

neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new(NEO4J_URL, logger: Logger.new(STDOUT))

The wait_for_connection configuration option is only for the neo4j gem. It's also specifically in the railtie, so it is only used in Rails:

https://github.com/neo4jrb/neo4j/blob/master/lib/neo4j/railtie.rb#L63

Maybe it should be pushed lower into the neo4j gem or into the neo4j-core gem, but it isn't there currently.

Going to close because I don't think that it's a bug, but feel free to comment and/or re-open

cheerfulstoic commented 6 years ago

I actually realized that there should still be an issue for this because there should at least be some documentation to make this less confusing. I'll put this on my list to look at shortly

rushilagr commented 6 years ago

Hi Brian,

Thanks for the quick reply.

I agree with you that the documentation regarding this is quite misleading and needs some fixing. On Sat, Aug 19, 2017 at 10:17 PM Brian Underwood notifications@github.com wrote:

Reopened #299 https://github.com/neo4jrb/neo4j-core/issues/299.

— You are receiving this because you authored the thread.

Reply to this email directly, view it on GitHub https://github.com/neo4jrb/neo4j-core/issues/299#event-1212957688, or mute the thread https://github.com/notifications/unsubscribe-auth/AFbWQJ9IExpBGmRli6lyvbjYk0nZNeP9ks5sZxGFgaJpZM4O8Uh1 .

rushilagr commented 6 years ago

Brian the syntax provided by you is still not logging outgoing queries

neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new(NEO4J_URL, logger: Logger.new(STDOUT))

cheerfulstoic commented 6 years ago

Hrmmm, I'm not sure why that is. I'll give it a try in a bit. For the moment, I made a few changes to the Configuration doc page to hopefully make it a bit clearer

cheerfulstoic commented 6 years ago

Ok, I've looked at this deeper and that logger seems to only currently be used for the Bolt adaptor. But it is possible to instrument the queries and do whatever you like with them. The neo4j-core gem uses ActiveSupport::Notifications to allow subscriptions to certain events. So if you want to do something when a query is made:

Neo4j::Core::CypherSession::Adaptors::Base.subscribe_to_query do |message|
  logger.info message
end
# or simply:
Neo4j::Core::CypherSession::Adaptors::Base.subscribe_to_query(&method(:puts))

For HTTP you can subscribe to the requests as well like so:

Neo4j::Core::CypherSession::Adaptors::HTTP.subscribe_to_request do |message|
end

Those events are actually how the neo4j gem logs queries.

cheerfulstoic commented 6 years ago

I put together a section in the configuration docs which I think addresses this. Definitely let me know if anything still isn't covered:

http://neo4jrb.readthedocs.io/en/8.1.x/Configuration.html#instrumented-events

rushilagr commented 6 years ago

Hey Brian, thanks again for the quick replies.

Works beautifully now.

On a side note, I was wondering how you have implemented the subscribe to event feature. Have never seen this in Ruby before. Would love it if you could point me in the direction of some resources.

cheerfulstoic commented 6 years ago

The neo4j-core gem brings in the activesupport gem for various purposes and one of the modules that activesupport provides is aActiveSupport::Notifications which allows you to setup various instrumented events which people can subscribe to. Rails, for example, uses this to have events around controller actions which gives you information like the amount of time that they took.

In the neo4j-core gem, there is a module called Instrumentable which makes it easy to throw these in. You can see an example of the definition here:

https://github.com/neo4jrb/neo4j-core/blob/master/lib/neo4j/core/cypher_session/adaptors/http.rb#L94

And the instrumentation triggering here:

https://github.com/neo4jrb/neo4j-core/blob/master/lib/neo4j/core/cypher_session/adaptors/http.rb#L25 and https://github.com/neo4jrb/neo4j-core/blob/master/lib/neo4j/core/cypher_session/adaptors/http.rb#L129

rushilagr commented 6 years ago

Thanks!