Documentation
¶
Overview ¶
Package gin provides tracing middleware for the Gin web framework.
Example ¶
To start tracing requests, add the trace middleware to your Gin router.
package main
import (
gintrace "github.com/DataDog/dd-trace-go/contrib/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
func main() {
// Create your router and use the middleware.
r := gin.New()
r.Use(gintrace.Middleware("my-web-app"))
r.GET("/hello", func(c *gin.Context) {
c.String(200, "hello world!")
})
// Profit!
r.Run(":8080")
}
Index ¶
- func HTML(c *gin.Context, code int, name string, obj interface{})
- func Middleware(service string) gin.HandlerFunc
- func MiddlewareTracer(service string, t *tracer.Tracer) gin.HandlerFunc
- func NewChildSpan(name string, c *gin.Context) *tracer.Span
- func Span(c *gin.Context) (*tracer.Span, bool)
- func SpanDefault(c *gin.Context) *tracer.Span
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTML ¶
HTML will trace the rendering of the template as a child of the span in the given context.
Example ¶
package main
import (
gintrace "github.com/DataDog/dd-trace-go/contrib/gin-gonic/gin"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.Use(gintrace.Middleware("my-web-app"))
r.LoadHTMLGlob("templates/*")
r.GET("/index", func(c *gin.Context) {
// This will render the html and trace the execution time.
gintrace.HTML(c, 200, "index.tmpl", gin.H{
"title": "Main website",
})
})
}
func Middleware ¶
func Middleware(service string) gin.HandlerFunc
Middleware returns middleware that will trace requests with the default tracer.
func MiddlewareTracer ¶
func MiddlewareTracer(service string, t *tracer.Tracer) gin.HandlerFunc
MiddlewareTracer returns middleware that will trace requests with the given tracer.
func NewChildSpan ¶
NewChildSpan will create a span that is the child of the span stored in the context.
func Span ¶
Span returns the Span stored in the given Context and true. If it doesn't exist, it will returns (nil, false)
func SpanDefault ¶
SpanDefault returns the span stored in the given Context. If none exists, it will return an empty span.
Example ¶
package main
import (
gintrace "github.com/DataDog/dd-trace-go/contrib/gin-gonic/gin"
"github.com/DataDog/dd-trace-go/tracer"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.Use(gintrace.Middleware("image-encoder"))
r.GET("/image/encode", func(c *gin.Context) {
// The middleware patches a span to the request. Let's add some metadata,
// and create a child span.
span := gintrace.SpanDefault(c)
span.SetMeta("user.handle", "admin")
span.SetMeta("user.id", "1234")
encodeSpan := tracer.NewChildSpan("image.encode", span)
// encode a image
encodeSpan.Finish()
uploadSpan := tracer.NewChildSpan("image.upload", span)
// upload the image
uploadSpan.Finish()
c.String(200, "ok!")
})
}
Types ¶
This section is empty.