tracer

package
v0.0.0-...-6503e2e Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2022 License: MIT Imports: 7 Imported by: 0

README

Tracer

Example

Init tracer
func main() {
	err := tracer.Initialize(tracer.Config{
		ServiceName: "test-app",
		SamplerConfig: &jaegerConfig.SamplerConfig{
			Type:  "probabilistic",
			Param: 1,
		},
		ReporterConfig: &jaegerConfig.ReporterConfig{
			LogSpans:            true,
			BufferFlushInterval: 1 * time.Second,
			LocalAgentHostPort:  "localhost:6831",
		},
	})

	if err != nil {
		log.Fatal("Failed to init tracer")
	}
}

Using tracer
// Calculate dummy calculate
func Calculate(ctx context.Context) {
	span, ctxParent := tracer.StartSpanFromContext(ctx)
	defer span.Finish()

	SingleFunc(ctxParent)
	FuncWithSubFunc(ctxParent)
}


// SingleFunc single function without sub func
func SingleFunc(ctx context.Context) {
	span, _ := tracer.StartSpanFromContext(ctx)
	defer span.Finish()

	time.Sleep(250 * time.Millisecond)
}

// FuncWithSubFunc function with sub function
func FuncWithSubFunc(ctx context.Context) {
	span, ctxParent := tracer.StartSpanFromContext(ctx)
	defer span.Finish()

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		SubFunc1(ctxParent)
		wg.Done()
	}()

	wg.Add(1)
	go func() {
		SubFunc2(ctxParent)
		wg.Done()
	}()

	wg.Wait()
	time.Sleep(50 * time.Millisecond)
}

// SubFunc1 sub function
func SubFunc1(ctx context.Context) {
	span, _ := tracer.StartSpanFromContext(ctx)
	defer span.Finish()

	time.Sleep(300 * time.Millisecond)
}

// SubFunc2 sub function
func SubFunc2(ctx context.Context) {
	span, _ := tracer.StartSpanFromContext(ctx)
	defer span.Finish()

	time.Sleep(200 * time.Millisecond)
}

Result

Tracer Result

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize(c Config) (err error)

Initialize init tracer

func StartSpanFromContext

func StartSpanFromContext(ctx context.Context) (opentracing.Span, context.Context)

StartSpanFromContext create span with function name as default span name

func StartSpanWithName

func StartSpanWithName(ctx context.Context, operationName string) (opentracing.Span, context.Context)

StartSpanWithName to give span custom name

Types

type Config

type Config struct {
	ServiceName    string
	SamplerConfig  *jaegerConfig.SamplerConfig
	ReporterConfig *jaegerConfig.ReporterConfig
}

Config used to set jaeger config

Jump to

Keyboard shortcuts

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