A Go library that attaches hooks to any database/sql driver for query instrumentation without code changes.
sqlhooks is a Go library that enables developers to attach hooks to any database/sql driver, providing instrumentation capabilities for SQL queries. It allows for easy logging, performance measurement, and query monitoring without requiring modifications to existing database interaction code. The library follows a minimalist design philosophy, offering a simple, unobtrusive way to extend driver behavior while maintaining compatibility with Go's standard database/sql interface.
Go developers working with the database/sql package who need to instrument SQL queries for observability, such as logging execution times, auditing queries, or monitoring performance without altering their application's core database logic.
Developers choose sqlhooks because it is driver-agnostic, working with any database/sql-compatible driver like SQLite, MySQL, and PostgreSQL, and integrates non-invasively by wrapping existing drivers. Its minimal performance impact, as shown in benchmarks, and support for Go's context package for request-scoped instrumentation make it a practical choice for adding observability with negligible overhead.
Attach hooks to any database/sql driver
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Enables custom logging, timing, and auditing by attaching hooks before and after SQL execution, as shown in the example code that prints queries and elapsed times.
Works with any driver conforming to Go's database/sql interface, including SQLite, MySQL, and PostgreSQL, making it versatile for multi-database setups as stated in the features.
Integrates by wrapping existing database drivers without altering application logic, demonstrated in the usage example where drivers are registered with hooks.
Leverages Go's context package to pass data like timestamps between hook stages, enabling request-scoped instrumentation as illustrated in the Before and After hook methods.
Benchmarks show negligible overhead, with only minor increases in execution times (e.g., from 6163 ns to 6329 ns for SQLite), ensuring low impact on query performance.
V2 introduces breaking changes from previous versions, requiring migration efforts for existing users, as noted in the installation section with separate fetch commands for old versions.
Provides only the hook mechanism; developers must implement all logging, timing, or auditing logic themselves, increasing initial setup time and complexity.
Only works with drivers that strictly adhere to Go's database/sql package, excluding custom or non-compliant drivers, which restricts use in specialized environments.