Back to godoc.org

Package gin

v1.8.1-0...-a106eec
Latest Go to latest

The highest tagged major version is .

Published: Sep 30, 2019 | License: BSD-3-Clause | Module: github.com/DevotedHealth/dd-trace-go

Overview

Package gin provides functions to trace the gin-gonic/gin package (https://github.com/gin-gonic/gin).

Example

To start tracing requests, add the trace middleware to your Gin router.

Code:

package main

import (
	"github.com/gin-gonic/gin"
	gintrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin"
)

func main() {
	// Create a gin.Engine
	r := gin.New()

	// Use the tracer middleware with your desired service name.
	r.Use(gintrace.Middleware("my-web-app"))

	// Set up some endpoints.
	r.GET("/hello", func(c *gin.Context) {
		c.String(200, "hello world!")
	})

	// And start gathering request traces.
	r.Run(":8080")
}
Example (SpanFromContext)

Code:

package main

import (
	"github.com/gin-gonic/gin"
	gintrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin"
	"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)

func main() {
	r := gin.Default()
	r.Use(gintrace.Middleware("image-encoder"))
	r.GET("/image/encode", func(c *gin.Context) {
		ctx := c.Request.Context()
		// create a child span to track operation timing.
		encodeSpan, _ := tracer.StartSpanFromContext(ctx, "image.encode")
		// encode a image
		encodeSpan.Finish()

		c.String(200, "ok!")
	})

}

Index

Examples

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

Code:

package main

import (
	"github.com/gin-gonic/gin"
	gintrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/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) {
		// 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, opts ...Option) gin.HandlerFunc

Middleware returns middleware that will trace incoming requests.

type Option

type Option func(*config)

Option specifies instrumentation configuration options.

func WithAnalytics

func WithAnalytics(on bool) Option

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) Option

WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier