gin

package module
v0.0.0-...-7ebff8f Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0, BSD-3-Clause Imports: 16 Imported by: 0

Documentation

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.

package main

import (
	gintrace "github.com/DataDog/dd-trace-go/v2/contrib/gin-gonic/gin"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/gin-gonic/gin"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

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

Example (SpanFromContext)
package main

import (
	gintrace "github.com/DataDog/dd-trace-go/v2/contrib/gin-gonic/gin"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/gin-gonic/gin"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

	r := gin.Default()
	// make sure that the middleware is initialized before registering any routes
	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 an image
		encodeSpan.Finish()

		c.String(200, "ok!")
	})
}
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/v2/contrib/gin-gonic/gin"
	"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"

	"github.com/gin-gonic/gin"
)

func main() {
	tracer.Start()
	defer tracer.Stop()

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

func Middleware

func Middleware(service string, opts ...Option) gin.HandlerFunc

Middleware returns middleware that will trace incoming requests. If service is empty then the default service name will be used.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option describes options for the Gin integration.

type OptionFn

type OptionFn func(*config)

OptionFn represents options applicable to Middleware.

func WithAnalytics

func WithAnalytics(on bool) OptionFn

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) OptionFn

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

func WithHeaderTags

func WithHeaderTags(headers []string) OptionFn

WithHeaderTags enables the integration to attach HTTP request headers as span tags. Warning: Using this feature can risk exposing sensitive data such as authorization tokens to Datadog. Special headers can not be sub-selected. E.g., an entire Cookie header would be transmitted, without the ability to choose specific Cookies.

func WithIgnoreRequest

func WithIgnoreRequest(f func(c *gin.Context) bool) OptionFn

WithIgnoreRequest specifies a function to use for determining if the incoming HTTP request tracing should be skipped.

func WithResourceNamer

func WithResourceNamer(namer func(c *gin.Context) string) OptionFn

WithResourceNamer specifies a function which will be used to obtain a resource name for a given gin request, using the request's context.

Jump to

Keyboard shortcuts

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