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

Commit

Permalink
Remove Thrift headers from Jaeger public headers (#172)
Browse files Browse the repository at this point in the history
Adresses #121
so that thrift is a private dependency of jaeger.

Signed-off-by: Benoit De Backer <[email protected]>
  • Loading branch information
BeDeBaMu authored and yurishkuro committed Sep 25, 2019
1 parent b083108 commit 11bbde1
Show file tree
Hide file tree
Showing 19 changed files with 253 additions and 188 deletions.
22 changes: 22 additions & 0 deletions src/jaegertracing/LogRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,25 @@
*/

#include "jaegertracing/LogRecord.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"

namespace jaegertracing {
void LogRecord::thrift(thrift::Log& log) const
{
log.__set_timestamp(std::chrono::duration_cast<std::chrono::microseconds>(
_timestamp.time_since_epoch())
.count());

std::vector<thrift::Tag> fields;
fields.reserve(_fields.size());
std::transform(std::begin(_fields),
std::end(_fields),
std::back_inserter(fields),
[](const Tag& tag) {
thrift::Tag thriftTag;
tag.thrift(thriftTag);
return thriftTag;
});
log.__set_fields(fields);
}
} // namespace jaegertracing
29 changes: 7 additions & 22 deletions src/jaegertracing/LogRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "jaegertracing/Compilers.h"
#include "jaegertracing/Tag.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"
#include <algorithm>
#include <chrono>
#include <iterator>
Expand All @@ -29,6 +28,9 @@
#include <opentracing/span.h>

namespace jaegertracing {
namespace thrift {
class Log;
}

class LogRecord {
public:
Expand All @@ -48,34 +50,17 @@ class LogRecord {
{
}

LogRecord(const opentracing::LogRecord & other)
: _timestamp(other.timestamp),
_fields(other.fields.begin(), other.fields.end())
LogRecord(const opentracing::LogRecord& other)
: _timestamp(other.timestamp)
, _fields(other.fields.begin(), other.fields.end())
{
}

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

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

thrift::Log thrift() const
{
thrift::Log log;
log.__set_timestamp(
std::chrono::duration_cast<std::chrono::microseconds>(
_timestamp.time_since_epoch())
.count());

std::vector<thrift::Tag> fields;
fields.reserve(_fields.size());
std::transform(std::begin(_fields),
std::end(_fields),
std::back_inserter(fields),
[](const Tag& tag) { return tag.thrift(); });
log.__set_fields(fields);

return log;
}
void thrift(thrift::Log& log) const;

private:
Clock::time_point _timestamp;
Expand Down
24 changes: 24 additions & 0 deletions src/jaegertracing/Reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,27 @@
*/

#include "jaegertracing/Reference.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"

namespace jaegertracing {
void Reference::thrift(thrift::SpanRef& spanRef) const
{
switch (_type) {
case Type::ChildOfRef: {
spanRef.__set_refType(thrift::SpanRefType::CHILD_OF);
} break;
case Type::FollowsFromRef: {
spanRef.__set_refType(thrift::SpanRefType::FOLLOWS_FROM);
} break;
default: {
std::ostringstream oss;
oss << "Invalid span reference type " << static_cast<int>(_type)
<< ", context " << _spanContext;
throw std::invalid_argument(oss.str());
} break;
}
spanRef.__set_traceIdHigh(_spanContext.traceID().high());
spanRef.__set_traceIdLow(_spanContext.traceID().low());
spanRef.__set_spanId(_spanContext.spanID());
}
} // namespace jaegertracing
27 changes: 4 additions & 23 deletions src/jaegertracing/Reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

#include "jaegertracing/Compilers.h"
#include "jaegertracing/SpanContext.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"

namespace jaegertracing {
namespace thrift {
class SpanRef;
}

class Reference {
public:
Expand All @@ -42,28 +44,7 @@ class Reference {

Type type() const { return _type; }

thrift::SpanRef thrift() const
{
thrift::SpanRef spanRef;
switch (_type) {
case Type::ChildOfRef: {
spanRef.__set_refType(thrift::SpanRefType::CHILD_OF);
} break;
case Type::FollowsFromRef: {
spanRef.__set_refType(thrift::SpanRefType::FOLLOWS_FROM);
} break;
default: {
std::ostringstream oss;
oss << "Invalid span reference type " << static_cast<int>(_type)
<< ", context " << _spanContext;
throw std::invalid_argument(oss.str());
} break;
}
spanRef.__set_traceIdHigh(_spanContext.traceID().high());
spanRef.__set_traceIdLow(_spanContext.traceID().low());
spanRef.__set_spanId(_spanContext.spanID());
return spanRef;
}
void thrift(thrift::SpanRef& spanRef) const;

private:
SpanContext _spanContext;
Expand Down
10 changes: 7 additions & 3 deletions src/jaegertracing/ReferenceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "jaegertracing/Reference.h"
#include "jaegertracing/SpanContext.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"
#include <gtest/gtest.h>
#include <stdexcept>

Expand All @@ -25,11 +26,14 @@ TEST(Reference, testThriftConversion)
{
const SpanContext context;
const Reference childRef(context, Reference::Type::ChildOfRef);
ASSERT_NO_THROW(childRef.thrift());
thrift::SpanRef thriftChildRef;
ASSERT_NO_THROW(childRef.thrift(thriftChildRef));
const Reference followsFromRef(context, Reference::Type::FollowsFromRef);
ASSERT_NO_THROW(followsFromRef.thrift());
thrift::SpanRef thriftFollowsFromRef;
ASSERT_NO_THROW(followsFromRef.thrift(thriftFollowsFromRef));
const Reference invalidRef(context, static_cast<Reference::Type>(-1));
ASSERT_THROW(invalidRef.thrift(), std::invalid_argument);
thrift::SpanRef thriftInvalidRef;
ASSERT_THROW(invalidRef.thrift(thriftInvalidRef), std::invalid_argument);
}

} // namespace jaegertracing
55 changes: 55 additions & 0 deletions src/jaegertracing/Span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "jaegertracing/Span.h"
#include "jaegertracing/Tracer.h"
#include "jaegertracing/baggage/BaggageSetter.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"
#include <cassert>
#include <cstdint>
#include <istream>
Expand Down Expand Up @@ -163,4 +164,58 @@ void Span::setSamplingPriority(const opentracing::Value& value)
_context.debugID());
}

void Span::thrift(thrift::Span& span) const
{
std::lock_guard<std::mutex> lock(_mutex);
span.__set_traceIdHigh(_context.traceID().high());
span.__set_traceIdLow(_context.traceID().low());
span.__set_spanId(_context.spanID());
span.__set_parentSpanId(_context.parentID());
span.__set_operationName(_operationName);

std::vector<thrift::SpanRef> refs;
refs.reserve(_references.size());
std::transform(std::begin(_references),
std::end(_references),
std::back_inserter(refs),
[](const Reference& ref) {
thrift::SpanRef thriftRef;
ref.thrift(thriftRef);
return thriftRef;
});
span.__set_references(refs);

span.__set_flags(_context.flags());
span.__set_startTime(std::chrono::duration_cast<std::chrono::microseconds>(
_startTimeSystem.time_since_epoch())
.count());
span.__set_duration(
std::chrono::duration_cast<std::chrono::microseconds>(_duration)
.count());

std::vector<thrift::Tag> tags;
tags.reserve(_tags.size());
std::transform(std::begin(_tags),
std::end(_tags),
std::back_inserter(tags),
[](const Tag& tag) {
thrift::Tag thriftTag;
tag.thrift(thriftTag);
return thriftTag;
});
span.__set_tags(tags);

std::vector<thrift::Log> logs;
logs.reserve(_logs.size());
std::transform(std::begin(_logs),
std::end(_logs),
std::back_inserter(logs),
[](const LogRecord& log) {
thrift::Log thriftLog;
log.thrift(thriftLog);
return thriftLog;
});
span.__set_logs(logs);
}

} // namespace jaegertracing
51 changes: 5 additions & 46 deletions src/jaegertracing/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
#include "jaegertracing/Reference.h"
#include "jaegertracing/SpanContext.h"
#include "jaegertracing/Tag.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"

namespace jaegertracing {

class Tracer;

namespace thrift {
class Span;
}

class Span : public opentracing::Span {
public:
using SteadyClock = opentracing::SteadyClock;
Expand Down Expand Up @@ -104,51 +107,7 @@ class Span : public opentracing::Span {

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

thrift::Span thrift() const
{
std::lock_guard<std::mutex> lock(_mutex);
thrift::Span span;
span.__set_traceIdHigh(_context.traceID().high());
span.__set_traceIdLow(_context.traceID().low());
span.__set_spanId(_context.spanID());
span.__set_parentSpanId(_context.parentID());
span.__set_operationName(_operationName);

std::vector<thrift::SpanRef> refs;
refs.reserve(_references.size());
std::transform(std::begin(_references),
std::end(_references),
std::back_inserter(refs),
[](const Reference& ref) { return ref.thrift(); });
span.__set_references(refs);

span.__set_flags(_context.flags());
span.__set_startTime(
std::chrono::duration_cast<std::chrono::microseconds>(
_startTimeSystem.time_since_epoch())
.count());
span.__set_duration(
std::chrono::duration_cast<std::chrono::microseconds>(_duration)
.count());

std::vector<thrift::Tag> tags;
tags.reserve(_tags.size());
std::transform(std::begin(_tags),
std::end(_tags),
std::back_inserter(tags),
[](const Tag& tag) { return tag.thrift(); });
span.__set_tags(tags);

std::vector<thrift::Log> logs;
logs.reserve(_logs.size());
std::transform(std::begin(_logs),
std::end(_logs),
std::back_inserter(logs),
[](const LogRecord& log) { return log.thrift(); });
span.__set_logs(logs);

return span;
}
void thrift(thrift::Span& span) const;

template <typename Stream>
void print(Stream& out) const
Expand Down
4 changes: 3 additions & 1 deletion src/jaegertracing/SpanTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "jaegertracing/Span.h"
#include "jaegertracing/thrift-gen/jaeger_types.h"
#include <gtest/gtest.h>
#include <string>

Expand All @@ -25,7 +26,8 @@ TEST(Span, testThriftConversion)
const Span span;
ASSERT_TRUE(span.serviceName().empty());
ASSERT_TRUE(span.operationName().empty());
ASSERT_NO_THROW(span.thrift());
thrift::Span thriftSpan;
ASSERT_NO_THROW(span.thrift(thriftSpan));
}

} // namespace jaegertracing
Loading

0 comments on commit 11bbde1

Please sign in to comment.