# README
tracer
Tracer library wrapped in go.opentelemetry.io/otel.
Example of use
Initialize the trace, specifying exporter and resource.
import "github.com/go-dev-frame/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
# Functions
Close tracer.
GetProvider get tracer provider.
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.
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.
NewConsoleExporter output to console.
NewFileExporter output to file, note: close the file before ending.
NewJaegerAgentExporter use jaeger agent as exporter, e.g.
NewJaegerExporter use jaeger collector as exporter, e.g.
NewResource returns a resource describing this application.
NewSpan create a span, to end a span you must call span.End().
SetTraceName each service corresponds to a traceName.
WithAttributes set service attributes.
WithEnvironment set service environment.
WithPassword set password.
WithServiceName set service name.
WithServiceVersion set service version.
WithUsername set username.
# Interfaces
ResourceOption modifying struct field values by means of an interface.
# Type aliases
JaegerOption set fields.