/
service.go
152 lines (138 loc) · 5.47 KB
/
service.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
package service
import (
"flag"
"net"
http1 "net/http"
"os"
endpoint1 "github.com/go-kit/kit/endpoint"
log "github.com/go-kit/kit/log"
prometheus "github.com/go-kit/kit/metrics/prometheus"
lightsteptracergo "github.com/lightstep/lightstep-tracer-go"
endpoint "github.com/money-hub/MoneyDodo.service/authentication/pkg/endpoint"
http "github.com/money-hub/MoneyDodo.service/authentication/pkg/http"
service "github.com/money-hub/MoneyDodo.service/authentication/pkg/service"
group "github.com/oklog/oklog/pkg/group"
opentracinggo "github.com/opentracing/opentracing-go"
zipkingoopentracing "github.com/openzipkin/zipkin-go-opentracing"
prometheus1 "github.com/prometheus/client_golang/prometheus"
appdash "sourcegraph.com/sourcegraph/appdash"
opentracing "sourcegraph.com/sourcegraph/appdash/opentracing"
)
var tracer opentracinggo.Tracer
var logger log.Logger
// Define our flags. Your service probably won't need to bind listeners for
// all* supported transports, but we do it here for demonstration purposes.
var fs = flag.NewFlagSet("authentication", flag.ExitOnError)
var debugAddr = fs.String("debug.addr", ":8080", "Debug and metrics listen address")
var httpAddr = fs.String("http-addr", ":8001", "HTTP listen address")
var grpcAddr = fs.String("grpc-addr", ":8082", "gRPC listen address")
var thriftAddr = fs.String("thrift-addr", ":8083", "Thrift listen address")
var thriftProtocol = fs.String("thrift-protocol", "binary", "binary, compact, json, simplejson")
var thriftBuffer = fs.Int("thrift-buffer", 0, "0 for unbuffered")
var thriftFramed = fs.Bool("thrift-framed", false, "true to enable framing")
var zipkinURL = fs.String("zipkin-url", "", "Enable Zipkin tracing via a collector URL e.g. http://localhost:9411/api/v1/spans")
var lightstepToken = fs.String("lightstep-token", "", "Enable LightStep tracing via a LightStep access token")
var appdashAddr = fs.String("appdash-addr", "", "Enable Appdash tracing via an Appdash server host:port")
func Run() {
fs.Parse(os.Args[1:])
// Create a single logger, which we'll use and give to other components.
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
// Determine which tracer to use. We'll pass the tracer to all the
// components that use it, as a dependency
if *zipkinURL != "" {
logger.Log("tracer", "Zipkin", "URL", *zipkinURL)
collector, err := zipkingoopentracing.NewHTTPCollector(*zipkinURL)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
defer collector.Close()
recorder := zipkingoopentracing.NewRecorder(collector, false, "localhost:80", "authentication")
tracer, err = zipkingoopentracing.NewTracer(recorder)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
} else if *lightstepToken != "" {
logger.Log("tracer", "LightStep")
tracer = lightsteptracergo.NewTracer(lightsteptracergo.Options{AccessToken: *lightstepToken})
defer lightsteptracergo.FlushLightStepTracer(tracer)
} else if *appdashAddr != "" {
logger.Log("tracer", "Appdash", "addr", *appdashAddr)
collector := appdash.NewRemoteCollector(*appdashAddr)
tracer = opentracing.NewTracer(collector)
defer collector.Close()
} else {
logger.Log("tracer", "none")
tracer = opentracinggo.GlobalTracer()
}
svc := service.New(getServiceMiddleware(logger))
eps := endpoint.New(svc, getEndpointMiddleware(logger))
g := createService(eps)
// initMetricsEndpoint(g)
// initCancelInterrupt(g)
logger.Log("exit", g.Run())
}
func initHttpHandler(endpoints endpoint.Endpoints, g *group.Group) {
options := defaultHttpOptions(logger, tracer)
// Add your http options here
httpHandler := http.NewHTTPHandler(endpoints, options)
httpListener, err := net.Listen("tcp", *httpAddr)
if err != nil {
logger.Log("transport", "HTTP", "during", "Listen", "err", err)
}
g.Add(func() error {
logger.Log("transport", "HTTP", "addr", *httpAddr)
return http1.Serve(httpListener, httpHandler)
}, func(error) {
httpListener.Close()
})
}
func getServiceMiddleware(logger log.Logger) (mw []service.Middleware) {
mw = []service.Middleware{}
mw = addDefaultServiceMiddleware(logger, mw)
// Append your middleware here
return
}
func getEndpointMiddleware(logger log.Logger) (mw map[string][]endpoint1.Middleware) {
mw = map[string][]endpoint1.Middleware{}
duration := prometheus.NewSummaryFrom(prometheus1.SummaryOpts{
Help: "Request duration in seconds.",
Name: "request_duration_seconds",
Namespace: "example",
Subsystem: "authentication",
}, []string{"method", "success"})
addDefaultEndpointMiddleware(logger, duration, mw)
// Add you endpoint middleware here
return
}
// func initMetricsEndpoint(g *group.Group) {
// http1.DefaultServeMux.Handle("/metrics", promhttp.Handler())
// debugListener, err := net.Listen("tcp", *debugAddr)
// if err != nil {
// logger.Log("transport", "debug/HTTP", "during", "Listen", "err", err)
// }
// g.Add(func() error {
// logger.Log("transport", "debug/HTTP", "addr", *debugAddr)
// return http1.Serve(debugListener, http1.DefaultServeMux)
// }, func(error) {
// debugListener.Close()
// })
// }
// func initCancelInterrupt(g *group.Group) {
// 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)
// })
// }