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

Make UdpSender lazy to be able to recover from early DNS issues #726

Merged
merged 1 commit into from Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -27,8 +27,11 @@ public class UdpSender extends ThriftSender {
public static final String DEFAULT_AGENT_UDP_HOST = "localhost";
public static final int DEFAULT_AGENT_UDP_COMPACT_PORT = 6831;

@ToString.Exclude private Agent.Client agentClient;
@ToString.Exclude private ThriftUdpTransport udpTransport;
private final String host;
private final int port;

@ToString.Exclude private volatile Agent.Client agentClient;
@ToString.Exclude private volatile ThriftUdpTransport udpTransport;

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

udpTransport = ThriftUdpTransport.newThriftUdpClient(host, port);
agentClient = new Agent.Client(protocolFactory.getProtocol(udpTransport));
this.host = host;
this.port = port;
}

private Agent.Client getAgentClient() {
Agent.Client localRef = this.agentClient;
if (localRef == null) {
synchronized (this) {
localRef = this.agentClient;
if (localRef == null) {
udpTransport = ThriftUdpTransport.newThriftUdpClient(host, port);
localRef = new Agent.Client(protocolFactory.getProtocol(udpTransport));
this.agentClient = localRef;
}
}
}

return localRef;
}

@Override
public void send(Process process, List<io.jaegertracing.thriftjava.Span> spans) throws SenderException {
try {
agentClient.emitBatch(new Batch(process, spans));
getAgentClient().emitBatch(new Batch(process, spans));
} catch (Exception e) {
throw new SenderException(String.format("Could not send %d spans", spans.size()), e, spans.size());
}
Expand All @@ -72,7 +91,11 @@ public int close() throws SenderException {
try {
return super.close();
} finally {
udpTransport.close();
synchronized (this) {
if (udpTransport != null) {
udpTransport.close();
}
}
}
}
}
Expand Up @@ -17,7 +17,6 @@
import static org.junit.Assert.assertTrue;

import io.jaegertracing.Configuration;
import io.jaegertracing.internal.senders.NoopSender;
import io.jaegertracing.spi.Sender;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -47,7 +46,7 @@ public void testSenderWithAgentDataFromEnv() {
System.setProperty(Configuration.JAEGER_AGENT_HOST, "jaeger-agent");
System.setProperty(Configuration.JAEGER_AGENT_PORT, "6832");
Sender sender = Configuration.SenderConfiguration.fromEnv().getSender();
assertTrue(sender instanceof NoopSender);
assertTrue(sender instanceof UdpSender);
}

@Test
Expand Down
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2020, The Jaeger 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 io.jaegertracing.thrift.internal.senders;

import io.jaegertracing.internal.exceptions.SenderException;
import org.junit.Test;

import java.net.SocketException;
import java.util.Collections;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class UdpLazinessTest {
@Test
public void testLazyInitializationOfAgent() {
UdpSender udpSender = new UdpSender("agent.acme.test", 55555, 0);

try {
udpSender.send(null, Collections.emptyList());
fail("Send should fail!");
} catch (SenderException e) {
assertTrue("send should throw a socket exception", e.getCause().getCause() instanceof SocketException);
}
}
}