55"fmt"
66"net/http"
77"net/url"
8+ "strconv"
89"strings"
910"sync"
1011"time"
@@ -17,7 +18,8 @@ import (
1718"github.com/coder/coder/v2/coderd/tracing"
1819)
1920
20- var sensitivePatterns = []string {"code" ,"token" ,"key" ,"secret" ,"password" ,"auth" ,"credential" ,"api_key" }
21+ var safeParams = []string {"page" ,"limit" ,"offset" }
22+ var countParams = []string {"ids" ,"template_ids" }
2123
2224func safeQueryParams (params url.Values ) []slog.Field {
2325if len (params )== 0 {
@@ -26,25 +28,42 @@ func safeQueryParams(params url.Values) []slog.Field {
2628
2729fields := make ([]slog.Field ,0 ,len (params ))
2830for key ,values := range params {
29- sensitive := false
30-
31- // Check if this parameter should be redacted
32- for _ ,pattern := range sensitivePatterns {
33- if strings .Contains (strings .ToLower (key ),pattern ) {
34- sensitive = true
31+ // Check if this parameter should be included
32+ for _ ,pattern := range safeParams {
33+ if strings .EqualFold (key ,pattern ) {
34+ // Prepend query parameters in the log line to ensure we don't have issues with collisions
35+ // in case any other internal logging fields already log fields with similar names
36+ fieldName := "query_" + key
37+
38+ // Log the actual values for non-sensitive parameters
39+ if len (values )== 1 {
40+ fields = append (fields ,slog .F (fieldName ,values [0 ]))
41+ continue
42+ }
43+ fields = append (fields ,slog .F (fieldName ,values ))
3544}
3645}
37- if ! sensitive {
46+ // Some query params we just want to log the count of the params length
47+ for _ ,pattern := range countParams {
48+ if ! strings .EqualFold (key ,pattern ) {
49+ continue
50+ }
51+ count := 0
52+
3853// Prepend query parameters in the log line to ensure we don't have issues with collisions
3954// in case any other internal logging fields already log fields with similar names
4055fieldName := "query_" + key
4156
42- // Log the actual values for non-sensitive parameters
43- if len (values )== 1 {
44- fields = append (fields ,slog .F (fieldName ,values [0 ]))
45- continue
57+ // Count comma-separated values for CSV format
58+ for _ ,v := range values {
59+ if strings .Contains (v ,"," ) {
60+ count += len (strings .Split (v ,"," ))
61+ continue
62+ }
63+ count ++
4664}
47- fields = append (fields ,slog .F (fieldName ,values ))
65+ // For logging we always want strings
66+ fields = append (fields ,slog .F (fieldName + "_count" ,strconv .Itoa (count )))
4867}
4968}
5069return fields