This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 613
/
main.go
167 lines (147 loc) · 3.8 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
corelog "log"
"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/microservices-demo/user/api"
"github.com/microservices-demo/user/db"
"github.com/microservices-demo/user/db/mongodb"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
stdprometheus "github.com/prometheus/client_golang/prometheus"
commonMiddleware "github.com/weaveworks/common/middleware"
)
var (
port string
zip string
)
var (
HTTPLatency = stdprometheus.NewHistogramVec(stdprometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: stdprometheus.DefBuckets,
}, []string{"method", "path", "status_code", "isWS"})
)
const (
ServiceName = "user"
)
func init() {
stdprometheus.MustRegister(HTTPLatency)
flag.StringVar(&zip, "zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
flag.StringVar(&port, "port", "8084", "Port on which to run")
db.Register("mongodb", &mongodb.Mongo{})
}
func main() {
flag.Parse()
// Mechanical stuff.
errc := make(chan error)
// Log domain.
var logger log.Logger
{
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)
}
// Find service local IP.
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
localAddr := conn.LocalAddr().(*net.UDPAddr)
host := strings.Split(localAddr.String(), ":")[0]
defer conn.Close()
var tracer stdopentracing.Tracer
{
if zip == "" {
tracer = stdopentracing.NoopTracer{}
} else {
logger := log.With(logger, "tracer", "Zipkin")
logger.Log("addr", zip)
collector, err := zipkin.NewHTTPCollector(
zip,
zipkin.HTTPLogger(logger),
)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
tracer, err = zipkin.NewTracer(
zipkin.NewRecorder(collector, false, fmt.Sprintf("%v:%v", host, port), ServiceName),
)
if err != nil {
logger.Log("err", err)
os.Exit(1)
}
}
stdopentracing.InitGlobalTracer(tracer)
}
dbconn := false
for !dbconn {
err := db.Init()
if err != nil {
if err == db.ErrNoDatabaseSelected {
corelog.Fatal(err)
}
corelog.Print(err)
} else {
dbconn = true
}
}
fieldKeys := []string{"method"}
// Service domain.
var service api.Service
{
service = api.NewFixedService()
service = api.LoggingMiddleware(logger)(service)
service = api.NewInstrumentingService(
kitprometheus.NewCounterFrom(
stdprometheus.CounterOpts{
Namespace: "microservices_demo",
Subsystem: "user",
Name: "request_count",
Help: "Number of requests received.",
},
fieldKeys),
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "microservices_demo",
Subsystem: "user",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}, fieldKeys),
service,
)
}
// Endpoint domain.
endpoints := api.MakeEndpoints(service, tracer)
// HTTP router
router := api.MakeHTTPHandler(endpoints, logger, tracer)
httpMiddleware := []commonMiddleware.Interface{
commonMiddleware.Instrument{
Duration: HTTPLatency,
RouteMatcher: router,
},
}
// Handler
handler := commonMiddleware.Merge(httpMiddleware...).Wrap(router)
// Create and launch the HTTP server.
go func() {
logger.Log("transport", "HTTP", "port", port)
errc <- http.ListenAndServe(fmt.Sprintf(":%v", port), handler)
}()
// Capture interrupts.
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
errc <- fmt.Errorf("%s", <-c)
}()
logger.Log("exit", <-errc)
}