> ## Documentation Index
> Fetch the complete documentation index at: https://docs.appsignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Instrumentation

## Setup

To instrument your SQL usage, you'll need to import the OpenTelemetry official instrumentation packages:

<CodeGroup>
  ```go Go theme={null}
  import(
    "github.com/XSAM/otelsql"
    semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
  )
  ```
</CodeGroup>

Your DB connection must be opened using the `otelsql` package, it's also important to query the database using the `QueryContext` function to pass the current context, that way, the spans will be properly nested and structured. The example below shows how to instrument queries to a MySQL database in a web request context.

For more information about usage and SQL drivers support, take a look at the [package repository](https://github.com/XSAM/otelsql).

<CodeGroup>
  ```go Go theme={null}
  func mysqlQuery(w http.ResponseWriter, r *http.Request) {
  	ctx := r.Context()

  	db, err := otelsql.Open(
  		"mysql",
  		"root:password@tcp(0.0.0.0:3306)/mydb",
  		otelsql.WithAttributes(semconv.DBSystemMySQL),
  	)
  	if err != nil {
  		panic(err)
  	}
  	defer db.Close()

  	rows, err := db.QueryContext(ctx, "select * from mysql.user")
  	if err != nil {
  		panic(err)
  	}
  	defer rows.Close()
  }
  ```
</CodeGroup>
