-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[Inference API] Auto-propagate product origin for every subclass of ElasticInferenceServiceRequest #123141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Inference API] Auto-propagate product origin for every subclass of ElasticInferenceServiceRequest #123141
Changes from 1 commit
77325e5
82d6754
70a3287
fe52d44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,30 @@ | |
|
|
||
| package org.elasticsearch.xpack.inference.external.request.elastic; | ||
|
|
||
| import org.apache.http.client.methods.HttpRequestBase; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.xpack.inference.external.request.HttpRequest; | ||
| import org.elasticsearch.xpack.inference.external.request.Request; | ||
|
|
||
| public interface ElasticInferenceServiceRequest extends Request {} | ||
| public abstract class ElasticInferenceServiceRequest implements Request { | ||
|
|
||
| private final String productOrigin; | ||
|
|
||
| public ElasticInferenceServiceRequest(String productOrigin) { | ||
| this.productOrigin = productOrigin; | ||
| } | ||
|
|
||
| public String getProductOrigin() { | ||
| return productOrigin; | ||
| } | ||
|
|
||
| @Override | ||
| public final HttpRequest createHttpRequest() { | ||
| HttpRequestBase request = createHttpRequestBase(); | ||
| // TODO: consider moving tracing here, too | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wanted to keep it out of scope for this PR, but I think it also makes sense to move the tracing logic here, so we don't risk forgetting it for new subclasses of |
||
| request.setHeader(Task.X_ELASTIC_PRODUCT_ORIGIN_HTTP_HEADER, productOrigin); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't really need to care as EIS handles the case, if this header is absent and/or empty and sets |
||
| return new HttpRequest(request, getInferenceEntityId()); | ||
| } | ||
|
|
||
| protected abstract HttpRequestBase createHttpRequestBase(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.external.request.elastic; | ||
|
|
||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.client.methods.HttpRequestBase; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xpack.inference.external.request.Request; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
|
|
||
| public class ElasticInferenceServiceRequestTests extends ESTestCase { | ||
|
|
||
| public void testElasticInferenceServiceRequestSubclasses_Decorate_HttpRequest_WithProductOrigin() { | ||
| var productOrigin = "elastic"; | ||
| var elasticInferenceServiceRequestWrapper = getDummyElasticInferenceServiceRequest(productOrigin); | ||
| var httpRequest = elasticInferenceServiceRequestWrapper.createHttpRequest(); | ||
| var productOriginHeader = httpRequest.httpRequestBase().getFirstHeader(Task.X_ELASTIC_PRODUCT_ORIGIN_HTTP_HEADER); | ||
|
|
||
| // Make sure this header only exists one time | ||
| assertThat(httpRequest.httpRequestBase().getHeaders(Task.X_ELASTIC_PRODUCT_ORIGIN_HTTP_HEADER).length, equalTo(1)); | ||
| assertThat(productOriginHeader.getValue(), equalTo(productOrigin)); | ||
| } | ||
|
|
||
| private static ElasticInferenceServiceRequest getDummyElasticInferenceServiceRequest(String productOrigin) { | ||
| return new ElasticInferenceServiceRequest(productOrigin) { | ||
| @Override | ||
| protected HttpRequestBase createHttpRequestBase() { | ||
| return new HttpGet("http://localhost:8080"); | ||
| } | ||
|
|
||
| @Override | ||
| public URI getURI() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Request truncate() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean[] getTruncationInfo() { | ||
| return new boolean[0]; | ||
| } | ||
|
|
||
| @Override | ||
| public String getInferenceEntityId() { | ||
| return ""; | ||
| } | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made this
finalso it's clear that a new subclass ofElasticInferenceServiceRequestshould overridecreateHttpRequestBaseand notcreateHttpRequest.