Skip to content

Instantly share code, notes, and snippets.

@RohitRox
Last active December 20, 2018 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RohitRox/af7143efc8bb1de3eef4af5778340863 to your computer and use it in GitHub Desktop.
Save RohitRox/af7143efc8bb1de3eef4af5778340863 to your computer and use it in GitHub Desktop.
New Relic Go Sample
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"time"
newrelic "github.com/newrelic/go-agent"
)
type Post struct {
UserId int `json:"userId"`
Id int `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
}
func checkWISConnection(txn newrelic.Transaction) (string, error) {
defer newrelic.StartSegment(txn, "checkWISConnection").End()
wreq, wreqErr := http.NewRequest("GET", fmt.Sprintf("%v/worker/service-status", os.Getenv("WORKER_INFO_URL")), nil)
if wreqErr != nil {
return "Failed to initialize request", wreqErr
}
client := &http.Client{}
_, err := client.Do(wreq)
if err != nil {
return "Failed to do request", err
}
fmt.Printf("WIS service status check passed")
time.Sleep(100 * time.Millisecond)
return "ok", nil
}
func checkInternet(txn newrelic.Transaction) (string, error) {
defer newrelic.StartSegment(txn, "checkInternet").End()
url := "https://jsonplaceholder.typicode.com/posts"
httpClient := http.Client{
Timeout: time.Second * 2,
}
httpReq, reqErr := http.NewRequest(http.MethodGet, url, nil)
if reqErr != nil {
return "Failed to initialize request", reqErr
}
httpRes, getErr := httpClient.Do(httpReq)
if getErr != nil {
return "Failed to do request", getErr
}
body, bodyErr := ioutil.ReadAll(httpRes.Body)
if bodyErr != nil {
return "Failed to read response", getErr
}
var posts []Post
json.Unmarshal(body, &posts)
fmt.Printf("Got %d number of posts.", len(posts))
time.Sleep(100 * time.Millisecond)
return "ok", nil
}
type handler struct {
App newrelic.Application
}
func (h *handler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
// The call to StartTransaction must include the response writer and the
// request.
txn := h.App.StartTransaction("server-txn", writer, req)
defer txn.End()
if req.URL.String() == "/segments" {
internetStatus, internetStatusErr := checkInternet(txn)
if internetStatusErr != nil {
fmt.Printf("Error:: %v", internetStatusErr)
}
wisStatus, wisStatusErr := checkWISConnection(txn)
if wisStatusErr != nil {
fmt.Printf("Error:: %v", wisStatusErr)
}
time.Sleep(100 * time.Millisecond)
resText := fmt.Sprintf("New Relic demo operation completed: CheckInternet: %s, WIS Status: %s", internetStatus, wisStatus)
io.WriteString(writer, resText)
} else {
txn.WriteHeader(http.StatusNotFound)
}
}
func mustGetEnv(key string) string {
if val := os.Getenv(key); "" != val {
return val
}
panic(fmt.Sprintf("environment variable %s unset", key))
}
func makeApplication() (newrelic.Application, error) {
cfg := newrelic.NewConfig("wis-worker-information-support-service", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
cfg.DistributedTracer.Enabled = true
cfg.CrossApplicationTracer.Enabled = false
app, err := newrelic.NewApplication(cfg)
if nil != err {
return nil, err
}
// Wait for the application to connect.
if err = app.WaitForConnection(5 * time.Second); nil != err {
return nil, err
}
return app, nil
}
func main() {
app, err := makeApplication()
if nil != err {
fmt.Println(err)
os.Exit(1)
}
server := http.Server{
Addr: ":8000",
Handler: &handler{App: app},
}
server.ListenAndServe()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment