forked from go-kit/kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addsvc.go
306 lines (294 loc) · 11 KB
/
addsvc.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"text/tabwriter"
"github.com/apache/thrift/lib/go/thrift"
lightstep "github.com/lightstep/lightstep-tracer-go"
"github.com/oklog/oklog/pkg/group"
stdopentracing "github.com/opentracing/opentracing-go"
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
zipkin "github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"sourcegraph.com/sourcegraph/appdash"
appdashot "sourcegraph.com/sourcegraph/appdash/opentracing"
"github.com/AshleyDumaine/kit/log"
"github.com/AshleyDumaine/kit/metrics"
"github.com/AshleyDumaine/kit/metrics/prometheus"
kitgrpc "github.com/AshleyDumaine/kit/transport/grpc"
addpb "github.com/AshleyDumaine/kit/examples/addsvc/pb"
"github.com/AshleyDumaine/kit/examples/addsvc/pkg/addendpoint"
"github.com/AshleyDumaine/kit/examples/addsvc/pkg/addservice"
"github.com/AshleyDumaine/kit/examples/addsvc/pkg/addtransport"
addthrift "github.com/AshleyDumaine/kit/examples/addsvc/thrift/gen-go/addsvc"
)
func main() {
// Define our flags. Your service probably won't need to bind listeners for
// *all* supported transports, or support both Zipkin and LightStep, and so
// on, but we do it here for demonstration purposes.
fs := flag.NewFlagSet("addsvc", flag.ExitOnError)
var (
debugAddr = fs.String("debug.addr", ":8080", "Debug and metrics listen address")
httpAddr = fs.String("http-addr", ":8081", "HTTP listen address")
grpcAddr = fs.String("grpc-addr", ":8082", "gRPC listen address")
thriftAddr = fs.String("thrift-addr", ":8083", "Thrift listen address")
jsonRPCAddr = fs.String("jsonrpc-addr", ":8084", "JSON RPC listen address")
thriftProtocol = fs.String("thrift-protocol", "binary", "binary, compact, json, simplejson")
thriftBuffer = fs.Int("thrift-buffer", 0, "0 for unbuffered")
thriftFramed = fs.Bool("thrift-framed", false, "true to enable framing")
zipkinURL = fs.String("zipkin-url", "", "Enable Zipkin tracing via HTTP reporter URL e.g. http://localhost:9411/api/v2/spans")
zipkinBridge = fs.Bool("zipkin-ot-bridge", false, "Use Zipkin OpenTracing bridge instead of native implementation")
lightstepToken = fs.String("lightstep-token", "", "Enable LightStep tracing via a LightStep access token")
appdashAddr = fs.String("appdash-addr", "", "Enable Appdash tracing via an Appdash server host:port")
)
fs.Usage = usageFor(fs, os.Args[0]+" [flags]")
fs.Parse(os.Args[1:])
// Create a single logger, which we'll use and give to other components.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
var zipkinTracer *zipkin.Tracer
{
if *zipkinURL != "" {
var (
err error
hostPort = "localhost:80"
serviceName = "addsvc"
reporter = zipkinhttp.NewReporter(*zipkinURL)
)
defer reporter.Close()
zEP, _ := zipkin.NewEndpoint(serviceName, hostPort)
zipkinTracer, err = zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(zEP))
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
if !(*zipkinBridge) {
logger.Log("tracer", "Zipkin", "type", "Native", "URL", *zipkinURL)
}
}
}
// Determine which OpenTracing tracer to use. We'll pass the tracer to all the
// components that use it, as a dependency.
var tracer stdopentracing.Tracer
{
if *zipkinBridge && zipkinTracer != nil {
logger.Log("tracer", "Zipkin", "type", "OpenTracing", "URL", *zipkinURL)
tracer = zipkinot.Wrap(zipkinTracer)
zipkinTracer = nil // do not instrument with both native tracer and opentracing bridge
} else if *lightstepToken != "" {
logger.Log("tracer", "LightStep") // probably don't want to print out the token :)
tracer = lightstep.NewTracer(lightstep.Options{
AccessToken: *lightstepToken,
})
defer lightstep.FlushLightStepTracer(tracer)
} else if *appdashAddr != "" {
logger.Log("tracer", "Appdash", "addr", *appdashAddr)
tracer = appdashot.NewTracer(appdash.NewRemoteCollector(*appdashAddr))
} else {
tracer = stdopentracing.GlobalTracer() // no-op
}
}
// Create the (sparse) metrics we'll use in the service. They, too, are
// dependencies that we pass to components that use them.
var ints, chars metrics.Counter
{
// Business-level metrics.
ints = prometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "example",
Subsystem: "addsvc",
Name: "integers_summed",
Help: "Total count of integers summed via the Sum method.",
}, []string{})
chars = prometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: "example",
Subsystem: "addsvc",
Name: "characters_concatenated",
Help: "Total count of characters concatenated via the Concat method.",
}, []string{})
}
var duration metrics.Histogram
{
// Endpoint-level metrics.
duration = prometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "example",
Subsystem: "addsvc",
Name: "request_duration_seconds",
Help: "Request duration in seconds.",
}, []string{"method", "success"})
}
http.DefaultServeMux.Handle("/metrics", promhttp.Handler())
// Build the layers of the service "onion" from the inside out. First, the
// business logic service; then, the set of endpoints that wrap the service;
// and finally, a series of concrete transport adapters. The adapters, like
// the HTTP handler or the gRPC server, are the bridge between Go kit and
// the interfaces that the transports expect. Note that we're not binding
// them to ports or anything yet; we'll do that next.
var (
service = addservice.New(logger, ints, chars)
endpoints = addendpoint.New(service, logger, duration, tracer, zipkinTracer)
httpHandler = addtransport.NewHTTPHandler(endpoints, tracer, zipkinTracer, logger)
grpcServer = addtransport.NewGRPCServer(endpoints, tracer, zipkinTracer, logger)
thriftServer = addtransport.NewThriftServer(endpoints)
jsonrpcHandler = addtransport.NewJSONRPCHandler(endpoints, logger)
)
// Now we're to the part of the func main where we want to start actually
// running things, like servers bound to listeners to receive connections.
//
// The method is the same for each component: add a new actor to the group
// struct, which is a combination of 2 anonymous functions: the first
// function actually runs the component, and the second function should
// interrupt the first function and cause it to return. It's in these
// functions that we actually bind the Go kit server/handler structs to the
// concrete transports and run them.
//
// Putting each component into its own block is mostly for aesthetics: it
// clearly demarcates the scope in which each listener/socket may be used.
var g group.Group
{
// The debug listener mounts the http.DefaultServeMux, and serves up
// stuff like the Prometheus metrics route, the Go debug and profiling
// routes, and so on.
debugListener, err := net.Listen("tcp", *debugAddr)
if err != nil {
logger.Log("transport", "debug/HTTP", "during", "Listen", "err", err)
os.Exit(1)
}
g.Add(func() error {
logger.Log("transport", "debug/HTTP", "addr", *debugAddr)
return http.Serve(debugListener, http.DefaultServeMux)
}, func(error) {
debugListener.Close()
})
}
{
// The HTTP listener mounts the Go kit HTTP handler we created.
httpListener, err := net.Listen("tcp", *httpAddr)
if err != nil {
logger.Log("transport", "HTTP", "during", "Listen", "err", err)
os.Exit(1)
}
g.Add(func() error {
logger.Log("transport", "HTTP", "addr", *httpAddr)
return http.Serve(httpListener, httpHandler)
}, func(error) {
httpListener.Close()
})
}
{
// The gRPC listener mounts the Go kit gRPC server we created.
grpcListener, err := net.Listen("tcp", *grpcAddr)
if err != nil {
logger.Log("transport", "gRPC", "during", "Listen", "err", err)
os.Exit(1)
}
g.Add(func() error {
logger.Log("transport", "gRPC", "addr", *grpcAddr)
// we add the Go Kit gRPC Interceptor to our gRPC service as it is used by
// the here demonstrated zipkin tracing middleware.
baseServer := grpc.NewServer(grpc.UnaryInterceptor(kitgrpc.Interceptor))
addpb.RegisterAddServer(baseServer, grpcServer)
return baseServer.Serve(grpcListener)
}, func(error) {
grpcListener.Close()
})
}
{
// The Thrift socket mounts the Go kit Thrift server we created earlier.
// There's a lot of boilerplate involved here, related to configuring
// the protocol and transport; blame Thrift.
thriftSocket, err := thrift.NewTServerSocket(*thriftAddr)
if err != nil {
logger.Log("transport", "Thrift", "during", "Listen", "err", err)
os.Exit(1)
}
g.Add(func() error {
logger.Log("transport", "Thrift", "addr", *thriftAddr)
var protocolFactory thrift.TProtocolFactory
switch *thriftProtocol {
case "binary":
protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
case "compact":
protocolFactory = thrift.NewTCompactProtocolFactory()
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "simplejson":
protocolFactory = thrift.NewTSimpleJSONProtocolFactory()
default:
return fmt.Errorf("invalid Thrift protocol %q", *thriftProtocol)
}
var transportFactory thrift.TTransportFactory
if *thriftBuffer > 0 {
transportFactory = thrift.NewTBufferedTransportFactory(*thriftBuffer)
} else {
transportFactory = thrift.NewTTransportFactory()
}
if *thriftFramed {
transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
}
return thrift.NewTSimpleServer4(
addthrift.NewAddServiceProcessor(thriftServer),
thriftSocket,
transportFactory,
protocolFactory,
).Serve()
}, func(error) {
thriftSocket.Close()
})
}
{
httpListener, err := net.Listen("tcp", *jsonRPCAddr)
if err != nil {
logger.Log("transport", "JSONRPC over HTTP", "during", "Listen", "err", err)
os.Exit(1)
}
g.Add(func() error {
logger.Log("transport", "JSONRPC over HTTP", "addr", *jsonRPCAddr)
return http.Serve(httpListener, jsonrpcHandler)
}, func(error) {
httpListener.Close()
})
}
{
// This function just sits and waits for ctrl-C.
cancelInterrupt := make(chan struct{})
g.Add(func() error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-c:
return fmt.Errorf("received signal %s", sig)
case <-cancelInterrupt:
return nil
}
}, func(error) {
close(cancelInterrupt)
})
}
logger.Log("exit", g.Run())
}
func usageFor(fs *flag.FlagSet, short string) func() {
return func() {
fmt.Fprintf(os.Stderr, "USAGE\n")
fmt.Fprintf(os.Stderr, " %s\n", short)
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "FLAGS\n")
w := tabwriter.NewWriter(os.Stderr, 0, 2, 2, ' ', 0)
fs.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(w, "\t-%s %s\t%s\n", f.Name, f.DefValue, f.Usage)
})
w.Flush()
fmt.Fprintf(os.Stderr, "\n")
}
}