All Projects → gchaincl → Sqlhooks

gchaincl / Sqlhooks

Licence: mit
Attach hooks to any database/sql driver

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Sqlhooks

Brave Opentracing
Bridge between OpenTracing and Brave
Stars: ✭ 64 (-83.88%)
Mutual labels:  tracing, instrumentation, opentracing
java-jaxrs
OpenTracing Java JAX-RS instrumentation
Stars: ✭ 37 (-90.68%)
Mutual labels:  instrumentation, tracing, opentracing
Java Specialagent
Automatic instrumentation for 3rd-party libraries in Java applications with OpenTracing.
Stars: ✭ 156 (-60.71%)
Mutual labels:  tracing, instrumentation, opentracing
Java Spring Cloud
Distributed tracing for Spring Boot, Cloud and other Spring projects
Stars: ✭ 326 (-17.88%)
Mutual labels:  tracing, instrumentation, opentracing
go-sensor
🚀 Go Distributed Tracing & Metrics Sensor for Instana
Stars: ✭ 90 (-77.33%)
Mutual labels:  instrumentation, tracing, opentracing
Molten
php probe for zipkin and opentracing
Stars: ✭ 740 (+86.4%)
Mutual labels:  tracing, instrumentation, opentracing
Instrumentedsql
A sql driver that will wrap any other driver and log/trace all its calls
Stars: ✭ 244 (-38.54%)
Mutual labels:  sql, tracing, opentracing
Zipkin Go Opentracing
OpenTracing Bridge for Zipkin Go
Stars: ✭ 472 (+18.89%)
Mutual labels:  tracing, instrumentation, opentracing
ruby-sensor
💎 Ruby Distributed Tracing & Metrics Sensor for Instana
Stars: ✭ 23 (-94.21%)
Mutual labels:  instrumentation, tracing, opentracing
zipkin-ruby-opentracing
OpenTracing Tracer implementation for Zipkin in Ruby
Stars: ✭ 15 (-96.22%)
Mutual labels:  instrumentation, tracing, opentracing
Jplusone
Tool for automatic detection and asserting "N+1 SELECT problem" occurences in JPA based Spring Boot Java applications and finding origin of JPA issued SQL statements in general
Stars: ✭ 91 (-77.08%)
Mutual labels:  sql, tracing, instrumentation
zipkin-cpp-opentracing
OpenTracing Tracer implementation for Zipkin in C++
Stars: ✭ 46 (-88.41%)
Mutual labels:  instrumentation, tracing, opentracing
nginx-opentracing
Instrument nginx for OpenTracing.
Stars: ✭ 21 (-94.71%)
Mutual labels:  instrumentation, tracing, opentracing
java-web-servlet-filter
OpenTracing Java Web Servlet Filter Instrumentation
Stars: ✭ 20 (-94.96%)
Mutual labels:  instrumentation, tracing, opentracing
java-okhttp
OpenTracing Okhttp client instrumentation
Stars: ✭ 21 (-94.71%)
Mutual labels:  instrumentation, tracing, opentracing
Rel
💎 Modern Database Access Layer for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API
Stars: ✭ 317 (-20.15%)
Mutual labels:  sql, database
Grdb.swift
A toolkit for SQLite databases, with a focus on application development
Stars: ✭ 4,637 (+1068.01%)
Mutual labels:  sql, database
Datafuse
Datafuse is a free Cloud-Native Analytics DBMS(Inspired by ClickHouse) implemented in Rust
Stars: ✭ 327 (-17.63%)
Mutual labels:  sql, database
Monetdb Old
This is the official mirror of the MonetDB Mercurial repository. Please note that we do not accept pull requests on github. The regression test results can be found on the MonetDB Testweb http://monetdb.cwi.nl/testweb/web/status.php .For contributions please see: https://www.monetdb.org/Developers
Stars: ✭ 317 (-20.15%)
Mutual labels:  sql, database
Freezer
A simple & fluent Android ORM, how can it be easier ? RxJava2 compatible
Stars: ✭ 326 (-17.88%)
Mutual labels:  sql, database

sqlhooks Build Status Coverage Status Go Report Card

Attach hooks to any database/sql driver.

The purpose of sqlhooks is to provide a way to instrument your sql statements, making really easy to log queries or measure execution time without modifying your actual code.

Install

go get github.com/gchaincl/sqlhooks/v2

Requires Go >= 1.8.x

Breaking changes

V2 isn't backward compatible with previous versions, if you want to fetch old versions, you can use go modules or get them from gopkg.in

go get github.com/gchaincl/sqlhooks
go get gopkg.in/gchaincl/sqlhooks.v1

Usage GoDoc

// This example shows how to instrument sql queries in order to display the time that they consume
package main

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"github.com/gchaincl/sqlhooks/v2"
	"github.com/mattn/go-sqlite3"
)

// Hooks satisfies the sqlhook.Hooks interface
type Hooks struct {}

// Before hook will print the query with it's args and return the context with the timestamp
func (h *Hooks) Before(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	fmt.Printf("> %s %q", query, args)
	return context.WithValue(ctx, "begin", time.Now()), nil
}

// After hook will get the timestamp registered on the Before hook and print the elapsed time
func (h *Hooks) After(ctx context.Context, query string, args ...interface{}) (context.Context, error) {
	begin := ctx.Value("begin").(time.Time)
	fmt.Printf(". took: %s\n", time.Since(begin))
	return ctx, nil
}

func main() {
	// First, register the wrapper
	sql.Register("sqlite3WithHooks", sqlhooks.Wrap(&sqlite3.SQLiteDriver{}, &Hooks{}))

	// Connect to the registered wrapped driver
	db, _ := sql.Open("sqlite3WithHooks", ":memory:")

	// Do you're stuff
	db.Exec("CREATE TABLE t (id INTEGER, text VARCHAR(16))")
	db.Exec("INSERT into t (text) VALUES(?), (?)", "foo", "bar")
	db.Query("SELECT id, text FROM t")
}

/*
Output should look like:
> CREATE TABLE t (id INTEGER, text VARCHAR(16)) []. took: 121.238µs
> INSERT into t (text) VALUES(?), (?) ["foo" "bar"]. took: 36.364µs
> SELECT id, text FROM t []. took: 4.653µs
*/

Benchmarks

 go test -bench=. -benchmem
 BenchmarkSQLite3/Without_Hooks-4                  200000              8572 ns/op             627 B/op         16 allocs/op
 BenchmarkSQLite3/With_Hooks-4                     200000             10231 ns/op             738 B/op         18 allocs/op
 BenchmarkMySQL/Without_Hooks-4                     10000            108421 ns/op             437 B/op         10 allocs/op
 BenchmarkMySQL/With_Hooks-4                        10000            226085 ns/op             597 B/op         13 allocs/op
 BenchmarkPostgres/Without_Hooks-4                  10000            125718 ns/op             649 B/op         17 allocs/op
 BenchmarkPostgres/With_Hooks-4                      5000            354831 ns/op            1122 B/op         27 allocs/op
 PASS
 ok      github.com/gchaincl/sqlhooks    11.713s
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].