1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"os"
"github.com/gin-gonic/gin"
nrgin "github.com/newrelic/go-agent/v3/integrations/nrgin"
"github.com/newrelic/go-agent/v3/newrelic"
)
func makeGinEndpoint(s string) func(*gin.Context) {
return func(c *gin.Context) {
c.Writer.WriteString(s)
}
}
func v1login(c *gin.Context) { c.Writer.WriteString("v1 login") }
func v1submit(c *gin.Context) { c.Writer.WriteString("v1 submit") }
func v1read(c *gin.Context) { c.Writer.WriteString("v1 read") }
func endpoint404(c *gin.Context) {
c.Writer.WriteHeader(404)
c.Writer.WriteString("returning 404")
}
func endpointChangeCode(c *gin.Context) {
// gin.ResponseWriter buffers the response code so that it can be
// changed before the first write.
c.Writer.WriteHeader(404)
c.Writer.WriteHeader(200)
c.Writer.WriteString("actually ok!")
}
func endpointResponseHeaders(c *gin.Context) {
// Since gin.ResponseWriter buffers the response code, response headers
// can be set afterwards.
c.Writer.WriteHeader(200)
c.Writer.Header().Set("Content-Type", "application/json")
c.Writer.WriteString(`{"zip":"zap"}`)
}
func endpointNotFound(c *gin.Context) {
c.Writer.WriteString("there's no endpoint for that!")
}
func endpointAccessTransaction(c *gin.Context) {
txn := nrgin.Transaction(c)
txn.SetName("custom-name")
c.Writer.WriteString("changed the name of the transaction!")
}
func main() {
app, err := newrelic.NewApplication(
newrelic.ConfigAppName("Gin App"),
newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
newrelic.ConfigDebugLogger(os.Stdout),
)
if nil != err {
fmt.Println(err)
os.Exit(1)
}
router := gin.Default()
router.Use(nrgin.Middleware(app))
router.GET("/404", endpoint404)
router.GET("/change", endpointChangeCode)
router.GET("/headers", endpointResponseHeaders)
router.GET("/txn", endpointAccessTransaction)
// Since the handler function name is used as the transaction name,
// anonymous functions do not get usefully named. We encourage
// transforming anonymous functions into named functions.
router.GET("/anon", func(c *gin.Context) {
c.Writer.WriteString("anonymous function handler")
})
v1 := router.Group("/v1")
v1.GET("/login", v1login)
v1.GET("/submit", v1submit)
v1.GET("/read", v1read)
router.NoRoute(endpointNotFound)
router.Run(":8000")
}
|