slowlog: github.com/go-mysql/slowlog Index | Files

package slowlog

import "github.com/go-mysql/slowlog"

Package slowlog provides functions and data structures for working with the MySQL slow log.

Index

Package Files

aggregator.go class.go event.go metrics.go parser.go

Constants

const (
    // MAX_EXAMPLE_BYTES defines the maximum Example.Query size.
    MAX_EXAMPLE_BYTES = 1024 * 10
)

Variables

var Debug = false
var (
    // ErrStarted is returned if Parser.Start is called more than once.
    ErrStarted = errors.New("parser is started")
)

type Aggregator Uses

type Aggregator struct {
    // contains filtered or unexported fields
}

An Aggregator groups events by class ID. When there are no more events, a call to Finalize computes all metric statistics and returns a Result.

func NewAggregator Uses

func NewAggregator(samples bool, utcOffset time.Duration, outlierTime float64) *Aggregator

NewAggregator returns a new Aggregator.

func (*Aggregator) AddEvent Uses

func (a *Aggregator) AddEvent(event Event, id, fingerprint string)

AddEvent adds the event to the aggregator, automatically creating new classes as needed.

func (*Aggregator) Finalize Uses

func (a *Aggregator) Finalize() Result

Finalize calculates all metric statistics and returns a Result. Call this function when done adding events to the aggregator.

type BoolStats Uses

type BoolStats struct {
    Sum uint64 // %true = Sum/Cnt
    // contains filtered or unexported fields
}

BoolStats are boolean-based metrics like QC_Hit and Filesort.

type Class Uses

type Class struct {
    Id            string   // 32-character hex checksum of fingerprint
    Fingerprint   string   // canonical form of query: values replaced with "?"
    Metrics       Metrics  // statistics for each metric, e.g. max Query_time
    TotalQueries  uint     // total number of queries in class
    UniqueQueries uint     // unique number of queries in class
    Example       *Example `json:",omitempty"` // sample query with max Query_time
    // contains filtered or unexported fields
}

A Class represents all events with the same fingerprint and class ID. This is only enforced by convention, so be careful not to mix events from different classes.

func NewClass Uses

func NewClass(id, fingerprint string, sample bool) *Class

NewClass returns a new Class for the class ID and fingerprint. If sample is true, the query with the greatest Query_time is saved.

func (*Class) AddEvent Uses

func (c *Class) AddEvent(e Event, outlier bool)

AddEvent adds an event to the query class.

func (*Class) Finalize Uses

func (c *Class) Finalize(rateLimit uint)

Finalize calculates all metric statistics. Call this function when done adding events to the class.

type Event Uses

type Event struct {
    Offset        uint64 // byte offset in file at which event starts
    Ts            string // raw timestamp of event
    Admin         bool   // true if Query is admin command
    Query         string // SQL query or admin command
    User          string
    Host          string
    Db            string
    TimeMetrics   map[string]float64 // *_time and *_wait metrics
    NumberMetrics map[string]uint64  // most metrics
    BoolMetrics   map[string]bool    // yes/no metrics
    RateType      string             // Percona Server rate limit type
    RateLimit     uint               // Percona Server rate limit value
}

An Event is a query like "SELECT col FROM t WHERE id = 1", some metrics like Query_time (slow log) or SUM_TIMER_WAIT (Performance Schema), and other metadata like default database, timestamp, etc. Metrics and metadata are not guaranteed to be defined--and frequently they are not--but at minimum an event is expected to define the query and Query_time metric. Other metrics and metadata vary according to MySQL version, distro, and configuration.

func NewEvent Uses

func NewEvent() *Event

NewEvent returns a new Event with initialized metric maps.

type Example Uses

type Example struct {
    QueryTime float64 // Query_time
    Db        string  // Schema: <db> or USE <db>
    Query     string  // truncated to MAX_EXAMPLE_BYTES
    Ts        string  `json:",omitempty"` // in MySQL time zone
}

A Example is a real query and its database, timestamp, and Query_time. If the query is larger than MAX_EXAMPLE_BYTES, it is truncated and "..." is appended.

type FileParser Uses

type FileParser struct {
    // contains filtered or unexported fields
}

FileParser represents a file-based Parser. This is the canonical Parser because the slow log is a file.

func NewFileParser Uses

func NewFileParser(file *os.File) *FileParser

NewFileParser returns a new FileParser that reads from the open file. The file is not closed.

func (*FileParser) Error Uses

func (p *FileParser) Error() error

Error returns an error, if any, encountered while parsing the slow log.

func (*FileParser) Events Uses

func (p *FileParser) Events() <-chan Event

Events returns the channel to which events from the slow log are sent. The channel is closed when there are no more events. Events are not sent until Start is called.

func (*FileParser) Start Uses

func (p *FileParser) Start(opt Options) error

Start starts the parser. Events are sent to the unbuffered Events channel. Parsing stops on EOF, error, or call to Stop. The Events channel is closed when parsing stops.

func (*FileParser) Stop Uses

func (p *FileParser) Stop()

Stop stops the parser before parsing the next event or while blocked on sending the current event to the event channel.

type Metrics Uses

type Metrics struct {
    TimeMetrics   map[string]*TimeStats   `json:",omitempty"`
    NumberMetrics map[string]*NumberStats `json:",omitempty"`
    BoolMetrics   map[string]*BoolStats   `json:",omitempty"`
}

Metrics encapsulate the metrics of an event like Query_time and Rows_sent.

func NewMetrics Uses

func NewMetrics() Metrics

NewMetrics returns a pointer to an initialized Metrics structure.

func (*Metrics) AddEvent Uses

func (m *Metrics) AddEvent(e Event, outlier bool)

AddEvent saves all the metrics of the event.

func (*Metrics) Finalize Uses

func (m *Metrics) Finalize(rateLimit uint)

Finalize calculates the statistics of the added metrics. Call this function when done adding events.

type NumberStats Uses

type NumberStats struct {
    Sum uint64
    Min uint64 `json:",omitempty"`
    Avg uint64 `json:",omitempty"`
    Med uint64 `json:",omitempty"` // median
    P95 uint64 `json:",omitempty"` // 95th percentile
    Max uint64 `json:",omitempty"`
    // contains filtered or unexported fields
}

NumberStats are integer-based metrics like Rows_sent and Merge_passes.

type Options Uses

type Options struct {
    StartOffset        uint64          // byte offset in file at which to start parsing
    FilterAdminCommand map[string]bool // admin commands to ignore
}

Options encapsulate common options for making a new LogParser.

type Parser Uses

type Parser interface {
    Start(Options) error
    Events() <-chan Event
    Stop()
    Error() error
}

A Parser parses events from a slow log. The canonical Parser is FileParser because the slow log is a file. The caller receives events on the Events channel. This channel is closed when there are no more events. Any error during parsing is returned by Error.

type Result Uses

type Result struct {
    Global    *Class            // all classes
    Class     map[string]*Class // keyed on class ID
    RateLimit uint
    Error     string
}

A Result contains a global class and per-ID classes with finalized metric statistics. The classes are keyed on class ID.

type TimeStats Uses

type TimeStats struct {
    Sum float64
    Min float64 `json:",omitempty"`
    Avg float64 `json:",omitempty"`
    Med float64 `json:",omitempty"` // median
    P95 float64 `json:",omitempty"` // 95th percentile
    Max float64 `json:",omitempty"`
    // contains filtered or unexported fields
}

TimeStats are microsecond-based metrics like Query_time and Lock_time.

Package slowlog imports 11 packages (graph). Updated 2017-02-26. Refresh now. Tools for package owners.