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)
41
void QueryLog::registerSessionEventsDo(Session &, EventObserverList &observers)
43
registerEvent(observers, AFTER_STATEMENT);
46
bool QueryLog::observeEventDo(EventData &data)
48
// Don't log and return successful if...
49
if (not sysvar_enabled // all logging is disabled
50
|| not sysvar_file_enabled) // or file logging is disabled
55
afterStatement((AfterStatementEventData &)data);
58
fprintf(stderr, "query_log: Unexpected event '%s'\n",
59
EventObserver::eventName(data.event));
65
bool QueryLog::afterStatement(AfterStatementEventData &data)
67
Session *session= &data.session;
69
// For the moment we're only interestd in queries, not admin
71
if (session->command != COM_QUERY)
74
// Query end time (microseconds from epoch)
75
uint64_t t_mark= session->times.getCurrentTimestamp(false);
78
* Time values, first in microseconds so we can check the thresholds.
80
_event.execution_time= session->times.getElapsedTime();
81
_event.lock_time= (t_mark - session->times.utime_after_lock);
82
_event.session_time= (t_mark - session->times.getConnectMicroseconds());
85
* Check thresholds as soon as possible, return early if possible.
86
* This avoid unnecessary work; i.e. don't construct the whole event
87
* only to throw it away at the end because it fails a threshold.
89
if ( _event.execution_time < sysvar_threshold_execution_time
90
|| _event.lock_time < sysvar_threshold_lock_time
91
|| _event.session_time < sysvar_threshold_session_time)
95
* Convert from microseconds to seconds, e.g. 42 to 0.000042
96
* This is done for the user who may read the log. It's a lot
97
* easier to see half a second as 0.5 than 500000.
99
_event.execution_time= _event.execution_time * 0.000001;
100
_event.lock_time= _event.lock_time * 0.000001;
101
_event.session_time= _event.session_time * 0.000001;
106
_event.session_id= session->getSessionId();
107
_event.query_id= session->getQueryId();
108
_event.rows_examined= session->examined_row_count;
109
_event.rows_sent= session->sent_row_count;
110
_event.tmp_tables= session->tmp_table;
111
_event.warnings= session->total_warn_count;
113
if ( _event.rows_examined < sysvar_threshold_rows_examined
114
|| _event.rows_sent < sysvar_threshold_rows_sent
115
|| _event.tmp_tables < sysvar_threshold_tmp_tables
116
|| _event.warnings < sysvar_threshold_warnings)
120
* Boolean values, as strings
122
_event.error= session->is_error() ? "true" : "false";
125
* String values, may be blank
127
_event.schema= session->schema()->c_str();
132
_event.query= session->getQueryString()->c_str();
135
* Timestamp values, convert from microseconds from epoch to
136
* ISO timestamp like 2002-01-31T10:00:01.123456
138
boost::posix_time::ptime t_start= session->times.start_timer();
139
_event.ts= boost::posix_time::to_iso_extended_string(t_start);
141
// Pass the event to the loggers.
142
_logger_file->logEvent((const event_t *)&_event);
144
return false; // success