| 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 logscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/ |
| 35 | |
| 36 | import ( |
| 37 | "timescip-go gomod github.com/golang/go/src go1.25.4 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 Eventscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event# struct { |
| 47 | Offsetscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Offset. uint64 // byte offset in file at which event starts |
| 48 | OffsetEndscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#OffsetEnd. uint64 // byte offset in file at which event ends |
| 49 | Tsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Ts. timescip-go gomod github.com/golang/go/src go1.25.4 time/.Timescip-go gomod github.com/golang/go/src go1.25.4 time/Time# // timestamp of event |
| 50 | Adminscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Admin. bool // true if Query is admin command |
| 51 | Queryscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Query. string // SQL query or admin command |
| 52 | Userscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#User. string |
| 53 | Hostscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Host. string |
| 54 | Dbscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Db. string |
| 55 | Serverscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#Server. string |
| 56 | LabelsKeyscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#LabelsKey. []string |
| 57 | LabelsValuescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#LabelsValue. []string |
| 58 | TimeMetricsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#TimeMetrics. map[string]float64 // *_time and *_wait metrics |
| 59 | NumberMetricsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#NumberMetrics. map[string]uint64 // most metrics |
| 60 | BoolMetricsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#BoolMetrics. map[string]bool // yes/no metrics |
| 61 | RateTypescip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#RateType. string // Percona Server rate limit type |
| 62 | RateLimitscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#RateLimit. uint // Percona Server rate limit value |
| 63 | } |
| 64 | |
| 65 | // NewEvent returns a new Event with initialized metric maps. |
| 66 | func NewEventscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/NewEvent().() *Eventscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event# { |
| 67 | event := new(Eventscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#) |
| 68 | event.TimeMetricsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#TimeMetrics. = make(map[string]float64) |
| 69 | event.NumberMetricsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#NumberMetrics. = make(map[string]uint64) |
| 70 | event.BoolMetricsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event#BoolMetrics. = make(map[string]bool) |
| 71 | return event |
| 72 | } |
| 73 | |
| 74 | // Options encapsulate common options for making a new LogParser. |
| 75 | type Optionsscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Options# struct { |
| 76 | StartOffsetscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Options#StartOffset. uint64 // byte offset in file at which to start parsing |
| 77 | FilterAdminCommandscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Options#FilterAdminCommand. map[string]bool // admin commands to ignore |
| 78 | Debugscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Options#Debug. bool // print trace info to STDERR with standard library logger |
| 79 | Debugfscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Options#Debugf. func(format string, v ...interface{}) // use this function for logging instead of log.Printf (Debug still should be true) |
| 80 | DefaultLocationscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Options#DefaultLocation. *timescip-go gomod github.com/golang/go/src go1.25.4 time/.Locationscip-go gomod github.com/golang/go/src go1.25.4 time/Location# // DefaultLocation to assume for logs in MySQL < 5.7 format. |
| 81 | } |
| 82 | |
| 83 | // A LogParser sends events to a channel. |
| 84 | type LogParserscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/LogParser# interface { |
| 85 | Startscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/LogParser#Start.() error |
| 86 | Stopscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/LogParser#Stop.() |
| 87 | EventChanscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/LogParser#EventChan.() <-chan *Eventscip-go gomod github.com/percona/go-mysql . `github.com/percona/go-mysql/log`/Event# |
| 88 | } |