Categorygithub.com/openzipkin/zipkin-go
modulepackage
0.4.3
Repository: https://github.com/openzipkin/zipkin-go.git
Documentation: pkg.go.dev

# README

Zipkin Library for Go

GHA codecov Go Report Card GoDoc Gitter chat Sourcegraph

Zipkin Go is the official Go Tracer / Tracing implementation for Zipkin, supported by the OpenZipkin community.

package organization

zipkin-go is built with interoperability in mind within the OpenZipkin community and even 3rd parties, the library consists of several packages.

The main tracing implementation can be found in the root folder of this repository. Reusable parts not considered core implementation or deemed beneficiary for usage by others are placed in their own packages within this repository.

model

This library implements the Zipkin V2 Span Model which is available in the model package. It contains a Go data model compatible with the Zipkin V2 API and can automatically sanitize, parse and (de)serialize to and from the required JSON representation as used by the official Zipkin V2 Collectors.

propagation

The propagation package and B3 subpackage hold the logic for propagating SpanContext (span identifiers and sampling flags) between services participating in traces. Currently Zipkin B3 Propagation is supported for HTTP and GRPC.

middleware

The middleware subpackages contain officially supported middleware handlers and tracing wrappers.

http

An easy to use http.Handler middleware for tracing server side requests is provided. This allows one to use this middleware in applications using standard library servers as well as most available higher level frameworks. Some frameworks will have their own instrumentation and middleware that maps better for their ecosystem.

For HTTP client operations NewTransport can return a http.RoundTripper implementation that can either wrap the standard http.Client's Transport or a custom provided one and add per request tracing. Since HTTP Requests can have one or multiple redirects it is advisable to always enclose HTTP Client calls with a Span either around the *http.Client call level or parent function level.

For convenience NewClient is provided which returns a HTTP Client which embeds *http.Client and provides an application span around the HTTP calls when calling the DoWithAppSpan() method.

grpc

Easy to use grpc.StatsHandler middleware are provided for tracing gRPC server and client requests.

For a server, pass NewServerHandler when calling NewServer, e.g.,

import (
	"google.golang.org/grpc"
	zipkingrpc "github.com/openzipkin/zipkin-go/middleware/grpc"
)

server = grpc.NewServer(grpc.StatsHandler(zipkingrpc.NewServerHandler(tracer)))

For a client, pass NewClientHandler when calling Dial, e.g.,

import (
	"google.golang.org/grpc"
	zipkingrpc "github.com/openzipkin/zipkin-go/middleware/grpc"
)

conn, err = grpc.Dial(addr, grpc.WithStatsHandler(zipkingrpc.NewClientHandler(tracer)))

reporter

The reporter package holds the interface which the various Reporter implementations use. It is exported into its own package as it can be used by 3rd parties to use these Reporter packages in their own libraries for exporting to the Zipkin ecosystem. The zipkin-go tracer also uses the interface to accept 3rd party Reporter implementations.

HTTP Reporter

Most common Reporter type used by Zipkin users transporting Spans to the Zipkin server using JSON over HTTP. The reporter holds a buffer and reports to the backend asynchronously.

Kafka Reporter

High performance Reporter transporting Spans to the Zipkin server using a Kafka Producer digesting JSON V2 Spans. The reporter uses the Sarama async producer underneath.

Usage and Examples

HTTP Server Example

Go Support Policy

zipkin-go follows the same version policy as Go's Release Policy: two versions. zipkin-go will ensure these versions work and bugs are valid if there's an issue with a current Go version.

Additionally, zipkin-go intentionally delays usage of language or standard library features one additional version. For example, when Go 1.29 is released, zipkin-go can use language features or standard libraries added in 1.27. This is a convenience for embedders who have a slower version policy than Go. However, only supported Go versions may be used to raise support issues.

# Packages

No description provided by the author
Package idgenerator contains several Span and Trace ID generators which can be used by the Zipkin tracer.
No description provided by the author
Package model contains the Zipkin V2 model which is used by the Zipkin Go tracer implementation.
Package propagation holds the required function signatures for Injection and Extraction as used by the Zipkin Tracer.
No description provided by the author
Package reporter holds the Reporter interface which is used by the Zipkin Tracer to send finished spans.

# Functions

AlwaysSample will always return true.
BaggageFromContext takes a context and returns access to BaggageFields if available.
FlushOnFinish when set to false will disable span.Finish() to send the Span to the Reporter automatically (which is the default behavior).
IsNoop tells whether the span is noop or not.
Kind sets the kind of the span being created.
NeverSample will always return false.
NewBoundarySampler is appropriate for high-traffic instrumentation who provision random trace ids, and make the sampling decision only once.
NewContext stores a Zipkin Span into Go's context propagation mechanism.
NewCountingSampler is appropriate for low-traffic instrumentation or those who do not provision random trace ids.
NewEndpoint creates a new endpoint given the provided serviceName and hostPort.
NewModuloSampler provides a generic type Sampler.
NewTracer returns a new Zipkin Tracer.
Parent will use provided SpanContext as parent to the span being created.
RemoteEndpoint sets the remote endpoint of the span being created.
SpanFromContext retrieves a Zipkin Span from Go's context propagation mechanism if found.
SpanOrNoopFromContext retrieves a Zipkin Span from Go's context propagation mechanism if found.
StartTime uses a given start time for the span being created.
Tags sets initial tags for the span being created.
WithExtractFailurePolicy allows one to set the ExtractFailurePolicy.
WithIDGenerator allows one to set a custom ID Generator.
WithLocalEndpoint sets the local endpoint of the tracer.
WithNoopSpan if set to true will switch to a NoopSpan implementation if the trace is not sampled.
WithNoopTracer allows one to start the Tracer as Noop implementation.
WithSampler allows one to set a Sampler function.
WithSharedSpans allows to place client-side and server-side annotations for a RPC call in the same span (Zipkin V1 behavior) or different spans (more in line with other tracing solutions).
WithTags allows one to set default tags to be added to each created span.
WithTraceID128Bit if set to true will instruct the Tracer to start traces with 128 bit TraceID's.

# Constants

ExtractFailurePolicyOptions.
ExtractFailurePolicyOptions.
ExtractFailurePolicyOptions.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.
Common Tag values.

# Variables

Tracer Option Errors.
Tracer Option Errors.

# Structs

Tracer is our Zipkin tracer implementation.

# Interfaces

Span interface as returned by Tracer.StartSpan().

# Type aliases

ExtractFailurePolicy deals with Extraction errors.
Sampler functions return if a Zipkin span should be sampled, based on its traceID.
SpanOption allows for functional options to adjust behavior and payload of the Span to be created with tracer.StartSpan().
Tag holds available types.
TracerOption allows for functional options to adjust behavior of the Tracer to be created with NewTracer().