-
Notifications
You must be signed in to change notification settings - Fork 440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support passing attributes with AddEvent #60
Changes from 37 commits
2983982
0f7b217
642eb73
cdb6281
8aee486
56332bc
998d1ed
1515e08
355bea0
67dae8a
ff80f15
b25271c
5cfe001
9399c02
1b605b1
0462eea
4b0b985
aeefb7a
73e5a44
6382f42
9ecdcda
18761c6
a77ca9a
7d81a52
bb7147d
f61290f
92da4a8
841d831
66228c3
f9fabf1
0d90ba5
273d5b2
0389471
0fcf7c1
cea161b
c275ae8
48151b1
8f0b128
5338087
9bb4444
aa39603
fc0e120
45636a5
e92a7c4
92df11e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "opentelemetry/nostd/span.h" | ||
#include "opentelemetry/nostd/string_view.h" | ||
#include "opentelemetry/nostd/variant.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
using AttributeValue = nostd::variant<bool, | ||
int, | ||
g-easy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int64_t, | ||
unsigned int, | ||
uint64_t, | ||
double, | ||
nostd::string_view, | ||
nostd::span<const bool>, | ||
nostd::span<const int>, | ||
nostd::span<const int64_t>, | ||
nostd::span<const unsigned int>, | ||
nostd::span<const uint64_t>, | ||
nostd::span<const nostd::string_view>>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's also this discussion going on: open-telemetry/opentelemetry-specification#376. I'm not sure how serious that is, but it seems the idea is getting traction and is actively discussed currently. Maybe @reiley knows how likely this is to come and whether we should consider it. |
||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "opentelemetry/nostd/function_ref.h" | ||
#include "opentelemetry/trace/attribute_value.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
class KeyValueIterable | ||
{ | ||
public: | ||
virtual ~KeyValueIterable() = default; | ||
|
||
virtual bool ForEachKeyValue( | ||
g-easy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nostd::function_ref<bool(nostd::string_view, AttributeValue)> callback) const noexcept = 0; | ||
|
||
virtual size_t size() const noexcept = 0; | ||
}; | ||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#pragma once | ||
|
||
#include <iterator> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
#include "opentelemetry/nostd/utility.h" | ||
#include "opentelemetry/trace/key_value_iterable.h" | ||
#include "opentelemetry/version.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace trace | ||
{ | ||
namespace detail | ||
{ | ||
inline void take_key_value(nostd::string_view, AttributeValue) {} | ||
|
||
template <class T> | ||
auto is_key_value_iterable_impl(T iterable) | ||
/* -> decltype(std::pair<nostd::string_view, AttributeValue>(std::begin(iterable)->first, */ | ||
/* std::begin(iterable)->second), */ | ||
-> decltype(take_key_value(std::begin(iterable)->first, std::begin(iterable)->second), | ||
nostd::size(iterable), | ||
std::true_type{}); | ||
|
||
std::false_type is_key_value_iterable_impl(...); | ||
|
||
template <class T> | ||
struct is_key_value_iterable | ||
{ | ||
static const bool value = decltype(detail::is_key_value_iterable_impl(std::declval<T>()))::value; | ||
}; | ||
} // namespace detail | ||
|
||
template <class T> | ||
class KeyValueIterableView final : public KeyValueIterable | ||
{ | ||
static_assert(detail::is_key_value_iterable<T>::value, "Must be a key-value iterable"); | ||
|
||
public: | ||
explicit KeyValueIterableView(const T &container) noexcept : container_{&container} {} | ||
|
||
// KeyValueIterable | ||
bool ForEachKeyValue(nostd::function_ref<bool(nostd::string_view, AttributeValue)> callback) const | ||
noexcept override | ||
{ | ||
auto iter = std::begin(*container_); | ||
auto last = std::end(*container_); | ||
for (; iter != last; ++iter) | ||
{ | ||
if (!callback(iter->first, iter->second)) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
size_t size() const noexcept override { return nostd::size(*container_); } | ||
|
||
private: | ||
const T *container_; | ||
}; | ||
} // namespace trace | ||
OPENTELEMETRY_END_NAMESPACE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "opentelemetry/trace/key_value_iterable_view.h" | ||
|
||
#include <map> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
using namespace opentelemetry; | ||
|
||
static int TakeKeyValues(const trace::KeyValueIterable &iterable) | ||
{ | ||
std::map<std::string, trace::AttributeValue> result; | ||
int count = 0; | ||
iterable.ForEachKeyValue([&](nostd::string_view key, trace::AttributeValue value) noexcept { | ||
++count; | ||
return true; | ||
}); | ||
return count; | ||
} | ||
|
||
template <class T, nostd::enable_if_t<trace::detail::is_key_value_iterable<T>::value> * = nullptr> | ||
static int TakeKeyValues(const T &iterable) | ||
{ | ||
return TakeKeyValues(trace::KeyValueIterableView<T>{iterable}); | ||
} | ||
|
||
TEST(KeyValueIterableViewTest, is_key_value_iterable) | ||
{ | ||
using M1 = std::map<std::string, std::string>; | ||
EXPECT_TRUE(bool{trace::detail::is_key_value_iterable<M1>::value}); | ||
|
||
using M2 = std::map<std::string, int>; | ||
EXPECT_TRUE(bool{trace::detail::is_key_value_iterable<M2>::value}); | ||
|
||
using M3 = std::map<std::string, trace::AttributeValue>; | ||
EXPECT_TRUE(bool{trace::detail::is_key_value_iterable<M3>::value}); | ||
|
||
struct A | ||
{}; | ||
using M4 = std::map<std::string, A>; | ||
EXPECT_FALSE(bool{trace::detail::is_key_value_iterable<M4>::value}); | ||
} | ||
|
||
TEST(KeyValueIterableViewTest, ForEachKeyValue) | ||
{ | ||
std::map<std::string, std::string> m1 = {{"abc", "123"}, {"xyz", "456"}}; | ||
EXPECT_EQ(TakeKeyValues(m1), 2); | ||
|
||
std::vector<std::pair<std::string, int>> v1 = {{"abc", 123}, {"xyz", 456}}; | ||
EXPECT_EQ(TakeKeyValues(v1), 2); | ||
} | ||
|
||
TEST(KeyValueIterableViewTest, ForEachKeyValueWithExit) | ||
{ | ||
using M = std::map<std::string, std::string>; | ||
M m1 = {{"abc", "123"}, {"xyz", "456"}}; | ||
trace::KeyValueIterableView<M> iterable{m1}; | ||
int count = 0; | ||
auto exit = iterable.ForEachKeyValue([&count](nostd::string_view /*key*/, | ||
trace::AttributeValue /*value*/) noexcept { | ||
++count; | ||
return false; | ||
}); | ||
EXPECT_EQ(count, 1); | ||
EXPECT_FALSE(exit); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder about moving this from
trace
tocommon
? We will need the same functionality for metrics labels and, I assume, also for logs at some point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done