Skip to content

Commit

Permalink
Merge pull request #15 from koron/test-on-actions
Browse files Browse the repository at this point in the history
test on GitHub Actions
  • Loading branch information
koron authored Dec 4, 2023
2 parents 3ce6642 + 2edb277 commit d2ca247
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 55 deletions.
22 changes: 0 additions & 22 deletions .circleci/config.yml

This file was deleted.

26 changes: 26 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Go

on: [push]

jobs:

build:
name: Build
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
steps:

- uses: actions/checkout@v4

- uses: actions/setup-go@v4
with:
go-version: '>=1.20.10'

- run: go test ./...

- run: go build

# based on: github.com/koron-go/_skeleton/.github/workflows/go.yml
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.PHONY: build
build:
go build -gcflags '-e'

.PHONY: test
test:
go test -gcflags '-e' ./...

.PHONY: bench
bench:
go test -bench ./...

.PHONY: tags
tags:
gotags -f tags -R .

.PHONY: cover
cover:
mkdir -p tmp
go test -coverprofile tmp/_cover.out ./...
go tool cover -html tmp/_cover.out -o tmp/cover.html

.PHONY: checkall
checkall: vet staticcheck

.PHONY: vet
vet:
go vet ./...

.PHONY: staticcheck
staticcheck:
staticcheck ./...

.PHONY: clean
clean:
go clean
rm -f tags
rm -f tmp/_cover.out tmp/cover.html

# based on: github.com/koron-go/_skeleton/Makefile
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# DB (MySQL) query logger

