Last active
May 28, 2018 04:30
-
-
Save axw/01c524388ba07389da3703bc2c00d832 to your computer and use it in GitHub Desktop.
Instrumenting an application using net/http and database/sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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