Skip to content

Send host header so connections can work over proxies / load balancers #100

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ public class ElasticSearchMetricSender {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchMetricSender.class);
private RestClient client;
private String esIndex;
private String host;
private List<String> metricList;
private String authUser;
private String authPwd;
private String awsEndpoint;

public ElasticSearchMetricSender(RestClient cli, String index, String user, String pwd, String endpoint) {
public ElasticSearchMetricSender(RestClient cli, String host, String index, String user, String pwd, String endpoint) {
this.client = cli;
this.esIndex = index;
this.metricList = new LinkedList<String>();
this.authUser = user.trim();
this.authPwd = pwd.trim();
this.awsEndpoint = endpoint;
this.host = host;
}

/**
Expand Down Expand Up @@ -73,15 +75,16 @@ public void addToList(String metric) {
/**
* This method sets the Basic Authorization header to requests
*/
private Request setAuthorizationHeader(Request request) {
private Request setHeaders(Request request) {
RequestOptions.Builder options = request.getOptions().toBuilder();
if (this.awsEndpoint.equals("") && !this.authPwd.equals("")) {
String encodedCredentials = Base64.getEncoder()
.encodeToString((this.authUser + ":" + this.authPwd).getBytes());
RequestOptions.Builder options = request.getOptions().toBuilder();
options.addHeader("Authorization", "Basic " + encodedCredentials);
request.setOptions(options);


}
options.addHeader("Host", host);
request.setOptions(options);
return request;
}

Expand All @@ -90,7 +93,7 @@ private Request setAuthorizationHeader(Request request) {
*/
public void createIndex() {
try {
this.client.performRequest(setAuthorizationHeader(new Request("PUT", "/" + this.esIndex)));
this.client.performRequest(setHeaders(new Request("PUT", "/" + this.esIndex)));
} catch (IOException e) {
logger.info("Index already exists!");
}
Expand All @@ -100,7 +103,7 @@ public int getElasticSearchVersion() {
Request request = new Request("GET", "/" );
int elasticSearchVersion = -1;
try {
Response response = this.client.performRequest(setAuthorizationHeader(request));
Response response = this.client.performRequest(setHeaders(request));
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK && logger.isErrorEnabled()) {
logger.error("Unable to perform request to ElasticSearch engine for index {}. Response status: {}",
this.esIndex, response.getStatusLine().toString());
Expand Down Expand Up @@ -149,7 +152,7 @@ public void sendRequest(int elasticSearchVersionPrefix) {

try {

Response response = this.client.performRequest(setAuthorizationHeader(request));
Response response = this.client.performRequest(setHeaders(request));

if (logger.isErrorEnabled()) {
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.apache.jmeter.JMeter;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.util.JMeterUtils;
Expand Down Expand Up @@ -119,7 +118,8 @@ public void setupTest(BackendListenerContext context) throws Exception {
public void onFailure(Node node) {
logger.error("Error with node: " + node.toString());
}
}).build();
})
.build();
} else {
AWS4Signer signer = new AWS4Signer();
signer.setServiceName(SERVICE_NAME);
Expand All @@ -133,7 +133,9 @@ public void onFailure(Node node) {
convertParameterToSet(context, ES_SAMPLE_FILTER, this.filters);
convertParameterToSet(context, ES_FIELDS, this.fields);

this.sender = new ElasticSearchMetricSender(client, context.getParameter(ES_INDEX).toLowerCase(),
this.sender = new ElasticSearchMetricSender(client,
context.getParameter(ES_HOST),
context.getParameter(ES_INDEX).toLowerCase(),
context.getParameter(ES_AUTH_USER), context.getParameter(ES_AUTH_PWD),
context.getParameter(ES_AWS_ENDPOINT));
this.sender.createIndex();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onFailure(Node node) {
System.err.println("Error with node: " + node.toString());
}
}).setMaxRetryTimeoutMillis(60000).build();
sender = new ElasticSearchMetricSender(client, "test_" + sdf.format(new Date()), "logstashTest",
sender = new ElasticSearchMetricSender(client, "", "test_" + sdf.format(new Date()), "logstashTest",
"logstashTest", "");
}

Expand Down