-
Notifications
You must be signed in to change notification settings - Fork 2
/
micro.go
239 lines (190 loc) · 7.52 KB
/
micro.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
package micro
import (
"context"
"log"
"net"
"os"
"net/http"
"strconv"
"strings"
// grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
// grpc_validator "github.com/grpc-ecosystem/go-grpc-middleware/validator"
// grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
// "google.golang.org/grpc/reflection"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
// SwaggerFile is the swagger file (local path)
const SwaggerFile = "/swagger.json"
// Service - to represent the microservice
type Service struct {
GRPCServer *grpc.Server
grpcServices []grpcService
streamInterceptors []grpc.StreamServerInterceptor
unaryInterceptors []grpc.UnaryServerInterceptor
upRedoc bool
grpcGatewayPort uint16
grpcPort uint16
// OTEL Meter
meterProvider *sdkmetric.MeterProvider
// OTEL Trace
tracerProvider *sdktrace.TracerProvider
}
// ReverseProxyFunc - a callback that the caller should implement to steps to reverse-proxy the HTTP/1 requests to gRPC
// type ReverseProxyFunc func(ctx context.Context, mux *runtime.ServeMux, grpcHostAndPort string, opts []grpc.DialOption) error
type RegisterServiceHandlerFunc func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
type grpcService struct {
serviceDesc *grpc.ServiceDesc
srv interface{}
registerServiceHandlerFunc RegisterServiceHandlerFunc
}
// NewService - to create the microservice object
func NewService() *Service {
s := Service{}
s.upRedoc = os.Getenv("MICRO_REDOC") == "1"
s.grpcGatewayPort = 80
s.grpcPort = 9090
s.initOpenTelemetry()
/*
tracer := opentracing.GlobalTracer()
s.streamInterceptors = []grpc.StreamServerInterceptor{}
s.streamInterceptors = append(s.streamInterceptors, grpc_prometheus.StreamServerInterceptor)
s.streamInterceptors = append(s.streamInterceptors, grpc_validator.StreamServerInterceptor())
s.streamInterceptors = append(s.streamInterceptors, otgrpc.OpenTracingStreamServerInterceptor(tracer))
s.streamInterceptors = append(s.streamInterceptors, streamInterceptors...)
s.unaryInterceptors = []grpc.UnaryServerInterceptor{}
s.unaryInterceptors = append(s.unaryInterceptors, grpc_prometheus.UnaryServerInterceptor)
s.unaryInterceptors = append(s.unaryInterceptors, grpc_validator.UnaryServerInterceptor())
s.unaryInterceptors = append(s.unaryInterceptors, otgrpc.OpenTracingServerInterceptor(tracer))
s.unaryInterceptors = append(s.unaryInterceptors, unaryInterceptors...)
s.GRPCServer = grpc.NewServer(
grpc_middleware.WithStreamServerChain(s.streamInterceptors...),
grpc_middleware.WithUnaryServerChain(s.unaryInterceptors...),
)*/
return &s
}
func (s *Service) SetGRPCGatewayPort(port uint16) {
s.grpcGatewayPort = port
}
func (s *Service) SetGRPCPort(port uint16) {
s.grpcPort = port
}
func (s *Service) AddService(serviceDesc *grpc.ServiceDesc, srv interface{}, registerServiceHandlerFunc RegisterServiceHandlerFunc) {
s.grpcServices = append(s.grpcServices, grpcService{
serviceDesc: serviceDesc,
srv: srv,
registerServiceHandlerFunc: registerServiceHandlerFunc,
})
}
// UpRedoc - to configure redoc server to run up at /docs endpoint
func (s *Service) UpRedoc(up bool) *Service {
s.upRedoc = up
return s
}
// Start - to start the microservice with listening on the ports
func (s *Service) Start() error {
s.GRPCServer = grpc.NewServer(
grpc.StatsHandler(otelgrpc.NewServerHandler()),
// grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
// grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),
)
// start http1.0 server & swagger server in the background
go func() {
// Start HTTP/1.0 server at :80
if err := s.grpcGateway(); err != nil {
log.Fatalf("failed to start gRPC gateway: %v", err)
}
}()
// Setup /metrics for prometheus
//grpc_prometheus.Register(s.GRPCServer)
// Register reflection service on gRPC server.
//reflection.Register(s.GRPCServer)
for _, grpcService := range s.grpcServices {
s.GRPCServer.RegisterService(grpcService.serviceDesc, grpcService.srv)
}
grpcHost := strings.Join([]string{":", strconv.FormatUint(uint64(s.grpcPort), 10)}, "")
lis, err := net.Listen("tcp", grpcHost)
if err != nil {
return err
}
return s.GRPCServer.Serve(lis)
}
func (s *Service) Stop() {
s.shutdownOpenTelemetry()
}
func (s *Service) grpcGateway() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{}))
// opts := []grpc.DialOption{grpc.WithInsecure()}
// configure /metrics HTTP/1 endpoint
patternMetrics := runtime.MustPattern(runtime.NewPattern(1, []int{2, 0}, []string{"metrics"}, ""))
mux.Handle("GET", patternMetrics, func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
promhttp.Handler().ServeHTTP(w, r)
})
// configure /docs HTTP/1 endpoint
patternRedoc := runtime.MustPattern(runtime.NewPattern(1, []int{2, 0}, []string{"docs"}, ""))
mux.Handle("GET", patternRedoc, redoc)
// configure /swagger.json HTTP/1 endpoint
patternSwaggerJSON := runtime.MustPattern(runtime.NewPattern(1, []int{2, 0}, []string{"swagger.json"}, ""))
mux.Handle("GET", patternSwaggerJSON, func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
http.ServeFile(w, r, SwaggerFile)
})
grpcHostAndPort := strings.Join([]string{":", strconv.FormatUint(uint64(s.grpcPort), 10)}, "")
conn, err := grpc.DialContext(
context.Background(),
grpcHostAndPort,
grpc.WithInsecure(),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
// grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
// grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
)
if err != nil {
log.Printf("Cannot establish gRPC connection from gRPC gateway: %v\n", err)
return err
}
for _, grpcService := range s.grpcServices {
err := grpcService.registerServiceHandlerFunc(ctx, mux, conn)
if err != nil {
return err
}
}
var handler http.Handler
handler = otelhttp.NewHandler(mux, "grpc-gateway: mux.ServeHTTP()")
handler = newTraceparentHandler(handler)
return http.ListenAndServe(strings.Join([]string{":", strconv.FormatUint(uint64(s.grpcGatewayPort), 10)}, ""), handler)
}
// https://uptrace.dev/opentelemetry/opentelemetry-traceparent.html#injecting-traceparent-header
type traceparentHandler struct {
next http.Handler
props propagation.TextMapPropagator
}
func newTraceparentHandler(next http.Handler) *traceparentHandler {
return &traceparentHandler{
next: next,
props: otel.GetTextMapPropagator(),
}
}
func (h *traceparentHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// https://faun.pub/multi-hop-tracing-with-opentelemetry-in-golang-792df5feb37c
// Extract Span/Trace info from the request header
ctx := otel.GetTextMapPropagator().Extract(
req.Context(), propagation.HeaderCarrier(req.Header),
)
tracer := otel.GetTracerProvider().Tracer("")
ctx, span := tracer.Start(ctx, "grpc-gateway: " + req.Method + " " + req.URL.String(),
trace.WithSpanKind(trace.SpanKindServer),
)
defer span.End()
h.props.Inject(ctx, propagation.HeaderCarrier(w.Header()))
h.next.ServeHTTP(w, req.WithContext(ctx))
}