Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit 27059eb

Browse files
authored
Make UdpSender lazy to be able to recover from early DNS issues (#726)
Fixes #369 Signed-off-by: Phillip Schichtel <[email protected]>
1 parent 9b0ed16 commit 27059eb

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

jaeger-thrift/src/main/java/io/jaegertracing/thrift/internal/senders/UdpSender.java

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ public class UdpSender extends ThriftSender {
2727
public static final String DEFAULT_AGENT_UDP_HOST = "localhost";
2828
public static final int DEFAULT_AGENT_UDP_COMPACT_PORT = 6831;
2929

30-
@ToString.Exclude private Agent.Client agentClient;
31-
@ToString.Exclude private ThriftUdpTransport udpTransport;
30+
private final String host;
31+
private final int port;
32+
33+
@ToString.Exclude private volatile Agent.Client agentClient;
34+
@ToString.Exclude private volatile ThriftUdpTransport udpTransport;
3235

3336
/**
3437
* This constructor expects Jaeger running running on {@value #DEFAULT_AGENT_UDP_HOST}
@@ -54,14 +57,30 @@ public UdpSender(String host, int port, int maxPacketSize) {
5457
port = DEFAULT_AGENT_UDP_COMPACT_PORT;
5558
}
5659

57-
udpTransport = ThriftUdpTransport.newThriftUdpClient(host, port);
58-
agentClient = new Agent.Client(protocolFactory.getProtocol(udpTransport));
60+
this.host = host;
61+
this.port = port;
62+
}
63+
64+
private Agent.Client getAgentClient() {
65+
Agent.Client localRef = this.agentClient;
66+
if (localRef == null) {
67+
synchronized (this) {
68+
localRef = this.agentClient;
69+
if (localRef == null) {
70+
udpTransport = ThriftUdpTransport.newThriftUdpClient(host, port);
71+
localRef = new Agent.Client(protocolFactory.getProtocol(udpTransport));
72+
this.agentClient = localRef;
73+
}
74+
}
75+
}
76+
77+
return localRef;
5978
}
6079

6180
@Override
6281
public void send(Process process, List<io.jaegertracing.thriftjava.Span> spans) throws SenderException {
6382
try {
64-
agentClient.emitBatch(new Batch(process, spans));
83+
getAgentClient().emitBatch(new Batch(process, spans));
6584
} catch (Exception e) {
6685
throw new SenderException(String.format("Could not send %d spans", spans.size()), e, spans.size());
6786
}
@@ -72,7 +91,11 @@ public int close() throws SenderException {
7291
try {
7392
return super.close();
7493
} finally {
75-
udpTransport.close();
94+
synchronized (this) {
95+
if (udpTransport != null) {
96+
udpTransport.close();
97+
}
98+
}
7699
}
77100
}
78101
}

jaeger-thrift/src/test/java/io/jaegertracing/thrift/internal/senders/ThriftSenderFactoryTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static org.junit.Assert.assertTrue;
1818

1919
import io.jaegertracing.Configuration;
20-
import io.jaegertracing.internal.senders.NoopSender;
2120
import io.jaegertracing.spi.Sender;
2221
import org.junit.Before;
2322
import org.junit.Test;
@@ -47,7 +46,7 @@ public void testSenderWithAgentDataFromEnv() {
4746
System.setProperty(Configuration.JAEGER_AGENT_HOST, "jaeger-agent");
4847
System.setProperty(Configuration.JAEGER_AGENT_PORT, "6832");
4948
Sender sender = Configuration.SenderConfiguration.fromEnv().getSender();
50-
assertTrue(sender instanceof NoopSender);
49+
assertTrue(sender instanceof UdpSender);
5150
}
5251

5352
@Test
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2020, The Jaeger Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package io.jaegertracing.thrift.internal.senders;
15+
16+
import io.jaegertracing.internal.exceptions.SenderException;
17+
import org.junit.Test;
18+
19+
import java.net.SocketException;
20+
import java.util.Collections;
21+
22+
import static org.junit.Assert.assertTrue;
23+
import static org.junit.Assert.fail;
24+
25+
public class UdpLazinessTest {
26+
@Test
27+
public void testLazyInitializationOfAgent() {
28+
UdpSender udpSender = new UdpSender("agent.acme.test", 55555, 0);
29+
30+
try {
31+
udpSender.send(null, Collections.emptyList());
32+
fail("Send should fail!");
33+
} catch (SenderException e) {
34+
assertTrue("send should throw a socket exception", e.getCause().getCause() instanceof SocketException);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)