1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright 2011 Daniel Nichter
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <drizzled/item.h>
24
#include <drizzled/module/option_map.h>
25
#include <drizzled/session.h>
26
#include <drizzled/session/times.h>
27
#include <drizzled/plugin.h>
28
#include "query_log.h"
31
using namespace drizzled;
32
using namespace plugin;
34
QueryLog::QueryLog(bool enabled, QueryLoggerFile *logger_file) :
35
drizzled::plugin::EventObserver("query_log"),
36
sysvar_enabled(enabled),
37
_logger_file(logger_file)
45
void QueryLog::registerSessionEventsDo(Session &, EventObserverList &observers)
47
registerEvent(observers, AFTER_STATEMENT);
50
bool QueryLog::observeEventDo(EventData &data)
52
// Don't log and return successful if...
53
if (not sysvar_enabled // all logging is disabled
54
|| not sysvar_file_enabled) // or file logging is disabled
59
afterStatement((AfterStatementEventData &)data);
62
fprintf(stderr, "query_log: Unexpected event '%s'\n",
63
EventObserver::eventName(data.event));
69
bool QueryLog::afterStatement(AfterStatementEventData &data)
71
Session *session= &data.session;
73
// For the moment we're only interestd in queries, not admin
75
if (session->command != COM_QUERY)
78
// Query end time (microseconds from epoch)
79
uint64_t t_mark= session->times.getCurrentTimestamp(false);
82
* Time values, first in microseconds so we can check the thresholds.
84
_event.execution_time= session->times.getElapsedTime();
85
_event.lock_time= (t_mark - session->times.utime_after_lock);
86
_event.session_time= (t_mark - session->times.getConnectMicroseconds());
89
* Check thresholds as soon as possible, return early if possible.
90
* This avoid unnecessary work; i.e. don't construct the whole event
91
* only to throw it away at the end because it fails a threshold.
93
if ( _event.execution_time < sysvar_threshold_execution_time
94
|| _event.lock_time < sysvar_threshold_lock_time
95
|| _event.session_time < sysvar_threshold_session_time)
99
* Convert from microseconds to seconds, e.g. 42 to 0.000042
100
* This is done for the user who may read the log. It's a lot
101
* easier to see half a second as 0.5 than 500000.
103
_event.execution_time= _event.execution_time * 0.000001;
104
_event.lock_time= _event.lock_time * 0.000001;
105
_event.session_time= _event.session_time * 0.000001;
110
_event.session_id= session->getSessionId();
111
_event.query_id= session->getQueryId();
112
_event.rows_examined= session->examined_row_count;
113
_event.rows_sent= session->sent_row_count;
114
_event.tmp_tables= session->tmp_table;
115
_event.warnings= session->total_warn_count;
117
if ( _event.rows_examined < sysvar_threshold_rows_examined
118
|| _event.rows_sent < sysvar_threshold_rows_sent
119
|| _event.tmp_tables < sysvar_threshold_tmp_tables
120
|| _event.warnings < sysvar_threshold_warnings)
124
* Boolean values, as strings
126
_event.error= session->is_error() ? "true" : "false";
129
* String values, may be blank
131
_event.schema= session->schema()->c_str();
136
_event.query= session->getQueryString()->c_str();
139
* Timestamp values, convert from microseconds from epoch to
140
* ISO timestamp like 2002-01-31T10:00:01.123456
142
boost::posix_time::ptime t_start= session->times.start_timer();
143
_event.ts= boost::posix_time::to_iso_extended_string(t_start);
145
// Pass the event to the loggers.
146
_logger_file->logEvent((const event_t *)&_event);
148
return false; // success