ginopentracing

package module
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

README

go-gin-opentracing

Go Report Card Release

OpenTracing middleware from the Gin http framework.

Based on opentracing-go, this middleware adds an OpenTracing span for every http request, using the inbound requestID (if one exists) and adds the request span to the gin.Context as the key: tracing-context.

Required Reading

In order to understand the Go platform API, one must first be familiar with the OpenTracing project and terminology more specifically.

Installation

$ go get github.com/zgcom/go-gin-opentracing

Usage as middleware

package main

import (
	"fmt"
	"os"

	ginopentracing "github.com/Bose/go-gin-opentracing"
	"github.com/gin-gonic/gin"
	"github.com/opentracing/opentracing-go"
)

func main() {
	r := gin.Default()
	hostName, err := os.Hostname()
	if err != nil {
		hostName = "unknown"
	}
	// initialize the global singleton for tracing...
	tracer, reporter, closer, err := ginopentracing.InitTracing(fmt.Sprintf("go-gin-opentracing-example::%s", hostName), "localhost:5775", ginopentracing.WithEnableInfoLog(true))
	if err != nil {
		panic("unable to init tracing")
	}
	defer closer.Close()
	defer reporter.Close()
	opentracing.SetGlobalTracer(tracer)

	// create the middleware
	p := ginopentracing.OpenTracer([]byte("api-request-"))

	// tell gin to use the middleware
	r.Use(p)

	r.GET("/", func(c *gin.Context) {
		c.JSON(200, "Hello world!")
	})

	r.Run(":29090")
}

See the example.go file

Usage within gin.Handler

Within the gin Handler you can access this request span and use it to create additional spans, and/or add span tags.

func HelloWorld(c *gin.Context) {
	if cspan, ok := c.Get("tracing-context"); ok {
		span = tracing.StartSpanWithParent(cspan.(opentracing.Span).Context(), "helloword", c.Request.Method, c.Request.URL.Path)	
	} else {
		span = tracing.StartSpanWithHeader(&c.Request.Header, "helloworld", c.Request.Method, c.Request.URL.Path)
	}
	defer span.Finish()
	c.String(200, "Hello.")
}

Documentation

Index

Constants

This section is empty.

Variables

Config - the open tracing config singleton

Functions

func InitDevelopment

func InitDevelopment(tracingAgentHostPort []byte)

InitDevelopment - DEPRECATED (uses deprecated jaeger client func) init a production tracer environment example: Create the default tracer and schedule its closing when main returns.

   func main() {
 tracing.InitDevelopment() # defaults to "localhost:6831" for tracing agent
	tracer, closer, _ := tracing.Config.New("passport-gigya-user-access") // the service name is the param to New()
	defer closer.Close()
	opentracing.SetGlobalTracer(tracer)

func InitMacDocker

func InitMacDocker(tracingAgentHostPort []byte)

InitMacDocker - DEPRECATED (uses deprecated jaeger client func) init a production tracer environment example: Create the default tracer and schedule its closing when main returns.

   func main() {
 tracing.InitMacDocker() # defaults to "host.docker.internal:6831 for tracing agent
	tracer, closer, _ := tracing.Config.New("passport-gigya-user-access") // the service name is the param to New()
	defer closer.Close()
	opentracing.SetGlobalTracer(tracer)

func InitProduction

func InitProduction(sampleProbability float64, tracingAgentHostPort []byte)

InitProduction - DEPRECATED (uses deprecated jaeger client func) init a production tracer environment example: Create the default tracer and schedule its closing when main returns.

   func main() {
	tracing.InitProduction("jaegeragent.svc.cluster.local:6831")
	tracer, closer, _ := tracing.Config.New("passport-gigya-user-access") // the service name is the param to New()
	defer closer.Close()
	opentracing.SetGlobalTracer(tracer)

func InitTracing

func InitTracing(serviceName string, tracingAgentHostPort string, opt ...Option) (
	tracer opentracing.Tracer,
	reporter jaeger.Reporter,
	closer io.Closer,
	err error)

InitTracing - init opentracing with options (WithSampleProbability, WithEnableInfoLog) defaults: constant sampling, no info logging

func InjectTraceID

func InjectTraceID(ctx opentracing.SpanContext, header http.Header)

InjectTraceID injects the span ID into the provided HTTP header object, so that the current span will be propogated downstream to the server responding to an HTTP request. Specifying the span ID in this way will allow the tracing system to connect spans between servers.

 Usage:
         // resty example
	    r := resty.R()
	    injectTraceID(span, r.Header)
	    resp, err := r.Get(fmt.Sprintf("http://localhost:8000/users/%s", bosePersonID))

         // galapagos_clients example
         c := galapagos_clients.GetHTTPClient()
         req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:8000/users/%s", bosePersonID))
         injectTraceID(span, req.Header)
         c.Do(req)

func OpenTracer

func OpenTracer(operationPrefix []byte) gin.HandlerFunc

OpenTracer - middleware that addes opentracing

func StartDBSpanWithParent

func StartDBSpanWithParent(parent opentracing.SpanContext, operationName, dbInstance, dbType, dbStatement string) opentracing.Span

StartDBSpanWithParent - start a DB operation span

func StartSpan

func StartSpan(operationName, method, path string) opentracing.Span

StartSpan will start a new span with no parent span.

func StartSpanWithHeader

func StartSpanWithHeader(header *http.Header, operationName, method, path string) opentracing.Span

StartSpanWithHeader will look in the headers to look for a parent span before starting the new span. example:

func handleGet(c *gin.Context) {
   span := StartSpanWithHeader(&c.Request.Header, "api-request", method, path)
   defer span.Finish()
   c.Set("tracing-context", span) // add the span to the context so it can be used for the duration of the request.
   bosePersonID := c.Param("bosePersonID")
   span.SetTag("bosePersonID", bosePersonID)

func StartSpanWithParent

func StartSpanWithParent(parent opentracing.SpanContext, operationName, method, path string) opentracing.Span

StartSpanWithParent will start a new span with a parent span. example:

span:= StartSpanWithParent(c.Get("tracing-context"),

Types

type LogrusAdapter

type LogrusAdapter struct {
	InfoLevel bool
}

LogrusAdapter - an adapter to log span info

func (LogrusAdapter) Error

func (l LogrusAdapter) Error(msg string)

Error - logrus adapter for span errors

func (LogrusAdapter) Infof

func (l LogrusAdapter) Infof(msg string, args ...interface{})

Infof - logrus adapter for span info logging

type Option

type Option func(*options)

Option - define options for NewJWTCache()

func WithEnableInfoLog

func WithEnableInfoLog(enable bool) Option

WithEnableInfoLog - optional: enable Info logging for tracing

func WithSampleProbability

func WithSampleProbability(sampleProbability float64) Option

WithSampleProbability - optional sample probability

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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