Skip to content

Commit 5c2f246

Browse files
authored
Merge pull request #5 from Vinelab/fault-tolerance
Ensure library is fault tolerant
2 parents c99cd9c + fdf7dc0 commit 5c2f246

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

drivers/zipkin/tracer.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package zipkin
33
import (
44
"errors"
55
"fmt"
6+
"log"
67
"net"
8+
"time"
79

810
"github.com/Vinelab/tracing-go"
911
"github.com/Vinelab/tracing-go/formats"
@@ -14,11 +16,14 @@ import (
1416
httpreporter "github.com/openzipkin/zipkin-go/reporter/http"
1517
)
1618

17-
var (
19+
const (
1820
// MaxTagLen controls the maximum size of tag value in bytes
19-
// Defaults to 1048576 bytes (1MB)
2021
MaxTagLen = 1048576
22+
// DefaultRequestTimeout sets maximum timeout for http request to send spans
23+
DefaultRequestTimeout = time.Second * 5
24+
)
2125

26+
var (
2227
// ErrCollectorIPNotFound is returned if you used hostname in place of collector IP
2328
// and we weren't able to resolve the valid address from it
2429
ErrCollectorIPNotFound = errors.New("unable to resolve collector's IP address")
@@ -52,22 +57,35 @@ type TracerOptions struct {
5257
// Reporter option allows to inject your own reporter for tests
5358
// Defaults to http reporter
5459
Reporter reporter.Reporter
60+
// Timeout sets maximum timeout for http request to send spans
61+
// Setting this to a too high value is not recommended because it
62+
// may degrade your system performance when collector is down.
63+
// Note that reporter will re-try after the first failure.
64+
// See this issue for more details: https://github.com/openzipkin/zipkin-go/issues/147
65+
RequestTimeout time.Duration
5566
}
5667

5768
// NewTracer returns a new Zipkin tracer.
5869
func NewTracer(opt TracerOptions) (*Tracer, error) {
5970
ipAddr, err := resolveCollectorIP(opt.Host)
6071
if err != nil {
61-
return nil, err
72+
log.Printf("Unable to resolve collector's IP address from hostname %s: %s", opt.Host, err.Error())
6273
}
6374
opt.Host = ipAddr
6475

76+
var timeout time.Duration
77+
if opt.RequestTimeout != 0 {
78+
timeout = opt.RequestTimeout
79+
} else {
80+
timeout = DefaultRequestTimeout
81+
}
82+
6583
var rep reporter.Reporter
6684
if opt.Reporter != nil {
6785
rep = opt.Reporter
6886
} else {
6987
url := fmt.Sprintf("http://%s:%s/api/v2/spans", opt.Host, opt.Port)
70-
rep = httpreporter.NewReporter(url)
88+
rep = httpreporter.NewReporter(url, httpreporter.Timeout(timeout))
7189
}
7290

7391
endpoint, err := openzipkin.NewEndpoint(opt.ServiceName, fmt.Sprintf("%s:%s", opt.Host, opt.Port))
@@ -229,10 +247,10 @@ func resolveCollectorIP(host string) (string, error) {
229247

230248
ips, err := net.LookupIP(host)
231249
if err != nil {
232-
return host, err
250+
return "127.0.0.1", err
233251
}
234252

235-
for _, ip := range ips {
253+
for _, ip := range ips {
236254
if ip.IsLoopback() {
237255
if ip.To4() != nil {
238256
return ip.String(), nil
@@ -244,7 +262,7 @@ func resolveCollectorIP(host string) (string, error) {
244262
return ip.String(), nil
245263
}
246264

247-
return "", ErrCollectorIPNotFound
265+
return "127.0.0.1", ErrCollectorIPNotFound
248266
}
249267

250268
func registerDefaultExtractionFormats() map[string]tracing.Extractor {

0 commit comments

Comments
 (0)