-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from instana/instrument_db_sql
Add instrumentation for database/sql
- Loading branch information
Showing
9 changed files
with
1,062 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// +build go1.10 | ||
|
||
package instana_test | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
instana "github.com/instana/go-sensor" | ||
) | ||
|
||
// This example demonstrates how to instrument an *sql.DB instance created with sql.OpenDB() and driver.Connector | ||
func ExampleWrapSQLConnector() { | ||
// Here we initialize a new instance of instana.Sensor, however it is STRONGLY recommended | ||
// to use a single instance throughout your application | ||
sensor := instana.NewSensor("my-http-client") | ||
|
||
// Instrument the connector. Normally this would be a type provided by the driver library. | ||
// Here we use a test mock to avoid bringing external dependencies. | ||
// | ||
// Note that instana.WrapSQLConnector() requires the connection string to send it later | ||
// along with database spans. | ||
connector := instana.WrapSQLConnector(sensor, "driver connection string", &sqlConnector{}) | ||
|
||
// Use wrapped connector to initialize the database client. Note that | ||
db := sql.OpenDB(connector) | ||
|
||
// Inject parent span into the context | ||
span := sensor.Tracer().StartSpan("entry") | ||
ctx := instana.ContextWithSpan(context.Background(), span) | ||
|
||
// Query the database, passing the context containing the active span | ||
db.QueryContext(ctx, "SELECT * FROM users;") | ||
|
||
// SQL queries that are not expected to return a result set are also supported | ||
db.ExecContext(ctx, "UPDATE users SET last_seen_at = NOW();") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Oops, something went wrong.