[![CircleCI](https://circleci.com/gh/koron/dbquerylog.svg?style=svg)](https://circleci.com/gh/koron/dbquerylog)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/koron/dbquerylog)](https://pkg.go.dev/github.com/koron/dbquerylog)
[![Actions/Go](https://github.com/koron/dbquerylog/workflows/Go/badge.svg)](https://github.com/koron/dbquerylog/actions?query=workflow%3AGo)
[![Go Report Card](https://goreportcard.com/badge/github.com/koron/dbquerylog)](https://goreportcard.com/report/github.com/koron/dbquerylog)

dbquerylog converts TCP dump to DB query log.

dbquerylog parses the output of TCP dump as MySQL's Client/Server protocol, and
extracts information about queries from it.

dbquerylog is tested with MySQL 5.6/5.7.
It isn't tested with 8.0 or above yet.

## Getting start

How to use.
Expand Down
2 changes: 1 addition & 1 deletion cmd/packet-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *conn) Closed() {

func main() {
flag.Parse()
asm := mysqlasm.New(nil, newConn)
asm := mysqlasm.New(context.Background(), newConn)
asm.Warn = log.New(os.Stderr, "WARN ", log.LstdFlags)
err := asm.Assemble(context.Background(), os.Stdin, nil)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/koron/dbquerylog

go 1.20

require (
github.com/go-sql-driver/mysql v1.7.1
github.com/google/gopacket v1.1.19
)

require (
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
)
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"flag"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -236,7 +235,7 @@ func main() {
if debugFlag {
dbg = log.New(os.Stderr, " [DBG] ", 0)
} else {
dbg = log.New(ioutil.Discard, "", 0)
dbg = log.New(io.Discard, "", 0)
}
dec, ok := gopacket.DecodersByLayerName[decoder]
if !ok {
Expand All @@ -255,7 +254,7 @@ func main() {
}

ctx := context.Background()
asm := mysqlasm.New(nil, newConn)
asm := mysqlasm.New(ctx, newConn)
asm.Warn = warn
err := asm.Assemble(ctx, os.Stdin, dec)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions mysqlasm/asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ type Assembler struct {
}

func New(ctx context.Context, f ConnFactory) *Assembler {
if ctx == nil {
ctx = context.Background()
}
return &Assembler{
ctx: ctx,
f: f,
Expand Down
19 changes: 10 additions & 9 deletions parser/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ func (bm *bitmap) get(n uint16) bool {
return bm.b[i]&(1<<j) != 0
}

func (bm *bitmap) rank() uint16 {
c := bm.m
for i := uint16(0); i < bm.m; i++ {
if bm.get(i) {
c--
}
}
return c
}
// Unused private function.
//func (bm *bitmap) rank() uint16 {
// c := bm.m
// for i := uint16(0); i < bm.m; i++ {
// if bm.get(i) {
// c--
// }
// }
// return c
//}
18 changes: 9 additions & 9 deletions parser/decbuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type decbuf struct {
err error
}

var EOB = errors.New("not enough buffer")
var ErrNotEnoughBuffer = errors.New("not enough buffer")

func (b *decbuf) Read(p []byte) (int, error) {
n, m := len(p), len(b.buf)
Expand All @@ -35,7 +35,7 @@ func (b *decbuf) ReadUint8() (uint8, error) {
return 0, b.err
}
if len(b.buf) < 1 {
b.err = EOB
b.err = ErrNotEnoughBuffer
return 0, b.err
}
r := b.buf[0]
Expand All @@ -48,7 +48,7 @@ func (b *decbuf) ReadUint16() (uint16, error) {
return 0, b.err
}
if len(b.buf) < 2 {
b.err = EOB
b.err = ErrNotEnoughBuffer
return 0, b.err
}
r := binary.LittleEndian.Uint16(b.buf)
Expand All @@ -61,7 +61,7 @@ func (b *decbuf) ReadUint32() (uint32, error) {
return 0, b.err
}
if len(b.buf) < 4 {
b.err = EOB
b.err = ErrNotEnoughBuffer
return 0, b.err
}
r := binary.LittleEndian.Uint32(b.buf)
Expand All @@ -74,7 +74,7 @@ func (b *decbuf) ReadUint64() (uint64, error) {
return 0, b.err
}
if len(b.buf) < 8 {
b.err = EOB
b.err = ErrNotEnoughBuffer
return 0, b.err
}
r := binary.LittleEndian.Uint64(b.buf)
Expand All @@ -84,7 +84,7 @@ func (b *decbuf) ReadUint64() (uint64, error) {

func (b *decbuf) readNUint(n int) (uint64, error) {
if len(b.buf) < n {
b.err = EOB
b.err = ErrNotEnoughBuffer
return 0, b.err
}
r := uint64(0)
Expand All @@ -100,7 +100,7 @@ func (b *decbuf) ReadUintV() (*UintV, error) {
return nil, b.err
}
if len(b.buf) < 1 {
b.err = EOB
b.err = ErrNotEnoughBuffer
return nil, b.err
}
f := b.buf[0]
Expand Down Expand Up @@ -161,7 +161,7 @@ func (b *decbuf) ReadStringV() (*StringV, error) {
return nil, b.err
}
if len(b.buf) < int(n) {
b.err = EOB
b.err = ErrNotEnoughBuffer
return nil, b.err
}
s := StringV(b.buf[:n])
Expand All @@ -174,7 +174,7 @@ func (b *decbuf) Discard(n int) error {
return b.err
}
if len(b.buf) < n {
b.err = EOB
b.err = ErrNotEnoughBuffer
return b.err
}
b.buf = b.buf[n:]
Expand Down
5 changes: 0 additions & 5 deletions parser/readn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import (
"io"
)

type reader interface {
io.Reader
io.ByteReader
}

func readN(r io.Reader, b []byte) error {
for len(b) > 0 {
n, err := r.Read(b)
Expand Down
5 changes: 3 additions & 2 deletions tcpasm/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ func (a *Assembler) flushLoop(ctx context.Context, asm *tcpassembly.Assembler) {
if d == 0 {
d = 5 * time.Minute
}
ch := time.Tick(a.FlushInterval)
ticker := time.NewTicker(a.FlushInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ch:
case <-ticker.C:
asm.FlushWithOptions(tcpassembly.FlushOptions{
T: time.Now().Add(-d),
})
Expand Down

0 comments on commit d2ca247

Please sign in to comment.