Implement Authable statements for improved logging

This commit is contained in:
Tobie Morgan Hitchcock 2017-03-24 21:25:16 +00:00
parent 84445997eb
commit 5a558c0d6f
7 changed files with 404 additions and 2 deletions

View file

@ -267,11 +267,18 @@ func (e *executor) execute(ctx context.Context, quit <-chan bool, send chan<- *R
// query, along with the query execution // query, along with the query execution
// speed, so we can analyse slow queries. // speed, so we can analyse slow queries.
log.WithPrefix("sql").WithFields(map[string]interface{}{ log := log.WithPrefix("sql").WithFields(map[string]interface{}{
"id": e.web.Get("id"), "id": e.web.Get("id"),
"kind": e.web.Get("auth").(*cnf.Auth).Kind, "kind": e.web.Get("auth").(*cnf.Auth).Kind,
"auth": e.web.Get("auth").(*cnf.Auth).Data, "auth": e.web.Get("auth").(*cnf.Auth).Data,
}).Debugln(stm) })
if stm, ok := stm.(sql.AuthableStatement); ok {
ns, db := stm.Auth()
log = log.WithField("ns", ns).WithField("db", db)
}
log.Debugln(stm)
// Check to see if the current statement is // Check to see if the current statement is
// a TRANSACTION statement, and if it is // a TRANSACTION statement, and if it is

View file

@ -38,6 +38,10 @@ type Statements []Statement
// Other // Other
// -------------------------------------------------- // --------------------------------------------------
type AuthableStatement interface {
Auth() (string, string)
}
type KillableStatement interface { type KillableStatement interface {
Begin() Begin()
Cease() Cease()

131
sql/auth.gen.go Normal file
View file

@ -0,0 +1,131 @@
// Code generated by https://github.com/abcum/tmpl
// Source file: auth.gen.go.tmpl
// DO NOT EDIT!
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
func (s *UseStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *InfoStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *LetStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *ReturnStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *LiveStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *SelectStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *CreateStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *UpdateStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DeleteStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RelateStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineNamespaceStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveNamespaceStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineDatabaseStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveDatabaseStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineLoginStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveLoginStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineTokenStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveTokenStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineScopeStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveScopeStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineTableStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveTableStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineFieldStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveFieldStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineIndexStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveIndexStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *DefineViewStatement) Auth() (string, string) {
return s.NS, s.DB
}
func (s *RemoveViewStatement) Auth() (string, string) {
return s.NS, s.DB
}

23
sql/auth.gen.go.tmpl Normal file
View file

@ -0,0 +1,23 @@
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
{{with $types := .}}{{range $k := $types}}
func (s *{{$k.name}}Statement) Auth() (string, string) {
return s.NS, s.DB
}
{{end}}{{end}}

33
sql/auth.gen.json Normal file
View file

@ -0,0 +1,33 @@
[
{ "name": "Use" },
{ "name": "Info" },
{ "name": "Let" },
{ "name": "Return" },
{ "name": "Live" },
{ "name": "Select" },
{ "name": "Create" },
{ "name": "Update" },
{ "name": "Delete" },
{ "name": "Relate" },
{ "name": "DefineNamespace" },
{ "name": "RemoveNamespace" },
{ "name": "DefineDatabase" },
{ "name": "RemoveDatabase" },
{ "name": "DefineLogin" },
{ "name": "RemoveLogin" },
{ "name": "DefineToken" },
{ "name": "RemoveToken" },
{ "name": "DefineScope" },
{ "name": "RemoveScope" },
{ "name": "DefineTable" },
{ "name": "RemoveTable" },
{ "name": "DefineField" },
{ "name": "RemoveField" },
{ "name": "DefineIndex" },
{ "name": "RemoveIndex" },
{ "name": "DefineView" },
{ "name": "RemoveView" }
]

View file

@ -15,6 +15,7 @@
package sql package sql
//go:generate go get -u github.com/abcum/tmpl //go:generate go get -u github.com/abcum/tmpl
//go:generate tmpl -file=auth.gen.json auth.gen.go.tmpl
//go:generate tmpl -file=kill.gen.json kill.gen.go.tmpl //go:generate tmpl -file=kill.gen.json kill.gen.go.tmpl
//go:generate go get -u github.com/ugorji/go/codec/codecgen //go:generate go get -u github.com/ugorji/go/codec/codecgen

203
sql/kill.gen.go Normal file
View file

@ -0,0 +1,203 @@
// Code generated by https://github.com/abcum/tmpl
// Source file: kill.gen.go.tmpl
// DO NOT EDIT!
// Copyright © 2016 Abcum Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"time"
)
func (s *SelectStatement) Begin() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
s.killable.ticker = time.AfterFunc(s.Timeout, func() {
s.killable.ticker.Stop()
s.killable.ticker = nil
close(s.killable.closer)
})
}
func (s *SelectStatement) Cease() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
if s.killable.ticker != nil {
s.killable.ticker.Stop()
}
}
func (s *SelectStatement) Timedout() <-chan struct{} {
if s.Timeout == 0 {
return nil
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
return s.killable.closer
}
func (s *CreateStatement) Begin() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
s.killable.ticker = time.AfterFunc(s.Timeout, func() {
s.killable.ticker.Stop()
s.killable.ticker = nil
close(s.killable.closer)
})
}
func (s *CreateStatement) Cease() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
if s.killable.ticker != nil {
s.killable.ticker.Stop()
}
}
func (s *CreateStatement) Timedout() <-chan struct{} {
if s.Timeout == 0 {
return nil
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
return s.killable.closer
}
func (s *UpdateStatement) Begin() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
s.killable.ticker = time.AfterFunc(s.Timeout, func() {
s.killable.ticker.Stop()
s.killable.ticker = nil
close(s.killable.closer)
})
}
func (s *UpdateStatement) Cease() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
if s.killable.ticker != nil {
s.killable.ticker.Stop()
}
}
func (s *UpdateStatement) Timedout() <-chan struct{} {
if s.Timeout == 0 {
return nil
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
return s.killable.closer
}
func (s *DeleteStatement) Begin() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
s.killable.ticker = time.AfterFunc(s.Timeout, func() {
s.killable.ticker.Stop()
s.killable.ticker = nil
close(s.killable.closer)
})
}
func (s *DeleteStatement) Cease() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
if s.killable.ticker != nil {
s.killable.ticker.Stop()
}
}
func (s *DeleteStatement) Timedout() <-chan struct{} {
if s.Timeout == 0 {
return nil
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
return s.killable.closer
}
func (s *RelateStatement) Begin() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
s.killable.ticker = time.AfterFunc(s.Timeout, func() {
s.killable.ticker.Stop()
s.killable.ticker = nil
close(s.killable.closer)
})
}
func (s *RelateStatement) Cease() {
if s.Timeout == 0 {
return
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
if s.killable.ticker != nil {
s.killable.ticker.Stop()
}
}
func (s *RelateStatement) Timedout() <-chan struct{} {
if s.Timeout == 0 {
return nil
}
if s.killable.closer == nil {
s.killable.closer = make(chan struct{})
}
return s.killable.closer
}