github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/docs/sql.md (about)

     1  ## Tracing SQL Driver Databases
     2  
     3  The tracer provides means to trace database calls made through the Go standard library, for drivers compliant with the [database/sql/driver package](https://pkg.go.dev/database/sql/driver@go1.21.3).
     4  
     5  The tracer is able to auto detect popular databases such as Postgres and MySQL, but will still provide more generic database spans to every driver that fulfills the Go standard library.
     6  
     7  Tracing your database calls with the Instana Go Tracer is easier than you think. Simply replace the standard `sql.Open` function by the `instana.SQLInstrumentAndOpen` wrapper function.
     8  
     9  In practical terms, your current code that looks something like this:
    10  
    11  ```go
    12  db, err := sql.Open(s, driverName, connString)
    13  ```
    14  
    15  Will be changed to this:
    16  
    17  ```go
    18  c = instana.InitCollector(&instana.Options{
    19    Service: "Database Call App",
    20  })
    21  
    22  db, err := instana.SQLInstrumentAndOpen(c, driverName, connString)
    23  ```
    24  
    25  The `instana.SQLInstrumentAndOpen` will return the expected `(*sql.DB, error)` return, so the rest of your code needs no further changes.
    26  
    27  ### Complete Example
    28  
    29  [MySQL Example](../example/sql-mysql/main.go)
    30  ```go
    31  package main
    32  
    33  import (
    34    "io"
    35    "net/http"
    36  
    37    _ "github.com/go-sql-driver/mysql"
    38    instana "github.com/instana/go-sensor"
    39  )
    40  
    41  func main() {
    42    s := instana.InitCollector(&instana.Options{
    43      Service: "MySQL app",
    44    })
    45  
    46    db, err := instana.SQLInstrumentAndOpen(s, "mysql", "go:gopw@tcp(localhost:3306)/godb")
    47    if err != nil {
    48      panic(err)
    49    }
    50  
    51    r, err := db.QueryContext(req.Context(), "SELECT 'Current date is' || CURDATE();")
    52  
    53    if err != nil {
    54      panic(err)
    55    }
    56  
    57    var buf, res string
    58  
    59    for r.Next() {
    60      r.Scan(&buf)
    61      res += " " + buf
    62    }
    63  }
    64  ```
    65  
    66  -----
    67  [README](../README.md) |
    68  [Tracer Options](options.md) |
    69  [Tracing HTTP Outgoing Requests](roundtripper.md) |
    70  [Tracing Other Go Packages](other_packages.md) |
    71  [Instrumenting Code Manually](manual_instrumentation.md)