| 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 event |
| 32 | |
| 33 | import ( |
| 34 | "fmt" |
| 35 | "time" |
| 36 | |
| 37 | "github.com/percona/go-mysql/log" |
| 38 | ) |
| 39 | |
| 40 | // A Result contains a global class and per-ID classes with finalized metric |
| 41 | // statistics. The classes are keyed on class ID. |
| 42 | type Result struct { |
| 43 | Global *Class // all classes |
| 44 | Class map[string]*Class // keyed on class ID |
| 45 | RateLimit uint |
| 46 | Error string |
| 47 | } |
| 48 | |
| 49 | // An Aggregator groups events by class ID. When there are no more events, |
| 50 | // a call to Finalize computes all metric statistics and returns a Result. |
| 51 | type Aggregator struct { |
| 52 | samples bool |
| 53 | utcOffset time.Duration |
| 54 | outlierTime float64 |
| 55 | // -- |
| 56 | global *Class |
| 57 | classes map[string]*Class |
| 58 | rateLimit uint |
| 59 | } |
| 60 | |
| 61 | // NewAggregator returns a new Aggregator. |
| 62 | // outlierTime is https://www.percona.com/doc/percona-server/5.5/diagnostics/slow_extended_55.html#slow_query_log_always_write_time |
| 63 | func NewAggregator(samples bool, utcOffset time.Duration, outlierTime float64) *Aggregator { |
| 64 | a := &Aggregator{ |
| 65 | samples: samples, |
| 66 | utcOffset: utcOffset, |
| 67 | outlierTime: outlierTime, |
| 68 | // -- |
| 69 | global: NewClass("", "", "", "", "", "", false), |
| 70 | classes: make(map[string]*Class), |
| 71 | } |
| 72 | return a |
| 73 | } |
| 74 | |
| 75 | // AddEvent adds the event to the aggregator, automatically creating new classes |
| 76 | // as needed. |
| 77 | func (a *Aggregator) AddEvent(event *log.Event, id, user, host, db, server, fingerprint string) { |
| 78 | if a.rateLimit != event.RateLimit { |
| 79 | a.rateLimit = event.RateLimit |
| 80 | } |
| 81 | |
| 82 | outlier := false |
| 83 | if a.outlierTime > 0 && event.TimeMetrics["Query_time"] > a.outlierTime { |
| 84 | outlier = true |
| 85 | } |
| 86 | |
| 87 | a.global.AddEvent(event, outlier) |
| 88 | |
| 89 | // Group events by all dimentions. |
| 90 | ident := fmt.Sprintf("%s;%s;%s;%s;%s", id, user, host, db, server) |
| 91 | class, ok := a.classes[ident] |
| 92 | if !ok { |
| 93 | class = NewClass(id, user, host, db, server, fingerprint, a.samples) |
| 94 | a.classes[ident] = class |
| 95 | } |
| 96 | class.AddEvent(event, outlier) |
| 97 | } |
| 98 | |
| 99 | // Finalize calculates all metric statistics and returns a Result. |
| 100 | // Call this function when done adding events to the aggregator. |
| 101 | func (a *Aggregator) Finalize() Result { |
| 102 | a.global.Finalize(a.rateLimit) |
| 103 | a.global.UniqueQueries = uint(len(a.classes)) |
| 104 | for _, class := range a.classes { |
| 105 | class.Finalize(a.rateLimit) |
| 106 | class.UniqueQueries = 1 |
| 107 | if class.Example != nil && class.Example.Ts != "" { |
| 108 | if t, err := time.Parse("2006-01-02 15:04:05", class.Example.Ts); err != nil { |
| 109 | class.Example.Ts = "" |
| 110 | } else { |
| 111 | class.Example.Ts = t.Add(a.utcOffset).Format("2006-01-02 15:04:05") |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | return Result{ |
| 116 | Global: a.global, |
| 117 | Class: a.classes, |
| 118 | RateLimit: a.rateLimit, |
| 119 | } |
| 120 | } |