Skip to content

Commit a83daf4

Browse files
committed
binary annotation as stream
1 parent bdc706b commit a83daf4

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

src/Span.h

+52-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <codecvt>
88
#include <memory>
99
#include <chrono>
10+
#include <tuple>
1011

1112
#include <thrift/protocol/TProtocol.h>
1213

@@ -854,16 +855,21 @@ namespace __impl
854855
{
855856

856857
template <typename K, typename V>
857-
struct Annotation
858+
struct __annotation
858859
{
859860
static void apply(Span &span, const std::pair<K, V> &value)
860861
{
861862
span.annotate(value.first, value.second);
862863
}
864+
865+
static void apply(Span &span, const std::tuple<K, V, Endpoint *> &value)
866+
{
867+
span.annotate(std::get<0>(value), std::get<1>(value), std::get<2>(value));
868+
}
863869
};
864870

865871
template <>
866-
struct Annotation<const char *, Endpoint *>
872+
struct __annotation<const char *, Endpoint *>
867873
{
868874
static void apply(Span &span, const std::pair<const char *, Endpoint *> &value)
869875
{
@@ -881,12 +887,48 @@ struct Annotation<const char *, Endpoint *>
881887
}
882888
};
883889

890+
template <typename K>
891+
struct __annotation<K, const char *>
892+
{
893+
static void apply(Span &span, const std::pair<K, const char *> &value)
894+
{
895+
span.annotate(value.first, value.second);
896+
}
897+
898+
static void apply(Span &span, const std::tuple<K, const char *, Endpoint *> &value)
899+
{
900+
span.annotate(std::get<0>(value), std::get<1>(value), -1, std::get<2>(value));
901+
}
902+
};
903+
904+
template <typename K>
905+
struct __annotation<K, const wchar_t *>
906+
{
907+
static void apply(Span &span, const std::pair<K, const wchar_t *> &value)
908+
{
909+
span.annotate(value.first, value.second);
910+
}
911+
912+
static void apply(Span &span, const std::tuple<K, const wchar_t *, Endpoint *> &value)
913+
{
914+
span.annotate(std::get<0>(value), std::get<1>(value), -1, std::get<2>(value));
915+
}
916+
};
917+
884918
} // namespace __impl
885919

886920
template <typename K, typename V>
887921
Span &operator<<(Span &span, const std::pair<K, V> &value)
888922
{
889-
__impl::Annotation<K, V>::apply(span, value);
923+
__impl::__annotation<K, V>::apply(span, value);
924+
925+
return span;
926+
}
927+
928+
template <typename K, typename V>
929+
Span &operator<<(Span &span, const std::tuple<K, V, Endpoint *> &value)
930+
{
931+
__impl::__annotation<K, V>::apply(span, value);
890932

891933
return span;
892934
}
@@ -959,32 +1001,31 @@ Endpoint &Endpoint::with_ipv6(const std::string &ip)
9591001
namespace __impl
9601002
{
9611003
template <typename T>
962-
struct __annotation
1004+
struct __binary_annotation
9631005
{
964-
static size_t size_of(bool *) { return sizeof(T); }
9651006
};
9661007
template <>
967-
struct __annotation<bool>
1008+
struct __binary_annotation<bool>
9681009
{
9691010
static const AnnotationType type = AnnotationType::BOOL;
9701011
};
9711012
template <>
972-
struct __annotation<int16_t>
1013+
struct __binary_annotation<int16_t>
9731014
{
9741015
static const AnnotationType type = AnnotationType::I16;
9751016
};
9761017
template <>
977-
struct __annotation<int32_t>
1018+
struct __binary_annotation<int32_t>
9781019
{
9791020
static const AnnotationType type = AnnotationType::I32;
9801021
};
9811022
template <>
982-
struct __annotation<int64_t>
1023+
struct __binary_annotation<int64_t>
9831024
{
9841025
static const AnnotationType type = AnnotationType::I64;
9851026
};
9861027
template <>
987-
struct __annotation<double>
1028+
struct __binary_annotation<double>
9881029
{
9891030
static const AnnotationType type = AnnotationType::DOUBLE;
9901031
};
@@ -1007,7 +1048,7 @@ inline BinaryAnnotation Span::annotate(const std::string &key, const T &value, c
10071048

10081049
annotation.__set_key(key);
10091050
annotation.__set_value(std::string(reinterpret_cast<const char *>(&value), sizeof(T)));
1010-
annotation.__set_annotation_type(__impl::__annotation<T>::type);
1051+
annotation.__set_annotation_type(__impl::__binary_annotation<T>::type);
10111052

10121053
if (endpoint)
10131054
{

test/TestSpan.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Mocks.hpp"
22

33
#include <utility>
4+
#include <tuple>
45

56
#define RAPIDJSON_HAS_STDSTRING 1
67
#include <rapidjson/stringbuffer.h>
@@ -283,14 +284,22 @@ TEST(span, annotate_stream)
283284
<< std::string("hello")
284285
<< std::make_pair("world", &host)
285286
<< std::make_pair("key", "hello")
287+
<< std::make_pair("string", L"测试")
286288
<< std::make_pair("key", std::string("world"))
287289
<< std::make_pair("bool", true)
288290
<< std::make_pair("i16", (int16_t)123)
289291
<< std::make_pair("i32", (int32_t)123)
290292
<< std::make_pair("i64", (int64_t)123)
291293
<< std::make_pair("double", (double)12.3)
292-
<< std::make_pair("string", L"测试");
294+
<< std::make_tuple("key", "hello", &host)
295+
<< std::make_tuple("string", L"测试", &host)
296+
<< std::make_tuple("key", std::string("world"), &host)
297+
<< std::make_tuple("bool", true, &host)
298+
<< std::make_tuple("i16", (int16_t)123, &host)
299+
<< std::make_tuple("i32", (int32_t)123, &host)
300+
<< std::make_tuple("i64", (int64_t)123, &host)
301+
<< std::make_tuple("double", (double)12.3, &host);
293302

294303
ASSERT_EQ(span.message().annotations.size(), 3);
295-
ASSERT_EQ(span.message().binary_annotations.size(), 8);
304+
ASSERT_EQ(span.message().binary_annotations.size(), 16);
296305
}

0 commit comments

Comments
 (0)