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

Commit 11bbde1

Browse files
BeDeBaMuyurishkuro
authored andcommitted
Remove Thrift headers from Jaeger public headers (#172)
Adresses #121 so that thrift is a private dependency of jaeger. Signed-off-by: Benoit De Backer <[email protected]>
1 parent b083108 commit 11bbde1

19 files changed

+253
-188
lines changed

src/jaegertracing/LogRecord.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,25 @@
1515
*/
1616

1717
#include "jaegertracing/LogRecord.h"
18+
#include "jaegertracing/thrift-gen/jaeger_types.h"
19+
20+
namespace jaegertracing {
21+
void LogRecord::thrift(thrift::Log& log) const
22+
{
23+
log.__set_timestamp(std::chrono::duration_cast<std::chrono::microseconds>(
24+
_timestamp.time_since_epoch())
25+
.count());
26+
27+
std::vector<thrift::Tag> fields;
28+
fields.reserve(_fields.size());
29+
std::transform(std::begin(_fields),
30+
std::end(_fields),
31+
std::back_inserter(fields),
32+
[](const Tag& tag) {
33+
thrift::Tag thriftTag;
34+
tag.thrift(thriftTag);
35+
return thriftTag;
36+
});
37+
log.__set_fields(fields);
38+
}
39+
} // namespace jaegertracing

src/jaegertracing/LogRecord.h

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "jaegertracing/Compilers.h"
2121
#include "jaegertracing/Tag.h"
22-
#include "jaegertracing/thrift-gen/jaeger_types.h"
2322
#include <algorithm>
2423
#include <chrono>
2524
#include <iterator>
@@ -29,6 +28,9 @@
2928
#include <opentracing/span.h>
3029

3130
namespace jaegertracing {
31+
namespace thrift {
32+
class Log;
33+
}
3234

3335
class LogRecord {
3436
public:
@@ -48,34 +50,17 @@ class LogRecord {
4850
{
4951
}
5052

51-
LogRecord(const opentracing::LogRecord & other)
52-
: _timestamp(other.timestamp),
53-
_fields(other.fields.begin(), other.fields.end())
53+
LogRecord(const opentracing::LogRecord& other)
54+
: _timestamp(other.timestamp)
55+
, _fields(other.fields.begin(), other.fields.end())
5456
{
5557
}
5658

5759
const Clock::time_point& timestamp() const { return _timestamp; }
5860

5961
const std::vector<Tag>& fields() const { return _fields; }
6062

61-
thrift::Log thrift() const
62-
{
63-
thrift::Log log;
64-
log.__set_timestamp(
65-
std::chrono::duration_cast<std::chrono::microseconds>(
66-
_timestamp.time_since_epoch())
67-
.count());
68-
69-
std::vector<thrift::Tag> fields;
70-
fields.reserve(_fields.size());
71-
std::transform(std::begin(_fields),
72-
std::end(_fields),
73-
std::back_inserter(fields),
74-
[](const Tag& tag) { return tag.thrift(); });
75-
log.__set_fields(fields);
76-
77-
return log;
78-
}
63+
void thrift(thrift::Log& log) const;
7964

8065
private:
8166
Clock::time_point _timestamp;

src/jaegertracing/Reference.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@
1515
*/
1616

1717
#include "jaegertracing/Reference.h"
18+
#include "jaegertracing/thrift-gen/jaeger_types.h"
19+
20+
namespace jaegertracing {
21+
void Reference::thrift(thrift::SpanRef& spanRef) const
22+
{
23+
switch (_type) {
24+
case Type::ChildOfRef: {
25+
spanRef.__set_refType(thrift::SpanRefType::CHILD_OF);
26+
} break;
27+
case Type::FollowsFromRef: {
28+
spanRef.__set_refType(thrift::SpanRefType::FOLLOWS_FROM);
29+
} break;
30+
default: {
31+
std::ostringstream oss;
32+
oss << "Invalid span reference type " << static_cast<int>(_type)
33+
<< ", context " << _spanContext;
34+
throw std::invalid_argument(oss.str());
35+
} break;
36+
}
37+
spanRef.__set_traceIdHigh(_spanContext.traceID().high());
38+
spanRef.__set_traceIdLow(_spanContext.traceID().low());
39+
spanRef.__set_spanId(_spanContext.spanID());
40+
}
41+
} // namespace jaegertracing

src/jaegertracing/Reference.h

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424

2525
#include "jaegertracing/Compilers.h"
2626
#include "jaegertracing/SpanContext.h"
27-
#include "jaegertracing/thrift-gen/jaeger_types.h"
2827

2928
namespace jaegertracing {
29+
namespace thrift {
30+
class SpanRef;
31+
}
3032

3133
class Reference {
3234
public:
@@ -42,28 +44,7 @@ class Reference {
4244

4345
Type type() const { return _type; }
4446

45-
thrift::SpanRef thrift() const
46-
{
47-
thrift::SpanRef spanRef;
48-
switch (_type) {
49-
case Type::ChildOfRef: {
50-
spanRef.__set_refType(thrift::SpanRefType::CHILD_OF);
51-
} break;
52-
case Type::FollowsFromRef: {
53-
spanRef.__set_refType(thrift::SpanRefType::FOLLOWS_FROM);
54-
} break;
55-
default: {
56-
std::ostringstream oss;
57-
oss << "Invalid span reference type " << static_cast<int>(_type)
58-
<< ", context " << _spanContext;
59-
throw std::invalid_argument(oss.str());
60-
} break;
61-
}
62-
spanRef.__set_traceIdHigh(_spanContext.traceID().high());
63-
spanRef.__set_traceIdLow(_spanContext.traceID().low());
64-
spanRef.__set_spanId(_spanContext.spanID());
65-
return spanRef;
66-
}
47+
void thrift(thrift::SpanRef& spanRef) const;
6748

6849
private:
6950
SpanContext _spanContext;

src/jaegertracing/ReferenceTest.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "jaegertracing/Reference.h"
1818
#include "jaegertracing/SpanContext.h"
19+
#include "jaegertracing/thrift-gen/jaeger_types.h"
1920
#include <gtest/gtest.h>
2021
#include <stdexcept>
2122

@@ -25,11 +26,14 @@ TEST(Reference, testThriftConversion)
2526
{
2627
const SpanContext context;
2728
const Reference childRef(context, Reference::Type::ChildOfRef);
28-
ASSERT_NO_THROW(childRef.thrift());
29+
thrift::SpanRef thriftChildRef;
30+
ASSERT_NO_THROW(childRef.thrift(thriftChildRef));
2931
const Reference followsFromRef(context, Reference::Type::FollowsFromRef);
30-
ASSERT_NO_THROW(followsFromRef.thrift());
32+
thrift::SpanRef thriftFollowsFromRef;
33+
ASSERT_NO_THROW(followsFromRef.thrift(thriftFollowsFromRef));
3134
const Reference invalidRef(context, static_cast<Reference::Type>(-1));
32-
ASSERT_THROW(invalidRef.thrift(), std::invalid_argument);
35+
thrift::SpanRef thriftInvalidRef;
36+
ASSERT_THROW(invalidRef.thrift(thriftInvalidRef), std::invalid_argument);
3337
}
3438

3539
} // namespace jaegertracing

src/jaegertracing/Span.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "jaegertracing/Span.h"
1818
#include "jaegertracing/Tracer.h"
1919
#include "jaegertracing/baggage/BaggageSetter.h"
20+
#include "jaegertracing/thrift-gen/jaeger_types.h"
2021
#include <cassert>
2122
#include <cstdint>
2223
#include <istream>
@@ -163,4 +164,58 @@ void Span::setSamplingPriority(const opentracing::Value& value)
163164
_context.debugID());
164165
}
165166

167+
void Span::thrift(thrift::Span& span) const
168+
{
169+
std::lock_guard<std::mutex> lock(_mutex);
170+
span.__set_traceIdHigh(_context.traceID().high());
171+
span.__set_traceIdLow(_context.traceID().low());
172+
span.__set_spanId(_context.spanID());
173+
span.__set_parentSpanId(_context.parentID());
174+
span.__set_operationName(_operationName);
175+
176+
std::vector<thrift::SpanRef> refs;
177+
refs.reserve(_references.size());
178+
std::transform(std::begin(_references),
179+
std::end(_references),
180+
std::back_inserter(refs),
181+
[](const Reference& ref) {
182+
thrift::SpanRef thriftRef;
183+
ref.thrift(thriftRef);
184+
return thriftRef;
185+
});
186+
span.__set_references(refs);
187+
188+
span.__set_flags(_context.flags());
189+
span.__set_startTime(std::chrono::duration_cast<std::chrono::microseconds>(
190+
_startTimeSystem.time_since_epoch())
191+
.count());
192+
span.__set_duration(
193+
std::chrono::duration_cast<std::chrono::microseconds>(_duration)
194+
.count());
195+
196+
std::vector<thrift::Tag> tags;
197+
tags.reserve(_tags.size());
198+
std::transform(std::begin(_tags),
199+
std::end(_tags),
200+
std::back_inserter(tags),
201+
[](const Tag& tag) {
202+
thrift::Tag thriftTag;
203+
tag.thrift(thriftTag);
204+
return thriftTag;
205+
});
206+
span.__set_tags(tags);
207+
208+
std::vector<thrift::Log> logs;
209+
logs.reserve(_logs.size());
210+
std::transform(std::begin(_logs),
211+
std::end(_logs),
212+
std::back_inserter(logs),
213+
[](const LogRecord& log) {
214+
thrift::Log thriftLog;
215+
log.thrift(thriftLog);
216+
return thriftLog;
217+
});
218+
span.__set_logs(logs);
219+
}
220+
166221
} // namespace jaegertracing

src/jaegertracing/Span.h

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
#include "jaegertracing/Reference.h"
2828
#include "jaegertracing/SpanContext.h"
2929
#include "jaegertracing/Tag.h"
30-
#include "jaegertracing/thrift-gen/jaeger_types.h"
3130

3231
namespace jaegertracing {
3332

3433
class Tracer;
3534

35+
namespace thrift {
36+
class Span;
37+
}
38+
3639
class Span : public opentracing::Span {
3740
public:
3841
using SteadyClock = opentracing::SteadyClock;
@@ -104,51 +107,7 @@ class Span : public opentracing::Span {
104107

105108
friend void swap(Span& lhs, Span& rhs) { lhs.swap(rhs); }
106109

107-
thrift::Span thrift() const
108-
{
109-
std::lock_guard<std::mutex> lock(_mutex);
110-
thrift::Span span;
111-
span.__set_traceIdHigh(_context.traceID().high());
112-
span.__set_traceIdLow(_context.traceID().low());
113-
span.__set_spanId(_context.spanID());
114-
span.__set_parentSpanId(_context.parentID());
115-
span.__set_operationName(_operationName);
116-
117-
std::vector<thrift::SpanRef> refs;
118-
refs.reserve(_references.size());
119-
std::transform(std::begin(_references),
120-
std::end(_references),
121-
std::back_inserter(refs),
122-
[](const Reference& ref) { return ref.thrift(); });
123-
span.__set_references(refs);
124-
125-
span.__set_flags(_context.flags());
126-
span.__set_startTime(
127-
std::chrono::duration_cast<std::chrono::microseconds>(
128-
_startTimeSystem.time_since_epoch())
129-
.count());
130-
span.__set_duration(
131-
std::chrono::duration_cast<std::chrono::microseconds>(_duration)
132-
.count());
133-
134-
std::vector<thrift::Tag> tags;
135-
tags.reserve(_tags.size());
136-
std::transform(std::begin(_tags),
137-
std::end(_tags),
138-
std::back_inserter(tags),
139-
[](const Tag& tag) { return tag.thrift(); });
140-
span.__set_tags(tags);
141-
142-
std::vector<thrift::Log> logs;
143-
logs.reserve(_logs.size());
144-
std::transform(std::begin(_logs),
145-
std::end(_logs),
146-
std::back_inserter(logs),
147-
[](const LogRecord& log) { return log.thrift(); });
148-
span.__set_logs(logs);
149-
150-
return span;
151-
}
110+
void thrift(thrift::Span& span) const;
152111

153112
template <typename Stream>
154113
void print(Stream& out) const

src/jaegertracing/SpanTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "jaegertracing/Span.h"
18+
#include "jaegertracing/thrift-gen/jaeger_types.h"
1819
#include <gtest/gtest.h>
1920
#include <string>
2021

@@ -25,7 +26,8 @@ TEST(Span, testThriftConversion)
2526
const Span span;
2627
ASSERT_TRUE(span.serviceName().empty());
2728
ASSERT_TRUE(span.operationName().empty());
28-
ASSERT_NO_THROW(span.thrift());
29+
thrift::Span thriftSpan;
30+
ASSERT_NO_THROW(span.thrift(thriftSpan));
2931
}
3032

3133
} // namespace jaegertracing

0 commit comments

Comments
 (0)