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

Commit f325ae1

Browse files
isaachierblack-adder
authored andcommitted
Fix host IP formatting and improve local IP API (#4)
1 parent dd369e2 commit f325ae1

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if(HUNTER_ENABLED)
3939
endif()
4040
endif()
4141

42-
project(jaegertracing VERSION 0.0.5)
42+
project(jaegertracing VERSION 0.0.7)
4343

4444
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR
4545
CMAKE_CXX_COMPILER_ID MATCHES "Clang")

src/jaegertracing/Tracer.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Tracer : public opentracing::Tracer,
194194
const std::shared_ptr<logging::Logger>& logger,
195195
const std::shared_ptr<metrics::Metrics>& metrics)
196196
: _serviceName(serviceName)
197-
, _hostIPv4(net::IPAddress::host(AF_INET))
197+
, _hostIPv4(net::IPAddress::localIP(AF_INET))
198198
, _sampler(sampler)
199199
, _reporter(reporter)
200200
, _metrics(metrics)
@@ -219,9 +219,7 @@ class Tracer : public opentracing::Tracer,
219219
_logger->error("Unable to determine this host's IP address");
220220
}
221221
else {
222-
std::ostringstream oss;
223-
oss << _hostIPv4;
224-
_tags.push_back(Tag(kTracerIPTagKey, oss.str()));
222+
_tags.push_back(Tag(kTracerIPTagKey, _hostIPv4.host()));
225223
}
226224

227225
std::random_device device;

src/jaegertracing/TracerTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ TEST(Tracer, testTracer)
115115
options.references.emplace_back(opentracing::SpanReferenceType::ChildOfRef,
116116
&debugCtx);
117117

118+
const auto& tags = tracer->tags();
119+
auto itr = std::find_if(
120+
std::begin(tags),
121+
std::end(tags),
122+
[](const Tag& tag) { return tag.key() == kTracerIPTagKey; });
123+
ASSERT_NE(std::end(tags), itr);
124+
ASSERT_TRUE(itr->value().is<std::string>());
125+
ASSERT_EQ(net::IPAddress::v4(itr->value().get<std::string>()).host(),
126+
net::IPAddress::localIP(AF_INET).host());
127+
118128
std::unique_ptr<Span> span(static_cast<Span*>(
119129
tracer->StartSpanWithOptions("test-operation", options).release()));
120130
ASSERT_TRUE(static_cast<bool>(span));

src/jaegertracing/net/IPAddress.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ struct IfAddrDeleter : public std::function<void(ifaddrs*)> {
3434

3535
} // anonymous namespace
3636

37-
IPAddress IPAddress::host(int family)
37+
IPAddress IPAddress::localIP(int family)
3838
{
39-
return host([family](const ifaddrs* ifAddr) {
39+
return localIP([family](const ifaddrs* ifAddr) {
4040
return ifAddr->ifa_addr->sa_family == family;
4141
});
4242
}
4343

44-
IPAddress IPAddress::host(std::function<bool(const ifaddrs*)> filter)
44+
IPAddress IPAddress::localIP(std::function<bool(const ifaddrs*)> filter)
4545
{
4646
auto* ifAddrRawPtr = static_cast<ifaddrs*>(nullptr);
4747
getifaddrs(&ifAddrRawPtr);

src/jaegertracing/net/IPAddress.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ class IPAddress {
6464
return versionFromString(ip, port, AF_INET6);
6565
}
6666

67-
static IPAddress host(int family);
67+
static IPAddress localIP(int family);
6868

69-
static IPAddress host(std::function<bool(const ifaddrs*)> filter);
69+
static IPAddress localIP(std::function<bool(const ifaddrs*)> filter);
7070

7171
IPAddress()
7272
: _addr()

0 commit comments

Comments
 (0)