~drizzle-trunk/drizzle/development

2311.1.3 by Daniel Nichter
Change to GPLv3 license.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
4
 *  Copyright 2011 Daniel Nichter
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
5
 *
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
6
 *  This program is free software: you can redistribute it and/or modify
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
7
 *  it under the terms of the GNU General Public License as published by
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
8
 *  the Free Software Foundation, either version 3 of the License, or
9
 *  (at your option) any later version.
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
10
 *
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.
15
 *
16
 *  You should have received a copy of the GNU General Public License
2311.1.3 by Daniel Nichter
Change to GPLv3 license.
17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
2311.1.1 by Daniel Nichter
Add query_log plugin. It's tested and documented.
18
 */
19
20
#include <config.h>
21
#include <string>
22
#include <fcntl.h>
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"
29
30
using namespace std;
31
using namespace drizzled;
32
using namespace plugin;
33
34
QueryLog::QueryLog(bool enabled, QueryLoggerFile *logger_file) :
35
  drizzled::plugin::EventObserver("query_log"),
36
  sysvar_enabled(enabled),
37
  _logger_file(logger_file)
38
{
39
}
40
41
QueryLog::~QueryLog()
42
{
43
}
44
45
void QueryLog::registerSessionEventsDo(Session &, EventObserverList &observers)
46
{
47
  registerEvent(observers, AFTER_STATEMENT);
48
}
49
50
bool QueryLog::observeEventDo(EventData &data)
51
{
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
55
    return false;
56
57
  switch (data.event) {
58
  case AFTER_STATEMENT:
59
    afterStatement((AfterStatementEventData &)data);
60
    break;
61
  default:
62
    fprintf(stderr, "query_log: Unexpected event '%s'\n",
63
      EventObserver::eventName(data.event));
64
  }
65
66
  return false;
67
}
68
69
bool QueryLog::afterStatement(AfterStatementEventData &data)
70
{
71
  Session *session= &data.session;
72
73
  // For the moment we're only interestd in queries, not admin
74
  // command and such.
75
  if (session->command != COM_QUERY)
76
    return false;
77
78
  // Query end time (microseconds from epoch)
79
  uint64_t t_mark= session->times.getCurrentTimestamp(false);
80
81
  /**
82
   * Time values, first in microseconds so we can check the thresholds.
83
   */
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());
87
88
  /**
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.
92
   */
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)
96
    return false;
97
98
  /**
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.
102
   */
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;
106
107
  /**
108
   * Integer values
109
   */
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;
116
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)
121
    return false;
122
123
  /**
124
   * Boolean values, as strings
125
   */
126
  _event.error= session->is_error() ? "true" : "false"; 
127
 
128
  /**
129
   * String values, may be blank
130
   */ 
131
  _event.schema= session->schema()->c_str();
132
133
  /**
134
   * The query string
135
   */
136
  _event.query= session->getQueryString()->c_str();
137
138
  /**
139
   * Timestamp values, convert from microseconds from epoch to
140
   * ISO timestamp like 2002-01-31T10:00:01.123456
141
   */
142
  boost::posix_time::ptime t_start= session->times.start_timer();
143
  _event.ts= boost::posix_time::to_iso_extended_string(t_start);
144
145
  // Pass the event to the loggers.
146
  _logger_file->logEvent((const event_t *)&_event);
147
148
  return false; // success
149
}