otelgin

package
v0.0.0-...-95754d6 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	OtelTraceContextKey = "opentelemetry.gin"
)

Variables

This section is empty.

Functions

func CaptureError

func CaptureError() gin.HandlerFunc

CaptureError captures error and panic to open-telemetry.

Example
package main

import (
	"github.com/gin-gonic/gin"

	otelgin "github.com/yeqown/opentelemetry-quake/x/gin"
)

func main() {
	r := gin.Default()

	r.Use(
		otelgin.CaptureError(),
	)
}
Output:

func ContextFrom

func ContextFrom(c *gin.Context) context.Context

func SpanFromContext

func SpanFromContext(c *gin.Context) trace.Span

SpanFromContext get the raw span from gin.Context.

func StartSpan

func StartSpan(c *gin.Context, op string, opts ...trace.SpanStartOption) (ctx context.Context, sp trace.Span)

StartSpan is a wrapper of opentelemetry.StartSpan, but it extracts span from gin.Context rather than context.Context. The return span is that derived by root span which is created by Tracing middleware.

func Tracing

func Tracing(config *Config) gin.HandlerFunc

Tracing creates a new otel.Tracer if never created and returns a gin.HandlerFunc. You only need to specify a CarrierFactory if your frontend doesn't obey TraceContext specification https://www.w3.org/TR/trace-context, otherwise you can leave it nil.

Example
package main

import (
	"github.com/gin-gonic/gin"

	sentryexporter "github.com/yeqown/opentelemetry-quake/sentryexporter"
	otelgin "github.com/yeqown/opentelemetry-quake/x/gin"
)

func main() {
	r := gin.Default()

	r.Use(
		otelgin.Tracing(otelgin.DefaultConfig()),
	)

	// sentry trace header
	r.Use(
		otelgin.Tracing(&otelgin.Config{
			Factory:     sentryexporter.CarrierFactory,
			LogRequest:  true,
			LogResponse: true,
		}),
	)
}
Output:

Types

type CarrierFactory

type CarrierFactory func(h http.Header) propagation.TextMapCarrier

CarrierFactory is a factory function to tell Tracing middleware how to fill the TraceContext from carrier. Checkout pkg/exporter/sentry/adaptor.go#CarrierFactory for detail.

type Config

type Config struct {
	Factory                 CarrierFactory
	LogRequest, LogResponse bool
}

Config helps user to control Tracing middleware about how to handle the request and response. Such as: - log the request and response body or not, - how to extract TraceContext from request.

func DefaultConfig

func DefaultConfig() *Config

func (*Config) ApplyCarrierFactory

func (c *Config) ApplyCarrierFactory(factory CarrierFactory) *Config

func (*Config) EnableLogPayloads

func (c *Config) EnableLogPayloads() *Config

Jump to

Keyboard shortcuts

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