Documentation ¶
Overview ¶
Package splunkmysql provides instrumentation for the github.com/go-sql-driver/mysql package.
To use this package, replace any blank identified imports of the github.com/go-sql-driver/mysql package with an import of this package and use the splunksql.Open function as a replacement for any sql.Open function use. For example, if your code looks like this to start.
import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) // ... db, err := sql.Open("mysql", "user:password@/dbname") // ...
Update to this.
import ( _ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/go-sql-driver/mysql/splunkmysql" "github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql" ) // ... db, err := splunksql.Open("mysql", "user:password@/dbname") // ...
Example ¶
package main import ( "database/sql" "fmt" "net/http" "strconv" "strings" "time" "github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql" _ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/go-sql-driver/mysql/splunkmysql" ) type server struct { DB *sql.DB } func (s *server) listenAndServe() error { // Requests to /square/n will return the square of n. http.HandleFunc("/square/", s.handle) return http.ListenAndServe(":80", nil) } func (s *server) handle(w http.ResponseWriter, req *http.Request) { idx := strings.LastIndex(req.URL.Path, "/") n, err := strconv.Atoi(req.URL.Path[idx+1:]) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } query := "SELECT squareNumber FROM squarenum WHERE number = ?" var nSquared int // Propagate the context to ensure created spans are included in any // existing trace. if err := s.DB.QueryRowContext(req.Context(), query, n).Scan(&nSquared); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "%d", nSquared) } func main() { // Create a traced connection to the MySQL database. db, err := splunksql.Open("mysql", "user:password@/dbname") if err != nil { panic(err) } defer db.Close() // Set recommended default DB configuration. db.SetConnMaxLifetime(time.Minute * 3) db.SetMaxOpenConns(10) db.SetMaxIdleConns(10) // Validate DSN data by opening a connection. There is no parent context // to pass here so the span created from this operation will be in its own // trace. if err := db.Ping(); err != nil { panic(err) } srv := &server{DB: db} if err := srv.listenAndServe(); err != nil { panic(err) } }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.