@@ -27,6 +27,7 @@ import (
27
27
28
28
const (
29
29
retryOnBadConnTimeout = 5 * time .Minute
30
+ requestIDHeaderName = "X-Request-ID"
30
31
)
31
32
32
33
type wrapperFunc func (rt http.RoundTripper ) (http.RoundTripper , error )
@@ -194,7 +195,7 @@ func (c *Client) Send(
194
195
195
196
// If available, add the request id as an HTTP header
196
197
if reqID != "" {
197
- req .Header .Add ("X-Request-ID" , reqID )
198
+ req .Header .Add (requestIDHeaderName , reqID )
198
199
}
199
200
200
201
// copy headers.
@@ -220,27 +221,30 @@ func (c *Client) Send(
220
221
c .log .With ("error" , err ).Debugf (msg )
221
222
continue
222
223
}
223
- c .checkApiVersionHeaders (reqID , resp )
224
+ c .checkApiVersionHeaders (req , resp )
224
225
225
226
return resp , nil
226
227
}
227
228
228
229
return nil , fmt .Errorf ("all hosts failed: %w" , multiErr )
229
230
}
230
231
231
- func (c * Client ) checkApiVersionHeaders (reqID string , resp * http.Response ) {
232
+ func (c * Client ) checkApiVersionHeaders (req * http. Request , resp * http.Response ) {
232
233
const elasticApiVersionHeaderKey = "Elastic-Api-Version"
233
234
const warningHeaderKey = "Warning"
234
235
235
236
warning := resp .Header .Get (warningHeaderKey )
237
+ requestID := req .Header .Get (requestIDHeaderName )
236
238
if warning != "" {
237
- c .log .With ("http.request.id" , reqID ).Warnf ("warning in fleet response: %q" , warning )
239
+ c .log .With ("http.request.id" , requestID ).Warnf ("warning in fleet response: %q" , warning )
238
240
}
239
241
240
- if downgradeVersion := resp .Header .Get (elasticApiVersionHeaderKey ); resp .StatusCode == http .StatusBadRequest && downgradeVersion != "" {
242
+ requestAPIVersion := req .Header .Get (elasticApiVersionHeaderKey )
243
+ downgradeVersion := resp .Header .Get (elasticApiVersionHeaderKey )
244
+ if resp .StatusCode == http .StatusBadRequest && downgradeVersion != "" && downgradeVersion != requestAPIVersion {
241
245
// fleet server requested a downgrade to a different api version, we should bubble up an error until some kind
242
246
// of fallback mechanism can instantiate the requested version. This is not yet implemented so we log an error
243
- c .log .With ("http.request.id" , reqID ).Errorf ("fleet requested a different api version %q but this is currently not implemented" , downgradeVersion )
247
+ c .log .With ("http.request.id" , requestID ).Errorf ("fleet requested a different api version %q but this is currently not implemented" , downgradeVersion )
244
248
}
245
249
}
246
250
0 commit comments