apiserver: add key/value pair to httplog
This commit is contained in:
		@@ -18,6 +18,7 @@ package filterlatency
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -64,7 +65,7 @@ func TrackCompleted(handler http.Handler) http.Handler {
 | 
				
			|||||||
		latency := completedAt.Sub(fr.startedTimestamp)
 | 
							latency := completedAt.Sub(fr.startedTimestamp)
 | 
				
			||||||
		metrics.RecordFilterLatency(ctx, fr.name, latency)
 | 
							metrics.RecordFilterLatency(ctx, fr.name, latency)
 | 
				
			||||||
		if klog.V(3).Enabled() && latency > minFilterLatencyToLog {
 | 
							if klog.V(3).Enabled() && latency > minFilterLatencyToLog {
 | 
				
			||||||
			httplog.AddInfof(ctx, "%s=%s", fr.name, latency.String())
 | 
								httplog.AddKeyValue(ctx, fmt.Sprintf("fl_%s", fr.name), latency.String())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -55,6 +55,7 @@ type respLogger struct {
 | 
				
			|||||||
	status             int
 | 
						status             int
 | 
				
			||||||
	statusStack        string
 | 
						statusStack        string
 | 
				
			||||||
	addedInfo          strings.Builder
 | 
						addedInfo          strings.Builder
 | 
				
			||||||
 | 
						addedKeyValuePairs []interface{}
 | 
				
			||||||
	startTime          time.Time
 | 
						startTime          time.Time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	captureErrorOutput bool
 | 
						captureErrorOutput bool
 | 
				
			||||||
@@ -181,6 +182,20 @@ func AddInfof(ctx context.Context, format string, data ...interface{}) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (rl *respLogger) AddKeyValue(key string, value interface{}) {
 | 
				
			||||||
 | 
						rl.addedKeyValuePairs = append(rl.addedKeyValuePairs, key, value)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// AddKeyValue adds a (key, value) pair to the httplog associated
 | 
				
			||||||
 | 
					// with the request.
 | 
				
			||||||
 | 
					// Use this function if you want your data to show up in httplog
 | 
				
			||||||
 | 
					// in a more structured and readable way.
 | 
				
			||||||
 | 
					func AddKeyValue(ctx context.Context, key string, value interface{}) {
 | 
				
			||||||
 | 
						if rl := respLoggerFromContext(ctx); rl != nil {
 | 
				
			||||||
 | 
							rl.AddKeyValue(key, value)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Log is intended to be called once at the end of your request handler, via defer
 | 
					// Log is intended to be called once at the end of your request handler, via defer
 | 
				
			||||||
func (rl *respLogger) Log() {
 | 
					func (rl *respLogger) Log() {
 | 
				
			||||||
	latency := time.Since(rl.startTime)
 | 
						latency := time.Since(rl.startTime)
 | 
				
			||||||
@@ -204,6 +219,7 @@ func (rl *respLogger) Log() {
 | 
				
			|||||||
		"audit-ID", auditID,
 | 
							"audit-ID", auditID,
 | 
				
			||||||
		"srcIP", rl.req.RemoteAddr,
 | 
							"srcIP", rl.req.RemoteAddr,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						keysAndValues = append(keysAndValues, rl.addedKeyValuePairs...)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if rl.hijacked {
 | 
						if rl.hijacked {
 | 
				
			||||||
		keysAndValues = append(keysAndValues, "hijacked", true)
 | 
							keysAndValues = append(keysAndValues, "hijacked", true)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user