/
config.go
183 lines (155 loc) · 5.31 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package tracing provides the canonical tracing functionality used by Go-based
// Istio components.
//
// The package provides direct integration with the Cobra command-line processor which makes it
// easy to build programs that use a consistent interface for tracing. Here's an example
// of a simple Cobra-based program using this tracing package:
//
// func main() {
// // get the default tracing options
// options := tracing.DefaultOptions()
//
// rootCmd := &cobra.Command{
// Run: func(cmd *cobra.Command, args []string) {
//
// // configure the tracing system
// if _, err := tracing.Configure(options); err != nil {
// // print an error and quit
// }
//
// // Generate a trace
// ot.StartSpan(...)
// },
// }
//
// // add tracing-specific flags to the cobra command
// options.AttachCobraFlags(rootCmd)
// rootCmd.SetArgs(os.Args[1:])
// rootCmd.Execute()
// }
//
// The point of this package is to configure the global OpenTracing tracer
// for the process as read from ot.GlobalTracer and used by ot.StartSpan.
package tracing
import (
"fmt"
"io"
"time"
ot "github.com/opentracing/opentracing-go"
jaeger "github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/transport"
"github.com/uber/jaeger-client-go/transport/zipkin"
zk "github.com/uber/jaeger-client-go/zipkin"
"go.uber.org/zap"
"istio.io/istio/pkg/log"
)
/* TODO:
* - Support only tracing when trace context information is already present (mixer)
* - Support tracing for some percentage of requests (pilot)
*/
type holder struct {
closer io.Closer
tracer ot.Tracer
}
var (
httpTimeout = 5 * time.Second
poolSpans = jaeger.TracerOptions.PoolSpans(false)
logger = spanLogger{}
)
// indirection for testing
type newZipkin func(url string, options ...zipkin.HTTPOption) (*zipkin.HTTPTransport, error)
// Configure initializes Istio's tracing subsystem.
//
// You typically call this once at process startup.
// Once this call returns, the tracing system is ready to accept data.
func Configure(serviceName string, options *Options) (io.Closer, error) {
return configure(serviceName, options, zipkin.NewHTTPTransport)
}
func configure(serviceName string, options *Options, nz newZipkin) (io.Closer, error) {
if err := options.Validate(); err != nil {
return nil, err
}
reporters := make([]jaeger.Reporter, 0, 3)
sampler, err := jaeger.NewProbabilisticSampler(options.SamplingRate)
if err != nil {
return nil, fmt.Errorf("could not build trace sampler: %v", err)
}
if options.ZipkinURL != "" {
trans, err := nz(options.ZipkinURL, zipkin.HTTPLogger(logger), zipkin.HTTPTimeout(httpTimeout))
if err != nil {
return nil, fmt.Errorf("could not build zipkin reporter: %v", err)
}
reporters = append(reporters, jaeger.NewRemoteReporter(trans))
}
if options.JaegerURL != "" {
reporters = append(reporters, jaeger.NewRemoteReporter(transport.NewHTTPTransport(options.JaegerURL, transport.HTTPTimeout(httpTimeout))))
}
if options.LogTraceSpans {
reporters = append(reporters, logger)
}
var rep jaeger.Reporter
if len(reporters) == 0 {
// leave the default NoopTracer in place since there's no place for tracing to go...
return holder{}, nil
} else if len(reporters) == 1 {
rep = reporters[0]
} else {
rep = jaeger.NewCompositeReporter(reporters...)
}
var tracer ot.Tracer
var closer io.Closer
if options.ZipkinURL != "" {
zipkinPropagator := zk.NewZipkinB3HTTPHeaderPropagator()
injector := jaeger.TracerOptions.Injector(ot.HTTPHeaders, zipkinPropagator)
extractor := jaeger.TracerOptions.Extractor(ot.HTTPHeaders, zipkinPropagator)
tracer, closer = jaeger.NewTracer(serviceName, sampler, rep, poolSpans, injector, extractor, jaeger.TracerOptions.Gen128Bit(true))
} else {
tracer, closer = jaeger.NewTracer(serviceName, sampler, rep, poolSpans, jaeger.TracerOptions.Gen128Bit(true))
}
// NOTE: global side effect!
ot.SetGlobalTracer(tracer)
return holder{
closer: closer,
tracer: tracer,
}, nil
}
func (h holder) Close() error {
if ot.GlobalTracer() == h.tracer {
ot.SetGlobalTracer(ot.NoopTracer{})
}
var err error
if h.closer != nil {
err = h.closer.Close()
}
return err
}
type spanLogger struct{}
// Report implements the Report() method of jaeger.Reporter
func (spanLogger) Report(span *jaeger.Span) {
log.Info("Reporting span",
zap.String("operation", span.OperationName()),
zap.String("span", span.String()))
}
// Close implements the Close() method of jaeger.Reporter.
func (spanLogger) Close() {}
// Error implements the Error() method of log.Logger.
func (spanLogger) Error(msg string) {
log.Error(msg)
}
// Infof implements the Infof() method of log.Logger.
func (spanLogger) Infof(msg string, args ...interface{}) {
log.Infof(msg, args...)
}