-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpass_api.go
288 lines (243 loc) · 8.03 KB
/
pass_api.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.
package main
import (
"fmt"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
)
func respondToGetPasses(c *gin.Context) {
ctx := c.Request.Context()
logrus.WithContext(ctx).Info("Get all passes")
var passes []mountainPass
err := db.SelectContext(ctx, &passes, "SELECT * FROM mountain_pass")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.IndentedJSON(http.StatusOK, passes)
}
func respondToGetSinglePass(c *gin.Context) {
ctx := c.Request.Context()
logrus.WithContext(ctx).Info("Fetching single pass")
id := c.Param("id")
var pass mountainPass
err := db.GetContext(ctx, &pass, "SELECT * FROM mountain_pass WHERE id=$1", id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Pass not found"})
return
}
c.IndentedJSON(http.StatusOK, pass)
}
func respondToPostPasses(c *gin.Context) {
ctx := c.Request.Context()
logrus.WithContext(ctx).Info("Creating pass")
var newPass mountainPass
if err := c.BindJSON(&newPass); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid input"})
return
}
// Use RETURNING to get the auto-generated ID
var id int
err := db.QueryRowContext(
ctx,
"INSERT INTO mountain_pass (name, country, ascent) VALUES ($1, $2, $3) RETURNING id",
newPass.Name, newPass.Country, newPass.Ascent,
).Scan(&id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Set the ID on the newPass struct
newPass.ID = id
c.IndentedJSON(http.StatusCreated, newPass)
}
func respondToDeletePass(c *gin.Context) {
ctx := c.Request.Context()
logrus.WithContext(ctx).Info("Deleting pass")
id := c.Param("id")
result, err := db.ExecContext(ctx, "DELETE FROM mountain_pass WHERE id=$1", id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
rowsAffected, err := result.RowsAffected()
if err != nil || rowsAffected == 0 {
c.JSON(http.StatusNotFound, gin.H{"error": "Pass not found"})
return
}
c.Status(http.StatusNoContent)
}
// Searches for passes by elevation range. This features an _extremely_ inefficient query
// that has been "designed" to stand out in APM.
func respondToSearchByElevation(c *gin.Context) {
ctx := c.Request.Context()
logrus.WithContext(ctx).Info("Searching passes by elevation range")
min := c.Query("min")
max := c.Query("max")
if min == "" || max == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Both min and max elevation parameters are required"})
return
}
query := `
WITH pass_data AS (
SELECT * FROM mountain_pass WHERE ascent BETWEEN $1 AND $2
),
all_countries AS (
SELECT DISTINCT country FROM mountain_pass
),
all_combos AS (
-- Intentional cartesian product to create huge result set
SELECT p.*, c.country as related_country
FROM pass_data p
CROSS JOIN all_countries c
)
SELECT
p.*,
(SELECT COUNT(*) FROM mountain_pass mp
WHERE mp.country = p.country) as country_total,
(SELECT string_agg(mp.name, ', ')
FROM mountain_pass mp
WHERE mp.country = p.country
AND mp.name LIKE CONCAT('%', SUBSTRING(p.name FROM 1 FOR 1), '%')
) as similar_passes,
(SELECT COUNT(*) FROM mountain_pass mp
WHERE mp.name ~ ('^' || SUBSTRING(p.name FROM 1 FOR 3) || '.*')
) as regex_matches,
(SELECT string_agg(mp.name, ', ')
FROM mountain_pass mp
WHERE mp.country = p.related_country
) as same_country_passes
FROM all_combos p
ORDER BY LENGTH(p.name) * p.ascent * COALESCE(p.longitude, 0) * COALESCE(p.latitude, 0) DESC
`
// Create a struct that extends mountainPass to include the additional fields
var passes []struct {
mountainPass
RelatedCountry string `db:"related_country" json:"related_country"`
CountryTotal int `db:"country_total" json:"country_total"`
SimilarPasses string `db:"similar_passes" json:"similar_passes"`
RegexMatches int `db:"regex_matches" json:"regex_matches"`
SameCountryPasses string `db:"same_country_passes" json:"same_country_passes"`
}
// Add intentional delay to make it even slower
// Use a transaction to hold a lock longer
tx, err := db.BeginTxx(ctx, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
defer tx.Rollback()
// Execute a simple count query first to acquire shared locks
var count int
err = tx.GetContext(ctx, &count, "SELECT COUNT(*) FROM mountain_pass")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Now run our expensive query
err = tx.SelectContext(ctx, &passes, query, min, max)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// Log information about the range size to help debug
logrus.WithContext(ctx).Infof("Retrieved %d passes for range %s-%s", len(passes), min, max)
c.IndentedJSON(http.StatusOK, passes)
}
// Fetches an image for a given pass from the pass-image-api service
// This endpoint demonstrates potential failures when dealing with external services
func respondToGetPassImage(c *gin.Context) {
ctx := c.Request.Context()
logrus.WithContext(ctx).Info("Fetching pass image")
passID := c.Param("id")
// First, verify that the pass exists
var pass mountainPass
err := db.GetContext(ctx, &pass, "SELECT * FROM mountain_pass WHERE id=$1", passID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Pass not found"})
return
}
// Check if we have coordinates
if pass.Latitude == 0 || pass.Longitude == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Pass has no coordinates"})
return
}
// Construct the URL to the image service
// The actual image API takes lat/long/size rather than pass ID
// Format: /images/{long}/{lat}/{size_px}
imageSize := 500 // Default size in pixels
imageServiceURL := "http://pass-image-api:8080/images/" +
toString(pass.Longitude) + "/" +
toString(pass.Latitude) + "/" +
toString(imageSize)
// Optional radius parameter
radius := 1.5 // Slightly larger view than default
imageServiceURL += "?radius=" + toString(radius)
// Create a traced HTTP client with Datadog
httpClient := httptrace.WrapClient(&http.Client{
// No timeout - intentionally problematic
})
// Use instrumented HTTP client for request
req, err := http.NewRequestWithContext(ctx, "GET", imageServiceURL, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to create request",
"details": err.Error(),
})
return
}
// Execute the HTTP request with tracing
resp, err := httpClient.Do(req)
if err != nil {
// Will fail if the service is down or unreachable
c.JSON(http.StatusServiceUnavailable, gin.H{
"error": "Failed to connect to image service",
"details": err.Error(),
})
return
}
defer resp.Body.Close()
// Check if the image service returned an error
if resp.StatusCode != http.StatusOK {
c.JSON(resp.StatusCode, gin.H{
"error": "Image service returned an error",
"status": resp.Status,
})
return
}
// Read the image data - no maximum size limit, potential memory issues
imageData, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "Failed to read image data",
"details": err.Error(),
})
return
}
// Set the content type from the response
contentType := resp.Header.Get("Content-Type")
if contentType == "" {
// Default to png as used in the Rust service
contentType = "image/png"
}
// Return the image data
c.Data(http.StatusOK, contentType, imageData)
}
// Helper function to convert various types to string
func toString(v interface{}) string {
switch val := v.(type) {
case int:
return strconv.Itoa(val)
case float64:
return strconv.FormatFloat(val, 'f', 6, 64)
default:
return fmt.Sprintf("%v", v)
}
}