Skip to content

Commit 75cbb04

Browse files
Adrian Coleadriancole
Adrian Cole
authored andcommitted
Adds ES_TIMEOUT to override the default 10second ES api request timeout
Implicitly, OkHttp defaults to 10seconds per request. This makes it possible to override that for lengthy searches. `ES_TIMEOUT` accepts milliseconds and is used for connect, read and write socket timeouts. Fixes openzipkin#1814
1 parent 9b88d12 commit 75cbb04

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

zipkin-autoconfigure/storage-elasticsearch-http/src/main/java/zipkin/autoconfigure/storage/elasticsearch/http/ZipkinElasticsearchHttpStorageProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class ZipkinElasticsearchHttpStorageProperties implements Serializable {
5252
private HttpLoggingInterceptor.Level httpLogging;
5353
/** When true, Redundantly queries indexes made with pre v1.31 collectors. Defaults to true. */
5454
private boolean legacyReadsEnabled = true;
55+
/**
56+
* Controls the connect, read and write socket timeouts (in milliseconds) for Elasticsearch Api
57+
* requests. Defaults to 10000 (10 seconds)
58+
*/
59+
private int timeout = 10_000;
5560

5661
public String getPipeline() {
5762
return pipeline;
@@ -161,6 +166,14 @@ public void setLegacyReadsEnabled(boolean legacyReadsEnabled) {
161166
this.legacyReadsEnabled = legacyReadsEnabled;
162167
}
163168

169+
public int getTimeout() {
170+
return timeout;
171+
}
172+
173+
public void setTimeout(int timeout) {
174+
this.timeout = timeout;
175+
}
176+
164177
public ElasticsearchHttpStorage.Builder toBuilder(OkHttpClient client) {
165178
ElasticsearchHttpStorage.Builder builder = ElasticsearchHttpStorage.builder(client);
166179
if (hosts != null) builder.hosts(hosts);

zipkin-autoconfigure/storage-elasticsearch-http/src/main/java/zipkin/autoconfigure/storage/elasticsearch/http/ZipkinElasticsearchOkHttpAutoConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515

1616
import java.util.Collections;
1717
import java.util.List;
18+
import java.util.concurrent.TimeUnit;
1819
import okhttp3.Interceptor;
1920
import okhttp3.OkHttpClient;
2021
import org.springframework.beans.factory.annotation.Autowired;
2122
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.beans.factory.annotation.Value;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2325
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2426
import org.springframework.context.annotation.Bean;
@@ -53,14 +55,19 @@ public class ZipkinElasticsearchOkHttpAutoConfiguration {
5355
@Bean
5456
@Qualifier("zipkinElasticsearchHttp")
5557
@ConditionalOnMissingBean
56-
OkHttpClient elasticsearchOkHttpClient() {
58+
OkHttpClient elasticsearchOkHttpClient(
59+
@Value("${zipkin.storage.elasticsearch.timeout:10000}") int timeout
60+
) {
5761
OkHttpClient.Builder builder = elasticsearchOkHttpClientBuilder != null
5862
? elasticsearchOkHttpClientBuilder
5963
: new OkHttpClient.Builder();
6064

6165
for (Interceptor interceptor : networkInterceptors) {
6266
builder.addNetworkInterceptor(interceptor);
6367
}
68+
builder.connectTimeout(timeout, TimeUnit.MILLISECONDS);
69+
builder.readTimeout(timeout, TimeUnit.MILLISECONDS);
70+
builder.writeTimeout(timeout, TimeUnit.MILLISECONDS);
6471
return builder.build();
6572
}
6673
}

zipkin-autoconfigure/storage-elasticsearch-http/src/test/java/zipkin/storage/elasticsearch/http/ZipkinElasticsearchHttpStorageAutoConfigurationTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,50 @@ public void usesInterceptorsQualifiedWith_zipkinElasticsearchHttp() {
207207
.containsOnlyOnce(InterceptorConfiguration.one, InterceptorConfiguration.two);
208208
}
209209

210+
@Test
211+
public void timeout_defaultsTo10Seconds() {
212+
context = new AnnotationConfigApplicationContext();
213+
addEnvironment(context,
214+
"zipkin.storage.type:elasticsearch",
215+
"zipkin.storage.elasticsearch.hosts:http://host1:9200"
216+
);
217+
context.register(PropertyPlaceholderAutoConfiguration.class,
218+
ZipkinElasticsearchOkHttpAutoConfiguration.class,
219+
InterceptorConfiguration.class);
220+
context.refresh();
221+
222+
OkHttpClient client = context.getBean(OkHttpClient.class);
223+
assertThat(client.connectTimeoutMillis())
224+
.isEqualTo(10_000);
225+
assertThat(client.readTimeoutMillis())
226+
.isEqualTo(10_000);
227+
assertThat(client.writeTimeoutMillis())
228+
.isEqualTo(10_000);
229+
}
230+
231+
@Test
232+
public void timeout_override() {
233+
context = new AnnotationConfigApplicationContext();
234+
int timeout = 30_000;
235+
addEnvironment(context,
236+
"zipkin.storage.type:elasticsearch",
237+
"zipkin.storage.elasticsearch.hosts:http://host1:9200",
238+
"zipkin.storage.elasticsearch.timeout:" + timeout
239+
);
240+
context.register(PropertyPlaceholderAutoConfiguration.class,
241+
ZipkinElasticsearchOkHttpAutoConfiguration.class,
242+
InterceptorConfiguration.class);
243+
context.refresh();
244+
245+
OkHttpClient client = context.getBean(OkHttpClient.class);
246+
assertThat(client.connectTimeoutMillis())
247+
.isEqualTo(timeout);
248+
assertThat(client.readTimeoutMillis())
249+
.isEqualTo(timeout);
250+
assertThat(client.writeTimeoutMillis())
251+
.isEqualTo(timeout);
252+
}
253+
210254
@Test
211255
public void strictTraceId_defaultsToTrue() {
212256
context = new AnnotationConfigApplicationContext();

zipkin-server/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ The following apply when `STORAGE_TYPE` is set to `elasticsearch`:
175175
files, or ec2 profiles) to sign outbound requests to the cluster.
176176
* `ES_PIPELINE`: Only valid when the destination is Elasticsearch 5.x. Indicates the ingest
177177
pipeline used before spans are indexed. No default.
178+
* `ES_TIMEOUT`: Controls the connect, read and write socket timeouts (in milliseconds) for
179+
Elasticsearch Api. Defaults to 10000 (10 seconds)
178180
* `ES_MAX_REQUESTS`: Only valid when the transport is http. Sets maximum in-flight requests from
179181
this process to any Elasticsearch host. Defaults to 64.
180182
* `ES_AWS_DOMAIN`: The name of the AWS-hosted elasticsearch domain to use. Supercedes any set

zipkin-server/src/main/resources/zipkin-server-shared.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ zipkin:
102102
hosts: ${ES_HOSTS:}
103103
pipeline: ${ES_PIPELINE:}
104104
max-requests: ${ES_MAX_REQUESTS:64}
105+
timeout: ${ES_TIMEOUT:10000}
105106
aws:
106107
domain: ${ES_AWS_DOMAIN:}
107108
region: ${ES_AWS_REGION:}

0 commit comments

Comments
 (0)