-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdryrun.go
More file actions
440 lines (373 loc) · 13.6 KB
/
dryrun.go
File metadata and controls
440 lines (373 loc) · 13.6 KB
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package reverseproxy
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/GoCodeAlone/modular"
)
// DryRunConfig provides configuration for dry-run functionality.
type DryRunConfig struct {
// Enabled determines if dry-run mode is available
Enabled bool `json:"enabled" yaml:"enabled" toml:"enabled" env:"DRY_RUN_ENABLED" default:"false"`
// LogResponses determines if response bodies should be logged (can be verbose)
LogResponses bool `json:"log_responses" yaml:"log_responses" toml:"log_responses" env:"DRY_RUN_LOG_RESPONSES" default:"false"`
// MaxResponseSize is the maximum response size to compare (in bytes)
MaxResponseSize int64 `json:"max_response_size" yaml:"max_response_size" toml:"max_response_size" env:"DRY_RUN_MAX_RESPONSE_SIZE" default:"1048576"` // 1MB
// CompareHeaders determines which headers should be compared
CompareHeaders []string `json:"compare_headers" yaml:"compare_headers" toml:"compare_headers" env:"DRY_RUN_COMPARE_HEADERS"`
// IgnoreHeaders lists headers to ignore during comparison
IgnoreHeaders []string `json:"ignore_headers" yaml:"ignore_headers" toml:"ignore_headers" env:"DRY_RUN_IGNORE_HEADERS"`
// DefaultResponseBackend specifies which backend response to return by default ("primary" or "secondary")
DefaultResponseBackend string `json:"default_response_backend" yaml:"default_response_backend" toml:"default_response_backend" env:"DRY_RUN_DEFAULT_RESPONSE_BACKEND" default:"primary"`
}
// DryRunResult represents the result of a dry-run comparison.
type DryRunResult struct {
Timestamp time.Time `json:"timestamp"`
RequestID string `json:"requestId,omitempty"`
TenantID string `json:"tenantId,omitempty"`
Endpoint string `json:"endpoint"`
Method string `json:"method"`
PrimaryBackend string `json:"primaryBackend"`
SecondaryBackend string `json:"secondaryBackend"`
PrimaryResponse ResponseInfo `json:"primaryResponse"`
SecondaryResponse ResponseInfo `json:"secondaryResponse"`
Comparison ComparisonResult `json:"comparison"`
Duration DurationInfo `json:"duration"`
ReturnedResponse string `json:"returnedResponse"` // "primary" or "secondary" - indicates which response was returned to client
}
// ResponseInfo contains information about a backend response.
type ResponseInfo struct {
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
BodySize int64 `json:"bodySize"`
ResponseTime time.Duration `json:"responseTime"`
Error string `json:"error,omitempty"`
}
// ComparisonResult contains the results of comparing two responses.
type ComparisonResult struct {
StatusCodeMatch bool `json:"statusCodeMatch"`
HeadersMatch bool `json:"headersMatch"`
BodyMatch bool `json:"bodyMatch"`
Differences []string `json:"differences,omitempty"`
HeaderDiffs map[string]HeaderDiff `json:"headerDiffs,omitempty"`
}
// HeaderDiff represents a difference in header values.
type HeaderDiff struct {
Primary string `json:"primary"`
Secondary string `json:"secondary"`
}
// DurationInfo contains timing information for the dry-run.
type DurationInfo struct {
Total time.Duration `json:"total"`
Primary time.Duration `json:"primary"`
Secondary time.Duration `json:"secondary"`
}
// DryRunHandler handles dry-run request processing.
type DryRunHandler struct {
config DryRunConfig
tenantIDHeader string
httpClient *http.Client
logger modular.Logger
}
// NewDryRunHandler creates a new dry-run handler.
func NewDryRunHandler(config DryRunConfig, tenantIDHeader string, logger modular.Logger) *DryRunHandler {
if tenantIDHeader == "" {
tenantIDHeader = "X-Tenant-ID" // Default fallback
}
return &DryRunHandler{
config: config,
tenantIDHeader: tenantIDHeader,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
logger: logger,
}
}
// ProcessDryRun processes a request in dry-run mode, sending it to both backends and comparing responses.
func (d *DryRunHandler) ProcessDryRun(ctx context.Context, req *http.Request, primaryBackend, secondaryBackend string) (*DryRunResult, error) {
if !d.config.Enabled {
return nil, ErrDryRunModeNotEnabled
}
startTime := time.Now()
// Create dry-run result
result := &DryRunResult{
Timestamp: startTime,
RequestID: req.Header.Get("X-Request-ID"),
TenantID: req.Header.Get(d.tenantIDHeader),
Endpoint: req.URL.Path,
Method: req.Method,
PrimaryBackend: primaryBackend,
SecondaryBackend: secondaryBackend,
}
// Read and store request body for replication
var requestBody []byte
if req.Body != nil {
var err error
requestBody, err = io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("failed to read request body: %w", err)
}
req.Body.Close()
}
// Send requests to both backends concurrently
primaryChan := make(chan ResponseInfo, 1)
secondaryChan := make(chan ResponseInfo, 1)
// Send request to primary backend
go func() {
defer func() {
if r := recover(); r != nil {
d.logger.Error("panic recovered in dry-run primary request", "error", r)
primaryChan <- ResponseInfo{Error: fmt.Sprintf("panic: %v", r)}
}
}()
primaryStart := time.Now()
response := d.sendRequest(ctx, req, primaryBackend, requestBody)
response.ResponseTime = time.Since(primaryStart)
primaryChan <- response
}()
// Send request to secondary backend
go func() {
defer func() {
if r := recover(); r != nil {
d.logger.Error("panic recovered in dry-run secondary request", "error", r)
secondaryChan <- ResponseInfo{Error: fmt.Sprintf("panic: %v", r)}
}
}()
secondaryStart := time.Now()
response := d.sendRequest(ctx, req, secondaryBackend, requestBody)
response.ResponseTime = time.Since(secondaryStart)
secondaryChan <- response
}()
// Collect responses, respecting context cancellation.
select {
case result.PrimaryResponse = <-primaryChan:
case <-ctx.Done():
result.PrimaryResponse = ResponseInfo{Error: ctx.Err().Error()}
}
select {
case result.SecondaryResponse = <-secondaryChan:
case <-ctx.Done():
result.SecondaryResponse = ResponseInfo{Error: ctx.Err().Error()}
}
// Calculate timing
result.Duration = DurationInfo{
Total: time.Since(startTime),
Primary: result.PrimaryResponse.ResponseTime,
Secondary: result.SecondaryResponse.ResponseTime,
}
// Determine which response to return based on configuration
if d.config.DefaultResponseBackend == "secondary" {
result.ReturnedResponse = "secondary"
} else {
result.ReturnedResponse = "primary" // Default to primary
}
// Compare responses
result.Comparison = d.compareResponses(result.PrimaryResponse, result.SecondaryResponse)
// Log the dry-run result
d.logDryRunResult(result)
return result, nil
}
// GetReturnedResponse returns the response information that should be sent to the client.
func (d *DryRunResult) GetReturnedResponse() ResponseInfo {
if d.ReturnedResponse == "secondary" {
return d.SecondaryResponse
}
return d.PrimaryResponse
}
// sendRequest sends a request to a specific backend and returns response information.
func (d *DryRunHandler) sendRequest(ctx context.Context, originalReq *http.Request, backend string, requestBody []byte) ResponseInfo {
response := ResponseInfo{}
// Create new request with proper URL joining
url := singleJoiningSlash(backend, originalReq.URL.Path)
if originalReq.URL.RawQuery != "" {
url += "?" + originalReq.URL.RawQuery
}
var bodyReader io.Reader
if len(requestBody) > 0 {
bodyReader = bytes.NewReader(requestBody)
}
req, err := http.NewRequestWithContext(ctx, originalReq.Method, url, bodyReader) //nolint:gosec // G704: url is built from configured backend address, not user input
if err != nil {
response.Error = fmt.Sprintf("failed to create request: %v", err)
return response
}
// Copy headers
for key, values := range originalReq.Header {
for _, value := range values {
req.Header.Add(key, value)
}
}
// Send request
resp, err := d.httpClient.Do(req) //nolint:gosec // G704: dry run intentionally makes requests to configured backends
if err != nil {
response.Error = fmt.Sprintf("request failed: %v", err)
return response
}
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("failed to close response body: %v\n", err)
}
}()
response.StatusCode = resp.StatusCode
// Read response body
bodyBytes, err := io.ReadAll(io.LimitReader(resp.Body, d.config.MaxResponseSize))
if err != nil {
response.Error = fmt.Sprintf("failed to read response body: %v", err)
return response
}
response.BodySize = int64(len(bodyBytes))
if d.config.LogResponses {
response.Body = string(bodyBytes)
}
// Copy response headers
response.Headers = make(map[string]string)
for key, values := range resp.Header {
if len(values) > 0 {
response.Headers[key] = values[0] // Take first value
}
}
return response
}
// compareResponses compares two responses and returns the comparison result.
func (d *DryRunHandler) compareResponses(primary, secondary ResponseInfo) ComparisonResult {
result := ComparisonResult{
Differences: []string{},
HeaderDiffs: make(map[string]HeaderDiff),
}
// Compare status codes
result.StatusCodeMatch = primary.StatusCode == secondary.StatusCode
if !result.StatusCodeMatch {
result.Differences = append(result.Differences,
fmt.Sprintf("Status code: primary=%d, secondary=%d", primary.StatusCode, secondary.StatusCode))
}
// Compare headers
result.HeadersMatch = d.compareHeaders(primary.Headers, secondary.Headers, result)
// Compare response bodies
result.BodyMatch = primary.Body == secondary.Body
if !result.BodyMatch && primary.Body != "" && secondary.Body != "" {
result.Differences = append(result.Differences, "Response body content differs")
}
// Check for errors
if primary.Error != "" || secondary.Error != "" {
if primary.Error != secondary.Error {
result.Differences = append(result.Differences,
fmt.Sprintf("Error: primary='%s', secondary='%s'", primary.Error, secondary.Error))
}
}
return result
}
// compareHeaders compares headers between two responses.
func (d *DryRunHandler) compareHeaders(primaryHeaders, secondaryHeaders map[string]string, result ComparisonResult) bool {
headersMatch := true
ignoreMap := make(map[string]bool)
// Build ignore map
for _, header := range d.config.IgnoreHeaders {
ignoreMap[header] = true
}
// Default headers to ignore
ignoreMap["Date"] = true
ignoreMap["X-Request-ID"] = true
ignoreMap["X-Trace-ID"] = true
// Compare headers that should be compared
compareMap := make(map[string]bool)
if len(d.config.CompareHeaders) > 0 {
for _, header := range d.config.CompareHeaders {
compareMap[header] = true
}
}
// Check all headers in primary response
for key, primaryValue := range primaryHeaders {
if ignoreMap[key] {
continue
}
// If compare headers are specified, only compare those
if len(compareMap) > 0 && !compareMap[key] {
continue
}
secondaryValue, exists := secondaryHeaders[key]
if !exists {
headersMatch = false
result.HeaderDiffs[key] = HeaderDiff{
Primary: primaryValue,
Secondary: "<missing>",
}
} else if primaryValue != secondaryValue {
headersMatch = false
result.HeaderDiffs[key] = HeaderDiff{
Primary: primaryValue,
Secondary: secondaryValue,
}
}
}
// Check headers that exist in secondary but not in primary
for key, secondaryValue := range secondaryHeaders {
if ignoreMap[key] {
continue
}
if len(compareMap) > 0 && !compareMap[key] {
continue
}
if _, exists := primaryHeaders[key]; !exists {
headersMatch = false
result.HeaderDiffs[key] = HeaderDiff{
Primary: "<missing>",
Secondary: secondaryValue,
}
}
}
return headersMatch
}
// logDryRunResult logs the dry-run result.
func (d *DryRunHandler) logDryRunResult(result *DryRunResult) {
logLevel := "info"
if len(result.Comparison.Differences) > 0 {
logLevel = "warn"
}
logAttrs := []interface{}{
"operation", "dry-run",
"endpoint", result.Endpoint,
"method", result.Method,
"primaryBackend", result.PrimaryBackend,
"secondaryBackend", result.SecondaryBackend,
"statusCodeMatch", result.Comparison.StatusCodeMatch,
"headersMatch", result.Comparison.HeadersMatch,
"bodyMatch", result.Comparison.BodyMatch,
"primaryStatus", result.PrimaryResponse.StatusCode,
"secondaryStatus", result.SecondaryResponse.StatusCode,
"primaryResponseTime", result.Duration.Primary,
"secondaryResponseTime", result.Duration.Secondary,
"totalDuration", result.Duration.Total,
}
if result.TenantID != "" {
logAttrs = append(logAttrs, "tenant", result.TenantID)
}
if result.RequestID != "" {
logAttrs = append(logAttrs, "requestId", result.RequestID)
}
if len(result.Comparison.Differences) > 0 {
logAttrs = append(logAttrs, "differences", result.Comparison.Differences)
}
if len(result.Comparison.HeaderDiffs) > 0 {
logAttrs = append(logAttrs, "headerDifferences", result.Comparison.HeaderDiffs)
}
if result.PrimaryResponse.Error != "" {
logAttrs = append(logAttrs, "primaryError", result.PrimaryResponse.Error)
}
if result.SecondaryResponse.Error != "" {
logAttrs = append(logAttrs, "secondaryError", result.SecondaryResponse.Error)
}
message := "Dry-run completed"
if len(result.Comparison.Differences) > 0 {
message = "Dry-run completed with differences"
}
switch logLevel {
case "warn":
d.logger.Warn(message, logAttrs...)
default:
d.logger.Info(message, logAttrs...)
}
}