Skip to content

Commit

Permalink
contrib/database/sql: Added an example showing how to use Sqlite. (#292)
Browse files Browse the repository at this point in the history
* contrib/database/sql: Added an example showing how to use Sqlite.

* contrib/database/sql: Fixed a comment typo.
  • Loading branch information
Erich Ess authored Aug 1, 2018
1 parent 588a6ff commit 888c7c4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions contrib/database/sql/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"

sqlite "github.com/mattn/go-sqlite3" // Setup application to use Sqlite
sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
Expand Down Expand Up @@ -54,3 +55,28 @@ func Example_context() {
}
rows.Close()
}

func Example_sqlite() {
// Register the driver that we will be using (in this case Sqlite) under a custom service name.
sqltrace.Register("sqlite", &sqlite.SQLiteDriver{}, sqltrace.WithServiceName("sqlite-example"))

// Open a connection to the DB using the driver we've just registered with tracing.
db, err := sqltrace.Open("sqlite", "./test.db")
if err != nil {
log.Fatal(err)
}

// Create a root span, giving name, server and resource.
_, ctx := tracer.StartSpanFromContext(context.Background(), "my-query",
tracer.SpanType("example"),
tracer.ServiceName("sqlite-example"),
tracer.ResourceName("initial-access"),
)

// Subsequent spans inherit their parent from context.
rows, err := db.QueryContext(ctx, "SELECT * FROM city LIMIT 5")
if err != nil {
log.Fatal(err)
}
rows.Close()
}

0 comments on commit 888c7c4

Please sign in to comment.