tracer

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 13 Imported by: 0

README

tracer

Tracer library wrapped in go.opentelemetry.io/otel.


Example of use

Initialize the trace, specifying exporter and resource.

import "github.com/liuzw3018/sponge/pkg/tracer"

func initTrace() {
	// exporter := tracer.NewConsoleExporter() // output to terminal

	// exporter, f, err := tracer.NewFileExporter("trace.json") // output to file

	// exporter, err := tracer.NewJaegerExporter("http://localhost:14268/api/traces") // output to jaeger, using collector http
	exporter, err := tracer.NewJaegerAgentExporter("192.168.3.37", "6831") // output to jaeger, using agent udp

	resource := tracer.NewResource(
		tracer.WithServiceName("your-service-name"),
		tracer.WithEnvironment("dev"),
		tracer.WithServiceVersion("demo"),
	)

	tracer.Init(exporter, resource) // collect all by default
	// tracer.Init(exporter, resource, 0.5) // collect half
}

Create a span in the program with ctx derived from the previous parent span.

	_, span := otel.Tracer(serviceName).Start(
		ctx,
		spanName,
		trace.WithAttributes(attribute.String("foo", "bar")), // customised attributes
	)
	defer span.End()

	// ......

documents https://opentelemetry.io/docs/instrumentation/go/

support OpenTelemetry in other libraries https://opentelemetry.io/registry/?language=go&component=instrumentation

Documentation

Overview

Package tracer is a library wrapped in go.opentelemetry.io/otel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(ctx context.Context) error

Close tracer

func Init

func Init(exporter trace.SpanExporter, res *resource.Resource, fractions ...float64)

Init Initialize tracer, parameter fraction is fraction, default is 1.0, value >= 1.0 means all links are sampled, value <= 0 means all are not sampled, 0 < value < 1 only samples percentage

func InitWithConfig

func InitWithConfig(appName string, appEnv string, appVersion string,
	jaegerAgentHost string, jaegerAgentPort string, jaegerSamplingRate float64)

InitWithConfig Initialize tracer according to configuration, fraction is fraction, default is 1.0, value >= 1.0 means all links are sampled, value <= 0 means all are not sampled, 0 < value < 1 only samples percentage

func NewConsoleExporter

func NewConsoleExporter() (sdkTrace.SpanExporter, error)

NewConsoleExporter output to console

func NewFileExporter

func NewFileExporter(filename string) (sdkTrace.SpanExporter, *os.File, error)

NewFileExporter output to file, note: close the file before ending

func NewJaegerAgentExporter

func NewJaegerAgentExporter(host string, port string) (sdkTrace.SpanExporter, error)

NewJaegerAgentExporter use jaeger agent as exporter, e.g. host=localhost port=6831

func NewJaegerExporter

func NewJaegerExporter(url string, opts ...JaegerOption) (sdkTrace.SpanExporter, error)

NewJaegerExporter use jaeger collector as exporter, e.g. default url=http://localhost:14268/api/traces

func NewResource

func NewResource(opts ...ResourceOption) *resource.Resource

NewResource returns a resource describing this application.

func NewSpan

func NewSpan(ctx context.Context, spanName string, tags map[string]interface{}) (context.Context, trace.Span)

NewSpan create a span, to end a span you must call span.End()

func SetTraceName

func SetTraceName(name string)

SetTraceName each service corresponds to a traceName

Types

type JaegerOption

type JaegerOption func(*jaegerOptions)

JaegerOption set fields

func WithPassword

func WithPassword(password string) JaegerOption

WithPassword set password

func WithUsername

func WithUsername(username string) JaegerOption

WithUsername set username

type ResourceOption

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

ResourceOption modifying struct field values by means of an interface

func WithAttributes

func WithAttributes(attributes map[string]string) ResourceOption

WithAttributes set service attributes

func WithEnvironment

func WithEnvironment(environment string) ResourceOption

WithEnvironment set service environment

func WithServiceName

func WithServiceName(name string) ResourceOption

WithServiceName set service name

func WithServiceVersion

func WithServiceVersion(version string) ResourceOption

WithServiceVersion set service version

Jump to

Keyboard shortcuts

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