Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Last active September 16, 2015 14:13
Show Gist options
  • Save hirokazumiyaji/476cdc989aa037672e68 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/476cdc989aa037672e68 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"./newrelic"
)
func main() {
agent, err := newrelic.NewAgent("license", "example", true)
if err != nil {
panic(err)
}
http.HandleFunc("/", agent.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello world")
}))
http.ListenAndServe(":8000", nil)
}
package newrelic
import (
metrics "github.com/yvasiyarov/go-metrics"
"github.com/yvasiyarov/gorelic"
type Agent struct {
newrelicAgent *gorelic.Agent
}
func NewAgent(license string, appname string, verbose bool) (*Agent, error) {
if license == "" {
return nil, fmt.Errorf("Please specify a NewRelic license")
}
agent := &Agent{
newrelicAgent: gorelic.NewAgent(),
}
agent.newrelicAgent.NewrelicLicense = license
agent.newrelicAgent.NewrelicName = appname
agent.newrelicAgent.HTTPTimer = metrics.NewTimer()
agent.newrelicAgent.CollectHTTPStat = true
agent.newrelicAgent.Verbose = verbose
agent.newrelicAgent.Run()
return agent, nil
}
func (a *Agent) HandleFunc(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
startTime := time.Now()
f(w, r)
if a.newrelicAgent != nil {
a.newrelicAgent.HTTPTimer.UpdateSince(startTime)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment