tracing

package
v0.0.0-...-41c389d Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

README

Package Feature Overview

  • Jeager tracing middleware for echo web framework
  • Easy to tracing any component like MySQL, Redis, Mongo
  • Distributed context propagation

Example

package main

import (
	"net/http"

	"github.com/labstack/echo"
	"gitlab.innotechx.com/shm/go.common/database/sqlx"
	
	"gitlab.innotechx.com/shm/go.common/http/echo/middleware"
	"gitlab.innotechx.com/shm/go.common/tracing"
	"gitlab.innotechx.com/shm/go.common/http/apicall"
)

func main() {
	
	e := echo.New()
	
	serviceName := "example"
	endpoint := "http://example"
	
	// Init global tracer
	// SampleRate must in the range between 0.0 and 1.0
	// 1.0 for sample all requests, default is 0.001.
	
	if closer, err := tracing.InitGlobalTracer(serviceName, endpoint, tracing.DefaultSampleRate); err != nil {
		panic("init tracing failed: " + err.Error())
	} else {
		defer closer.Close()
	}

	// Install tracing middleware to echo web framework.
	e.Use(middleware.Tracing(serviceName))

	e.GET("/", hello)
	e.Logger.Fatal(e.Start(":1323"))
}

func hello(c echo.Context) error {
	
	// If you want to tracing your redis, start a span manually.
	sp := tracing.Redis().StartSpan(c.Request().Context())
	
	_, err = redisIns.HGetAll("device_user_map").Result()
	
	// Set any tag you like to the span, optional.
	sp.SetTag("example", "example")
	
	// Log something you like to the span, optional.
	sp.LogKV("server", "server")
	
	tracing.Redis().FinishSpan(sp, err)
	
	// finish span manually.
	return c.String(http.StatusOK, "Hello, World!")
}

func propagateTracing()  {
	
	// call any api with tracing, tracing enable by default.
	
    _ := apicall.Post(ctx, uri, nil, struct {}{}, &result)
    
}

func initMysqlDB() (sqlx.DB, error) {
	
	// Init your db here.
	
	// Enable MySQL tracing by default.
	
	return sqlx.WrapDB(db), nil
}

Help

Documentation

Index

Constants

View Source
const (
	DefaultSampleRate = 0.01
)

DefaultSampleRate is the default sample rate

Variables

This section is empty.

Functions

func ContextToHTTP

func ContextToHTTP(ctx context.Context, req *http.Request)

ContextToHTTP propagate tracing info into HTTP header, useful in server side.

func HTTPToContext

func HTTPToContext(ctx context.Context, req *http.Request,
	tracer opentracing.Tracer, operationName string) context.Context

HTTPToContext for client side service get tracing info from HTTP header.

func InitGlobalTracer

func InitGlobalTracer(serviceName, endpoint string, sampleRate float64) (io.Closer, error)

InitGlobalTracer init a global tracer from a given endpoint

Types

type GeneralSpan

type GeneralSpan struct {
}

GeneralSpan for non db tracing, useful in boot up goroutine. default is ChildOfRef reference type

func General

func General() *GeneralSpan

General for external package get GeneralSpan struct

func (GeneralSpan) FinishSpan

func (GeneralSpan) FinishSpan(span opentracing.Span, err error)

FinishSpan finish a span

func (GeneralSpan) StartSpan

func (GeneralSpan) StartSpan(ctx context.Context, options ...Option) opentracing.Span

StartSpan start a span for general purpose, default is ChildOfRef reference type

type MongoSpan

type MongoSpan struct{}

MongoSpan for span operation in mongo

func Mongo

func Mongo() *MongoSpan

Mongo for external package get MongoSpan struct

func (MongoSpan) FinishSpan

func (MongoSpan) FinishSpan(span opentracing.Span, err error)

FinishSpan finish a span after mongo operation

func (MongoSpan) StartSpan

func (MongoSpan) StartSpan(ctx context.Context) opentracing.Span

StartSpan start a span from context before any mongo operation

type Option

type Option func(span *options)

Option for GeneralSpan option

func WithChildOfSpan

func WithChildOfSpan() Option

WithChildOfSpan set ChildOfRef reference type to the span NOTE: the difference of ChildOfRef type and FollowsFromRef type is logically at present, same behavior in frontend presention

func WithFollowsFromSpan

func WithFollowsFromSpan() Option

WithFollowsFromSpan set FollowsFromRef reference type to the span Example usage : sp := tracing.General().StartSpan(ctx, tracing.WithFollowsFromSpan())

type RedisSpan

type RedisSpan struct{}

RedisSpan for span operation in redis

func Redis

func Redis() *RedisSpan

Redis for external package get RedisSpan struct

func (RedisSpan) FinishSpan

func (RedisSpan) FinishSpan(span opentracing.Span, err error)

FinishSpan finish a span after redis operation

func (RedisSpan) StartSpan

func (RedisSpan) StartSpan(ctx context.Context) opentracing.Span

StartSpan start a span from context before any redis operation

Jump to

Keyboard shortcuts

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