-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
301 lines (284 loc) · 11.1 KB
/
server.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
// Package server implements a gRPC server (and optional REST gateway) implementation
// that satisfies the PiServiceClient interface requirements, with optional
// OpenTelemetry metrics and traces.
package server
import (
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/go-logr/logr"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
pi "github.com/memes/pi/v2"
cachepkg "github.com/memes/pi/v2/pkg/cache"
"github.com/memes/pi/v2/pkg/generated"
"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/attribute"
otelcodes "go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"
"google.golang.org/grpc/xds"
)
const (
// The default name to use when using OpenTelemetry components.
OpenTelemetryPackageIdentifier = "pkg.server"
)
type PiServer struct {
generated.UnimplementedPiServiceServer
// The logr.Logger implementation to use
logger logr.Logger
// An optional cache implementation
cache cachepkg.Cache
// Holds the instance specific metadata that will be returned in PiService responses
metadata *generated.GetDigitMetadata
// A gauge for calculation durations
calculationMs metric.Int64Histogram
// A counter for the number of errors returned by cache
cacheErrors metric.Int64Counter
// A counter for cache hits
cacheHits metric.Int64Counter
// A counter for cache misses
cacheMisses metric.Int64Counter
// A set of gRPC ServerOptions to use
serverOptions []grpc.ServerOption
// A set of gRPC DialOptions to use with REST gateway gRPC client
dialOptions []grpc.DialOption
}
// Defines the function signature for PiServer options.
type PiServerOption func(*PiServer)
// Create a new piServer and apply any options.
func NewPiServer(options ...PiServerOption) (*PiServer, error) {
var hostname string
if host, err := os.Hostname(); err == nil {
hostname = host
} else {
hostname = "unknown"
}
server := &PiServer{
logger: logr.Discard(),
cache: cachepkg.NewNoopCache(),
metadata: &generated.GetDigitMetadata{
Identity: hostname,
Tags: []string{},
Annotations: map[string]string{},
},
serverOptions: []grpc.ServerOption{
grpc.StatsHandler(otelgrpc.NewServerHandler()),
},
dialOptions: []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(otelgrpc.NewClientHandler()),
},
}
for _, option := range options {
option(server)
}
var err error
server.calculationMs, err = otel.Meter(OpenTelemetryPackageIdentifier).Int64Histogram(
OpenTelemetryPackageIdentifier+".calc_duration_ms",
metric.WithUnit("ms"),
metric.WithDescription("The duration (ms) of calculations"),
)
if err != nil {
return nil, fmt.Errorf("error returned while creating calculationMs Histogram: %w", err)
}
server.cacheErrors, err = otel.Meter(OpenTelemetryPackageIdentifier).Int64Counter(
OpenTelemetryPackageIdentifier+".cache_errors",
metric.WithDescription("The count of error responses from digit cache"),
)
if err != nil {
return nil, fmt.Errorf("error returned while creating cacheErrors Counter: %w", err)
}
server.cacheHits, err = otel.Meter(OpenTelemetryPackageIdentifier).Int64Counter(
OpenTelemetryPackageIdentifier+".cache_hits",
metric.WithDescription("The count of cache hits"),
)
if err != nil {
return nil, fmt.Errorf("error returned while creating cacheHits Counter: %w", err)
}
server.cacheMisses, err = otel.Meter(OpenTelemetryPackageIdentifier).Int64Counter(
OpenTelemetryPackageIdentifier+".cache_misses",
metric.WithDescription("The count of cache misses"),
)
if err != nil {
return nil, fmt.Errorf("error returned while creating cacheMisses Counter: %w", err)
}
return server, nil
}
// Use the supplied logger for the server and pi packages.
func WithLogger(logger logr.Logger) PiServerOption {
return func(s *PiServer) {
s.logger = logger
pi.Logger = logger
}
}
// Use the Cache implementation to store BBPDigits results to avoid recalculation
// of a digit that has already been calculated.
func WithCache(cache cachepkg.Cache) PiServerOption {
return func(s *PiServer) {
if cache != nil {
s.cache = cache
}
}
}
// Add the string tags to the server's metadata.
func WithTags(tags []string) PiServerOption {
return func(s *PiServer) {
if tags != nil {
s.metadata.Tags = append(s.metadata.Tags, tags...)
}
}
}
// Add the key-value annotations to the server's metadata.
func WithAnnotations(annotations map[string]string) PiServerOption {
return func(s *PiServer) {
for k, v := range annotations {
s.metadata.Annotations[k] = v
}
}
}
// Set the TransportCredentials to use for Pi Service gRPC listener.
func WithGRPCServerTransportCredentials(serverCredentials credentials.TransportCredentials) PiServerOption {
return func(s *PiServer) {
s.serverOptions = append(s.serverOptions, grpc.Creds(serverCredentials))
}
}
// Set the TransportCredentials to use for Pi Service REST-to-gRPC client.
func WithRestClientGRPCTransportCredentials(restClientGRPCCredentials credentials.TransportCredentials) PiServerOption {
return func(s *PiServer) {
if restClientGRPCCredentials != nil {
s.dialOptions = append(s.dialOptions, grpc.WithTransportCredentials(restClientGRPCCredentials))
}
}
}
// Set the authority string to use for REST-gRPC gateway calls.
func WithRestClientAuthority(restClientAuthority string) PiServerOption {
return func(s *PiServer) {
if restClientAuthority != "" {
s.dialOptions = append(s.dialOptions, grpc.WithAuthority(restClientAuthority))
}
}
}
// Implement the PiService GetDigit RPC method.
func (s *PiServer) GetDigit(ctx context.Context, in *generated.GetDigitRequest) (*generated.GetDigitResponse, error) {
logger := s.logger.WithValues("index", in.Index)
logger.Info("GetDigit: enter")
cacheIndex := (in.Index / 9) * 9
key := strconv.FormatUint(cacheIndex, 16)
attributes := []attribute.KeyValue{
attribute.Int(OpenTelemetryPackageIdentifier+".index", int(in.Index)),
attribute.String(OpenTelemetryPackageIdentifier+".cacheKey", key),
}
ctx, span := otel.Tracer(OpenTelemetryPackageIdentifier).Start(ctx, OpenTelemetryPackageIdentifier+"/GetDigit")
defer span.End()
span.SetAttributes(attributes...)
span.AddEvent("Checking cache")
digits, err := s.cache.GetValue(ctx, key)
if err != nil {
span.RecordError(err)
span.SetStatus(otelcodes.Error, err.Error())
s.cacheErrors.Add(ctx, 1, metric.WithAttributes(attributes...))
return nil, status.Error(codes.Internal, fmt.Sprintf("cache %T GetValue method returned an error: %v", s.cache, err)) //nolint:wrapcheck // Errors returned should be gRPC statuses
}
if digits == "" {
attributes := append(attributes, attribute.Bool(OpenTelemetryPackageIdentifier+".cache_hit", false))
span.SetAttributes(attributes...)
span.AddEvent("Calculating fractional digits")
s.cacheMisses.Add(ctx, 1, metric.WithAttributes(attributes...))
ts := time.Now()
digits = pi.BBPDigits(cacheIndex)
s.calculationMs.Record(ctx, time.Since(ts).Milliseconds(), metric.WithAttributes(attributes...))
err = s.cache.SetValue(ctx, key, digits)
if err != nil {
span.RecordError(err)
span.SetStatus(otelcodes.Error, err.Error())
s.cacheErrors.Add(ctx, 1, metric.WithAttributes(attributes...))
return nil, status.Error(codes.Internal, fmt.Sprintf("cache %T SetValue method returned an error: %v", s.cache, err)) //nolint:wrapcheck // Errors returned should be gRPC statuses
}
} else {
attributes := append(attributes, attribute.Bool(OpenTelemetryPackageIdentifier+".cache_hit", true))
span.SetAttributes(attributes...)
s.cacheHits.Add(ctx, 1, metric.WithAttributes(attributes...))
}
offset := in.Index % 9
digit, err := strconv.ParseUint(digits[offset:offset+1], 10, 32)
if err != nil {
span.RecordError(err)
span.SetStatus(otelcodes.Error, err.Error())
return nil, status.Error(codes.Internal, fmt.Sprintf("method GetDigit failed to parse as uint: %v", err)) //nolint:wrapcheck // Errors returned should be gRPC statuses
}
logger.Info("GetDigit: exit", "digit", digit)
return &generated.GetDigitResponse{
Index: in.Index,
Digit: uint32(digit),
Metadata: s.metadata,
}, nil
}
// Create a new grpc.Server that is ready to be attached to a net.Listener.
func (s *PiServer) NewGrpcServer() *grpc.Server {
s.logger.V(1).Info("Building a standard gRPC server")
grpcServer := grpc.NewServer(s.serverOptions...)
healthServer := health.NewServer()
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
grpc_health_v1.RegisterHealthServer(grpcServer, healthServer)
generated.RegisterPiServiceServer(grpcServer, s)
reflection.Register(grpcServer)
return grpcServer
}
// Create a new xds.GRPCServer that is ready to be attached to a net.Listener.
func (s *PiServer) NewXDSServer() (*xds.GRPCServer, error) {
s.logger.V(1).Info("xDS is enabled; building an xDS aware gRPC server")
xdsServer, err := xds.NewGRPCServer(s.serverOptions...)
if err != nil {
return nil, fmt.Errorf("failed to create new xDS gRPC server: %w", err)
}
healthServer := health.NewServer()
healthServer.SetServingStatus("", grpc_health_v1.HealthCheckResponse_SERVING)
grpc_health_v1.RegisterHealthServer(xdsServer, healthServer)
reflection.Register(xdsServer)
return xdsServer, nil
}
// Create a new REST gateway handler that translates and forwards incoming REST
// requests to the specified gRPC endpoint address.
func (s *PiServer) NewRestGatewayHandler(ctx context.Context, grpcAddress string) (http.Handler, error) {
mux := runtime.NewServeMux()
if err := generated.RegisterPiServiceHandlerFromEndpoint(ctx, mux, grpcAddress, s.dialOptions); err != nil {
return nil, fmt.Errorf("failed to register PiService handler for REST gateway: %w", err)
}
if err := mux.HandlePath("GET", "/api/v2/swagger.json",
func(w http.ResponseWriter, _ *http.Request, _ map[string]string) {
w.Header().Add("Content-Type", "application/json")
if _, err := w.Write(generated.SwaggerJSON); err != nil {
s.logger.Error(err, "Writing swagger JSON to response raised an error; continuing")
}
},
); err != nil {
return nil, fmt.Errorf("failed to register /api/v2 handler for swagger definition: %w", err)
}
if err := mux.HandlePath("GET", "/v1/digit/{index}",
func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
span := trace.SpanFromContext(r.Context())
span.SetAttributes(attribute.String(OpenTelemetryPackageIdentifier+".index", pathParams["index"]))
span.SetStatus(otelcodes.Error, "v1 API")
w.WriteHeader(http.StatusGone)
},
); err != nil {
return nil, fmt.Errorf("failed to register /v1 handler for REST gateway: %w", err)
}
return otelhttp.NewHandler(mux,
OpenTelemetryPackageIdentifier+"/RestGatewayHandler",
otelhttp.WithMessageEvents(otelhttp.ReadEvents, otelhttp.WriteEvents),
), nil
}