@@ -3,7 +3,9 @@ package zipkin
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "log"
6
7
"net"
8
+ "time"
7
9
8
10
"github.com/Vinelab/tracing-go"
9
11
"github.com/Vinelab/tracing-go/formats"
@@ -14,11 +16,14 @@ import (
14
16
httpreporter "github.com/openzipkin/zipkin-go/reporter/http"
15
17
)
16
18
17
- var (
19
+ const (
18
20
// MaxTagLen controls the maximum size of tag value in bytes
19
- // Defaults to 1048576 bytes (1MB)
20
21
MaxTagLen = 1048576
22
+ // DefaultRequestTimeout sets maximum timeout for http request to send spans
23
+ DefaultRequestTimeout = time .Second * 5
24
+ )
21
25
26
+ var (
22
27
// ErrCollectorIPNotFound is returned if you used hostname in place of collector IP
23
28
// and we weren't able to resolve the valid address from it
24
29
ErrCollectorIPNotFound = errors .New ("unable to resolve collector's IP address" )
@@ -52,22 +57,35 @@ type TracerOptions struct {
52
57
// Reporter option allows to inject your own reporter for tests
53
58
// Defaults to http reporter
54
59
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
55
66
}
56
67
57
68
// NewTracer returns a new Zipkin tracer.
58
69
func NewTracer (opt TracerOptions ) (* Tracer , error ) {
59
70
ipAddr , err := resolveCollectorIP (opt .Host )
60
71
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 ())
62
73
}
63
74
opt .Host = ipAddr
64
75
76
+ var timeout time.Duration
77
+ if opt .RequestTimeout != 0 {
78
+ timeout = opt .RequestTimeout
79
+ } else {
80
+ timeout = DefaultRequestTimeout
81
+ }
82
+
65
83
var rep reporter.Reporter
66
84
if opt .Reporter != nil {
67
85
rep = opt .Reporter
68
86
} else {
69
87
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 ) )
71
89
}
72
90
73
91
endpoint , err := openzipkin .NewEndpoint (opt .ServiceName , fmt .Sprintf ("%s:%s" , opt .Host , opt .Port ))
@@ -229,10 +247,10 @@ func resolveCollectorIP(host string) (string, error) {
229
247
230
248
ips , err := net .LookupIP (host )
231
249
if err != nil {
232
- return host , err
250
+ return "127.0.0.1" , err
233
251
}
234
252
235
- for _ , ip := range ips {
253
+ for _ , ip := range ips {
236
254
if ip .IsLoopback () {
237
255
if ip .To4 () != nil {
238
256
return ip .String (), nil
@@ -244,7 +262,7 @@ func resolveCollectorIP(host string) (string, error) {
244
262
return ip .String (), nil
245
263
}
246
264
247
- return "" , ErrCollectorIPNotFound
265
+ return "127.0.0.1 " , ErrCollectorIPNotFound
248
266
}
249
267
250
268
func registerDefaultExtractionFormats () map [string ]tracing.Extractor {
0 commit comments