| 1 | /* |
| 2 | Copyright (c) 2019, Percona LLC. |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | |
| 8 | * Redistributions of source code must retain the above copyright notice, this |
| 9 | list of conditions and the following disclaimer. |
| 10 | |
| 11 | * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | this list of conditions and the following disclaimer in the documentation |
| 13 | and/or other materials provided with the distribution. |
| 14 | |
| 15 | * Neither the name of the copyright holder nor the names of its |
| 16 | contributors may be used to endorse or promote products derived from |
| 17 | this software without specific prior written permission. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 27 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | // Package log provides an interface and data structures for MySQL log parsers. |
| 32 | // Log parsing yields events that are aggregated to calculate metric statistics |
| 33 | // like max Query_time. See also percona.com/go-mysql/event/. |
| 34 | package log |
| 35 | |
| 36 | import ( |
| 37 | "time" |
| 38 | ) |
| 39 | |
| 40 | // An event is a query like "SELECT col FROM t WHERE id = 1", some metrics like |
| 41 | // Query_time (slow log) or SUM_TIMER_WAIT (Performance Schema), and other |
| 42 | // metadata like default database, timestamp, etc. Metrics and metadata are not |
| 43 | // guaranteed to be defined--and frequently they are not--but at minimum an |
| 44 | // event is expected to define the query and Query_time metric. Other metrics |
| 45 | // and metadata vary according to MySQL version, distro, and configuration. |
| 46 | type Event struct { |
| 47 | Offset uint64 // byte offset in file at which event starts |
| 48 | OffsetEnd uint64 // byte offset in file at which event ends |
| 49 | Ts time.Time // timestamp of event |
| 50 | Admin bool // true if Query is admin command |
| 51 | Query string // SQL query or admin command |
| 52 | User string |
| 53 | Host string |
| 54 | Db string |
| 55 | Server string |
| 56 | LabelsKey []string |
| 57 | LabelsValue []string |
| 58 | TimeMetrics map[string]float64 // *_time and *_wait metrics |
| 59 | NumberMetrics map[string]uint64 // most metrics |
| 60 | BoolMetrics map[string]bool // yes/no metrics |
| 61 | RateType string // Percona Server rate limit type |
| 62 | RateLimit uint // Percona Server rate limit value |
| 63 | } |
| 64 | |
| 65 | // NewEvent returns a new Event with initialized metric maps. |
| 66 | func NewEvent() *Event { |
| 67 | event := new(Event) |
| 68 | event.TimeMetrics = make(map[string]float64) |
| 69 | event.NumberMetrics = make(map[string]uint64) |
| 70 | event.BoolMetrics = make(map[string]bool) |
| 71 | return event |
| 72 | } |
| 73 | |
| 74 | // Options encapsulate common options for making a new LogParser. |
| 75 | type Options struct { |
| 76 | StartOffset uint64 // byte offset in file at which to start parsing |
| 77 | FilterAdminCommand map[string]bool // admin commands to ignore |
| 78 | Debug bool // print trace info to STDERR with standard library logger |
| 79 | Debugf func(format string, v ...interface{}) // use this function for logging instead of log.Printf (Debug still should be true) |
| 80 | DefaultLocation *time.Location // DefaultLocation to assume for logs in MySQL < 5.7 format. |
| 81 | } |
| 82 | |
| 83 | // A LogParser sends events to a channel. |
| 84 | type LogParser interface { |
| 85 | Start() error |
| 86 | Stop() |
| 87 | EventChan() <-chan *Event |
| 88 | } |