Skip to content

Instantly share code, notes, and snippets.

@axw
Last active May 28, 2018 04:30
Show Gist options
  • Save axw/01c524388ba07389da3703bc2c00d832 to your computer and use it in GitHub Desktop.
Save axw/01c524388ba07389da3703bc2c00d832 to your computer and use it in GitHub Desktop.
Instrumenting an application using net/http and database/sql
package main
import (
"net/http"
"github.com/elastic/apm-agent-go/module/apmhttp"
"github.com/elastic/apm-agent-go/module/apmsql"
_ "github.com/elastic/apm-agent-go/module/apmsql/sqlite3"
)
func main() {
db, err := apmsql.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
if _, err := db.Exec("CREATE TABLE foo (id INTEGER PRIMARY KEY AUTOINCREMENT);"); err != nil {
panic(err)
}
http.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) {
rows, err := db.QueryContext(req.Context(), "SELECT * FROM foo")
if err != nil {
panic(err)
}
defer rows.Close()
w.WriteHeader(http.StatusTeapot)
})
http.ListenAndServe(":8080", apmhttp.Wrap(http.DefaultServeMux))
}
package main
import (
"database/sql"
"net/http"
_ "github.com/mattn/go-sqlite3"
)
func main() {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
panic(err)
}
if _, err := db.Exec("CREATE TABLE foo (id INTEGER PRIMARY KEY AUTOINCREMENT);"); err != nil {
panic(err)
}
http.HandleFunc("/foo", func(w http.ResponseWriter, req *http.Request) {
rows, err := db.QueryContext(req.Context(), "SELECT * FROM foo")
if err != nil {
panic(err)
}
defer rows.Close()
w.WriteHeader(http.StatusTeapot)
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment