Skip to content

Commit

Permalink
Makes AWS Elasticsearch errors a bit more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Cole authored and adriancole committed Nov 16, 2017
1 parent 5a9998d commit 5037e4f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;
import zipkin.storage.elasticsearch.http.ElasticsearchHttpStorage;

import static zipkin.internal.Util.checkArgument;
import static zipkin.internal.Util.checkNotNull;
import static zipkin2.elasticsearch.internal.JsonReaders.enterPath;

Expand All @@ -37,19 +37,29 @@ final class ElasticsearchDomainEndpoint implements ElasticsearchHttpStorage.Host
ElasticsearchDomainEndpoint(OkHttpClient client, HttpUrl baseUrl, String domain) {
this.client = checkNotNull(client, "client");
this.describeElasticsearchDomain = new Request.Builder().url(checkNotNull(baseUrl, "baseUrl")
.newBuilder("2015-01-01/es/domain")
.addPathSegment(checkNotNull(domain, "domain")).build()).build();
.newBuilder("2015-01-01/es/domain")
.addPathSegment(checkNotNull(domain, "domain")).build()).build();
}

@Override public List<String> get() {
try (Response response = client.newCall(describeElasticsearchDomain).execute()) {
String body = response.body().string();
if (!response.isSuccessful()) {
throw new IllegalStateException(response.body().string());
String message = describeElasticsearchDomain.url().encodedPath()
+ " failed with status " + response.code();
if (!body.isEmpty()) message += ": " + body;
throw new IllegalStateException(message);
}

JsonReader endpointReader =
enterPath(JsonReader.of(response.body().source()), "DomainStatus", "Endpoint");
checkArgument(endpointReader != null, "DomainStatus.Endpoint wasn't present in response");
enterPath(JsonReader.of(new Buffer().writeUtf8(body)), "DomainStatus", "Endpoint");

if (endpointReader == null) {
throw new IllegalStateException(
"DomainStatus.Endpoint wasn't present in response: " + body);
}

// TODO: DomainStatus.Endpoints which could also be present

String endpoint = endpointReader.nextString();
if (!endpoint.startsWith("https://")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2015-2017 The OpenZipkin Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package zipkin.autoconfigure.storage.elasticsearch.aws;

import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.assertj.core.api.Assertions.assertThat;

public class ElasticsearchDomainEndpointTest {
@Rule public ExpectedException thrown = ExpectedException.none();
@Rule public MockWebServer es = new MockWebServer();

ElasticsearchDomainEndpoint client = new ElasticsearchDomainEndpoint(
new OkHttpClient(),
es.url(""),
"zipkin53"
);

@Test public void publicUrl() throws Exception {
es.enqueue(new MockResponse().setBody("{\n"
+ " \"DomainStatus\": {\n"
+ " \"Endpoint\": \"search-zipkin53-mhdyquzbwwzwvln6phfzr3lldi.ap-southeast-1.es.amazonaws.com\",\n"
+ " \"Endpoints\": null\n"
+ " }\n"
+ "}"));

assertThat(client.get())
.containsExactly(
"https://search-zipkin53-mhdyquzbwwzwvln6phfzr3lldi.ap-southeast-1.es.amazonaws.com");
}

/** Not quite sure why, but some have reported receiving no URLs at all */
@Test public void noUrl() throws Exception {
// simplified.. URL is usually the only thing actually missing
String body = "{\"DomainStatus\": {}}";
es.enqueue(new MockResponse().setBody(body));

thrown.expect(IllegalStateException.class);
thrown.expectMessage("DomainStatus.Endpoint wasn't present in response: " + body);

client.get();
}

/** Not quite sure why, but some have reported receiving no URLs at all */
@Test public void unauthorizedNoMessage() throws Exception {
es.enqueue(new MockResponse().setResponseCode(403));

thrown.expect(IllegalStateException.class);
thrown.expectMessage("/2015-01-01/es/domain/zipkin53 failed with status 403");

client.get();
}
}

0 comments on commit 5037e4f

Please sign in to comment.