-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
372 lines (332 loc) · 8.68 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2019 Simon Pasquier
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/mwitkow/go-conntrack"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/api/books/v1"
"google.golang.org/api/option"
)
var (
help bool
listen string
timeout, delay, expiry time.Duration
errorRatio float64
gen *rand.Rand
store responseCache
incomingHeaders = []string{
"x-request-id",
"x-b3-traceid",
"x-b3-spanid",
"x-b3-parentspanid",
"x-b3-sampled",
"x-b3-flags",
"x-ot-span-context",
}
)
var (
incomingDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "details_incoming_requests_duration_seconds",
Help: "Histogram of incoming request latencies.",
},
[]string{"code"},
)
outgoingDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "details_outgoing_requests_duration_seconds",
Help: "Histogram of request latencies to the downstream API.",
},
[]string{"code"},
)
inflightRequests = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "details_incoming_requests_in_flight",
Help: "Number of in-flight requests to the details service.",
})
cacheSize = prometheus.NewGaugeFunc(
prometheus.GaugeOpts{
Name: "details_cache_size",
Help: "Number of items in the in-memory cache",
},
func() float64 {
if store == nil {
return 0
}
return float64(store.length())
},
)
)
func init() {
flag.BoolVar(&help, "help", false, "Help message")
flag.StringVar(&listen, "listen-address", ":8080", "Listen address")
flag.DurationVar(&timeout, "timeout", 5*time.Second, "Maximum duration to wait for downstream API")
flag.DurationVar(&delay, "delay", 0*time.Second, "Artifical delay to wait after receiving the response from the downstream API")
flag.DurationVar(&expiry, "cache-expiry", 0*time.Second, "How long to keep objects in the cache")
flag.Float64Var(&errorRatio, "error", 0.0, "Ratio of injected error responses")
if errorRatio < 0.0 {
errorRatio = 0.0
}
if errorRatio > 1.0 {
errorRatio = 1.0
}
gen = rand.New(rand.NewSource(time.Now().UnixNano()))
prometheus.MustRegister(incomingDuration, outgoingDuration, inflightRequests, cacheSize)
for _, c := range []int{http.StatusOK, http.StatusBadRequest, http.StatusNotFound, http.StatusInternalServerError, http.StatusServiceUnavailable} {
incomingDuration.WithLabelValues(fmt.Sprintf("%d", c))
}
}
type errorResponse struct {
Message string
}
type statusResponse struct {
Message string
}
type detailsResponse struct {
ID int `json:"id"`
Author string `json:"author"`
Year string `json:"year"`
Type string `json:"type"`
Pages int64 `json:"pages"`
Publisher string `json:"publisher"`
Language string `json:"language"`
Isbn10 string `json:"ISBN-10"`
Isbn13 string `json:"ISBN-13"`
}
type responseCache interface {
get(int) *detailsResponse
set(*detailsResponse)
length() int
}
type noopCache struct{}
func (c *noopCache) get(int) *detailsResponse { return nil }
func (c *noopCache) set(*detailsResponse) {}
func (c *noopCache) length() int { return 0 }
type cache struct {
expiry time.Duration
mtx sync.Mutex
entries map[int]*cacheEntry
}
type cacheEntry struct {
response *detailsResponse
ttl time.Time
}
func newCache(expiry time.Duration) *cache {
c := &cache{
mtx: sync.Mutex{},
entries: make(map[int]*cacheEntry),
expiry: expiry,
}
go func() {
for range time.After(1 * time.Second) {
c.mtx.Lock()
now := time.Now()
for k := range c.entries {
if c.entries[k].ttl.Before(now) {
delete(c.entries, k)
}
}
c.mtx.Unlock()
}
}()
return c
}
func (c *cache) get(id int) *detailsResponse {
c.mtx.Lock()
defer c.mtx.Unlock()
e, ok := c.entries[id]
if !ok {
return nil
}
if time.Now().After(e.ttl) {
return nil
}
return e.response
}
func (c *cache) set(d *detailsResponse) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.entries[d.ID] = &cacheEntry{
response: d,
ttl: time.Now().Add(c.expiry),
}
}
func (c *cache) length() int {
c.mtx.Lock()
defer c.mtx.Unlock()
return len(c.entries)
}
func writeResponseError(w http.ResponseWriter, code int, e error) {
b, err := json.Marshal(&errorResponse{Message: fmt.Sprintf("%s", e)})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(code)
_, _ = w.Write(b)
}
func writeResponseOK(w http.ResponseWriter, v interface{}) {
b, err := json.Marshal(v)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(b)
}
func getISBN(identifiers []*books.VolumeVolumeInfoIndustryIdentifiers, typ string) string {
for _, identifier := range identifiers {
if identifier.Type == typ {
return identifier.Identifier
}
}
return ""
}
func details(w http.ResponseWriter, r *http.Request) {
inflightRequests.Inc()
defer inflightRequests.Dec()
var (
err error
code = http.StatusOK
)
defer func() {
if err != nil {
log.Printf("/details/ error: %q", err)
writeResponseError(w, code, err)
}
}()
// Inject some errors if required.
if errorRatio > 0.0 && gen.Float64() < errorRatio {
err = fmt.Errorf("random error")
code = http.StatusServiceUnavailable
return
}
isbn := strings.TrimPrefix(r.URL.Path, "/details/")
if isbn == "0" {
// The productpage application always send 0 as the ISBN so
// hard-code here with one of the ISBN for "The comedy of errors".
isbn = "0486424618"
}
id, err := strconv.Atoi(isbn)
if err != nil {
code = http.StatusBadRequest
return
}
if v := store.get(id); v != nil {
writeResponseOK(w, v)
return
}
ctx := r.Context()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
svc, err := books.NewService(
ctx,
option.WithHTTPClient(
&http.Client{
Transport: promhttp.InstrumentRoundTripperDuration(outgoingDuration, &http.Transport{
IdleConnTimeout: 10 * timeout,
DialContext: conntrack.NewDialContextFunc(
conntrack.DialWithTracing(),
conntrack.DialWithName("google-api"),
),
}),
},
),
)
if err != nil {
//TODO: implement fallback response
code = http.StatusInternalServerError
return
}
volService := books.NewVolumesService(svc)
volCall := volService.List(fmt.Sprintf("isbn:%s", isbn))
volCall = volCall.Context(ctx)
// Add tracing headers.
header := volCall.Header()
for _, h := range incomingHeaders {
header.Add(h, r.Header.Get(h))
}
// Send the request to the downstream API.
vols, err := volCall.Do()
if err != nil {
code = http.StatusInternalServerError
if ctx.Err() != nil {
code = http.StatusServiceUnavailable
}
return
}
if len(vols.Items) == 0 {
err = fmt.Errorf("ISBN %s not found", isbn)
code = http.StatusNotFound
return
}
vol := vols.Items[0].VolumeInfo
book := &detailsResponse{
ID: id,
Author: vol.Authors[0],
Year: vol.PublishedDate,
Type: vol.PrintType,
Pages: vol.PageCount,
Publisher: vol.Publisher,
Language: vol.Language,
Isbn10: getISBN(vol.IndustryIdentifiers, "ISBN_10"),
Isbn13: getISBN(vol.IndustryIdentifiers, "ISBN_13"),
}
if vol.Language == "en" {
book.Language = "English"
}
if vol.PrintType == "BOOK" {
book.Type = "paperback"
}
store.set(book)
<-time.After(delay)
writeResponseOK(w, book)
}
func main() {
flag.Parse()
if help {
fmt.Fprintln(os.Stderr, "Bookinfo details service")
flag.PrintDefaults()
os.Exit(0)
}
if expiry > time.Duration(0) {
log.Printf("Using cache expiry (ttl=%v)", expiry)
store = newCache(expiry)
} else {
store = &noopCache{}
}
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
writeResponseOK(w, &statusResponse{Message: "OK"})
})
http.HandleFunc("/metrics", promhttp.Handler().ServeHTTP)
http.Handle("/details/", promhttp.InstrumentHandlerInFlight(
inflightRequests,
promhttp.InstrumentHandlerDuration(incomingDuration,
http.HandlerFunc(details),
),
))
log.Println("Listening on", listen)
log.Fatal(http.ListenAndServe(listen, nil))
}