Skip to content

Commit d90d263

Browse files
committed
split Endpoint from annontate pair
1 parent 58e03bf commit d90d263

File tree

5 files changed

+78
-99
lines changed

5 files changed

+78
-99
lines changed

bench/BenchSpan.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void bench_span_annonate_with_endpoint(benchmark::State &state)
5555

5656
while (state.KeepRunning())
5757
{
58-
span << std::make_pair(zipkin::TraceKeys::CLIENT_SEND, &endpoint);
58+
span << zipkin::TraceKeys::CLIENT_SEND << endpoint;
5959
}
6060

6161
state.SetItemsProcessed(span.message().annotations.size());
@@ -84,7 +84,7 @@ void bench_span_annonate_bool_with_endpoint(benchmark::State &state)
8484

8585
while (state.KeepRunning())
8686
{
87-
span << boost::make_tuple(zipkin::TraceKeys::CLIENT_SEND, false, &endpoint);
87+
span << std::make_pair(zipkin::TraceKeys::CLIENT_SEND, false) << endpoint;
8888
}
8989

9090
state.SetItemsProcessed(span.message().binary_annotations.size());
@@ -185,7 +185,7 @@ void bench_span_annonate_string_with_endpoint(benchmark::State &state)
185185

186186
while (state.KeepRunning())
187187
{
188-
span << boost::make_tuple(zipkin::TraceKeys::CLIENT_SEND, value, &endpoint);
188+
span << std::make_pair(zipkin::TraceKeys::CLIENT_SEND, value) << endpoint;
189189
}
190190

191191
state.SetItemsProcessed(span.message().binary_annotations.size());
@@ -228,7 +228,7 @@ void bench_span_serialize_binary(benchmark::State &state)
228228
zipkin::Endpoint endpoint("bench");
229229

230230
span << zipkin::TraceKeys::CLIENT_SEND
231-
<< boost::make_tuple(zipkin::TraceKeys::CLIENT_SEND, false, &endpoint)
231+
<< std::make_pair(zipkin::TraceKeys::CLIENT_SEND, false) << endpoint
232232
<< std::make_pair(zipkin::TraceKeys::CLIENT_SEND, L"hello world");
233233

234234
boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> buf(new apache::thrift::transport::TMemoryBuffer());
@@ -251,7 +251,7 @@ void bench_span_serialize_json(benchmark::State &state)
251251
zipkin::Endpoint endpoint("bench");
252252

253253
span << zipkin::TraceKeys::CLIENT_SEND
254-
<< boost::make_tuple(zipkin::TraceKeys::CLIENT_SEND, false, &endpoint)
254+
<< std::make_pair(zipkin::TraceKeys::CLIENT_SEND, false) << endpoint
255255
<< std::make_pair(zipkin::TraceKeys::CLIENT_SEND, L"hello world");
256256

257257
rapidjson::StringBuffer buffer;
@@ -274,7 +274,7 @@ void bench_span_serialize_pretty_json(benchmark::State &state)
274274
zipkin::Endpoint endpoint("bench");
275275

276276
span << zipkin::TraceKeys::CLIENT_SEND
277-
<< boost::make_tuple(zipkin::TraceKeys::CLIENT_SEND, false, &endpoint)
277+
<< std::make_pair(zipkin::TraceKeys::CLIENT_SEND, false) << endpoint
278278
<< std::make_pair(zipkin::TraceKeys::CLIENT_SEND, L"hello world");
279279

280280
rapidjson::StringBuffer buffer;

examples/grpc_greeter/greeter_client.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class GreeterClient
8181

8282
zipkin::Endpoint endpoint("greeter");
8383

84-
span << boost::make_tuple("name", user, &endpoint);
84+
span << std::make_pair("name", user) << endpoint;
8585

