MCPcopy
hub / github.com/kserve/kserve / ServeHTTP

Method ServeHTTP

pkg/logger/handler.go:115–208  ·  view source on GitHub ↗

call svc and add send request/responses to logUrl

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

113
114// call svc and add send request/responses to logUrl
115func (eh *LoggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
116 if network.IsKubeletProbe(r) {
117 if eh.next != nil {
118 eh.next.ServeHTTP(w, r)
119 }
120 return
121 }
122 // Record the time when the request arrives
123 requestTime := time.Now()
124 // Read request payload
125 body, err := io.ReadAll(r.Body)
126 if err != nil {
127 http.Error(w, "can't read body", http.StatusBadRequest)
128 return
129 }
130
131 metadata := map[string][]string{}
132 if eh.metadataHeaders != nil {
133 // Loop over header names
134 for name, values := range r.Header {
135 // Loop over all values for the name.
136 if slices.Contains(eh.metadataHeaders, name) {
137 metadata[name] = values
138 }
139 }
140 }
141
142 // Get or Create an ID
143 id := getOrCreateID(r)
144 contentType := r.Header.Get("Content-Type")
145 // log Request
146 if eh.logMode == v1beta1.LogAll || eh.logMode == v1beta1.LogRequest {
147 if err := QueueLogRequest(LogRequest{
148 Url: eh.logUrl,
149 Bytes: &body,
150 ContentType: contentType,
151 ReqType: CEInferenceRequest,
152 Id: id,
153 SourceUri: eh.sourceUri,
154 InferenceService: eh.inferenceService,
155 Namespace: eh.namespace,
156 Endpoint: eh.endpoint,
157 Component: eh.component,
158 Annotations: eh.annotations,
159 Metadata: metadata,
160 CertName: eh.certName,
161 TlsSkipVerify: eh.tlsSkipVerify,
162 OccurrenceTime: requestTime,
163 }); err != nil {
164 eh.log.Error(err, "Failed to log request")
165 }
166 }
167
168 // Proxy Request
169 r.Body = io.NopCloser(bytes.NewBuffer(body))
170 // TODO: Set a reasonable initial buffer size
171 var responseBuf bytes.Buffer
172 lrw := &loggingResponseWriter{ResponseWriter: w, responseBuffer: &responseBuf, log: eh.log}

Callers 6

TestLoggerFunction · 0.45
TestLoggerWithMetadataFunction · 0.45
TestLoggerWithAnnotationFunction · 0.45
TestBadResponseFunction · 0.45
TestLoggerWithS3StoreFunction · 0.45

Calls 6

getOrCreateIDFunction · 0.85
QueueLogRequestFunction · 0.85
NewReaderMethod · 0.80
GetMethod · 0.65
ErrorMethod · 0.45
InfoMethod · 0.45

Tested by 6

TestLoggerFunction · 0.36
TestLoggerWithMetadataFunction · 0.36
TestLoggerWithAnnotationFunction · 0.36
TestBadResponseFunction · 0.36
TestLoggerWithS3StoreFunction · 0.36