github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrlogrus/example/main.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  	"net/http"
    10  	"os"
    11  
    12  	newrelic "github.com/newrelic/go-agent"
    13  	"github.com/newrelic/go-agent/_integrations/nrlogrus"
    14  	"github.com/sirupsen/logrus"
    15  )
    16  
    17  func mustGetEnv(key string) string {
    18  	if val := os.Getenv(key); "" != val {
    19  		return val
    20  	}
    21  	panic(fmt.Sprintf("environment variable %s unset", key))
    22  }
    23  
    24  func main() {
    25  	cfg := newrelic.NewConfig("Logrus App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    26  	logrus.SetLevel(logrus.DebugLevel)
    27  	cfg.Logger = nrlogrus.StandardLogger()
    28  
    29  	app, err := newrelic.NewApplication(cfg)
    30  	if nil != err {
    31  		fmt.Println(err)
    32  		os.Exit(1)
    33  	}
    34  
    35  	http.HandleFunc(newrelic.WrapHandleFunc(app, "/", func(w http.ResponseWriter, r *http.Request) {
    36  		io.WriteString(w, "hello world")
    37  	}))
    38  
    39  	http.ListenAndServe(":8000", nil)
    40  }