From b7259cdf0379e99e6f66842694338486e68b8171 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 11 Jun 2020 14:20:29 +0000 Subject: [PATCH 01/10] avoiding standard datastructures --- api/include/opentelemetry/context/context.h | 361 +++++++++----------- api/test/context/runtime_context_test.cc | 53 +++ 2 files changed, 219 insertions(+), 195 deletions(-) create mode 100644 api/test/context/runtime_context_test.cc diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index ddb6227095..9bbe9a94c5 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,233 +1,204 @@ #pragma once +#include +#include #include -#include -#include +#include +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/variant.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - std::mutex context_id_mutex; - - /*The context class provides a context identifier */ - class Context{ - - public: - - /*The ContextKey class is used to obscure access from the - * user to the context map. The identifier is used as a key - * to the context map. - */ - class ContextKey{ - private: - friend class Context; - - std::string key_name_; - - int identifier_; - - - /* GetIdentifier: returns the identifier */ - int GetIdentifier(){ - return identifier_; - } - - /* Constructs a new ContextKey with the passed in name and - * identifier. - */ - ContextKey(std::string key_name, int identifier){ - key_name_ = key_name; - identifier_ = identifier; - } - - public: - - /* Consructs a new ContextKey with the passed in name and increments - * the identifier then assigns it to be the key's identifier. - */ - ContextKey(std::string key_name){ - key_name_ = key_name; - - context_id_mutex.lock(); - - Context::last_key_identifier_++; - - identifier_ = Context::last_key_identifier_; - - context_id_mutex.unlock(); - } - - }; - - - /* Creates a context object with no key/value pairs */ - Context(){ - ctx_map_ = std::map {}; - - } - - /* Contructor, creates a context object from a map - * of keys and identifiers - */ - Context(ContextKey key, int value){ - ctx_map_[key.GetIdentifier()] = value; - } - - - /* Accepts a new key/value pair and then returns a new - * context that contains both the original pairs and the new pair. - */ - Context WriteValue(ContextKey key, int value){ - std::map temp_map = ctx_map_; - - temp_map[key.GetIdentifier()] = value; - - return Context(temp_map); - } - - /* Class comparator to see if the context maps are the same. */ - bool operator == (const Context &context){ - if(context.ctx_map_ == ctx_map_){ - return true; - } - else{ - return false; - } - } - - /* Returns the value associated with the passed in key */ - int GetValue(ContextKey key){ - return ctx_map_[key.GetIdentifier()]; - } - - /* Returns a ContextKey that has the passed in name and the - * next available identifier.*/ - ContextKey CreateKey(std::string key_name){ - int id; - - context_id_mutex.lock(); - - last_key_identifier_++; - - id = last_key_identifier_; - - context_id_mutex.unlock(); - - return ContextKey(key_name,id); - } - - private: - - /* The identifier itself */ - std::map ctx_map_; - - /*Used to track that last ContextKey identifier and create the next one */ - static int last_key_identifier_; - - /* A constructor that accepts a key/value map */ - Context(std::map ctx_map){ - ctx_map_ = ctx_map; - } - +/*The context class provides a context identifier */ +class Context +{ +public: + /*The Key class is used to obscure access from the + * user to the context map. The identifier is used as a key + * to the context map. + */ + class Key + { + private: + friend class Context; + + nostd::string_view key_name_; + + Key* identifier_; + + /* GetIdentifier: returns the identifier */ + Key* GetIdentifier() { return identifier_; } + + /* Constructs a new Key with the passed in name and + * identifier. + */ + Key(nostd::string_view key_name, int identifier) + { + key_name_ = key_name; + identifier_ = this; + } + + public: + /* Consructs a new Key with the passed in name and increments + * the identifier then assigns it to be the key's identifier. + */ + Key(nostd::string_view key_name) + { + key_name_ = key_name; + identifier_ = this; + } }; + /* Creates a context object with no key/value pairs */ + Context() = default; + /* Contructor, creates a context object from a map + * of keys and identifiers + */ + Context(Key key, int value) { ctx_map_[key.GetIdentifier()] = value; } + /* Accepts a new key/value pair and then returns a new + * context that contains both the original pairs and the new pair. + */ + Context WriteValue(Key key, int value) + { + std::map temp_map = ctx_map_; + temp_map[key.GetIdentifier()] = value; + return Context(temp_map); + } + + /* Accepts a vector of key value pairs and combines them with the + * existing context map then returns a new context with the new + * combined map */ + Context WriteValues(std::vector> ctx_list) + { + std::map temp_map = ctx_map_; + + for (auto i = ctx_list.begin(); i != ctx_list.end(); i++) + { + temp_map[((*i).first).GetIdentifier()] = (*i).second; + } + + return Context(temp_map); + } + + /* Class comparator to see if the context maps are the same. */ + bool operator==(const Context &context) + { + if (context.ctx_map_ == ctx_map_) + { + return true; + } + else + { + return false; + } + } + + /* Returns the value associated with the passed in key */ + int GetValue(Key key) { return ctx_map_[key.GetIdentifier()]; } + + /* Returns a Key that has the passed in name and the + * next available identifier.*/ + Key CreateKey(nostd::string_view key_name) + { + return Key(key_name); + } + + /* Copy constructors */ + Context(const Context &other) = default; + Context &operator=(const Context &other) = default; + +private: + /* The identifier itself */ + std::map ctx_map_; + + /* A constructor that accepts a key/value map */ + Context(std::map ctx_map) { ctx_map_ = ctx_map; } +}; + +/* The RuntimeContext class provides a wrapper for + * propogating context through cpp. */ +class RuntimeContext +{ + +public: /* The token class provides an identifier that is used by - * the attach and detach methods to keep track of context - * objects. + * the attach and detach methods to keep track of context + * objects. */ - class Token{ + class Token + { - public: - - /* A constructor that sets the token's Context object to the - * one that was passed in. - */ - Token(Context &ctx){ - ctx_ = ctx; - } + private: + friend class RuntimeContext; - /* Returns the stored context object */ - Context GetContext(){ - return ctx_; - } + Context ctx_; - private: + /* A constructor that sets the token's Context object to the + * one that was passed in. + */ + Token(Context &ctx) { ctx_ = ctx; } - Context ctx_; + /* Returns the stored context object */ + Context GetContext() { return ctx_; } }; + /* A default constructor that will set the context to + * an empty context object. + */ + RuntimeContext() { context_ = Context(); } - /* The RuntimeContext class provides a wrapper for - * propogating context through cpp. */ - class RuntimeContext { - - public: - - - /* A default constructor that will set the context to - * an empty context object. - */ - RuntimeContext(){ - context_ = Context(); - } - - /* A constructor that will set the context as the passed in context. */ - RuntimeContext(Context &context){ - context_ = context; - } - - /* Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - */ - Token Attach(Context &context){ - - Token old_context_token = Token(context_); + /* A constructor that will set the context as the passed in context. */ + RuntimeContext(Context &context) { context_ = context; } - context_ = context; + /* Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + */ + Token Attach(Context &context) + { - return old_context_token; - } + Token old_context_token = Token(context_); + context_ = context; - /* Return the current context. */ - static Context GetCurrent(){ - Context context = context_; - return context_; - } + return old_context_token; + } + /* Return the current context. */ + static Context GetCurrent() + { + Context context = context_; + return context_; + } - /* Resets the context to a previous value stored in the - * passed in token. Returns zero if successful, -1 otherwise - */ - int Detach(Token &token){ - - if(token.GetContext() == context_){ + /* Resets the context to a previous value stored in the + * passed in token. Returns zero if successful, -1 otherwise + */ + int Detach(Token &token) + { - return -1; - } - - context_ = token.GetContext(); - - return 0; - } + if (token.GetContext() == context_) + { + return -1; + } - private: - - static thread_local Context context_; + context_ = token.GetContext(); - }; - - thread_local Context RuntimeContext::context_ = Context(); - int Context::last_key_identifier_ = 0; + return 0; + } +private: + static thread_local Context context_; +}; -} +thread_local Context RuntimeContext::context_ = Context(); +} // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/runtime_context_test.cc b/api/test/context/runtime_context_test.cc new file mode 100644 index 0000000000..0a2ab92cb5 --- /dev/null +++ b/api/test/context/runtime_context_test.cc @@ -0,0 +1,53 @@ +#include "opentelemetry/context/context.h" + +#include + +namespace +{ + +using opentelemetry::context::Context; +using opentelemetry::context::RuntimeContext; + +/* Tests whether the runtimeContext object properly returns the current context + */ +TEST(RuntimeContextTest, GetCurrentContext) +{ + + Context::Key test_key = Context::Key("test_key"); + Context test_context = Context(test_key, 7); + + RuntimeContext test_runtime = RuntimeContext(test_context); + + EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); +} + +/* Tests whether the runtimeContext object properly attaches and detaches + * the context object. + */ +TEST(RuntimeContextTest, AttachDetachContext) +{ + + Context::Key test_key = Context::Key("test_key"); + Context test_context = Context(test_key, 7); + + RuntimeContext test_runtime = RuntimeContext(test_context); + + Context::Key foo_key = Context::Key("foo_key"); + Context foo_context = Context(foo_key, 5); + + EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); + EXPECT_NE(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); + + RuntimeContext::Token test_token = test_runtime.Attach(foo_context); + + EXPECT_NE(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); + EXPECT_EQ(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); + + int detach_result = test_runtime.Detach(test_token); + + EXPECT_EQ(detach_result, 0); + + EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); + EXPECT_NE(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); +} +} // namespace From 34eacf256de9ab65404829f6367ec2a54080c7bb Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 17 Jun 2020 18:36:15 +0000 Subject: [PATCH 02/10] replaced std::map and created a thread_local context --- api/include/opentelemetry/context/context.h | 258 ++++++++------------ api/test/context/BUILD | 16 +- api/test/context/context_test.cc | 89 +++++-- api/test/context/runtime_context_test.cc | 119 +++++---- 4 files changed, 256 insertions(+), 226 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 9bbe9a94c5..bfb4bffa6b 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -7,198 +7,146 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/common/attribute_value.h" #include "opentelemetry/version.h" +#include "opentelemetry/trace/key_value_iterable_view.h" +#include "opentelemetry/trace/key_value_iterable.h" +#include "opentelemetry/context/key_value_iterable_modifiable.h" +//#include "opentelemetry/context/threadlocal_context.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -/*The context class provides a context identifier */ -class Context -{ + /*The context class provides a context identifier */ -public: - /*The Key class is used to obscure access from the - * user to the context map. The identifier is used as a key - * to the context map. - */ class Key { - private: - friend class Context; - - nostd::string_view key_name_; + private: + template + friend class Context; - Key* identifier_; + nostd::string_view key_name_; - /* GetIdentifier: returns the identifier */ - Key* GetIdentifier() { return identifier_; } + Key* identifier_; - /* Constructs a new Key with the passed in name and - * identifier. - */ - Key(nostd::string_view key_name, int identifier) - { - key_name_ = key_name; - identifier_ = this; - } - - public: - /* Consructs a new Key with the passed in name and increments - * the identifier then assigns it to be the key's identifier. - */ - Key(nostd::string_view key_name) - { - key_name_ = key_name; - identifier_ = this; - } - }; - /* Creates a context object with no key/value pairs */ - Context() = default; - /* Contructor, creates a context object from a map - * of keys and identifiers - */ - Context(Key key, int value) { ctx_map_[key.GetIdentifier()] = value; } + Key(nostd::string_view key_name, int identifier) + { + key_name_ = key_name; + identifier_ = this; + } - /* Accepts a new key/value pair and then returns a new - * context that contains both the original pairs and the new pair. - */ - Context WriteValue(Key key, int value) - { - std::map temp_map = ctx_map_; - temp_map[key.GetIdentifier()] = value; - return Context(temp_map); - } - - /* Accepts a vector of key value pairs and combines them with the - * existing context map then returns a new context with the new - * combined map */ - Context WriteValues(std::vector> ctx_list) - { - std::map temp_map = ctx_map_; - - for (auto i = ctx_list.begin(); i != ctx_list.end(); i++) - { - temp_map[((*i).first).GetIdentifier()] = (*i).second; - } + public: - return Context(temp_map); - } - - /* Class comparator to see if the context maps are the same. */ - bool operator==(const Context &context) - { - if (context.ctx_map_ == ctx_map_) - { - return true; - } - else - { - return false; - } - } - - /* Returns the value associated with the passed in key */ - int GetValue(Key key) { return ctx_map_[key.GetIdentifier()]; } - - /* Returns a Key that has the passed in name and the - * next available identifier.*/ - Key CreateKey(nostd::string_view key_name) - { - return Key(key_name); - } + Key* GetIdentifier() { return identifier_; } - /* Copy constructors */ - Context(const Context &other) = default; - Context &operator=(const Context &other) = default; -private: - /* The identifier itself */ - std::map ctx_map_; - - /* A constructor that accepts a key/value map */ - Context(std::map ctx_map) { ctx_map_ = ctx_map; } -}; - -/* The RuntimeContext class provides a wrapper for - * propogating context through cpp. */ -class RuntimeContext -{ + Key(nostd::string_view key_name) + { + key_name_ = key_name; + identifier_ = this; + } + }; -public: - /* The token class provides an identifier that is used by - * the attach and detach methods to keep track of context - * objects. - */ + template + class Context + { - class Token - { + public: - private: - friend class RuntimeContext; - Context ctx_; + Context() = default; + + Context(const T &attributes) { + KeyValueIterableModifiable iterable{attributes}; + key_val_map = iterable; + } + + Context(const KeyValueIterableModifiable iterable) { + key_val_map = iterable; + } + + Context WriteValues(const T &attributes) noexcept + { - /* A constructor that sets the token's Context object to the - * one that was passed in. - */ - Token(Context &ctx) { ctx_ = ctx; } + KeyValueIterableModifiable iterable = attributes; + iterable.Add(key_val_map.GetData()); + return Context(iterable.GetData()); + } - /* Returns the stored context object */ - Context GetContext() { return ctx_; } - }; + common::AttributeValue GetValue(Key key) { + return key_val_map.Get(key.GetIdentifier()); + } - /* A default constructor that will set the context to - * an empty context object. - */ - RuntimeContext() { context_ = Context(); } + private: + KeyValueIterableModifiable key_val_map; - /* A constructor that will set the context as the passed in context. */ - RuntimeContext(Context &context) { context_ = context; } + }; - /* Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - */ - Token Attach(Context &context) - { - Token old_context_token = Token(context_); - context_ = context; + template + class Token; - return old_context_token; - } + template + class RuntimeContext + { - /* Return the current context. */ - static Context GetCurrent() - { - Context context = context_; - return context_; - } - - /* Resets the context to a previous value stored in the - * passed in token. Returns zero if successful, -1 otherwise - */ - int Detach(Token &token) + public: + + Context GetCurrent(); + Token Attach(Context context); + int Detach(Token token); + }; + + + template + class ThreadLocalContext : public RuntimeContext + { + public: + + static Context GetCurrent(){ + return GetInstance(); + } + + int Detach(Token token){ + GetInstance() = token.GetCtx(); + return 0; + } + + Token Attach(Context context){ + Token old_context = Token(GetInstance()); + GetInstance() = context; + return old_context; + } + + private: + + static Context &GetInstance(){ + static Context instance; + return instance; + } + + }; + + + template + class Token { - if (token.GetContext() == context_) - { + private: + friend class RuntimeContext; + friend class ThreadLocalContext; - return -1; - } + Context ctx_; - context_ = token.GetContext(); + Token(Context ctx) { ctx_ = ctx; } - return 0; - } -private: - static thread_local Context context_; -}; + Context GetCtx() { return ctx_; } + }; -thread_local Context RuntimeContext::context_ = Context(); } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/BUILD b/api/test/context/BUILD index f69f02509c..e287b26b27 100644 --- a/api/test/context/BUILD +++ b/api/test/context/BUILD @@ -12,11 +12,21 @@ cc_test( ) cc_test( - name = "runtimeContext_test", + name = "runtime_context_test", srcs = [ - "runtimeContext_test.cc", + "runtime_context_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "thread_local_test", + srcs = [ + "thread_local_test.cc", ], - copts = ["-Wall","-std=c++17"], deps = [ "//api", "@com_google_googletest//:gtest_main", diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index a8903be443..4822422ec4 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -1,45 +1,94 @@ + #include "opentelemetry/context/context.h" +#include "opentelemetry/context/threadlocal_context.h" +#include "opentelemetry/context/key_value_iterable_modifiable.h" +#include "opentelemetry/nostd/string_view.h" + +#include "opentelemetry/common/attribute_value.h" #include -using namespace opentelemetry::context; + +using namespace opentelemetry; /* Tests whether the original context objects changes * when you write to it. */ TEST(Context_test, is_context_immutable) { + context::Key test_key = context::Key("test_key"); + context::Key other_key = context::Key("other_key"); + context::Key foo_key = context::Key("foo_key"); + + using M = std::map; + + M m1 = {{&test_key, "123"}, {&other_key, "456"}}; + M m2 = {{&foo_key, "000"}}; - Context test_context = Context(); - - Context::ContextKey test_key = test_context.CreateKey("test_key"); - - EXPECT_EQ(test_context.GetValue(test_key), NULL); - - Context new_test_context = test_context.WriteValue(test_key,7); + //trace::KeyValueIterable iterable{m1}; + //trace::KeyValueIterableView iterable_1{m1}; + + context::Context test_context = context::Context(m1); + + EXPECT_EQ(nostd::get(test_context.GetValue(test_key)), "123"); + EXPECT_EQ(nostd::get(test_context.GetValue(other_key)), "456"); + + context::Context foo_context = test_context.WriteValues(m2); - EXPECT_EQ(new_test_context.GetValue(test_key), 7); + EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "000"); + EXPECT_NE(nostd::get(test_context.GetValue(foo_key)), "000"); - EXPECT_EQ(test_context.GetValue(test_key), NULL); } /* Tests whether the new Context Objects inherits the keys and values * of the original context object */ -TEST(Context_test, context_write_new_object) -{ +TEST(Context_test, context_write_new_object){ + context::Key test_key = context::Key("test_key"); + context::Key other_key = context::Key("other_key"); + context::Key foo_key = context::Key("foo_key"); + + using M = std::map; + + M m1 = {{&test_key, "123"}, {&other_key, "456"}}; + M m2 = {{&foo_key, "000"}}; - Context::ContextKey test_key = Context::ContextKey("test_key"); + + context::KeyValueIterableModifiable iterable_1{m1}; + + context::Context test_context = context::Context(iterable_1); + + context::Context foo_context = test_context.WriteValues(m2); + + EXPECT_EQ(nostd::get(foo_context.GetValue(test_key)), "123"); + EXPECT_EQ(nostd::get(foo_context.GetValue(other_key)), "456"); + EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "000"); +} - Context test_context = Context(test_key, 7); +TEST(Context_test, thread_local_context){ - Context::ContextKey foo_key = test_context.CreateKey("foo_key"); + context::Key test_key = context::Key("test_key"); + context::Key other_key = context::Key("other_key"); + context::Key foo_key = context::Key("foo_key"); + + using M = std::map; + + M m1 = {{&test_key, "123"}, {&other_key, "456"}}; + M m2 = {{&foo_key, "000"}}; - Context new_test_context = test_context.WriteValue(foo_key,1); - - EXPECT_EQ(new_test_context.GetValue(test_key), 7); + + context::KeyValueIterableModifiable iterable_1{m1}; + + context::Context test_context = context::Context(iterable_1); + context::ThreadLocalContext test_thread_context; + + context::Token test_token = test_thread_context.Attach(test_context); - EXPECT_EQ(new_test_context.GetValue(foo_key), 1); -} + EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + + EXPECT_EQ(test_thread_context.Detach(test_token), 0); + + EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); +} diff --git a/api/test/context/runtime_context_test.cc b/api/test/context/runtime_context_test.cc index 0a2ab92cb5..c13abf13f3 100644 --- a/api/test/context/runtime_context_test.cc +++ b/api/test/context/runtime_context_test.cc @@ -1,53 +1,76 @@ #include "opentelemetry/context/context.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/context/key_value_iterable_modifiable.h" #include -namespace -{ -using opentelemetry::context::Context; -using opentelemetry::context::RuntimeContext; - -/* Tests whether the runtimeContext object properly returns the current context - */ -TEST(RuntimeContextTest, GetCurrentContext) -{ - - Context::Key test_key = Context::Key("test_key"); - Context test_context = Context(test_key, 7); - - RuntimeContext test_runtime = RuntimeContext(test_context); - - EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); -} - -/* Tests whether the runtimeContext object properly attaches and detaches - * the context object. - */ -TEST(RuntimeContextTest, AttachDetachContext) -{ - - Context::Key test_key = Context::Key("test_key"); - Context test_context = Context(test_key, 7); - - RuntimeContext test_runtime = RuntimeContext(test_context); - - Context::Key foo_key = Context::Key("foo_key"); - Context foo_context = Context(foo_key, 5); - - EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); - EXPECT_NE(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); - - RuntimeContext::Token test_token = test_runtime.Attach(foo_context); - - EXPECT_NE(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); - EXPECT_EQ(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); - - int detach_result = test_runtime.Detach(test_token); - - EXPECT_EQ(detach_result, 0); - - EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), test_context.GetValue(test_key)); - EXPECT_NE(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); -} -} // namespace + using namespace opentelemetry/*::Context*/; + //using opentelemetry::context::RuntimeContext; + + /* Tests whether the runtimeContext object properly returns the current context + */ + TEST(RuntimeContextTest, GetCurrentContext) + { + using M = std::map< context::Key* , nostd::string_view>; + + context::Key test_key = context::Key("test_key"); + + M m1 = {{&test_key, "123"}}; + + context::KeyValueIterableModifiable iterable_1{m1}; + + context::Context test_context = context::Context(iterable_1); + + context::RuntimeContext test_runtime;// = context::RuntimeContext(test_context); + + test_runtime.Attach(test_context); + + test_context.GetValue(test_key); + + test_runtime.GetCurrent().GetValue(test_key); + + EXPECT_EQ(nostd::get(test_runtime.GetCurrent().GetValue(test_key)), "123"); + EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(test_key)), nostd::get(test_context.GetValue(test_key)) ); + } + + /* Tests whether the runtimeContext object properly attaches and detaches + * the context object. + */ + TEST(RuntimeContextTest, AttachDetachContext) + { + + using M = std::map< context::Key* , nostd::string_view>; + + context::Key test_key = context::Key("test_key"); + + context::Key foo_key = context::Key("foo_key"); + + M m1 = {{&test_key, "123"}}; + + M m2 = {{&foo_key, "534"}}; + + context::KeyValueIterableModifiable iterable_1{m1}; + + context::Context test_context = context::Context(iterable_1); + + context::KeyValueIterableModifiable iterable_2{m2}; + + context::Context foo_context = context::Context(iterable_2); + + context::RuntimeContext test_runtime;// = context::RuntimeContext(test_context); + + test_runtime.Attach(test_context); + + EXPECT_EQ(nostd::get(test_runtime.GetCurrent().GetValue(test_key)), "123"); + EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(test_key)), nostd::get(test_context.GetValue(test_key)) ); + + context::RuntimeContext::Token old_context = test_runtime.Attach(foo_context); + + EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(foo_key)), nostd::get(foo_context.GetValue(foo_key)) ); + + test_runtime.Detach(old_context); + + EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(test_key)), nostd::get(test_context.GetValue(test_key)) ); + } From a0869937c89d8c5fda5b04e1136a2be18a14cc67 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 17 Jun 2020 18:37:23 +0000 Subject: [PATCH 03/10] replaced std::map and created a thread_local context --- .../context/key_value_iterable_modifiable.h | 98 +++++++++++++++++++ .../context/threadlocal_context.h | 15 +++ api/test/context/runtimeContext_test.cc | 61 ------------ api/test/context/thread_local_test.cc | 31 ++++++ 4 files changed, 144 insertions(+), 61 deletions(-) create mode 100644 api/include/opentelemetry/context/key_value_iterable_modifiable.h create mode 100644 api/include/opentelemetry/context/threadlocal_context.h delete mode 100644 api/test/context/runtimeContext_test.cc create mode 100644 api/test/context/thread_local_test.cc diff --git a/api/include/opentelemetry/context/key_value_iterable_modifiable.h b/api/include/opentelemetry/context/key_value_iterable_modifiable.h new file mode 100644 index 0000000000..ca1b4f7a2a --- /dev/null +++ b/api/include/opentelemetry/context/key_value_iterable_modifiable.h @@ -0,0 +1,98 @@ +#pragma once + +#include "opentelemetry/trace/key_value_iterable.h" +#include "opentelemetry/context/context.h" +#include "opentelemetry/version.h" +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/common/attribute_value.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace context +{ + + + template + class KeyValueIterableModifiable final : public trace::KeyValueIterable + { + //static_assert(trace::detail::is_key_value_iterable::value, "Must be a key-value iterable"); + + public: + KeyValueIterableModifiable(T container) noexcept : container_{container} {} + + KeyValueIterableModifiable() = default; + // KeyValueIterable + void PrintKeys() + { + std::cout << "Printing Keys" << std::endl; + auto iter = std::begin(container_); + auto last = std::end(container_); + for (; iter != last; ++iter) + { + std::cout /*<< iter->first << " "*/ << iter->second << std::endl; + } + // return true; + } + + void Add(T container) + { + + std::insert_iterator back (container_, std::begin(container_)); + + auto iter = std::begin(container); + auto last = std::end(container); + for (; iter != last; ++iter) + { + back = *iter; + } + + } + + template + common::AttributeValue Get(K key) + { + auto iter = std::begin(container_); + auto last = std::end(container_); + for (; iter != last; ++iter) + { + + if(iter->first == key){ + return iter->second; + } + + } + + return ""; + + + } + + T GetData(){ + return container_; + } + + // KeyValueIterable + bool ForEachKeyValue( + nostd::function_ref 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: + T container_; + }; +} // namespace nostd +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h new file mode 100644 index 0000000000..6b24bd8a2c --- /dev/null +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -0,0 +1,15 @@ +#pragma once + +#include "opentelemetry/version.h" +#include "opentelemetry/context/context.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace context +{ + + //template + //class RuntimeContext; + + +} +OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/runtimeContext_test.cc b/api/test/context/runtimeContext_test.cc deleted file mode 100644 index e283071c1d..0000000000 --- a/api/test/context/runtimeContext_test.cc +++ /dev/null @@ -1,61 +0,0 @@ -#include "opentelemetry/context/context.h" - -#include - -using namespace opentelemetry::context; - - -/* Tests whether the runtimeContext object properly returns the current context - */ -TEST(runtimeContext_test, get_current_context) -{ - - Context::ContextKey test_key = Context::ContextKey("test_key"); - Context test_context = Context(test_key, 7); - - RuntimeContext test_runtime = RuntimeContext(test_context); - - EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), - test_context.GetValue(test_key)); - -} - - - - -/* Tests whether the runtimeContext object properly attaches and detaches - * the context object. - */ -TEST(runtimeContext_test, attach_detach_context) -{ - - Context::ContextKey test_key = Context::ContextKey("test_key"); - Context test_context = Context(test_key, 7); - - RuntimeContext test_runtime = RuntimeContext(test_context); - - Context::ContextKey foo_key = Context::ContextKey("foo_key"); - Context foo_context = Context(foo_key, 5); - - EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), - test_context.GetValue(test_key)); - EXPECT_NE(test_runtime.GetCurrent().GetValue(foo_key), - foo_context.GetValue(foo_key)); - - Token test_token = test_runtime.Attach(foo_context); - - EXPECT_NE(test_runtime.GetCurrent().GetValue(test_key), - test_context.GetValue(test_key)); - EXPECT_EQ(test_runtime.GetCurrent().GetValue(foo_key), - foo_context.GetValue(foo_key)); - - int detach_result = test_runtime.Detach(test_token); - - EXPECT_EQ(detach_result, 0); - - EXPECT_EQ(test_runtime.GetCurrent().GetValue(test_key), - test_context.GetValue(test_key)); - EXPECT_NE(test_runtime.GetCurrent().GetValue(foo_key), - foo_context.GetValue(foo_key)); -} - diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc new file mode 100644 index 0000000000..c6fc887963 --- /dev/null +++ b/api/test/context/thread_local_test.cc @@ -0,0 +1,31 @@ +/* +#include "opentelemetry/context/context.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/context/key_value_iterable_modifiable.h" + */ + +#include "opentelemetry/context/context.h" +#include "opentelemetry/context/threadlocal_context.h" + +#include + + +using namespace opentelemetry/*::Context*/; + +TEST(ThreadLocalContextTest, GetThreadlocalContext) +{ + + using M = std::map< context::Key* , nostd::string_view>; + + context::Key test_key = context::Key("test_key"); + + M m1 = {{&test_key, "123"}}; + + +// std::cout << "Hello World" << std::endl; + +// context::ThreadLocalContext tlc; + +} + From e44e9260e64826d79617b0ce3edc6bff136b8828 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 17 Jun 2020 19:43:36 +0000 Subject: [PATCH 04/10] cleaned up --- api/include/opentelemetry/context/TBD | 0 api/include/opentelemetry/context/context.h | 129 ++++++------------ .../context/key_value_iterable_modifiable.h | 28 ++-- .../context/threadlocal_context.h | 65 ++++++++- api/test/context/context_test.cc | 16 ++- api/test/context/thread_local_test.cc | 35 +++-- 6 files changed, 136 insertions(+), 137 deletions(-) delete mode 100644 api/include/opentelemetry/context/TBD diff --git a/api/include/opentelemetry/context/TBD b/api/include/opentelemetry/context/TBD deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index bfb4bffa6b..fd673ed65c 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include @@ -12,62 +11,79 @@ #include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/trace/key_value_iterable.h" #include "opentelemetry/context/key_value_iterable_modifiable.h" -//#include "opentelemetry/context/threadlocal_context.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - /*The context class provides a context identifier */ + /*The Key class is used to obscure access from the + * user to the context map. The identifier is used as a key + * to the context map. + */ class Key { - private: - template - friend class Context; - - nostd::string_view key_name_; - - Key* identifier_; - - - - Key(nostd::string_view key_name, int identifier) - { - key_name_ = key_name; - identifier_ = this; - } public: + /* GetIdentifier: returns the identifier */ Key* GetIdentifier() { return identifier_; } + /* Constructs a new Key with the passed in name */ Key(nostd::string_view key_name) { key_name_ = key_name; identifier_ = this; } + + private: + template + friend class Context; + + nostd::string_view key_name_; + + Key* identifier_; + + + /* + Key(nostd::string_view key_name, int identifier) + { + key_name_ = key_name; + identifier_ = this; + } + */ }; - template + /* The context class provides a context identifier */ + template class Context { public: - + /* Creates a context object with no key/value pairs */ Context() = default; - + + /* Contructor, creates a context object from a map + * of keys and identifiers + */ Context(const T &attributes) { KeyValueIterableModifiable iterable{attributes}; key_val_map = iterable; } - + + /* Contructor, creates a context object from a + * KeyValueIterableModfiable object. + */ Context(const KeyValueIterableModifiable iterable) { key_val_map = iterable; } - + + /* Accepts a new iterable and then returns a new + * context that contains both the original pairs + * and the new pair. + */ Context WriteValues(const T &attributes) noexcept { @@ -75,7 +91,8 @@ namespace context iterable.Add(key_val_map.GetData()); return Context(iterable.GetData()); } - + + /* Returns the value associated with the passed in key */ common::AttributeValue GetValue(Key key) { return key_val_map.Get(key.GetIdentifier()); } @@ -84,69 +101,5 @@ namespace context KeyValueIterableModifiable key_val_map; }; - - - - template - class Token; - - template - class RuntimeContext - { - - public: - - Context GetCurrent(); - Token Attach(Context context); - int Detach(Token token); - }; - - - template - class ThreadLocalContext : public RuntimeContext - { - public: - - static Context GetCurrent(){ - return GetInstance(); - } - - int Detach(Token token){ - GetInstance() = token.GetCtx(); - return 0; - } - - Token Attach(Context context){ - Token old_context = Token(GetInstance()); - GetInstance() = context; - return old_context; - } - - private: - - static Context &GetInstance(){ - static Context instance; - return instance; - } - - }; - - - template - class Token - { - - private: - friend class RuntimeContext; - friend class ThreadLocalContext; - - Context ctx_; - - Token(Context ctx) { ctx_ = ctx; } - - - Context GetCtx() { return ctx_; } - }; - } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/key_value_iterable_modifiable.h b/api/include/opentelemetry/context/key_value_iterable_modifiable.h index ca1b4f7a2a..f6b552589f 100644 --- a/api/include/opentelemetry/context/key_value_iterable_modifiable.h +++ b/api/include/opentelemetry/context/key_value_iterable_modifiable.h @@ -14,25 +14,13 @@ namespace context template class KeyValueIterableModifiable final : public trace::KeyValueIterable { - //static_assert(trace::detail::is_key_value_iterable::value, "Must be a key-value iterable"); public: KeyValueIterableModifiable(T container) noexcept : container_{container} {} KeyValueIterableModifiable() = default; - // KeyValueIterable - void PrintKeys() - { - std::cout << "Printing Keys" << std::endl; - auto iter = std::begin(container_); - auto last = std::end(container_); - for (; iter != last; ++iter) - { - std::cout /*<< iter->first << " "*/ << iter->second << std::endl; - } - // return true; - } - + + void Add(T container) { @@ -65,25 +53,27 @@ namespace context } - + + /* Returns the stored data. */ T GetData(){ return container_; } - // KeyValueIterable bool ForEachKeyValue( nostd::function_ref callback) const noexcept override { + //TODO + /* auto iter = std::begin(container_); auto last = std::end(container_); for (; iter != last; ++iter) { -/* if (!callback(iter->first, iter->second)) + if (!callback(iter->first, iter->second)) { return false; - }*/ - } + } + }*/ return true; } diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 6b24bd8a2c..73b5c770b0 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -1,15 +1,68 @@ #pragma once -#include "opentelemetry/version.h" -#include "opentelemetry/context/context.h" +#include "opentelemetry/context/runtime_context.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - - //template - //class RuntimeContext; - + /* The ThreadLocalContext class is a derived class from RuntimeContext and provides a wrapper for + * propogating context through cpp thread locally. */ + template + class ThreadLocalContext : public RuntimeContext + { + public: + + /* The token class provides an identifier that is used by + * the attach and detach methods to keep track of context + * objects. + */ + class Token + { + private: + friend class ThreadLocalContext; + + Context ctx_ + ; + /* A constructor that sets the token's Context object to the + * one that was passed in. + */ + Token(Context ctx) { ctx_ = ctx; } + + /* Returns the stored context object */ + Context GetCtx() { return ctx_; } + }; + + /* Return the current context. */ + static Context GetCurrent(){ + return GetInstance(); + } + + /* Resets the context to a previous value stored in the + * passed in token. Returns zero if successful, -1 otherwise + */ + static int Detach(Token token){ + GetInstance() = token.GetCtx(); + return 0; + } + + /* Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + */ + static Token Attach(Context context){ + Token old_context = Token(GetInstance()); + GetInstance() = context; + return old_context; + } + + private: + + /* Provides storage and access to the thread_local context object */ + static Context &GetInstance(){ + static thread_local Context instance; + return instance; + } + + }; } OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 4822422ec4..6298638325 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -1,10 +1,9 @@ - #include "opentelemetry/context/context.h" -#include "opentelemetry/context/threadlocal_context.h" #include "opentelemetry/context/key_value_iterable_modifiable.h" #include "opentelemetry/nostd/string_view.h" - #include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/context/runtime_context.h" +#include "opentelemetry/context/threadlocal_context.h" #include @@ -16,19 +15,21 @@ using namespace opentelemetry; */ TEST(Context_test, is_context_immutable) { + /* context::Key test_key = context::Key("test_key"); context::Key other_key = context::Key("other_key"); context::Key foo_key = context::Key("foo_key"); - + */ using M = std::map; + context::Context test_context = context::Context(m1); + M m1 = {{&test_key, "123"}, {&other_key, "456"}}; M m2 = {{&foo_key, "000"}}; //trace::KeyValueIterable iterable{m1}; //trace::KeyValueIterableView iterable_1{m1}; - context::Context test_context = context::Context(m1); EXPECT_EQ(nostd::get(test_context.GetValue(test_key)), "123"); EXPECT_EQ(nostd::get(test_context.GetValue(other_key)), "456"); @@ -43,6 +44,7 @@ TEST(Context_test, is_context_immutable) /* Tests whether the new Context Objects inherits the keys and values * of the original context object */ +/* TEST(Context_test, context_write_new_object){ context::Key test_key = context::Key("test_key"); @@ -65,7 +67,8 @@ TEST(Context_test, context_write_new_object){ EXPECT_EQ(nostd::get(foo_context.GetValue(other_key)), "456"); EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "000"); } - +*/ +/* TEST(Context_test, thread_local_context){ context::Key test_key = context::Key("test_key"); @@ -92,3 +95,4 @@ TEST(Context_test, thread_local_context){ EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); } +*/ diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc index c6fc887963..875176de92 100644 --- a/api/test/context/thread_local_test.cc +++ b/api/test/context/thread_local_test.cc @@ -1,31 +1,30 @@ -/* -#include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/utility.h" -#include "opentelemetry/context/key_value_iterable_modifiable.h" - */ - -#include "opentelemetry/context/context.h" + #include "opentelemetry/context/threadlocal_context.h" #include +using namespace opentelemetry; -using namespace opentelemetry/*::Context*/; - -TEST(ThreadLocalContextTest, GetThreadlocalContext) -{ - - using M = std::map< context::Key* , nostd::string_view>; +TEST(Thread_Local_Context_Test, thread_local_context){ context::Key test_key = context::Key("test_key"); + context::Key other_key = context::Key("other_key"); + context::Key foo_key = context::Key("foo_key"); - M m1 = {{&test_key, "123"}}; + using M = std::map; + M m1 = {{&test_key, "123"}, {&other_key, "456"}}; + M m2 = {{&foo_key, "000"}}; -// std::cout << "Hello World" << std::endl; + context::KeyValueIterableModifiable iterable_1{m1}; -// context::ThreadLocalContext tlc; + context::Context test_context = context::Context(iterable_1); + context::ThreadLocalContext test_thread_context; -} + context::ThreadLocalContext::Token test_token = test_thread_context.Attach(test_context); + + EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + EXPECT_EQ(test_thread_context.Detach(test_token), 0); + EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); +} From 6edce29440b2ba610d3c1848246dbe35ac54919c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 17 Jun 2020 19:43:55 +0000 Subject: [PATCH 05/10] cleaned up --- .../opentelemetry/context/runtime_context.h | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 api/include/opentelemetry/context/runtime_context.h diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h new file mode 100644 index 0000000000..ceb863190f --- /dev/null +++ b/api/include/opentelemetry/context/runtime_context.h @@ -0,0 +1,53 @@ +#pragma once + +#include "context.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace context +{ + + /* The RuntimeContext class provides a wrapper for + * propogating context through cpp. */ + template + class RuntimeContext + { + public: + + /* The token class provides an identifier that is used by + * the attach and detach methods to keep track of context + * objects. + */ + class Token + { + private: + friend class RuntimeContext; + + Context ctx_; + + /* A constructor that sets the token's Context object to the + * one that was passed in. + */ + Token(Context ctx) { ctx_ = ctx; } + + /* Returns the stored context object */ + Context GetCtx() { return ctx_; } + }; + + /* Return the current context. */ + Context GetCurrent(); + + /* Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + */ + + Token Attach(Context context); + + /* Resets the context to a previous value stored in the + * passed in token. Returns zero if successful, -1 otherwise + */ + int Detach(Token token); + }; + + +} +OPENTELEMETRY_END_NAMESPACE From dff6f8f613f1d07891a27064ca6fdd09dde94280 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Sat, 20 Jun 2020 22:07:49 +0000 Subject: [PATCH 06/10] fixed undefined behavior in context.h --- api/include/opentelemetry/context/context.h | 139 ++++++++++++-------- api/test/context/context_test.cc | 134 ++++++++----------- api/test/context/thread_local_test.cc | 19 ++- 3 files changed, 150 insertions(+), 142 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index fd673ed65c..b1261534a7 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,59 +1,27 @@ #pragma once -#include +//#include #include #include +#include +#include +#include +#include "opentelemetry/nostd/utility.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/version.h" #include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/trace/key_value_iterable.h" -#include "opentelemetry/context/key_value_iterable_modifiable.h" +//#include "opentelemetry/context/key_value_iterable_modifiable.h" +#include "opentelemetry/trace/tracer.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - /*The Key class is used to obscure access from the - * user to the context map. The identifier is used as a key - * to the context map. - */ - class Key - { - - public: - - /* GetIdentifier: returns the identifier */ - Key* GetIdentifier() { return identifier_; } - - - /* Constructs a new Key with the passed in name */ - Key(nostd::string_view key_name) - { - key_name_ = key_name; - identifier_ = this; - } - - private: - template - friend class Context; - - nostd::string_view key_name_; - - Key* identifier_; - - - /* - Key(nostd::string_view key_name, int identifier) - { - key_name_ = key_name; - identifier_ = this; - } - */ - }; /* The context class provides a context identifier */ template @@ -62,44 +30,109 @@ namespace context public: - /* Creates a context object with no key/value pairs */ - Context() = default; + /*The Key class is used to obscure access from the + * user to the context map. The identifier is used as a key + * to the context map. + */ + class Key + { + + public: + + nostd::string_view GetIdentifier() {return nostd::string_view(str_data);} + + nostd::string_view GetName() { return key_name_; } + + /* Constructs a new Key with the passed in name */ + Key(nostd::string_view key_name) : key_name_{key_name} + { + + std::stringstream ss; + ss << (void *)this; + nostd::string_view test_view = "test"; + test_view = ss.str(); + + memcpy(str_data, test_view.data(), test_view.size()); + } + + private: + friend class Context; + + nostd::string_view key_name_; + + char str_data [50]; + + const nostd::string_view identifier_; + + bool operator == (const Key& key){ + if(identifier_ == key.identifier_){ + return true; + } + return false; + } + }; + + Key CreateKey(nostd::string_view key_name){ + + return Key(key_name); + } /* Contructor, creates a context object from a map * of keys and identifiers */ - Context(const T &attributes) { - KeyValueIterableModifiable iterable{attributes}; - key_val_map = iterable; + Context(const T &attributes): key_vals(attributes) { + + trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); } /* Contructor, creates a context object from a * KeyValueIterableModfiable object. */ - Context(const KeyValueIterableModifiable iterable) { - key_val_map = iterable; + Context() { } /* Accepts a new iterable and then returns a new * context that contains both the original pairs * and the new pair. */ - Context WriteValues(const T &attributes) noexcept + Context WriteValues(T &attributes) noexcept { - KeyValueIterableModifiable iterable = attributes; - iterable.Add(key_val_map.GetData()); - return Context(iterable.GetData()); + trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); + + std::insert_iterator back (attributes, std::begin(attributes)); + + + + auto iter = std::begin(key_vals); + auto last = std::end(key_vals); + for (; iter != last; ++iter) + { + back = *iter; + } + return Context(attributes); } - + /* Returns the value associated with the passed in key */ common::AttributeValue GetValue(Key key) { - return key_val_map.Get(key.GetIdentifier()); + auto iter = std::begin(key_vals); + auto last = std::end(key_vals); + int count = 0; + for (; iter != last; ++iter) + { + if(key.GetIdentifier() == iter->first){ + return iter->second; + } + count++; + } + + return ""; } private: - KeyValueIterableModifiable key_val_map; + T key_vals; }; + } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 6298638325..1a02e7259a 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -1,98 +1,76 @@ + +#include + #include "opentelemetry/context/context.h" -#include "opentelemetry/context/key_value_iterable_modifiable.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/common/attribute_value.h" -#include "opentelemetry/context/runtime_context.h" -#include "opentelemetry/context/threadlocal_context.h" #include using namespace opentelemetry; -/* Tests whether the original context objects changes - * when you write to it. - */ +/* Test ensurs that the context object doe not change when you write to it */ TEST(Context_test, is_context_immutable) { - /* - context::Key test_key = context::Key("test_key"); - context::Key other_key = context::Key("other_key"); - context::Key foo_key = context::Key("foo_key"); - */ - using M = std::map; - - context::Context test_context = context::Context(m1); - - M m1 = {{&test_key, "123"}, {&other_key, "456"}}; - M m2 = {{&foo_key, "000"}}; - - //trace::KeyValueIterable iterable{m1}; - //trace::KeyValueIterableView iterable_1{m1}; - - - EXPECT_EQ(nostd::get(test_context.GetValue(test_key)), "123"); - EXPECT_EQ(nostd::get(test_context.GetValue(other_key)), "456"); - - context::Context foo_context = test_context.WriteValues(m2); - - EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "000"); - EXPECT_NE(nostd::get(test_context.GetValue(foo_key)), "000"); + using M = std::map< std::string, common::AttributeValue>; + context::Context test_context = context::Context(); + + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("test_key"); + + M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + + context::Context foo_context = test_context.WriteValues(m1); + EXPECT_NE(nostd::get(test_context.GetValue(test_key)), "123"); + EXPECT_NE(nostd::get(test_context.GetValue(foo_key)), "456"); } /* Tests whether the new Context Objects inherits the keys and values * of the original context object */ -/* TEST(Context_test, context_write_new_object){ - context::Key test_key = context::Key("test_key"); - context::Key other_key = context::Key("other_key"); - context::Key foo_key = context::Key("foo_key"); - - using M = std::map; - - M m1 = {{&test_key, "123"}, {&other_key, "456"}}; - M m2 = {{&foo_key, "000"}}; - - - context::KeyValueIterableModifiable iterable_1{m1}; - - context::Context test_context = context::Context(iterable_1); - - context::Context foo_context = test_context.WriteValues(m2); - - EXPECT_EQ(nostd::get(foo_context.GetValue(test_key)), "123"); - EXPECT_EQ(nostd::get(foo_context.GetValue(other_key)), "456"); - EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "000"); + using M = std::map< std::string, common::AttributeValue>; + context::Context test_context = context::Context(); + + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); + + M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + M m2 = {{std::string(other_key.GetIdentifier()) , "000"}}; + + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); + + EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "123"); + EXPECT_EQ(nostd::get(other_context.GetValue(foo_key)), "456"); } -*/ /* -TEST(Context_test, thread_local_context){ - - context::Key test_key = context::Key("test_key"); - context::Key other_key = context::Key("other_key"); - context::Key foo_key = context::Key("foo_key"); - - using M = std::map; - - M m1 = {{&test_key, "123"}, {&other_key, "456"}}; - M m2 = {{&foo_key, "000"}}; - - - context::KeyValueIterableModifiable iterable_1{m1}; - - context::Context test_context = context::Context(iterable_1); - context::ThreadLocalContext test_thread_context; - - context::Token test_token = test_thread_context.Attach(test_context); - - EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); - - EXPECT_EQ(test_thread_context.Detach(test_token), 0); - - EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + TEST(Context_test, thread_local_context){ -} -*/ + context::Key test_key = context::Key("test_key"); + context::Key other_key = context::Key("other_key"); + context::Key foo_key = context::Key("foo_key"); + + using M = std::map; + + M m1 = {{&test_key, "123"}, {&other_key, "456"}}; + M m2 = {{&foo_key, "000"}}; + + + context::KeyValueIterableModifiable iterable_1{m1}; + + context::Context test_context = context::Context(iterable_1); + context::ThreadLocalContext test_thread_context; + + context::Token test_token = test_thread_context.Attach(test_context); + + EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + + EXPECT_EQ(test_thread_context.Detach(test_token), 0); + + EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + + } + */ diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc index 875176de92..e61dcff5f2 100644 --- a/api/test/context/thread_local_test.cc +++ b/api/test/context/thread_local_test.cc @@ -7,24 +7,21 @@ using namespace opentelemetry; TEST(Thread_Local_Context_Test, thread_local_context){ - context::Key test_key = context::Key("test_key"); - context::Key other_key = context::Key("other_key"); - context::Key foo_key = context::Key("foo_key"); + using M = std::map< std::string, common::AttributeValue>; - using M = std::map; + context::Context test_context = context::Context(); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("test_key"); - M m1 = {{&test_key, "123"}, {&other_key, "456"}}; - M m2 = {{&foo_key, "000"}}; + M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; - context::KeyValueIterableModifiable iterable_1{m1}; + context::Context foo_context = test_context.WriteValues(m1); - context::Context test_context = context::Context(iterable_1); context::ThreadLocalContext test_thread_context; - context::ThreadLocalContext::Token test_token = test_thread_context.Attach(test_context); - - EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + context::ThreadLocalContext::Token test_token = test_thread_context.Attach(foo_context); + EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); EXPECT_EQ(test_thread_context.Detach(test_token), 0); EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); } From e660ab7084df199a193489e0526b3a916d4adab8 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Sun, 21 Jun 2020 00:45:03 +0000 Subject: [PATCH 07/10] minor style --- api/include/opentelemetry/context/context.h | 46 +++++++------------ .../opentelemetry/context/runtime_context.h | 4 +- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index b1261534a7..1a7ba42678 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,6 +1,5 @@ #pragma once -//#include #include #include #include @@ -14,7 +13,6 @@ #include "opentelemetry/version.h" #include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/trace/key_value_iterable.h" -//#include "opentelemetry/context/key_value_iterable_modifiable.h" #include "opentelemetry/trace/tracer.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -38,15 +36,18 @@ namespace context { public: - + /*Returns the key's identifier*/ nostd::string_view GetIdentifier() {return nostd::string_view(str_data);} - + /*Returns the key's name */ nostd::string_view GetName() { return key_name_; } - /* Constructs a new Key with the passed in name */ + private: + friend class Context; + + /* Constructs a new Key with the passed in name. Sets the identifier as + * the address of this object. */ Key(nostd::string_view key_name) : key_name_{key_name} { - std::stringstream ss; ss << (void *)this; nostd::string_view test_view = "test"; @@ -55,8 +56,6 @@ namespace context memcpy(str_data, test_view.data(), test_view.size()); } - private: - friend class Context; nostd::string_view key_name_; @@ -64,45 +63,32 @@ namespace context const nostd::string_view identifier_; - bool operator == (const Key& key){ - if(identifier_ == key.identifier_){ - return true; - } - return false; - } }; + /* Creates a key with the passed in name and returns it. */ Key CreateKey(nostd::string_view key_name){ - return Key(key_name); } - /* Contructor, creates a context object from a map - * of keys and identifiers + /* Contructor, creates a context object from a map of keys + * and identifiers. */ Context(const T &attributes): key_vals(attributes) { - + /*Currently only used as a check, to ensure T is of the right type */ trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); } - /* Contructor, creates a context object from a - * KeyValueIterableModfiable object. - */ Context() { } - /* Accepts a new iterable and then returns a new - * context that contains both the original pairs - * and the new pair. + /* Accepts a new iterable and then returns a new context that + * contains both the original pairs and the new pair. */ Context WriteValues(T &attributes) noexcept { - trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); - - std::insert_iterator back (attributes, std::begin(attributes)); - + std::insert_iterator back (attributes, std::begin(attributes)); auto iter = std::begin(key_vals); auto last = std::end(key_vals); @@ -110,6 +96,7 @@ namespace context { back = *iter; } + return Context(attributes); } @@ -117,13 +104,12 @@ namespace context common::AttributeValue GetValue(Key key) { auto iter = std::begin(key_vals); auto last = std::end(key_vals); - int count = 0; + for (; iter != last; ++iter) { if(key.GetIdentifier() == iter->first){ return iter->second; } - count++; } return ""; diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index ceb863190f..75de839b6f 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -7,7 +7,7 @@ namespace context { /* The RuntimeContext class provides a wrapper for - * propogating context through cpp. */ + * propogating context through cpp. */ template class RuntimeContext { @@ -29,7 +29,7 @@ namespace context */ Token(Context ctx) { ctx_ = ctx; } - /* Returns the stored context object */ + /* Returns the stored context object. */ Context GetCtx() { return ctx_; } }; From aec8ca81511a20c516ada7c229139362f10e90b2 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Sun, 21 Jun 2020 00:56:47 +0000 Subject: [PATCH 08/10] style --- .../context/key_value_iterable_modifiable.h | 88 ------------------- api/test/context/context_test.cc | 33 +------ api/test/context/runtime_context_test.cc | 76 ---------------- api/test/context/thread_local_test.cc | 2 +- 4 files changed, 3 insertions(+), 196 deletions(-) delete mode 100644 api/include/opentelemetry/context/key_value_iterable_modifiable.h delete mode 100644 api/test/context/runtime_context_test.cc diff --git a/api/include/opentelemetry/context/key_value_iterable_modifiable.h b/api/include/opentelemetry/context/key_value_iterable_modifiable.h deleted file mode 100644 index f6b552589f..0000000000 --- a/api/include/opentelemetry/context/key_value_iterable_modifiable.h +++ /dev/null @@ -1,88 +0,0 @@ -#pragma once - -#include "opentelemetry/trace/key_value_iterable.h" -#include "opentelemetry/context/context.h" -#include "opentelemetry/version.h" -#include "opentelemetry/nostd/utility.h" -#include "opentelemetry/common/attribute_value.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace context -{ - - - template - class KeyValueIterableModifiable final : public trace::KeyValueIterable - { - - public: - KeyValueIterableModifiable(T container) noexcept : container_{container} {} - - KeyValueIterableModifiable() = default; - - - void Add(T container) - { - - std::insert_iterator back (container_, std::begin(container_)); - - auto iter = std::begin(container); - auto last = std::end(container); - for (; iter != last; ++iter) - { - back = *iter; - } - - } - - template - common::AttributeValue Get(K key) - { - auto iter = std::begin(container_); - auto last = std::end(container_); - for (; iter != last; ++iter) - { - - if(iter->first == key){ - return iter->second; - } - - } - - return ""; - - - } - - /* Returns the stored data. */ - T GetData(){ - return container_; - } - - bool ForEachKeyValue( - nostd::function_ref callback) const - noexcept override - { - //TODO - /* - 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: - T container_; - }; -} // namespace nostd -OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 1a02e7259a..72b1020201 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -1,4 +1,3 @@ - #include #include "opentelemetry/context/context.h" @@ -9,7 +8,7 @@ using namespace opentelemetry; /* Test ensurs that the context object doe not change when you write to it */ -TEST(Context_test, is_context_immutable) +TEST(ContextTest, ContextImmutability) { using M = std::map< std::string, common::AttributeValue>; context::Context test_context = context::Context(); @@ -28,7 +27,7 @@ TEST(Context_test, is_context_immutable) /* Tests whether the new Context Objects inherits the keys and values * of the original context object */ -TEST(Context_test, context_write_new_object){ +TEST(ContextTest, ContextInheritance){ using M = std::map< std::string, common::AttributeValue>; context::Context test_context = context::Context(); @@ -46,31 +45,3 @@ TEST(Context_test, context_write_new_object){ EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "123"); EXPECT_EQ(nostd::get(other_context.GetValue(foo_key)), "456"); } -/* - TEST(Context_test, thread_local_context){ - - context::Key test_key = context::Key("test_key"); - context::Key other_key = context::Key("other_key"); - context::Key foo_key = context::Key("foo_key"); - - using M = std::map; - - M m1 = {{&test_key, "123"}, {&other_key, "456"}}; - M m2 = {{&foo_key, "000"}}; - - - context::KeyValueIterableModifiable iterable_1{m1}; - - context::Context test_context = context::Context(iterable_1); - context::ThreadLocalContext test_thread_context; - - context::Token test_token = test_thread_context.Attach(test_context); - - EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); - - EXPECT_EQ(test_thread_context.Detach(test_token), 0); - - EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); - - } - */ diff --git a/api/test/context/runtime_context_test.cc b/api/test/context/runtime_context_test.cc deleted file mode 100644 index c13abf13f3..0000000000 --- a/api/test/context/runtime_context_test.cc +++ /dev/null @@ -1,76 +0,0 @@ -#include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/utility.h" -#include "opentelemetry/context/key_value_iterable_modifiable.h" - -#include - - - using namespace opentelemetry/*::Context*/; - //using opentelemetry::context::RuntimeContext; - - /* Tests whether the runtimeContext object properly returns the current context - */ - TEST(RuntimeContextTest, GetCurrentContext) - { - using M = std::map< context::Key* , nostd::string_view>; - - context::Key test_key = context::Key("test_key"); - - M m1 = {{&test_key, "123"}}; - - context::KeyValueIterableModifiable iterable_1{m1}; - - context::Context test_context = context::Context(iterable_1); - - context::RuntimeContext test_runtime;// = context::RuntimeContext(test_context); - - test_runtime.Attach(test_context); - - test_context.GetValue(test_key); - - test_runtime.GetCurrent().GetValue(test_key); - - EXPECT_EQ(nostd::get(test_runtime.GetCurrent().GetValue(test_key)), "123"); - EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(test_key)), nostd::get(test_context.GetValue(test_key)) ); - } - - /* Tests whether the runtimeContext object properly attaches and detaches - * the context object. - */ - TEST(RuntimeContextTest, AttachDetachContext) - { - - using M = std::map< context::Key* , nostd::string_view>; - - context::Key test_key = context::Key("test_key"); - - context::Key foo_key = context::Key("foo_key"); - - M m1 = {{&test_key, "123"}}; - - M m2 = {{&foo_key, "534"}}; - - context::KeyValueIterableModifiable iterable_1{m1}; - - context::Context test_context = context::Context(iterable_1); - - context::KeyValueIterableModifiable iterable_2{m2}; - - context::Context foo_context = context::Context(iterable_2); - - context::RuntimeContext test_runtime;// = context::RuntimeContext(test_context); - - test_runtime.Attach(test_context); - - EXPECT_EQ(nostd::get(test_runtime.GetCurrent().GetValue(test_key)), "123"); - EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(test_key)), nostd::get(test_context.GetValue(test_key)) ); - - context::RuntimeContext::Token old_context = test_runtime.Attach(foo_context); - - EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(foo_key)), nostd::get(foo_context.GetValue(foo_key)) ); - - test_runtime.Detach(old_context); - - EXPECT_EQ( nostd::get(test_runtime.GetCurrent().GetValue(test_key)), nostd::get(test_context.GetValue(test_key)) ); - } diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc index e61dcff5f2..b9de07ddd7 100644 --- a/api/test/context/thread_local_test.cc +++ b/api/test/context/thread_local_test.cc @@ -5,7 +5,7 @@ using namespace opentelemetry; -TEST(Thread_Local_Context_Test, thread_local_context){ +TEST(ThreadLocalContextTest, ThreadLocalContext){ using M = std::map< std::string, common::AttributeValue>; From 605cb75f25fa8415af080b82d91a09a546ff1ae7 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 22 Jun 2020 14:47:25 +0000 Subject: [PATCH 09/10] added tests and comparison operator to the context api --- api/include/opentelemetry/context/context.h | 98 +++++++++++++------ .../opentelemetry/context/runtime_context.h | 10 +- .../context/threadlocal_context.h | 22 +++-- api/test/context/BUILD | 11 --- api/test/context/context_test.cc | 72 +++++++++++++- api/test/context/thread_local_test.cc | 5 +- 6 files changed, 161 insertions(+), 57 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 1a7ba42678..2e5a07b24b 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -19,8 +19,6 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - - /* The context class provides a context identifier */ template class Context @@ -37,34 +35,30 @@ namespace context public: /*Returns the key's identifier*/ - nostd::string_view GetIdentifier() {return nostd::string_view(str_data);} + nostd::string_view GetIdentifier() {return nostd::string_view(identifier_);} /*Returns the key's name */ - nostd::string_view GetName() { return key_name_; } + nostd::string_view GetName() { return key_name_;} private: - friend class Context; - + friend class Context; + /* Constructs a new Key with the passed in name. Sets the identifier as * the address of this object. */ Key(nostd::string_view key_name) : key_name_{key_name} - { + { std::stringstream ss; ss << (void *)this; - nostd::string_view test_view = "test"; - test_view = ss.str(); - - memcpy(str_data, test_view.data(), test_view.size()); - } + nostd::string_view temp_view; + temp_view = ss.str(); + memcpy(identifier_, temp_view.data(), temp_view.size()); + } nostd::string_view key_name_; - - char str_data [50]; - - const nostd::string_view identifier_; + char identifier_ [50]; }; - + /* Creates a key with the passed in name and returns it. */ Key CreateKey(nostd::string_view key_name){ return Key(key_name); @@ -73,8 +67,8 @@ namespace context /* Contructor, creates a context object from a map of keys * and identifiers. */ - Context(const T &attributes): key_vals(attributes) { - /*Currently only used as a check, to ensure T is of the right type */ + Context(const T &attributes): key_vals_(attributes) { + /* Currently only used as a check, to ensure T is of the right type. */ trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); } @@ -86,26 +80,23 @@ namespace context */ Context WriteValues(T &attributes) noexcept { + /* Currently only used as a check, to ensure T is of the right type. */ trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); - + std::insert_iterator back (attributes, std::begin(attributes)); - - auto iter = std::begin(key_vals); - auto last = std::end(key_vals); - for (; iter != last; ++iter) + + for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) { back = *iter; } - + return Context(attributes); } /* Returns the value associated with the passed in key */ common::AttributeValue GetValue(Key key) { - auto iter = std::begin(key_vals); - auto last = std::end(key_vals); - - for (; iter != last; ++iter) + + for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) { if(key.GetIdentifier() == iter->first){ return iter->second; @@ -114,11 +105,56 @@ namespace context return ""; } + + /* Iterates through the current and comparing context objects + * to check for equality, */ + bool operator== (const Context &other){ + + /*Check for case where either both or one object has no contents. */ + if(std::begin(other.key_vals_) == std::end(other.key_vals_)){ + if(std::begin(key_vals_) == std::end(key_vals_)){ + return true; + } + else{ + return false; + } + } + + if(std::begin(key_vals_) == std::end(key_vals_)){ + return false; + } + + + /*Compare the contexts*/ + for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) + { + int found = 0; + + for(auto iter_other = std::begin(other.key_vals_); iter_other != std::end(other.key_vals_); iter_other++){ + if(iter->first == iter_other->first){ + if(nostd::get(iter->second) == nostd::get(iter_other->second)){ + found = 1; + break; + } + } + } + + if(found == 0){ + return false; + } + } + + return true; + } + + /* Copy Constructors. */ + Context(const Context& other) = default; + Context& operator=(const Context& other) = default; private: - T key_vals; + T key_vals_; }; - + } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index 75de839b6f..d7fe57100a 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -22,15 +22,15 @@ namespace context private: friend class RuntimeContext; - Context ctx_; + Context context_; /* A constructor that sets the token's Context object to the * one that was passed in. */ - Token(Context ctx) { ctx_ = ctx; } + Token(Context &context) { context_ = context; } /* Returns the stored context object. */ - Context GetCtx() { return ctx_; } + Context GetContext() { return context_; } }; /* Return the current context. */ @@ -40,12 +40,12 @@ namespace context * that can be used to reset to the previous Context. */ - Token Attach(Context context); + Token Attach(Context &context); /* Resets the context to a previous value stored in the * passed in token. Returns zero if successful, -1 otherwise */ - int Detach(Token token); + int Detach(Token &token); }; diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 73b5c770b0..3fd19f4094 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -21,15 +21,15 @@ namespace context private: friend class ThreadLocalContext; - Context ctx_ - ; + Context context_; + /* A constructor that sets the token's Context object to the * one that was passed in. */ - Token(Context ctx) { ctx_ = ctx; } + Token(Context &context) { context_ = context; } - /* Returns the stored context object */ - Context GetCtx() { return ctx_; } + /* Returns the stored context object. */ + Context GetContext() { return context_; } }; /* Return the current context. */ @@ -40,15 +40,21 @@ namespace context /* Resets the context to a previous value stored in the * passed in token. Returns zero if successful, -1 otherwise */ - static int Detach(Token token){ - GetInstance() = token.GetCtx(); + static int Detach(Token &token){ + + /* If the token context is the current context, return failure. */ + if(token.GetContext() == GetCurrent()){ + return 1; + } + + GetInstance() = token.GetContext(); return 0; } /* Sets the current 'Context' object. Returns a token * that can be used to reset to the previous Context. */ - static Token Attach(Context context){ + static Token Attach(Context &context){ Token old_context = Token(GetInstance()); GetInstance() = context; return old_context; diff --git a/api/test/context/BUILD b/api/test/context/BUILD index e287b26b27..c9d11c55e7 100644 --- a/api/test/context/BUILD +++ b/api/test/context/BUILD @@ -11,17 +11,6 @@ cc_test( ], ) -cc_test( - name = "runtime_context_test", - srcs = [ - "runtime_context_test.cc", - ], - deps = [ - "//api", - "@com_google_googletest//:gtest_main", - ], -) - cc_test( name = "thread_local_test", srcs = [ diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 72b1020201..a81db299fd 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -25,7 +25,7 @@ TEST(ContextTest, ContextImmutability) } /* Tests whether the new Context Objects inherits the keys and values - * of the original context object + * of the original context object. */ TEST(ContextTest, ContextInheritance){ @@ -45,3 +45,73 @@ TEST(ContextTest, ContextInheritance){ EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "123"); EXPECT_EQ(nostd::get(other_context.GetValue(foo_key)), "456"); } + +/* Tests that when you add a key value pair where the key is already in + * existance, they key is overwritten. + */ +TEST(ContextTest, ContextKeyOverwrite){ + + using M = std::map< std::string, common::AttributeValue>; + context::Context test_context = context::Context(); + + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); + + M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + M m2 = {{std::string(test_key.GetIdentifier()) , "000"}}; + + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); + + EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "000"); + EXPECT_NE(nostd::get(other_context.GetValue(test_key)), "123"); +} + +/* Tests that copying a context copies the key value pairs properly. */ +TEST(ContextTest, ContextCopy){ + + using M = std::map< std::string, common::AttributeValue>; + context::Context test_context = context::Context(); + + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); + + M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + M m2 = {{std::string(other_key.GetIdentifier()) , "000"}}; + + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); + context::Context copied_context = other_context; + + EXPECT_EQ(nostd::get(copied_context.GetValue(test_key)), "123"); + EXPECT_EQ(nostd::get(copied_context.GetValue(foo_key)), "456"); +} + +/* Tests that the comparison compares properly. */ +TEST(ContextTest, ContextCompare){ + + using M = std::map< std::string, common::AttributeValue>; + context::Context test_context = context::Context(); + + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); + + M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + M m2 = {{std::string(other_key.GetIdentifier()) , "000"}}; + + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); + context::Context copied_context = other_context; + + if(copied_context == other_context){ + + } + + if(copied_context == foo_context){ + ADD_FAILURE(); + } + +} diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc index b9de07ddd7..a9bab6c25a 100644 --- a/api/test/context/thread_local_test.cc +++ b/api/test/context/thread_local_test.cc @@ -5,7 +5,8 @@ using namespace opentelemetry; -TEST(ThreadLocalContextTest, ThreadLocalContext){ +/* Tests whether the ThreadLocalContext attaches and detaches as expected */ +TEST(ThreadLocalContextTest, ThreadLocalContextAttachDetach){ using M = std::map< std::string, common::AttributeValue>; @@ -22,6 +23,8 @@ TEST(ThreadLocalContextTest, ThreadLocalContext){ context::ThreadLocalContext::Token test_token = test_thread_context.Attach(foo_context); EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + EXPECT_EQ(test_thread_context.Detach(test_token), 0); + EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); } From e4d306912415c76c62fa049184380ee439f185c8 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 22 Jun 2020 14:49:10 +0000 Subject: [PATCH 10/10] auto formatted --- api/include/opentelemetry/context/context.h | 250 +++++++++--------- .../opentelemetry/context/runtime_context.h | 85 +++--- .../context/threadlocal_context.h | 111 ++++---- api/test/context/BUILD | 1 - api/test/context/context_test.cc | 102 +++---- api/test/context/thread_local_test.cc | 26 +- 6 files changed, 295 insertions(+), 280 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 2e5a07b24b..30c650cdea 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,160 +1,168 @@ #pragma once +#include +#include #include +#include #include -#include -#include -#include -#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/common/attribute_value.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/utility.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/common/attribute_value.h" -#include "opentelemetry/version.h" -#include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/trace/key_value_iterable.h" +#include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/trace/tracer.h" +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - /* The context class provides a context identifier */ - template - class Context - { - - public: - - /*The Key class is used to obscure access from the - * user to the context map. The identifier is used as a key - * to the context map. - */ - class Key - { +/* The context class provides a context identifier */ +template +class Context +{ - public: - /*Returns the key's identifier*/ - nostd::string_view GetIdentifier() {return nostd::string_view(identifier_);} - /*Returns the key's name */ - nostd::string_view GetName() { return key_name_;} +public: + /*The Key class is used to obscure access from the + * user to the context map. The identifier is used as a key + * to the context map. + */ + class Key + { + + public: + /*Returns the key's identifier*/ + nostd::string_view GetIdentifier() { return nostd::string_view(identifier_); } + /*Returns the key's name */ + nostd::string_view GetName() { return key_name_; } + + private: + friend class Context; + + /* Constructs a new Key with the passed in name. Sets the identifier as + * the address of this object. */ + Key(nostd::string_view key_name) : key_name_{key_name} + { + std::stringstream ss; + ss << (void *)this; + nostd::string_view temp_view; + temp_view = ss.str(); - private: - friend class Context; + memcpy(identifier_, temp_view.data(), temp_view.size()); + } - /* Constructs a new Key with the passed in name. Sets the identifier as - * the address of this object. */ - Key(nostd::string_view key_name) : key_name_{key_name} - { - std::stringstream ss; - ss << (void *)this; - nostd::string_view temp_view; - temp_view = ss.str(); + nostd::string_view key_name_; - memcpy(identifier_, temp_view.data(), temp_view.size()); - } + char identifier_[50]; + }; - nostd::string_view key_name_; + /* Creates a key with the passed in name and returns it. */ + Key CreateKey(nostd::string_view key_name) { return Key(key_name); } - char identifier_ [50]; - }; + /* Contructor, creates a context object from a map of keys + * and identifiers. + */ + Context(const T &attributes) : key_vals_(attributes) + { + /* Currently only used as a check, to ensure T is of the right type. */ + trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); + } - /* Creates a key with the passed in name and returns it. */ - Key CreateKey(nostd::string_view key_name){ - return Key(key_name); - } + Context() {} - /* Contructor, creates a context object from a map of keys - * and identifiers. - */ - Context(const T &attributes): key_vals_(attributes) { - /* Currently only used as a check, to ensure T is of the right type. */ - trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); - } + /* Accepts a new iterable and then returns a new context that + * contains both the original pairs and the new pair. + */ + Context WriteValues(T &attributes) noexcept + { + /* Currently only used as a check, to ensure T is of the right type. */ + trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); - Context() { - } + std::insert_iterator back(attributes, std::begin(attributes)); - /* Accepts a new iterable and then returns a new context that - * contains both the original pairs and the new pair. - */ - Context WriteValues(T &attributes) noexcept - { - /* Currently only used as a check, to ensure T is of the right type. */ - trace::KeyValueIterableView iterable = trace::KeyValueIterableView(attributes); + for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) + { + back = *iter; + } - std::insert_iterator back (attributes, std::begin(attributes)); + return Context(attributes); + } - for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) - { - back = *iter; - } + /* Returns the value associated with the passed in key */ + common::AttributeValue GetValue(Key key) + { - return Context(attributes); - } + for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) + { + if (key.GetIdentifier() == iter->first) + { + return iter->second; + } + } + + return ""; + } + + /* Iterates through the current and comparing context objects + * to check for equality, */ + bool operator==(const Context &other) + { + + /*Check for case where either both or one object has no contents. */ + if (std::begin(other.key_vals_) == std::end(other.key_vals_)) + { + if (std::begin(key_vals_) == std::end(key_vals_)) + { + return true; + } + else + { + return false; + } + } + + if (std::begin(key_vals_) == std::end(key_vals_)) + { + return false; + } - /* Returns the value associated with the passed in key */ - common::AttributeValue GetValue(Key key) { + /*Compare the contexts*/ + for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) + { + int found = 0; - for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) + for (auto iter_other = std::begin(other.key_vals_); iter_other != std::end(other.key_vals_); + iter_other++) + { + if (iter->first == iter_other->first) + { + if (nostd::get(iter->second) == + nostd::get(iter_other->second)) { - if(key.GetIdentifier() == iter->first){ - return iter->second; - } + found = 1; + break; } - - return ""; } - - /* Iterates through the current and comparing context objects - * to check for equality, */ - bool operator== (const Context &other){ - - /*Check for case where either both or one object has no contents. */ - if(std::begin(other.key_vals_) == std::end(other.key_vals_)){ - if(std::begin(key_vals_) == std::end(key_vals_)){ - return true; - } - else{ - return false; - } - } + } - if(std::begin(key_vals_) == std::end(key_vals_)){ - return false; - } - - - /*Compare the contexts*/ - for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) - { - int found = 0; - - for(auto iter_other = std::begin(other.key_vals_); iter_other != std::end(other.key_vals_); iter_other++){ - if(iter->first == iter_other->first){ - if(nostd::get(iter->second) == nostd::get(iter_other->second)){ - found = 1; - break; - } - } - } - - if(found == 0){ - return false; - } - } - - return true; - } + if (found == 0) + { + return false; + } + } - /* Copy Constructors. */ - Context(const Context& other) = default; - Context& operator=(const Context& other) = default; + return true; + } - private: - T key_vals_; + /* Copy Constructors. */ + Context(const Context &other) = default; + Context &operator=(const Context &other) = default; - }; +private: + T key_vals_; +}; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index d7fe57100a..b70f4ead1e 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -6,48 +6,45 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - /* The RuntimeContext class provides a wrapper for - * propogating context through cpp. */ - template - class RuntimeContext - { - public: - - /* The token class provides an identifier that is used by - * the attach and detach methods to keep track of context - * objects. - */ - class Token - { - private: - friend class RuntimeContext; - - Context context_; - - /* A constructor that sets the token's Context object to the - * one that was passed in. - */ - Token(Context &context) { context_ = context; } - - /* Returns the stored context object. */ - Context GetContext() { return context_; } - }; - - /* Return the current context. */ - Context GetCurrent(); - - /* Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - */ - - Token Attach(Context &context); - - /* Resets the context to a previous value stored in the - * passed in token. Returns zero if successful, -1 otherwise - */ - int Detach(Token &token); - }; - - -} +/* The RuntimeContext class provides a wrapper for + * propogating context through cpp. */ +template +class RuntimeContext +{ +public: + /* The token class provides an identifier that is used by + * the attach and detach methods to keep track of context + * objects. + */ + class Token + { + private: + friend class RuntimeContext; + + Context context_; + + /* A constructor that sets the token's Context object to the + * one that was passed in. + */ + Token(Context &context) { context_ = context; } + + /* Returns the stored context object. */ + Context GetContext() { return context_; } + }; + + /* Return the current context. */ + Context GetCurrent(); + + /* Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + */ + + Token Attach(Context &context); + + /* Resets the context to a previous value stored in the + * passed in token. Returns zero if successful, -1 otherwise + */ + int Detach(Token &token); +}; +} // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 3fd19f4094..7ecf2b5709 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -5,70 +5,69 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - /* The ThreadLocalContext class is a derived class from RuntimeContext and provides a wrapper for - * propogating context through cpp thread locally. */ - template - class ThreadLocalContext : public RuntimeContext +/* The ThreadLocalContext class is a derived class from RuntimeContext and + * provides a wrapper for + * propogating context through cpp thread locally. */ +template +class ThreadLocalContext : public RuntimeContext +{ +public: + /* The token class provides an identifier that is used by + * the attach and detach methods to keep track of context + * objects. + */ + class Token { - public: - - /* The token class provides an identifier that is used by - * the attach and detach methods to keep track of context - * objects. - */ - class Token - { - private: - friend class ThreadLocalContext; + private: + friend class ThreadLocalContext; - Context context_; + Context context_; - /* A constructor that sets the token's Context object to the - * one that was passed in. - */ - Token(Context &context) { context_ = context; } + /* A constructor that sets the token's Context object to the + * one that was passed in. + */ + Token(Context &context) { context_ = context; } - /* Returns the stored context object. */ - Context GetContext() { return context_; } - }; + /* Returns the stored context object. */ + Context GetContext() { return context_; } + }; - /* Return the current context. */ - static Context GetCurrent(){ - return GetInstance(); - } + /* Return the current context. */ + static Context GetCurrent() { return GetInstance(); } - /* Resets the context to a previous value stored in the - * passed in token. Returns zero if successful, -1 otherwise - */ - static int Detach(Token &token){ - - /* If the token context is the current context, return failure. */ - if(token.GetContext() == GetCurrent()){ - return 1; - } - - GetInstance() = token.GetContext(); - return 0; - } + /* Resets the context to a previous value stored in the + * passed in token. Returns zero if successful, -1 otherwise + */ + static int Detach(Token &token) + { - /* Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - */ - static Token Attach(Context &context){ - Token old_context = Token(GetInstance()); - GetInstance() = context; - return old_context; - } + /* If the token context is the current context, return failure. */ + if (token.GetContext() == GetCurrent()) + { + return 1; + } - private: - - /* Provides storage and access to the thread_local context object */ - static Context &GetInstance(){ - static thread_local Context instance; - return instance; - } + GetInstance() = token.GetContext(); + return 0; + } - }; + /* Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + */ + static Token Attach(Context &context) + { + Token old_context = Token(GetInstance()); + GetInstance() = context; + return old_context; + } -} +private: + /* Provides storage and access to the thread_local context object */ + static Context &GetInstance() + { + static thread_local Context instance; + return instance; + } +}; +} // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/BUILD b/api/test/context/BUILD index c9d11c55e7..68363db1c9 100644 --- a/api/test/context/BUILD +++ b/api/test/context/BUILD @@ -21,4 +21,3 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) - diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index a81db299fd..50c8635553 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -4,19 +4,19 @@ #include - using namespace opentelemetry; /* Test ensurs that the context object doe not change when you write to it */ TEST(ContextTest, ContextImmutability) { - using M = std::map< std::string, common::AttributeValue>; + using M = std::map; context::Context test_context = context::Context(); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("test_key"); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("test_key"); - M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; context::Context foo_context = test_context.WriteValues(m1); @@ -25,21 +25,23 @@ TEST(ContextTest, ContextImmutability) } /* Tests whether the new Context Objects inherits the keys and values - * of the original context object. + * of the original context object. */ -TEST(ContextTest, ContextInheritance){ +TEST(ContextTest, ContextInheritance) +{ - using M = std::map< std::string, common::AttributeValue>; + using M = std::map; context::Context test_context = context::Context(); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("foo_key"); - context::Context::Key other_key = test_context.CreateKey("other_key"); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); - M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; - M m2 = {{std::string(other_key.GetIdentifier()) , "000"}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; + M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; - context::Context foo_context = test_context.WriteValues(m1); + context::Context foo_context = test_context.WriteValues(m1); context::Context other_context = foo_context.WriteValues(m2); EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "123"); @@ -49,19 +51,21 @@ TEST(ContextTest, ContextInheritance){ /* Tests that when you add a key value pair where the key is already in * existance, they key is overwritten. */ -TEST(ContextTest, ContextKeyOverwrite){ +TEST(ContextTest, ContextKeyOverwrite) +{ - using M = std::map< std::string, common::AttributeValue>; + using M = std::map; context::Context test_context = context::Context(); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("foo_key"); - context::Context::Key other_key = test_context.CreateKey("other_key"); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); - M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; - M m2 = {{std::string(test_key.GetIdentifier()) , "000"}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; + M m2 = {{std::string(test_key.GetIdentifier()), "000"}}; - context::Context foo_context = test_context.WriteValues(m1); + context::Context foo_context = test_context.WriteValues(m1); context::Context other_context = foo_context.WriteValues(m2); EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "000"); @@ -69,49 +73,53 @@ TEST(ContextTest, ContextKeyOverwrite){ } /* Tests that copying a context copies the key value pairs properly. */ -TEST(ContextTest, ContextCopy){ +TEST(ContextTest, ContextCopy) +{ - using M = std::map< std::string, common::AttributeValue>; + using M = std::map; context::Context test_context = context::Context(); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("foo_key"); - context::Context::Key other_key = test_context.CreateKey("other_key"); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); - M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; - M m2 = {{std::string(other_key.GetIdentifier()) , "000"}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; + M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; - context::Context foo_context = test_context.WriteValues(m1); - context::Context other_context = foo_context.WriteValues(m2); + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); context::Context copied_context = other_context; - + EXPECT_EQ(nostd::get(copied_context.GetValue(test_key)), "123"); EXPECT_EQ(nostd::get(copied_context.GetValue(foo_key)), "456"); } /* Tests that the comparison compares properly. */ -TEST(ContextTest, ContextCompare){ +TEST(ContextTest, ContextCompare) +{ - using M = std::map< std::string, common::AttributeValue>; + using M = std::map; context::Context test_context = context::Context(); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("foo_key"); - context::Context::Key other_key = test_context.CreateKey("other_key"); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("foo_key"); + context::Context::Key other_key = test_context.CreateKey("other_key"); - M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; - M m2 = {{std::string(other_key.GetIdentifier()) , "000"}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; + M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; - context::Context foo_context = test_context.WriteValues(m1); - context::Context other_context = foo_context.WriteValues(m2); + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); context::Context copied_context = other_context; - - if(copied_context == other_context){ - + + if (copied_context == other_context) + { } - - if(copied_context == foo_context){ + + if (copied_context == foo_context) + { ADD_FAILURE(); } - } diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc index a9bab6c25a..8f20b75f2a 100644 --- a/api/test/context/thread_local_test.cc +++ b/api/test/context/thread_local_test.cc @@ -6,25 +6,29 @@ using namespace opentelemetry; /* Tests whether the ThreadLocalContext attaches and detaches as expected */ -TEST(ThreadLocalContextTest, ThreadLocalContextAttachDetach){ +TEST(ThreadLocalContextTest, ThreadLocalContextAttachDetach) +{ - using M = std::map< std::string, common::AttributeValue>; + using M = std::map; - context::Context test_context = context::Context(); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("test_key"); + context::Context test_context = context::Context(); + context::Context::Key test_key = test_context.CreateKey("test_key"); + context::Context::Key foo_key = test_context.CreateKey("test_key"); - M m1 = {{std::string(test_key.GetIdentifier()) , "123"}, {std::string(foo_key.GetIdentifier()) , "456"}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; context::Context foo_context = test_context.WriteValues(m1); - context::ThreadLocalContext test_thread_context; + context::ThreadLocalContext test_thread_context; context::ThreadLocalContext::Token test_token = test_thread_context.Attach(foo_context); - EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); - + EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), + "123"); + EXPECT_EQ(test_thread_context.Detach(test_token), 0); - - EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); + + EXPECT_NE(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), + "123"); }