Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chenquan/sqltrace
A low-code intrusion library that provides SQL tracing capabilities, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, ent, etc.)
https://github.com/chenquan/sqltrace
ent go golang gorm mysql opentelemetry oracle postgresql sql sqlite3 sqlserver sqlx tidb trace xorm
Last synced: 3 months ago
JSON representation
A low-code intrusion library that provides SQL tracing capabilities, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, ent, etc.)
- Host: GitHub
- URL: https://github.com/chenquan/sqltrace
- Owner: chenquan
- License: apache-2.0
- Created: 2022-09-11T03:12:43.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-02-26T13:06:13.000Z (11 months ago)
- Last Synced: 2024-06-19T23:15:10.524Z (7 months ago)
- Topics: ent, go, golang, gorm, mysql, opentelemetry, oracle, postgresql, sql, sqlite3, sqlserver, sqlx, tidb, trace, xorm
- Language: Go
- Homepage:
- Size: 327 KB
- Stars: 15
- Watchers: 1
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqltrace
A low-code intrusion library that provides SQL tracing capabilities, suitable for any relational database (Sqlite3, MySQL, Oracle, SQL Server,
PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various relational database (gorm, xorm, sqlx, etc.)> Unstable, use with caution in production environment
# 😜installation
```shell
go get -u github.com/chenquan/sqltrace
```# 👏how to use
## native
```go
package mainimport (
"context"
"database/sql"
"fmt""github.com/chenquan/sqltrace"
"github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)func main() {
// Create a sqlite3 driver with link tracking
driver := sqltrace.NewDriver(sqltrace.Config{
Name: "sqlite3_trace",
DataSourceName: "sqlite3",
Endpoint: "http://localhost:14268/api/traces",
Sampler: 1,
Batcher: "jaeger",
}, &sqlite3.SQLiteDriver{})
defer sqltrace.StopAgent()// register new driver
sql.Register("sqlite3_trace", driver)// open database
db, err := sql.Open("sqlite3_trace", "identifier.sqlite")
if err != nil {
panic(err)
}tracer := otel.GetTracerProvider().Tracer("sqlite3_trace")
ctx, span := tracer.Start(context.Background(),
"test",
trace.WithSpanKind(trace.SpanKindClient),
)
defer span.End()db.ExecContext(ctx, `CREATE TABLE t
(
age integer,
name TEXT
)`)
db.ExecContext(ctx, "insert into t values (?,?)", 1, "chenquan")// transaction
tx, err := db.BeginTx(ctx, nil)
stmt, err := tx.PrepareContext(ctx, "select age+1 as age,name from t where age = ?;")
stmt.QueryContext(ctx, 1)
tx.Commit()rows, err := db.QueryContext(ctx, "select age+1 as age,name from t;")
for rows.Next() {
var age int
var name string
err := rows.Scan(&age, &name)
if err != nil {
fmt.Println(err)
}fmt.Println(age, name)
}
}
```![](images/trace-native.png)
## gorm
```go
package mainimport (
"context"
"database/sql"
"fmt""github.com/chenquan/sqltrace"
"github.com/mattn/go-sqlite3"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)func main() {
// Create a sqlite3 driver with link tracking
driver := sqltrace.NewDriver(sqltrace.Config{
DataSourceName: "sqlite3",
}, &sqlite3.SQLiteDriver{})
defer sqltrace.StopAgent()// register new driver
sql.Register("sqlite3_trace", driver)db, err := gorm.Open(sqlite.Dialector{
DriverName: "sqlite3_trace",
DSN: "identifier.sqlite",
}, &gorm.Config{})
if err != nil {
panic(err)
}
tracer := otel.GetTracerProvider().Tracer("sqlite3_trace")
ctx, span := tracer.Start(context.Background(),
"test",
trace.WithSpanKind(trace.SpanKindClient),
)
defer span.End()type T struct {
Age int `db:"age"`
Name string `db:"name"`
}
db = db.WithContext(ctx)
db.Migrator().AutoMigrate(&T{})
db.Create(&T{
Age: 1,
Name: "chenquan",
})rows, err := db.Raw("select age+1 as age,name from ts;").Rows()
if err != nil {
panic(err)
}for rows.Next() {
var age int
var name string
err := rows.Scan(&age, &name)
if err != nil {
fmt.Println(err)
}fmt.Println(age, name)
}
db.Commit()
}```
![](images/trace-gorm.png)
# ⭐star
If you like or are using this project to learn or start your solution, please give it a star⭐. Thanks!
# 👐ecosystem
- [sqlbreaker](https://github.com/chenquan/sqlbreaker): A low-code intrusion library that provides SQL breaker capabilities, suitable for any
relational database (Sqlite3, MySQL, Oracle, SQL Server, PostgreSQL, TiDB, TDengine, etc.) and ORM libraries for various
relational database (gorm, xorm, sqlx, etc.)
- [sqlplus](https://github.com/chenquan/sqlplus): A sql enhancement tool library based on `database/sql/driver`