package main import ( "fmt" "time" "context" "strings" "github.com/gin-gonic/gin" "github.com/SkyAPM/go2sky" "github.com/SkyAPM/go2sky/reporter" ) func main() { // 创建一个Gin路由器 r := gin.Default() // skywalking服务地址 127.0.0.1:11800 rp, err := reporter.NewGRPCReporter("127.0.0.1:11800", reporter.WithCheckInterval(time.Second)) if err != nil { fmt.Println("create gosky reporter failed!") } defer rp.Close() fmt.Println("create gosky reporter ok!") // 服务名 test-demo tracer, err := go2sky.NewTracer("test.demo", go2sky.WithReporter(rp)) // 定义路由 r.GET("/", func(c *gin.Context) { var ret string span, _, err := tracer.CreateEntrySpan(context.WithValue(context.Background(), go2sky.CtxTraceKey, "1234"), time.Unix(1694509947, 0), strings.Join([]string{"test.demo", "index"}, "/"), func(headerKey string) (string, error) { return ret, nil }) if err != nil { fmt.Println("create new accountData span error %v \n", err) return } span.SetComponent(0) span.Tag("name", "test") span.Tag("age", "21") span.Tag("ip", "127.0.0.1") span.End(time.Now()) c.JSON(200, gin.H{ "message": "Hello World!", }) }) r.GET("/ping", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "pong!", }) }) r.POST("/submit", func(c *gin.Context) { var json struct { Name string `json:"name"` Email string `json:"email"` } if err := c.Bind(&json); err == nil { c.JSON(200, gin.H{ "message": "JSON received", "name": json.Name, "email": json.Email, }) } else { c.JSON(400, gin.H{"error": err.Error()}) } }) // 启动Gin应用程序 r.Run(":9999") } //go run main.go 运行
//http://127.0.0.1:9999/ 访问url上报数据
标签:span,string,err,reporter,golang,json,gin,上报,数据 From: https://www.cnblogs.com/boye169/p/17697398.html