otelxorm
is a hook for xorm that enables OpenTelemetry tracing for database operations.
To install otelxorm
, use go get
:
go get github.com/jenbonzhang/otelxorm
To use otelxorm
, first create an instance of xorm.Engine
:
engine, err := xorm.NewEngine(dbType, dsn)
if err != nil {
logrus.Errorf("Database connection failed err: %v. Database name: %s", err, name)
panic(err)
}
Then, add the otelxorm
hook to the engine:
engine.AddHook(otelxorm.Hook(
otelxorm.WithDBName(name), // Set the database name
otelxorm.WithFormatSQLReplace(), // Set the method for replacing parameters in formatted SQL statements
))
This will enable tracing for all database operations performed by the engine.
otelxorm
provides several options for configuration:
WithDBName(name string)
: Sets the name of the database being traced.WithFormatSQL(formatSQL func(sql string, args []interface{}) string) Option
: Sets the method for formatted SQL statements. By default,otelxorm
usesotelxorm.defaultFormatSQL
to format SQL statements and parameters.WithFormatSQLReplace()
this is use args to replace the sql parameters with$d
in the sql statement.
You can define your own implementation of the formatSQL
method and pass it to the WithFormatSQL
option when adding the otelxorm
hook to the engine. This will override the default implementation used by otelxorm
. Here's an example:
func myFormatSQL(sql string, args []interface{}) string {
// Custom implementation for formatting SQL statements and parameters
// ...
return formattedQuery
}
// ...
engine.AddHook(otelxorm.Hook(
otelxorm.WithDBName(name),
otelxorm.WithFormatSQL(myFormatSQL),
))
This will enable tracing for all database operations performed by the engine using your custom implementation of the formatSQL
method.
We welcome contributions! Please see our contributing guidelines for more information.
otelxorm
is licensed under the Apache 2.0 License. See LICENSE for more information.