8686
auto send_header = [&context](const char *key, const std::string &value) {
8787
std::string lower_key(key);

src/Span.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Annotation Span::annotate(const std::string &value, const Endpoint *endpoint)
297297

298298
m_span.annotations.push_back(annotation);
299299

300-
return Annotation(m_span.annotations.back());
300+
return Annotation(*this, m_span.annotations.back());
301301
}
302302

303303
BinaryAnnotation Span::annotate(const std::string &key, const uint8_t *value, size_t size, const Endpoint *endpoint)
@@ -315,7 +315,7 @@ BinaryAnnotation Span::annotate(const std::string &key, const uint8_t *value, si
315315

316316
m_span.binary_annotations.push_back(annotation);
317317

318-
return BinaryAnnotation(m_span.binary_annotations.back());
318+
return BinaryAnnotation(*this, m_span.binary_annotations.back());
319319
}
320320

321321
BinaryAnnotation Span::annotate(const std::string &key, const std::string &value, const Endpoint *endpoint)
@@ -333,7 +333,7 @@ BinaryAnnotation Span::annotate(const std::string &key, const std::string &value
333333

334334
m_span.binary_annotations.push_back(annotation);
335335

336-
return BinaryAnnotation(m_span.binary_annotations.back());
336+
return BinaryAnnotation(*this, m_span.binary_annotations.back());
337337
}
338338

339339
BinaryAnnotation Span::annotate(const std::string &key, const std::wstring &value, const Endpoint *endpoint)
@@ -353,7 +353,7 @@ BinaryAnnotation Span::annotate(const std::string &key, const std::wstring &valu
353353

354354
m_span.binary_annotations.push_back(annotation);
355355

356-
return BinaryAnnotation(m_span.binary_annotations.back());
356+
return BinaryAnnotation(*this, m_span.binary_annotations.back());
357357
}
358358

359359
size_t CachedSpan::cache_size(void) const

src/Span.h

Lines changed: 56 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ class Endpoint
132132
};
133133

134134
struct Tracer;
135-
136135
class CachedTracer;
136+
class Span;
137137

