Last active
September 16, 2015 14:13
-
-
Save hirokazumiyaji/476cdc989aa037672e68 to your computer and use it in GitHub Desktop.
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 ( | |
"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) | |
} |
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 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