github.com/epsagon/epsagon-go@v1.39.0/example/gin_wrapper/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"time"
     8  
     9  	"github.com/epsagon/epsagon-go/epsagon"
    10  	epsagongin "github.com/epsagon/epsagon-go/wrappers/gin"
    11  	epsagonhttp "github.com/epsagon/epsagon-go/wrappers/net/http"
    12  	"github.com/gin-gonic/gin"
    13  )
    14  
    15  func main() {
    16  	config := epsagon.NewTracerConfig(
    17  		"gin-wrapper-test", "",
    18  	)
    19  	config.MetadataOnly = false
    20  	r := epsagongin.GinRouterWrapper{
    21  		IRouter:  gin.Default(),
    22  		Hostname: "my-site",
    23  		Config:   config,
    24  	}
    25  
    26  	r.GET("/ping", func(c *gin.Context) {
    27  		time.Sleep(time.Second * 1)
    28  		fmt.Println("hello world")
    29  		c.JSON(200, gin.H{
    30  			"message": "pong",
    31  		})
    32  	})
    33  	r.POST("/ping", func(c *gin.Context) {
    34  		time.Sleep(time.Second * 1)
    35  		body, err := ioutil.ReadAll(c.Request.Body)
    36  		if err == nil {
    37  			fmt.Println("Recieved body: ", string(body))
    38  		} else {
    39  			fmt.Println("Error reading body: ", err)
    40  		}
    41  
    42  		client := http.Client{
    43  			Transport: epsagonhttp.NewTracingTransport(epsagongin.EpsagonContext(c))}
    44  		resp, err := client.Get("http://www.example.com")
    45  
    46  		if err == nil {
    47  			respBody, err := ioutil.ReadAll(resp.Body)
    48  			if err == nil {
    49  				fmt.Println("First 1000 bytes recieved: ", string(respBody[:1000]))
    50  			}
    51  		}
    52  
    53  		c.JSON(200, gin.H{
    54  			"message": "pong",
    55  		})
    56  	})
    57  
    58  	/* listen and serve on 0.0.0.0:<PORT> */
    59  	/* for windows - localhost:<PORT> */
    60  	/* Port arg format: ":<PORT>" "*/
    61  	err := r.Run(":3001")
    62  
    63  	if err != nil {
    64  		fmt.Println(err)
    65  	}
    66  }