bgpwtf/cccampix: draw the rest of the fucking owl
Change-Id: I49fd5906e69512e8f2d414f406edc0179522f225
diff --git a/go/mirko/BUILD.bazel b/go/mirko/BUILD.bazel
index 405987e..a771e96 100644
--- a/go/mirko/BUILD.bazel
+++ b/go/mirko/BUILD.bazel
@@ -5,7 +5,9 @@
srcs = [
"kubernetes.go",
"mirko.go",
+ "sql.go",
"sql_migrations.go",
+ "trace.go",
],
importpath = "code.hackerspace.pl/hscloud/go/mirko",
visibility = ["//visibility:public"],
diff --git a/go/mirko/sql.go b/go/mirko/sql.go
new file mode 100644
index 0000000..949d557
--- /dev/null
+++ b/go/mirko/sql.go
@@ -0,0 +1,35 @@
+package mirko
+
+import (
+ "context"
+ "database/sql"
+ "database/sql/driver"
+ "time"
+
+ "github.com/gchaincl/sqlhooks"
+ "golang.org/x/net/trace"
+)
+
+type sqlHooks struct{}
+
+func (h *sqlHooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
+ tr, ok := trace.FromContext(ctx)
+ if ok {
+ tr.LazyPrintf("SQL query: %s", query)
+ tr.LazyPrintf("SQL args: %+v", args)
+ }
+ return context.WithValue(ctx, "begin", time.Now()), nil
+}
+
+func (h *sqlHooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
+ begin := ctx.Value("begin").(time.Time)
+ tr, ok := trace.FromContext(ctx)
+ if ok {
+ tr.LazyPrintf("SQL took: %s", time.Since(begin).String())
+ }
+ return ctx, nil
+}
+
+func TraceSQL(driver driver.Driver, wrapped string) {
+ sql.Register(wrapped, sqlhooks.Wrap(driver, &sqlHooks{}))
+}
diff --git a/go/mirko/trace.go b/go/mirko/trace.go
new file mode 100644
index 0000000..33b4352
--- /dev/null
+++ b/go/mirko/trace.go
@@ -0,0 +1,37 @@
+package mirko
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/golang/glog"
+ "golang.org/x/net/trace"
+)
+
+func TraceInfof(ctx context.Context, f string, args ...interface{}) {
+ tr, ok := trace.FromContext(ctx)
+ if !ok {
+ fmtd := fmt.Sprintf(f, args...)
+ glog.Info("[no trace] %v", fmtd)
+ return
+ }
+ tr.LazyPrintf(f, args...)
+}
+
+func TraceWarningf(ctx context.Context, f string, args ...interface{}) {
+ glog.Warningf(f, args...)
+
+ tr, ok := trace.FromContext(ctx)
+ if ok {
+ tr.LazyPrintf(f, args...)
+ }
+}
+
+func TraceErrorf(ctx context.Context, f string, args ...interface{}) {
+ glog.Errorf(f, args...)
+
+ tr, ok := trace.FromContext(ctx)
+ if ok {
+ tr.LazyPrintf(f, args...)
+ }
+}