Commit 9b6597b2 authored by ale's avatar ale

Upgrade go-common (add tracing)

parent 9906d614
Pipeline #1587 passed with stages
in 1 minute and 20 seconds
......@@ -104,7 +104,10 @@ func (b *balancedBackend) Call(ctx context.Context, shard, path string, req, res
}
var tg targetGenerator = b.backendTracker
if b.sharded && shard != "" {
if b.sharded {
if shard == "" {
return fmt.Errorf("call without shard to sharded service %s", b.baseURI.String())
}
tg = newShardedGenerator(shard, b.baseURI.Host, b.resolver)
}
seq := newSequence(tg)
......
......@@ -7,6 +7,8 @@ import (
"net/http"
"sync"
"time"
"git.autistici.org/ai3/go-common/tracing"
)
// The transportCache is just a cache of http transports, each
......@@ -29,12 +31,12 @@ func newTransportCache(tlsConfig *tls.Config) *transportCache {
}
func (m *transportCache) newTransport(addr string) http.RoundTripper {
return &http.Transport{
return tracing.WrapTransport(&http.Transport{
TLSClientConfig: m.tlsConfig,
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) {
return netDialContext(ctx, network, addr)
},
}
})
}
func (m *transportCache) getTransport(addr string) http.RoundTripper {
......
......@@ -3,16 +3,18 @@ package serverutil
import (
"context"
"crypto/tls"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/pprof"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"
"time"
"git.autistici.org/ai3/go-common/tracing"
"github.com/coreos/go-systemd/daemon"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
......@@ -77,6 +79,10 @@ func (config *ServerConfig) buildHTTPServer(h http.Handler) (*http.Server, error
// the listener, otherwise it will handle graceful termination on
// SIGINT or SIGTERM and return nil.
func Serve(h http.Handler, config *ServerConfig, addr string) error {
// Wrap with tracing handler (exclude metrics and other
// debugging endpoints).
h = tracing.WrapHandler(h, guessEndpointName(addr))
// Create the HTTP server.
srv, err := config.buildHTTPServer(h)
if err != nil {
......@@ -139,8 +145,10 @@ func defaultHandler(h http.Handler) http.Handler {
// Add an endpoint to serve Prometheus metrics.
root.Handle("/metrics", promhttp.Handler())
// Add the net/http/pprof debug handlers.
root.Handle("/debug/pprof/", pprof.Handler(""))
// Let the default net/http handler deal with /debug/
// URLs. Packages such as net/http/pprof register their
// handlers there in ways that aren't reproducible.
root.Handle("/debug/", http.DefaultServeMux)
// Forward everything else to the main handler, adding
// Prometheus instrumentation (requests to /metrics and
......@@ -151,6 +159,18 @@ func defaultHandler(h http.Handler) http.Handler {
return root
}
func guessEndpointName(addr string) string {
_, port, err := net.SplitHostPort(addr)
if err != nil {
return addr
}
host, err := os.Hostname()
if err != nil {
return addr
}
return fmt.Sprintf("%s:%s", host, port)
}
// HTTP-related metrics.
var (
// Since we instrument the root HTTP handler, we don't really
......
package tracing
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"sync"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinHTTP "github.com/openzipkin/zipkin-go/reporter/http"
"go.opencensus.io/exporter/zipkin"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/trace"
)
var (
// Enabled reports whether tracing is globally enabled or not.
Enabled bool
// The active tracing configuration, if Enabled is true.
config tracingConfig
initOnce sync.Once
)
const globalTracingConfigPath = "/etc/tracing/client.conf"
type tracingConfig struct {
ReportURL string `json:"report_url"`
}
// Read the global tracing configuration file. Its location is
// hardcoded, but it can be overriden using the TRACING_CONFIG
// environment variable.
func readTracingConfig() error {
// Read and decode configuration.
cfgPath := globalTracingConfigPath
if s := os.Getenv("TRACING_CONFIG"); s != "" {
cfgPath = s
}
data, err := ioutil.ReadFile(cfgPath)
if err != nil {
return err
}
if err := json.Unmarshal(data, &config); err != nil {
log.Printf("warning: error in tracing configuration: %v, tracing disabled", err)
return err
}
if config.ReportURL == "" {
log.Printf("warning: tracing configuration contains no report_url, tracing disabled")
return errors.New("no report_url")
}
return nil
}
// Compute the service name for Zipkin: this is usually the program
// name (without path), but it can be overriden by the TRACING_SERVICE
// environment variable.
func getServiceName() string {
if s := os.Getenv("TRACING_SERVICE"); s != "" {
return s
}
return filepath.Base(os.Args[0])
}
// Initialize tracing. Tracing will be enabled if the system-wide
// tracing configuration file is present and valid. Explicitly set
// TRACING_ENABLE=0 in the environment to disable tracing.
//
// We need to check the configuration as soon as possible, because
// it's likely that client transports are created before HTTP servers,
// and we need to wrap them with opencensus at creation time.
func init() {
// Kill switch from environment.
if s := os.Getenv("TRACING_ENABLE"); s == "0" {
return
}
if err := readTracingConfig(); err != nil {
return
}
Enabled = true
}
func initTracing(endpointAddr string) {
initOnce.Do(func() {
localEndpoint, err := openzipkin.NewEndpoint(getServiceName(), endpointAddr)
if err != nil {
log.Printf("warning: error creating tracing endpoint: %v, tracing disabled", err)
return
}
reporter := zipkinHTTP.NewReporter(config.ReportURL)
ze := zipkin.NewExporter(reporter, localEndpoint)
trace.RegisterExporter(ze)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
log.Printf("tracing enabled (report_url %s)", config.ReportURL)
Enabled = true
})
}
// WrapTransport optionally wraps a http.RoundTripper with OpenCensus
// tracing functionality, if it is globally enabled.
func WrapTransport(t http.RoundTripper) http.RoundTripper {
if Enabled {
t = &ochttp.Transport{Base: t}
}
return t
}
// WrapHandler wraps a http.Handler with OpenCensus tracing
// functionality, if globally enabled.
func WrapHandler(h http.Handler, endpointAddr string) http.Handler {
if Enabled {
initTracing(endpointAddr)
h = &ochttp.Handler{Handler: h}
}
return h
}
......@@ -61,6 +61,12 @@ attributes:
* `cert`: path to the client certificate
* `key`: path to the private key
* `ca`: path to the CA used to validate the server
* `keystore_enable_groups`: (a list) if set, the keystore will only be
enabled for users that are members of these groups
* `u2f_app_id`: set the U2F AppID - if unset, it will be autodetected
based on the domain name in the request
* `url_path_prefix`: URL path prefix of the SSO server application
(default /)
* `http_server`: specifies standard parameters for the HTTP server
* `tls`: server-side TLS configuration
* `cert`: path to the server certificate
......
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="2" failures="0" time="0.000" name="git.autistici.org/id/go-sso">
<properties>
<property name="go.version" value="go1.10.3"></property>
</properties>
<testcase classname="go-sso" name="TestEd25519" time="0.000"></testcase>
<testcase classname="go-sso" name="TestLegacy" time="0.000"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0.000" name="git.autistici.org/id/go-sso/cmd/sso-server">
<properties>
<property name="go.version" value="go1.10.3"></property>
</properties>
<testcase classname="sso-server" name="TestMain_LoadConfig" time="0.000"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0.000" name="git.autistici.org/id/go-sso/httpsso">
<properties>
<property name="go.version" value="go1.10.3"></property>
</properties>
<testcase classname="httpsso" name="TestSSOWrapper" time="0.010"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0.000" name="git.autistici.org/id/go-sso/httputil">
<properties>
<property name="go.version" value="go1.10.3"></property>
</properties>
<testcase classname="httputil" name="TestExpiringSession" time="0.000"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0.000" name="git.autistici.org/id/go-sso/proxy">
<properties>
<property name="go.version" value="go1.10.3"></property>
</properties>
<testcase classname="proxy" name="TestProxy" time="0.010"></testcase>
</testsuite>
<testsuite tests="6" failures="0" time="0.000" name="git.autistici.org/id/go-sso/server">
<properties>
<property name="go.version" value="go1.10.3"></property>
</properties>
<testcase classname="server" name="TestHTTP_ServeStaticAsset" time="0.010"></testcase>
<testcase classname="server" name="TestHTTP_Login" time="0.000"></testcase>
<testcase classname="server" name="TestHTTP_LoginOTP" time="0.000"></testcase>
<testcase classname="server" name="TestHTTP_LoginWithKeyStore" time="0.010"></testcase>
<testcase classname="server" name="TestLoginService_Ok" time="0.000"></testcase>
<testcase classname="server" name="TestLoginService_SanityChecks" time="0.000"></testcase>
</testsuite>
</testsuites>
This diff is collapsed.
.DEFAULT_GOAL := test
.PHONY: test
test:
go test -v -race -cover ./...
.PHONY: bench
bench:
go test -v -run - -bench . -benchmem ./...
.PHONY: protoc
protoc:
protoc --go_out=. proto/v2/zipkin.proto
.PHONY: lint
lint:
# Ignore grep's exit code since no match returns 1.
-if [[ ! $TRAVIS_GO_VERSION = 1.8* ]]; then echo 'linting...' ; golint ./... ; fi
.PHONY: vet
vet:
go vet ./...
.PHONY: all
all: vet lint test bench
.PHONY: example
# Zipkin Library for Go
[![Travis CI](https://travis-ci.org/openzipkin/zipkin-go.svg?branch=master)](https://travis-ci.org/openzipkin/zipkin-go)
[![CircleCI](https://circleci.com/gh/openzipkin/zipkin-go.svg?style=shield)](https://circleci.com/gh/openzipkin/zipkin-go)
[![Appveyor CI](https://ci.appveyor.com/api/projects/status/1d0e5k96g10ajl63/branch/master?svg=true)](https://ci.appveyor.com/project/basvanbeek/zipkin-go)
[![Coverage Status](https://img.shields.io/coveralls/github/openzipkin/zipkin-go.svg)](https://coveralls.io/github/openzipkin/zipkin-go?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/openzipkin/zipkin-go)](https://goreportcard.com/report/github.com/openzipkin/zipkin-go)
[![GoDoc](https://godoc.org/github.com/openzipkin/zipkin-go?status.svg)](https://godoc.org/github.com/openzipkin/zipkin-go)
[![Gitter chat](https://badges.gitter.im/openzipkin/zipkin.svg)](https://gitter.im/openzipkin/zipkin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Sourcegraph](https://sourcegraph.com/github.com/openzipkin/zipkin-go/-/badge.svg)](https://sourcegraph.com/github.com/openzipkin/zipkin-go?badge)
Zipkin Go is the official Go Tracer 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
gRPC middleware / interceptors are planned for the near future.
### 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](https://godoc.org/github.com/Shopify/sarama#AsyncProducer)
underneath.
## usage and examples
[HTTP Server Example](example_httpserver_test.go)
version: v1.0.0.{build}
platform: x64
clone_folder: c:\gopath\src\github.com\openzipkin\zipkin-go
environment:
GOPATH: c:\gopath
install:
- echo %PATH%
- echo %GOPATH%
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
- go version
- go env
build_script:
- go get -t -v ./...
- go vet ./...
- go test -v -race -cover ./...
- go test -v -run - -bench . -benchmem ./...
version: 2
jobs:
build:
working_directory: /go/src/github.com/openzipkin/zipkin-go
parallelism: 1
docker:
- image: circleci/golang
steps:
- checkout
- run: go get -t -v -d ./...
- run: make vet test bench
package zipkin
import (
"context"
)
// SpanFromContext retrieves a Zipkin Span from Go's context propagation
// mechanism if found. If not found, returns nil.
func SpanFromContext(ctx context.Context) Span {
if s, ok := ctx.Value(spanKey).(Span); ok {
return s
}
return nil
}
// NewContext stores a Zipkin Span into Go's context propagation mechanism.
func NewContext(ctx context.Context, s Span) context.Context {
return context.WithValue(ctx, spanKey, s)
}
type ctxKey struct{}
var spanKey = ctxKey{}
/*
Package zipkin implements a native Zipkin instrumentation library for Go.
See https://zipkin.io for more information about Zipkin.
*/
package zipkin
package zipkin
import (
"net"
"strconv"
"strings"
"github.com/openzipkin/zipkin-go/model"
)
// NewEndpoint creates a new endpoint given the provided serviceName and
// hostPort.
func NewEndpoint(serviceName string, hostPort string) (*model.Endpoint, error) {
e := &model.Endpoint{
ServiceName: serviceName,
}
if hostPort == "" || hostPort == ":0" {
if serviceName == "" {
// if all properties are empty we should not have an Endpoint object.
return nil, nil
}
return e, nil
}
if strings.IndexByte(hostPort, ':') < 0 {
hostPort += ":0"
}
host, port, err := net.SplitHostPort(hostPort)
if err != nil {
return nil, err
}
p, err := strconv.ParseUint(port, 10, 16)
if err != nil {
return nil, err
}
e.Port = uint16(p)
addrs, err := net.LookupIP(host)
if err != nil {
return nil, err
}
for i := range addrs {
addr := addrs[i].To4()
if addr == nil {
// IPv6 - 16 bytes
if e.IPv6 == nil {
e.IPv6 = addrs[i].To16()
}
} else {
// IPv4 - 4 bytes
if e.IPv4 == nil {
e.IPv4 = addr
}
}
if e.IPv4 != nil && e.IPv6 != nil {
// Both IPv4 & IPv6 have been set, done...
break
}
}
return e, nil
}
/*
Package idgenerator contains several Span and Trace ID generators which can be
used by the Zipkin tracer. Additional third party generators can be plugged in
if they adhere to the IDGenerator interface.
*/
package idgenerator
import (
"math/rand"
"sync"
"time"
"github.com/openzipkin/zipkin-go/model"
)
var (
seededIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
// NewSource returns a new pseudo-random Source seeded with the given value.
// Unlike the default Source used by top-level functions, this source is not
// safe for concurrent use by multiple goroutines. Hence the need for a mutex.
seededIDLock sync.Mutex
)
// IDGenerator interface can be used to provide the Zipkin Tracer with custom
// implementations to generate Span and Trace IDs.
type IDGenerator interface {
SpanID(traceID model.TraceID) model.ID // Generates a new Span ID
TraceID() model.TraceID // Generates a new Trace ID
}
// NewRandom64 returns an ID Generator which can generate 64 bit trace and span
// id's
func NewRandom64() IDGenerator {
return &randomID64{}
}
// NewRandom128 returns an ID Generator which can generate 128 bit trace and 64
// bit span id's
func NewRandom128() IDGenerator {
return &randomID128{}
}
// NewRandomTimestamped generates 128 bit time sortable traceid's and 64 bit
// spanid's.
func NewRandomTimestamped() IDGenerator {
return &randomTimestamped{}
}
// randomID64 can generate 64 bit traceid's and 64 bit spanid's.
type randomID64 struct{}
func (r *randomID64) TraceID() (id model.TraceID) {
seededIDLock.Lock()
id = model.TraceID{
Low: uint64(seededIDGen.Int63()),
}
seededIDLock.Unlock()
return
}
func (r *randomID64) SpanID(traceID model.TraceID) (id model.ID) {
if !traceID.Empty() {
return model.ID(traceID.Low)
}
seededIDLock.Lock()
id = model.ID(seededIDGen.Int63())
seededIDLock.Unlock()
return
}
// randomID128 can generate 128 bit traceid's and 64 bit spanid's.
type randomID128 struct{}
func (r *randomID128) TraceID() (id model.TraceID) {
seededIDLock.Lock()
id = model.TraceID{
High: uint64(seededIDGen.Int63()),
Low: uint64(seededIDGen.Int63()),
}
seededIDLock.Unlock()
return
}
func (r *randomID128) SpanID(traceID model.TraceID) (id model.ID) {
if !traceID.Empty() {
return model.ID(traceID.Low)
}
seededIDLock.Lock()
id = model.ID(seededIDGen.Int63())
seededIDLock.Unlock()
return
}
// randomTimestamped can generate 128 bit time sortable traceid's compatible
// with AWS X-Ray and 64 bit spanid's.
type randomTimestamped struct{}
func (t *randomTimestamped) TraceID() (id model.TraceID) {
seededIDLock.Lock()
id = model.TraceID{
High: uint64(time.Now().Unix()<<32) + uint64(seededIDGen.Int31()),
Low: uint64(seededIDGen.Int63()),
}
seededIDLock.Unlock()
return
}
func (t *randomTimestamped) SpanID(traceID model.TraceID) (id model.ID) {
if !traceID.Empty() {
return model.ID(traceID.Low)
}
seededIDLock.Lock()
id = model.ID(seededIDGen.Int63())
seededIDLock.Unlock()
return
}
package model
import (
"encoding/json"
"errors"
"time"
)
// ErrValidTimestampRequired error
var ErrValidTimestampRequired = errors.New("valid annotation timestamp required")
// Annotation associates an event that explains latency with a timestamp.
type Annotation struct {
Timestamp time.Time
Value string
}
// MarshalJSON implements custom JSON encoding
func (a *Annotation) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Timestamp int64 `json:"timestamp"`