gin

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2017 License: BSD-3-Clause Imports: 5 Imported by: 0

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")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTML

func HTML(c *gin.Context, code int, name string, obj interface{})

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",
		})
	})
}
Output:

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

func NewChildSpan(name string, c *gin.Context) *tracer.Span

NewChildSpan will create a span that is the child of the span stored in the context.

func Span

func Span(c *gin.Context) (*tracer.Span, bool)

Span returns the Span stored in the given Context and true. If it doesn't exist, it will returns (nil, false)

func SpanDefault

func SpanDefault(c *gin.Context) *tracer.Span

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!")
	})

}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL