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

Commit 1b8ed62

Browse files
ankit-varma10isaachier
authored andcommitted
(bugfix) Unhandled exception - Abort raised when write to the jaeger-… (#80)
* (bugfix) Unhandled exception - Abort raised when write to the jaeger-agent fails Signed-off-by: ankit.varma10 <[email protected]> * (bugfix) review comments - application crashing when agent unavailable Signed-off-by: ankit.varma10 <[email protected]> * Add test case for new exception handling code Signed-off-by: Isaac Hier <[email protected]> * Add coverage for unknown exceptions Signed-off-by: Isaac Hier <[email protected]>
1 parent bcd3d3f commit 1b8ed62

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

src/jaegertracing/UDPTransport.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Uber Technologies, Inc.
2+
* Copyright (c) 2017-2018 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,7 +113,22 @@ int UDPTransport::flush()
113113
thrift::Batch batch;
114114
batch.__set_process(_process);
115115
batch.__set_spans(_spanBuffer);
116-
_client->emitBatch(batch);
116+
117+
try {
118+
_client->emitBatch(batch);
119+
} catch (const std::system_error& ex) {
120+
std::ostringstream oss;
121+
oss << "Could not send span " << ex.what()
122+
<< ", code=" << ex.code().value();
123+
throw Transport::Exception(oss.str(), _spanBuffer.size());
124+
} catch (const std::exception& ex) {
125+
std::ostringstream oss;
126+
oss << "Could not send span " << ex.what();
127+
throw Transport::Exception(oss.str(), _spanBuffer.size());
128+
} catch (...) {
129+
throw Transport::Exception("Could not send span, unknown error",
130+
_spanBuffer.size());
131+
}
117132

118133
resetBuffers();
119134

src/jaegertracing/UDPTransport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class UDPTransport : public Transport {
3636

3737
void close() override { _client->close(); }
3838

39+
protected:
40+
void setClient(std::unique_ptr<utils::UDPClient>&& client)
41+
{
42+
_client = std::move(client);
43+
}
44+
3945
private:
4046
void resetBuffers()
4147
{

src/jaegertracing/UDPTransportTest.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Uber Technologies, Inc.
2+
* Copyright (c) 2017-2018 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,50 @@
2323
#include "jaegertracing/utils/ErrorUtil.h"
2424

2525
namespace jaegertracing {
26+
namespace {
27+
28+
class MockUDPClient : public utils::UDPClient {
29+
public:
30+
enum class ExceptionType { kSystemError, kException, kString };
31+
32+
MockUDPClient(const net::IPAddress& serverAddr,
33+
int maxPacketSize,
34+
ExceptionType type)
35+
: UDPClient(serverAddr, maxPacketSize)
36+
, _type(type)
37+
{
38+
}
39+
40+
private:
41+
void emitBatch(const thrift::Batch& batch) override
42+
{
43+
switch (_type) {
44+
case ExceptionType::kSystemError:
45+
throw std::system_error();
46+
case ExceptionType::kException:
47+
throw std::exception();
48+
default:
49+
assert(_type == ExceptionType::kString);
50+
throw "error";
51+
}
52+
}
53+
54+
ExceptionType _type;
55+
};
56+
57+
class MockUDPTransport : public UDPTransport {
58+
public:
59+
MockUDPTransport(const net::IPAddress& ip,
60+
int maxPacketSize,
61+
MockUDPClient::ExceptionType type)
62+
: UDPTransport(ip, maxPacketSize)
63+
{
64+
setClient(std::unique_ptr<utils::UDPClient>(
65+
new MockUDPClient(ip, maxPacketSize, type)));
66+
}
67+
};
68+
69+
} // anonymous namespace
2670

2771
TEST(UDPTransport, testManyMessages)
2872
{
@@ -40,4 +84,25 @@ TEST(UDPTransport, testManyMessages)
4084
}
4185
}
4286

87+
TEST(UDPTransport, testExceptions)
88+
{
89+
const auto handle = testutils::TracerUtil::installGlobalTracer();
90+
const auto tracer =
91+
std::static_pointer_cast<const Tracer>(opentracing::Tracer::Global());
92+
93+
Span span(tracer);
94+
span.SetOperationName("test");
95+
96+
const MockUDPClient::ExceptionType exceptionTypes[] = {
97+
MockUDPClient::ExceptionType::kSystemError,
98+
MockUDPClient::ExceptionType::kException,
99+
MockUDPClient::ExceptionType::kString
100+
};
101+
for (auto type : exceptionTypes) {
102+
MockUDPTransport sender(net::IPAddress(), 0, type);
103+
sender.append(span);
104+
ASSERT_THROW(sender.flush(), Transport::Exception);
105+
}
106+
}
107+
43108
} // namespace jaegertracing

src/jaegertracing/utils/UDPClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017 Uber Technologies, Inc.
2+
* Copyright (c) 2017-2018 Uber Technologies, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ class UDPClient : public agent::thrift::AgentIf {
5454
if (static_cast<int>(size) > _maxPacketSize) {
5555
std::ostringstream oss;
5656
oss << "Data does not fit within one UDP packet"
57-
"; size "
57+
", size "
5858
<< size << ", max " << _maxPacketSize << ", spans "
5959
<< batch.spans.size();
6060
throw std::logic_error(oss.str());

0 commit comments

Comments
 (0)