Repository: pinpoint-apm/pinpoint
Issue: Agent cannot send traces over TCP to the collector
Labels: bug
module:agent

#### What version of pinpoint are you using?
1.8.1, 1.8.2, 1.8.3

#### Describe the bug
Agent cannot send traces over to the collector when using TCP to send spans.
The default UDP works fine.

#### What did you do to trigger the bug?
Steps to reproduce, for example:
1. Configure collector and agent to send spans over TCP.
2. Trigger a trace.

#### Expected behavior
Spans should be sent successfully.


Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SpanDataSenderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SpanDataSenderProvider.java
index d487bfdcfb05..4e2f0e435a90 100644
--- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SpanDataSenderProvider.java
+++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/SpanDataSenderProvider.java
@@ -24,7 +24,9 @@
 import com.navercorp.pinpoint.profiler.context.module.SpanStatClientFactory;
 import com.navercorp.pinpoint.profiler.context.thrift.MessageConverter;
 import com.navercorp.pinpoint.profiler.sender.DataSender;
+import com.navercorp.pinpoint.profiler.sender.MessageSerializer;
 import com.navercorp.pinpoint.profiler.sender.TcpDataSender;
+import com.navercorp.pinpoint.profiler.sender.ThriftMessageSerializer;
 import com.navercorp.pinpoint.profiler.sender.UdpDataSenderFactory;
 import com.navercorp.pinpoint.rpc.client.PinpointClientFactory;
 import org.apache.thrift.TBase;
@@ -75,7 +77,8 @@ public DataSender get() {
             }
 
             PinpointClientFactory pinpointClientFactory = clientFactoryProvider.get();
-            return new TcpDataSender("SpanDataSender", ip, port, pinpointClientFactory);
+            MessageSerializer<byte[]> messageSerializer = new ThriftMessageSerializer(messageConverter);
+            return new TcpDataSender("SpanDataSender", ip, port, pinpointClientFactory, messageSerializer);
         } else {
             UdpDataSenderFactory factory = new UdpDataSenderFactory(ip, port, UDP_EXECUTOR_NAME, writeQueueSize, timeout, sendBufferSize, messageConverter);
             return factory.create(ioType);
diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StatDataSenderProvider.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StatDataSenderProvider.java
index aab1c5993830..9df3301f1dbd 100644
--- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StatDataSenderProvider.java
+++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/StatDataSenderProvider.java
@@ -24,7 +24,9 @@
 import com.navercorp.pinpoint.profiler.context.thrift.BypassMessageConverter;
 import com.navercorp.pinpoint.profiler.context.thrift.MessageConverter;
 import com.navercorp.pinpoint.profiler.sender.DataSender;
+import com.navercorp.pinpoint.profiler.sender.MessageSerializer;
 import com.navercorp.pinpoint.profiler.sender.TcpDataSender;
+import com.navercorp.pinpoint.profiler.sender.ThriftMessageSerializer;
 import com.navercorp.pinpoint.profiler.sender.UdpDataSenderFactory;
 import com.navercorp.pinpoint.rpc.client.PinpointClientFactory;
 import org.apache.thrift.TBase;
@@ -77,7 +79,8 @@ public DataSender get() {
             }
 
             PinpointClientFactory pinpointClientFactory = clientFactoryProvider.get();
-            return new TcpDataSender("StatDataSender", ip, port, pinpointClientFactory);
+            MessageSerializer<byte[]> messageSerializer = new ThriftMessageSerializer(messageConverter);
+            return new TcpDataSender("StatDataSender", ip, port, pinpointClientFactory, messageSerializer);
         } else {
             UdpDataSenderFactory factory = new UdpDataSenderFactory(ip, port, UDP_EXECUTOR_NAME, writeQueueSize, timeout, sendBufferSize, messageConverter);
             return factory.create(ioType);
diff --git a/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/TcpDataSender.java b/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/TcpDataSender.java
index 78d7549f0fdf..94c0986ce780 100644
--- a/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/TcpDataSender.java
+++ b/profiler/src/main/java/com/navercorp/pinpoint/profiler/sender/TcpDataSender.java
@@ -194,24 +194,20 @@ public void stop() {
 
     protected void sendPacket(Object message) {
         try {
-            if (message instanceof TBase<?, ?>) {
-                final byte[] copy = messageSerializer.serializer(message);
-                if (copy == null) {
-                    return;
-                }
-                doSend(copy);
-                return;
-            }
-
             if (message instanceof RequestMessage<?>) {
                 final RequestMessage<?> requestMessage = (RequestMessage<?>) message;
                 if (doRequest(requestMessage)) {
                     return;
                 }
-            } else {
+            }
+
+            final byte[] copy = messageSerializer.serializer(message);
+            if (copy == null) {
                 logger.error("sendPacket fail. invalid dto type:{}", message.getClass());
                 return;
             }
+            doSend(copy);
+
         } catch (Exception e) {
             logger.warn("tcp send fail. Caused:{}", e.getMessage(), e);
         }


Test patch: