Drizzled Public API Documentation

query_log.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright 2011 Daniel Nichter
00005  *
00006  *  This program is free software: you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation, either version 3 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #include <config.h>
00021 #include <string>
00022 #include <fcntl.h>
00023 #include <drizzled/item.h>
00024 #include <drizzled/module/option_map.h>
00025 #include <drizzled/session.h>
00026 #include <drizzled/session/times.h>
00027 #include <drizzled/plugin.h>
00028 #include "query_log.h"
00029 
00030 using namespace std;
00031 using namespace drizzled;
00032 using namespace plugin;
00033 
00034 QueryLog::QueryLog(bool enabled, QueryLoggerFile *logger_file) :
00035   drizzled::plugin::EventObserver("query_log"),
00036   sysvar_enabled(enabled),
00037   _logger_file(logger_file)
00038 {
00039 }
00040 
00041 void QueryLog::registerSessionEventsDo(Session &, EventObserverList &observers)
00042 {
00043   registerEvent(observers, AFTER_STATEMENT);
00044 }
00045 
00046 bool QueryLog::observeEventDo(EventData &data)
00047 {
00048   // Don't log and return successful if...
00049   if (not sysvar_enabled          // all logging is disabled
00050       || not sysvar_file_enabled) // or file logging is disabled
00051     return false;
00052 
00053   switch (data.event) {
00054   case AFTER_STATEMENT:
00055     afterStatement((AfterStatementEventData &)data);
00056     break;
00057   default:
00058     fprintf(stderr, "query_log: Unexpected event '%s'\n",
00059       EventObserver::eventName(data.event));
00060   }
00061 
00062   return false;
00063 }
00064 
00065 bool QueryLog::afterStatement(AfterStatementEventData &data)
00066 {
00067   Session *session= &data.session;
00068 
00069   // For the moment we're only interestd in queries, not admin
00070   // command and such.
00071   if (session->command != COM_QUERY)
00072     return false;
00073 
00074   // Query end time (microseconds from epoch)
00075   uint64_t t_mark= session->times.getCurrentTimestamp(false);
00076 
00080   _event.execution_time= session->times.getElapsedTime();
00081   _event.lock_time= (t_mark - session->times.utime_after_lock);
00082   _event.session_time= (t_mark - session->times.getConnectMicroseconds());
00083 
00089   if (   _event.execution_time < sysvar_threshold_execution_time
00090       || _event.lock_time      < sysvar_threshold_lock_time
00091       || _event.session_time   < sysvar_threshold_session_time)
00092     return false;
00093 
00099   _event.execution_time= _event.execution_time * 0.000001;
00100   _event.lock_time= _event.lock_time * 0.000001;
00101   _event.session_time= _event.session_time * 0.000001;
00102 
00106   _event.session_id= session->getSessionId();
00107   _event.query_id= session->getQueryId();
00108   _event.rows_examined= session->examined_row_count;
00109   _event.rows_sent= session->sent_row_count;
00110   _event.tmp_tables= session->tmp_table;
00111   _event.warnings= session->total_warn_count;
00112 
00113   if (   _event.rows_examined < sysvar_threshold_rows_examined
00114       || _event.rows_sent     < sysvar_threshold_rows_sent
00115       || _event.tmp_tables    < sysvar_threshold_tmp_tables
00116       || _event.warnings      < sysvar_threshold_warnings)
00117     return false;
00118 
00122   _event.error= session->is_error() ? "true" : "false"; 
00123  
00127   _event.schema= session->schema()->c_str();
00128 
00132   _event.query= session->getQueryString()->c_str();
00133 
00138   boost::posix_time::ptime t_start= session->times.start_timer();
00139   _event.ts= boost::posix_time::to_iso_extended_string(t_start);
00140 
00141   // Pass the event to the loggers.
00142   _logger_file->logEvent((const event_t *)&_event);
00143 
00144   return false; // success
00145 }