Created
October 18, 2019 11:19
-
-
Save skaji/9ec8b2cbbaca5443140636bbf36d0c47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
"github.com/gin-gonic/gin" | |
"go.uber.org/zap" | |
gintrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin" | |
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | |
) | |
var logger, _ = zap.NewProduction() | |
func getEnv(key string, def string) string { | |
value := os.Getenv(key) | |
if value != "" { | |
return value | |
} | |
return def | |
} | |
func initDatadog(r *gin.Engine) { | |
host := os.Getenv("DATADOG_TRACE_AGENT_HOSTNAME") | |
if host == "" { | |
return | |
} | |
env := getEnv("DATADOG_ENV", "development") | |
addr := fmt.Sprintf("%s:%d", host, 8126) | |
service := getEnv("DATADOG_SERVICE_NAME", "gin-test") | |
log.Printf("Set datadog addr=%s, tag env=%s, service=%s\n", addr, env, service) | |
tracer.Start( | |
tracer.WithAgentAddr(addr), | |
tracer.WithGlobalTag("env", env), | |
) | |
r.Use(gintrace.Middleware(service)) | |
} | |
func accessLoop(url string) { | |
ticker := time.NewTicker(10 * time.Second) | |
defer ticker.Stop() | |
for range ticker.C { | |
res, err := http.Get(url) | |
if err != nil { | |
log.Println(err) | |
continue | |
} | |
io.Copy(ioutil.Discard, res.Body) | |
res.Body.Close() | |
} | |
} | |
func main() { | |
r := gin.Default() | |
initDatadog(r) | |
r.GET("/yahoo", func(c *gin.Context) { | |
}) | |
r.GET("/google", func(c *gin.Context) { | |
}) | |
r.Run(":8080") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment