On Friday, November 6, 2015 at 9:01:48 AM UTC-8, Timothy Kovalev wrote:
>
> Hi there!
>
> I am using sequel for its built-in sharding support. My configuration 
> looks like
> default: 
>   ... # some default connection settings
>
> users:
>   <<: *default 
>   database: database_shard_0
>   servers:
>     shard_0:
>       <<: *default
>       database: database_shard_0
>     shard_1:
>       <<: *default
>       database: database_shard_1
>
> Every query to database is logged to $stdout with standard ruby Logger.
>
> I need to log server name (shard_0/shard_1 from configuration) within the 
> query, or the name of database.
> For instance, log entry looks like:
>
> I, [2015-11-06T11:40:57.480074 #3775]  INFO -- : (0.000439s) SELECT 1 as 
> "result";
> While I need
> I, [2015-11-06T11:40:57.480074 #3775]  INFO -- shard_0: (0.000439s) SELECT 
> 1 as "result";
>
>
> What is the best way to achieve that?
>

There's not currently a clean way to achieve it that I know of. The logging 
API is lower level than the sharding support, and at the point that you are 
logging things, you don't know what shard you are using.  The logger itself 
doesn't have knowledge of the connection being used, just the SQL being run 
and any bound variables.

One way I can think to implement this would be overriding DB.pool.hold, 
recording the shard used in a thread-local variable, and changing the 
logger to look for that thread-local variable.  That's not going to give 
correct results if multiple shards are being used simultaneously, though.

One possible option is to add an SQL comment with the shard information to 
each query, and the logger will log that.  How that SQL comment should be 
added automatically to the query is another question, though.  It's fairly 
easy to handle it for dataset level stuff (override Dataset#execute* 
methods to append it), but it would be more complex for DML level code run 
directly on the database object, and would probably require adapter 
specific code.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to