github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrgin/v1/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  	"os"
     9  
    10  	"github.com/gin-gonic/gin"
    11  	"github.com/newrelic/go-agent"
    12  	"github.com/newrelic/go-agent/_integrations/nrgin/v1"
    13  )
    14  
    15  func makeGinEndpoint(s string) func(*gin.Context) {
    16  	return func(c *gin.Context) {
    17  		c.Writer.WriteString(s)
    18  	}
    19  }
    20  
    21  func v1login(c *gin.Context)  { c.Writer.WriteString("v1 login") }
    22  func v1submit(c *gin.Context) { c.Writer.WriteString("v1 submit") }
    23  func v1read(c *gin.Context)   { c.Writer.WriteString("v1 read") }
    24  
    25  func endpoint404(c *gin.Context) {
    26  	c.Writer.WriteHeader(404)
    27  	c.Writer.WriteString("returning 404")
    28  }
    29  
    30  func endpointChangeCode(c *gin.Context) {
    31  	// gin.ResponseWriter buffers the response code so that it can be
    32  	// changed before the first write.
    33  	c.Writer.WriteHeader(404)
    34  	c.Writer.WriteHeader(200)
    35  	c.Writer.WriteString("actually ok!")
    36  }
    37  
    38  func endpointResponseHeaders(c *gin.Context) {
    39  	// Since gin.ResponseWriter buffers the response code, response headers
    40  	// can be set afterwards.
    41  	c.Writer.WriteHeader(200)
    42  	c.Writer.Header().Set("Content-Type", "application/json")
    43  	c.Writer.WriteString(`{"zip":"zap"}`)
    44  }
    45  
    46  func endpointNotFound(c *gin.Context) {
    47  	c.Writer.WriteString("there's no endpoint for that!")
    48  }
    49  
    50  func endpointAccessTransaction(c *gin.Context) {
    51  	if txn := nrgin.Transaction(c); nil != txn {
    52  		txn.SetName("custom-name")
    53  	}
    54  	c.Writer.WriteString("changed the name of the transaction!")
    55  }
    56  
    57  func mustGetEnv(key string) string {
    58  	if val := os.Getenv(key); "" != val {
    59  		return val
    60  	}
    61  	panic(fmt.Sprintf("environment variable %s unset", key))
    62  }
    63  
    64  func main() {
    65  	cfg := newrelic.NewConfig("Gin App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    66  	cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    67  	app, err := newrelic.NewApplication(cfg)
    68  	if nil != err {
    69  		fmt.Println(err)
    70  		os.Exit(1)
    71  	}
    72  
    73  	router := gin.Default()
    74  	router.Use(nrgin.Middleware(app))
    75  
    76  	router.GET("/404", endpoint404)
    77  	router.GET("/change", endpointChangeCode)
    78  	router.GET("/headers", endpointResponseHeaders)
    79  	router.GET("/txn", endpointAccessTransaction)
    80  
    81  	// Since the handler function name is used as the transaction name,
    82  	// anonymous functions do not get usefully named.  We encourage
    83  	// transforming anonymous functions into named functions.
    84  	router.GET("/anon", func(c *gin.Context) {
    85  		c.Writer.WriteString("anonymous function handler")
    86  	})
    87  
    88  	v1 := router.Group("/v1")
    89  	v1.GET("/login", v1login)
    90  	v1.GET("/submit", v1submit)
    91  	v1.GET("/read", v1read)
    92  
    93  	router.NoRoute(endpointNotFound)
    94  
    95  	router.Run(":8000")
    96  }