138138
/**
139139
* \brief Associates an event that explains latency with a timestamp.
@@ -142,10 +142,13 @@ class CachedTracer;
142142
*/
143143
class Annotation
144144
{
145+
Span &m_span;
145146
::Annotation &m_annotation;
146147

147148
public:
148-
Annotation(::Annotation &annotation) : m_annotation(annotation) {}
149+
Annotation(Span &span, ::Annotation &annotation) : m_span(span), m_annotation(annotation) {}
150+
151+
Span &span(void) { return m_span; }
149152

150153
/**
151154
* \brief Microseconds from epoch.
@@ -390,10 +393,13 @@ struct TraceKeys
390393
*/
391394
class BinaryAnnotation
392395
{
396+
Span &m_span;
393397
::BinaryAnnotation &m_annotation;
394398

395399
public:
396-
BinaryAnnotation(::BinaryAnnotation &annotation) : m_annotation(annotation) {}
400+
BinaryAnnotation(Span &span, ::BinaryAnnotation &annotation) : m_span(span), m_annotation(annotation) {}
401+
402+
Span &span(void) { return m_span; }
397403

398404
/**
399405
* \brief The thrift type of value, most often AnnotationType#STRING.
@@ -906,112 +912,94 @@ class Span
906912
};
907913
};
908914

909-
static inline Span &operator<<(Span &span, const std::string &value)
910-
{
911-
span.annotate(value);
912-
913-
return span;
914-
}
915-
916915
namespace __impl
917916
{
918917

919918
template <typename K, typename V>
920919
struct __annotation
921920
{
922-
static void apply(Span &span, const std::pair<K, V> &value)
923-
{
924-
span.annotate(value.first, value.second);
925-
}
926-
927-
static void apply(Span &span, const boost::tuple<K, V, Endpoint *> &value)
928-
{
929-
span.annotate(boost::get<0>(value), boost::get<1>(value), boost::get<2>(value));
930-
}
931-
};
932-
933-
template <>
934-
struct __annotation<const char *, Endpoint *>
935-
{
936-
static void apply(Span &span, const std::pair<const char *, Endpoint *> &value)
921+
static BinaryAnnotation apply(Span &span, const std::pair<K, V> &value)
937922
{
938-
::Annotation annotation;
939-
940-
annotation.__set_timestamp(Span::now().count());
941-
annotation.__set_value(value.first);
942-
943-
if (value.second)
944-
{
945-
annotation.__set_host(value.second->host());
946-
}
947-
948-
span.message().annotations.push_back(annotation);
923+
return span.annotate(value.first, value.second);
949924
}
950-
};
951925

952-
template <>
953-
struct __annotation<std::string, Endpoint *>
954-
{
955-
static void apply(Span &span, const std::pair<std::string, Endpoint *> &value)
926+
static BinaryAnnotation apply(Span &span, const boost::tuple<K, V, Endpoint *> &value)
956927
{
957-
::Annotation annotation;
958-
959-
annotation.__set_timestamp(Span::now().count());
960-
annotation.__set_value(value.first);
961-
962-
if (value.second)
963-
{
964-
annotation.__set_host(value.second->host());
965-
}
966-
967-
span.message().annotations.push_back(annotation);
928+
return span.annotate(boost::get<0>(value), boost::get<1>(value), boost::get<2>(value));
968929
}
969930
};
970931

971932
template <typename K>
972933
struct __annotation<K, const char *>
973934
{
974-
static void apply(Span &span, const std::pair<K, const char *> &value)
935+
static BinaryAnnotation apply(Span &span, const std::pair<K, const char *> &value)
975936
{
976-
span.annotate(value.first, value.second);
937+
return span.annotate(value.first, value.second);
977938
}
978939

979-
static void apply(Span &span, const boost::tuple<K, const char *, Endpoint *> &value)
940+
static BinaryAnnotation apply(Span &span, const boost::tuple<K, const char *, Endpoint *> &value)
980941
{
981-
span.annotate(boost::get<0>(value), boost::get<1>(value), -1, boost::get<2>(value));
942+
return span.annotate(boost::get<0>(value), boost::get<1>(value), -1, boost::get<2>(value));
982943
}
983944
};
984945

985946
template <typename K>
986947
struct __annotation<K, const wchar_t *>
987948
{
988-
static void apply(Span &span, const std::pair<K, const wchar_t *> &value)
949+
static BinaryAnnotation apply(Span &span, const std::pair<K, const wchar_t *> &value)
989950
{
990-
span.annotate(value.first, value.second);
951+
return span.annotate(value.first, value.second);
991952
}
992953

993-
static void apply(Span &span, const boost::tuple<K, const wchar_t *, Endpoint *> &value)
954+
static BinaryAnnotation apply(Span &span, const boost::tuple<K, const wchar_t *, Endpoint *> &value)
994955
{
995-
span.annotate(boost::get<0>(value), boost::get<1>(value), -1, boost::get<2>(value));
956+
return span.annotate(boost::get<0>(value), boost::get<1>(value), -1, boost::get<2>(value));
996957
}
997958
};
998959

999960
} // namespace __impl
1000961

1001-
template <typename K, typename V>
1002-
Span &operator<<(Span &span, const std::pair<K, V> &value)
962+
static inline Annotation operator<<(Span &span, const std::string &value)
963+
{
964+
return span.annotate(value);
965+
}
966+
967+
static inline Annotation operator<<(Annotation annotation, const std::string &value)
968+
{
969+
return annotation.span().annotate(value);
970+
}
971+
972+
static inline Annotation operator<<(BinaryAnnotation annotation, const std::string &value)
1003973
{
1004-
__impl::__annotation<K, V>::apply(span, value);
974+
return annotation.span().annotate(value);
975+
}
976+
977+
static inline Span &operator<<(Annotation annotation, const zipkin::Endpoint &endpoint)
978+
{
979+
return annotation.with_endpoint(endpoint).span();
980+
}
981+
982+
static inline Span &operator<<(BinaryAnnotation annotation, const zipkin::Endpoint &endpoint)
983+
{
984+
return annotation.with_endpoint(endpoint).span();
985+
}
1005986

1006-
return span;
987+
template <typename K, typename V>
988+
BinaryAnnotation operator<<(Span &span, const std::pair<K, V> &value)
989+
{
990+
return __impl::__annotation<K, V>::apply(span, value);
1007991
}
1008992

1009993
template <typename K, typename V>
1010-
Span &operator<<(Span &span, const boost::tuple<K, V, Endpoint *> &value)
994+
BinaryAnnotation operator<<(Annotation annotation, const std::pair<K, V> &value)
1011995
{
1012-
__impl::__annotation<K, V>::apply(span, value);
996+
return __impl::__annotation<K, V>::apply(annotation.span(), value);
997+
}
1013998

1014-
return span;
999+
template <typename K, typename V>
1000+
BinaryAnnotation operator<<(BinaryAnnotation annotation, const std::pair<K, V> &value)
1001+
{
1002+
return __impl::__annotation<K, V>::apply(annotation.span(), value);
10151003
}
10161004

10171005
class CachedSpan : public Span
@@ -1143,7 +1131,7 @@ inline BinaryAnnotation Span::annotate(const std::string &key, const T &value, c
11431131

11441132
m_span.binary_annotations.push_back(annotation);
11451133

1146-
return BinaryAnnotation(m_span.binary_annotations.back());
1134+
return BinaryAnnotation(*this, m_span.binary_annotations.back());
11471135
}
11481136

11491137
namespace base64

test/TestSpan.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,16 @@ TEST(span, annotate_stream)
325325
zipkin::Endpoint host("host", addr);
326326

327327
span << zipkin::TraceKeys::CLIENT_SEND
328-
<< std::string("hello")
329-
<< std::make_pair("world", &host)
328+
<< std::string("hello") << host
330329
<< std::make_pair("key", "hello")
331-
<< std::make_pair("string", L"测试")
332-
<< std::make_pair("key", std::string("world"))
333-
<< std::make_pair("bool", true)
334-
<< std::make_pair("i16", (int16_t)123)
335-
<< std::make_pair("i32", (int32_t)123)
336-
<< std::make_pair("i64", (int64_t)123)
337-
<< std::make_pair("double", (double)12.3)
338-
<< boost::make_tuple("key", "hello", &host)
339-
<< boost::make_tuple("string", L"测试", &host)
340-
<< boost::make_tuple("key", std::string("world"), &host)
341-
<< boost::make_tuple("bool", true, &host)
342-
<< boost::make_tuple("i16", (int16_t)123, &host)
343-
<< boost::make_tuple("i32", (int32_t)123, &host)
344-
<< boost::make_tuple("i64", (int64_t)123, &host)
345-
<< boost::make_tuple("double", (double)12.3, &host);
346-
347-
ASSERT_EQ(span.message().annotations.size(), 3);
348-
ASSERT_EQ(span.message().binary_annotations.size(), 16);
330+
<< std::make_pair("string", L"测试") << host
331+
<< std::make_pair("key", std::string("world")) << host
332+
<< std::make_pair("bool", true) << host
333+
<< std::make_pair("i16", (int16_t)123) << host
334+
<< std::make_pair("i32", (int32_t)123) << host
335+
<< std::make_pair("i64", (int64_t)123) << host
336+
<< std::make_pair("double", (double)12.3) << host;
337+
338+
ASSERT_EQ(span.message().annotations.size(), 2);
339+
ASSERT_EQ(span.message().binary_annotations.size(), 8);
349340
}

0 commit comments

Comments
 (0)