package main

import (
	"context"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/tidbcloud/tiinf/monitor/threadlocal"
	"github.com/tidbcloud/tiinf/monitor/tilog"
	"github.com/tidbcloud/tiinf/monitor/titracing"
	"github.com/tidbcloud/tiinf/transport/http/tigin"
	"go.uber.org/zap"
)

func main() {
	// SHOULD be integrated with `titracing`
	_, err := titracing.InitJaeger("exmaple")
	if err != nil {
		tilog.Panic(context.TODO(), "failed to init jaeger", zap.Error(err))
	}

	// `tigin` has integrated `otelgin`
	engine := tigin.Default()

	// use the middleware to set context automatically
	engine.Use(threadlocal.SetLocalContextMiddleware)

	engine.GET("/example", func(c *gin.Context) {
		FunctionWithoutContext()
		c.String(http.StatusOK, "Hello World")
	})

	engine.Run() //nolint:errcheck
}

func FunctionWithoutContext() {
	// You can get the context with `threadlocal.GetContext`
	tilog.Info(threadlocal.GetContext(), "This is used to test pass context with threadlocal")
}
