From 78672966fcd345169aa888b180ef8fab2b11e4ed Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 14:25:00 +0000 Subject: [PATCH 01/31] demo context header --- api/include/opentelemetry/context/context.h | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 api/include/opentelemetry/context/context.h diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h new file mode 100644 index 0000000000..618ab6eca7 --- /dev/null +++ b/api/include/opentelemetry/context/context.h @@ -0,0 +1,49 @@ + + +class Context{ + public: + std::map _ctx; + Context(std::map ctx); + Context operator[] (std::string i); +} + +Context::Context(std::map ctx){ + _ctx=ctx; +} + +Context operator[](std::string){ + throw "Value Error"; +} + + + +/* The RuntimeContext class provides a wrapper for + * propogating context through cpp*/ +class RuntimeContext { + public: + + /* attach: Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + * + * Args: + * context : the conext to set. + */ + virtual int attach(RuntimeContext context); + + + /* get_current: Return the current context. + * + * Args: None + */ + virtual RuntimeContext get_current(); + + + /* detach: Resets the context to a previous value. + * + * Args: + * token: A reference to a previous context + */ + virtual void detach(int); + + +} From d5a360af78673a7fec7a1221c06bb9c3c1f2edb0 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 14:29:54 +0000 Subject: [PATCH 02/31] demo context header --- api/include/opentelemetry/context/context.h | 24 +++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 618ab6eca7..88c5d7c4ef 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,19 +1,25 @@ - +/*The context class provides a context identifier*/ class Context{ public: + + /*The identifier itself*/ std::map _ctx; + + /* Context: contructor + * + * Args: + * ctx: a map containing the context identifier + */ Context(std::map ctx); + + /* Context operator[]: Prevents the modification of the identifier + * + * Args: none + */ Context operator[] (std::string i); } -Context::Context(std::map ctx){ - _ctx=ctx; -} - -Context operator[](std::string){ - throw "Value Error"; -} @@ -26,7 +32,7 @@ class RuntimeContext { * that can be used to reset to the previous Context. * * Args: - * context : the conext to set. + * context : the context to set. */ virtual int attach(RuntimeContext context); From 5d3b713ac912c2d4ed94ee279d7bbf4cdb733c25 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 14:55:03 +0000 Subject: [PATCH 03/31] added file threadlocal_context.h --- .../context/threadlocal_context.h | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 api/include/opentelemetry/context/threadlocal_context.h diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h new file mode 100644 index 0000000000..71ea8607f5 --- /dev/null +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -0,0 +1,47 @@ +#ifndef THREAD_LOCAL_CONTEXT_H_ +#define THREAD_LOCAL_CONTEXT_H_ + + + + +#include "context.h" + + + +/* An implementation of the runtime context that uses thread local storage*/ +class ThreadLocalRuntimeContext :public RuntimeContext(){ + + + + + class Token{ + Token(); + } + + /* ThreadLocalRuntimeContext: Default constructor to set the + * current context to the thread local context + * + * Args: None + */ + ThreadLocalRuntimeContext(); + + /* attach: Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + * + * Args: + * context : the context to set. + */ + attach(Context context) + + + /* detach: Resets the context to a previous value. + * + * Args: + * token: A reference to a previous context + */ + void detach(Token token); + +} + + +#endif // THREAD_LOCAL_CONTEXT_H_ From d047ff7f6031f8dece74b2be16932a13f1307bbc Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 15:12:32 +0000 Subject: [PATCH 04/31] added fixes --- api/include/opentelemetry/context/context.h | 4 ++-- .../opentelemetry/context/threadlocal_context.h | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 88c5d7c4ef..9a55fad141 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -34,14 +34,14 @@ class RuntimeContext { * Args: * context : the context to set. */ - virtual int attach(RuntimeContext context); + virtual int attach(Context context); /* get_current: Return the current context. * * Args: None */ - virtual RuntimeContext get_current(); + virtual Context get_current(); /* detach: Resets the context to a previous value. diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 71ea8607f5..069ca3f5b5 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -11,7 +11,7 @@ /* An implementation of the runtime context that uses thread local storage*/ class ThreadLocalRuntimeContext :public RuntimeContext(){ - + class Token{ @@ -31,7 +31,14 @@ class ThreadLocalRuntimeContext :public RuntimeContext(){ * Args: * context : the context to set. */ - attach(Context context) + Token attach(Context context) + + /* get_current: Return the current context. + * + * Args: None + */ + Context get_current(); + /* detach: Resets the context to a previous value. From f283cc6dde5c42408e5c3ec2fd60c5e89298c1cc Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 15:55:41 +0000 Subject: [PATCH 05/31] Fixed syntatical errors, moved token class definition to context.h and added the contextvars_context.h file --- api/include/opentelemetry/context/context.h | 28 +++++++---- .../context/contextvars_context.h | 50 +++++++++++++++++++ .../context/threadlocal_context.h | 28 ++++------- 3 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 api/include/opentelemetry/context/contextvars_context.h diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 9a55fad141..9a4264dbff 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,25 +1,32 @@ +#ifndef CONTEXT_H_ +#define CONTEXT_H_ + /*The context class provides a context identifier*/ class Context{ public: - + /*The identifier itself*/ std::map _ctx; - + /* Context: contructor * * Args: * ctx: a map containing the context identifier */ Context(std::map ctx); - + /* Context operator[]: Prevents the modification of the identifier * * Args: none */ Context operator[] (std::string i); -} +}; +/* The token class provides:????*/ +class Token{ + Token(); +}; @@ -27,7 +34,7 @@ class Context{ * propogating context through cpp*/ class RuntimeContext { public: - + /* attach: Sets the current 'Context' object. Returns a token * that can be used to reset to the previous Context. * @@ -35,7 +42,7 @@ class RuntimeContext { * context : the context to set. */ virtual int attach(Context context); - + /* get_current: Return the current context. * @@ -50,6 +57,9 @@ class RuntimeContext { * token: A reference to a previous context */ virtual void detach(int); - - -} + + +}; + + +#endif //CONTEXT_H_ diff --git a/api/include/opentelemetry/context/contextvars_context.h b/api/include/opentelemetry/context/contextvars_context.h new file mode 100644 index 0000000000..d3407fa057 --- /dev/null +++ b/api/include/opentelemetry/context/contextvars_context.h @@ -0,0 +1,50 @@ +#ifndef CONTEXT_VARS_CONTEXT_H_ +#define CONTEXT_VARS_CONTEXT_H_ + +#include "context.h" +#include + + +/* An implementation of the RuntimeContext interface which wraps + * ContextVar under the hood */ +class ContextVarsRuntimeContext: public RuntimeContext{ + + std::string _CONTEXT_KEY = "current_context"; + + + /* ContextVarsRuntimeContext: Default constructor to set the + * current context to the thread local context + * + * Args: None + */ + ContextVarsRuntimeContext(); + + + /* attach: Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + * + * Args: + * context : the context to set. + */ + Context attach(Context context); + + + /* get_current: Return the current context. + * + * Args: None + */ + Context get_current(); + + /* detach: Resets the context to a previous value. + * + * Args: + * token: A reference to a previous context + */ + Token detach(Token token); + +}; + + + +#endif //CONTEXT_VARS_CONTEXT_H_ + diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 069ca3f5b5..2b267e9da5 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -1,22 +1,16 @@ #ifndef THREAD_LOCAL_CONTEXT_H_ #define THREAD_LOCAL_CONTEXT_H_ - - - #include "context.h" - +#include /* An implementation of the runtime context that uses thread local storage*/ -class ThreadLocalRuntimeContext :public RuntimeContext(){ +class ThreadLocalRuntimeContext: public RuntimeContext(){ - - - - class Token{ - Token(); - } + std::string _CONTEXT_KEY = "current_context"; + + /* ThreadLocalRuntimeContext: Default constructor to set the * current context to the thread local context @@ -33,11 +27,11 @@ class ThreadLocalRuntimeContext :public RuntimeContext(){ */ Token attach(Context context) - /* get_current: Return the current context. - * - * Args: None - */ - Context get_current(); + /* get_current: Return the current context. + * + * Args: None + */ + Context get_current(); @@ -48,7 +42,7 @@ class ThreadLocalRuntimeContext :public RuntimeContext(){ */ void detach(Token token); -} +}; #endif // THREAD_LOCAL_CONTEXT_H_ From e1de13f8fda488eca083e5c1568a023f5431be1e Mon Sep 17 00:00:00 2001 From: Sam Atac <65615762+satac2@users.noreply.github.com> Date: Mon, 8 Jun 2020 11:39:24 -0500 Subject: [PATCH 06/31] Update api/include/opentelemetry/context/context.h Co-authored-by: Kirk Kelsey --- api/include/opentelemetry/context/context.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 9a4264dbff..118fd58e1c 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,5 +1,4 @@ -#ifndef CONTEXT_H_ -#define CONTEXT_H_ +#pragma once /*The context class provides a context identifier*/ From d5b155d6ee2ef1a7600c31e7aa1b3123515958d8 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 17:04:01 +0000 Subject: [PATCH 07/31] changed define guards and minor syntactical changes --- api/include/opentelemetry/context/context.h | 10 ++++------ .../opentelemetry/context/contextvars_context.h | 5 +---- .../opentelemetry/context/threadlocal_context.h | 5 +---- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 9a4264dbff..7a572e68dc 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,6 +1,4 @@ -#ifndef CONTEXT_H_ -#define CONTEXT_H_ - +#pragma once /*The context class provides a context identifier*/ class Context{ @@ -20,7 +18,7 @@ class Context{ * * Args: none */ - Context operator[] (std::string i); + Context operator[] (std::string str); }; /* The token class provides:????*/ @@ -56,10 +54,10 @@ class RuntimeContext { * Args: * token: A reference to a previous context */ - virtual void detach(int); + virtual void detach(Token token); }; -#endif //CONTEXT_H_ + diff --git a/api/include/opentelemetry/context/contextvars_context.h b/api/include/opentelemetry/context/contextvars_context.h index d3407fa057..8ee7c7d63d 100644 --- a/api/include/opentelemetry/context/contextvars_context.h +++ b/api/include/opentelemetry/context/contextvars_context.h @@ -1,5 +1,4 @@ -#ifndef CONTEXT_VARS_CONTEXT_H_ -#define CONTEXT_VARS_CONTEXT_H_ +#pragma once #include "context.h" #include @@ -46,5 +45,3 @@ class ContextVarsRuntimeContext: public RuntimeContext{ -#endif //CONTEXT_VARS_CONTEXT_H_ - diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 2b267e9da5..255f7e69b9 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -1,5 +1,4 @@ -#ifndef THREAD_LOCAL_CONTEXT_H_ -#define THREAD_LOCAL_CONTEXT_H_ +#pragma once #include "context.h" @@ -44,5 +43,3 @@ class ThreadLocalRuntimeContext: public RuntimeContext(){ }; - -#endif // THREAD_LOCAL_CONTEXT_H_ From 80b2332a9d3e40f7c227ec55b0b01e93e17c573e Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 18:32:45 +0000 Subject: [PATCH 08/31] removing context vars class --- .../context/contextvars_context.h | 47 ------------------- 1 file changed, 47 deletions(-) delete mode 100644 api/include/opentelemetry/context/contextvars_context.h diff --git a/api/include/opentelemetry/context/contextvars_context.h b/api/include/opentelemetry/context/contextvars_context.h deleted file mode 100644 index 8ee7c7d63d..0000000000 --- a/api/include/opentelemetry/context/contextvars_context.h +++ /dev/null @@ -1,47 +0,0 @@ -#pragma once - -#include "context.h" -#include - - -/* An implementation of the RuntimeContext interface which wraps - * ContextVar under the hood */ -class ContextVarsRuntimeContext: public RuntimeContext{ - - std::string _CONTEXT_KEY = "current_context"; - - - /* ContextVarsRuntimeContext: Default constructor to set the - * current context to the thread local context - * - * Args: None - */ - ContextVarsRuntimeContext(); - - - /* attach: Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - * - * Args: - * context : the context to set. - */ - Context attach(Context context); - - - /* get_current: Return the current context. - * - * Args: None - */ - Context get_current(); - - /* detach: Resets the context to a previous value. - * - * Args: - * token: A reference to a previous context - */ - Token detach(Token token); - -}; - - - From 4d24115644fa1744f44529bba9fdb9aed0cb5c63 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 18:45:56 +0000 Subject: [PATCH 09/31] removed threadlocal context and made the runtime context methods static --- .../context/threadlocal_context.h | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 api/include/opentelemetry/context/threadlocal_context.h diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h deleted file mode 100644 index 255f7e69b9..0000000000 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include "context.h" - -#include - -/* An implementation of the runtime context that uses thread local storage*/ -class ThreadLocalRuntimeContext: public RuntimeContext(){ - - std::string _CONTEXT_KEY = "current_context"; - - - - /* ThreadLocalRuntimeContext: Default constructor to set the - * current context to the thread local context - * - * Args: None - */ - ThreadLocalRuntimeContext(); - - /* attach: Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - * - * Args: - * context : the context to set. - */ - Token attach(Context context) - - /* get_current: Return the current context. - * - * Args: None - */ - Context get_current(); - - - - /* detach: Resets the context to a previous value. - * - * Args: - * token: A reference to a previous context - */ - void detach(Token token); - -}; - From a17c028c8e100dcc8d910bff7cd967b5cd883325 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 18:45:59 +0000 Subject: [PATCH 10/31] removed threadlocal context and made the runtime context methods static --- api/include/opentelemetry/context/context.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 7a572e68dc..a97f55db1d 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -39,14 +39,14 @@ class RuntimeContext { * Args: * context : the context to set. */ - virtual int attach(Context context); + static Token attach(Context context); /* get_current: Return the current context. * * Args: None */ - virtual Context get_current(); + static Context get_current(); /* detach: Resets the context to a previous value. @@ -54,7 +54,7 @@ class RuntimeContext { * Args: * token: A reference to a previous context */ - virtual void detach(Token token); + static void detach(Token token); }; From d2df2acb429113c1057683870f0788879154ac05 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 18:56:48 +0000 Subject: [PATCH 11/31] added a constructor --- api/include/opentelemetry/context/context.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index a97f55db1d..ff99ebf3ed 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -33,6 +33,14 @@ class Token{ class RuntimeContext { public: + + /* RuntimeContext: A constructor that will set the current + * context to the threading local. + * + * Args: None. + */ + RuntimeContext(); + /* attach: Sets the current 'Context' object. Returns a token * that can be used to reset to the previous Context. * From c316636efa22b5d7bff7feeb3635f3f33cd59acf Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 19:15:57 +0000 Subject: [PATCH 12/31] fixed convention issue and removed unnecessary code --- api/include/opentelemetry/context/context.h | 56 +++++++++------------ 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index ff99ebf3ed..f42cc5826f 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,30 +1,27 @@ #pragma once +#include +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace context +{ + + /*The context class provides a context identifier*/ class Context{ - public: - + private: /*The identifier itself*/ - std::map _ctx; + std::map ctx_; - /* Context: contructor - * - * Args: - * ctx: a map containing the context identifier + public: + /* Context: contructor, creates a context object from a map + * of keys and identifiers */ Context(std::map ctx); - /* Context operator[]: Prevents the modification of the identifier - * - * Args: none - */ - Context operator[] (std::string str); }; -/* The token class provides:????*/ -class Token{ - Token(); -}; @@ -34,38 +31,35 @@ 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{ + Token(); + }; + + /* RuntimeContext: A constructor that will set the current * context to the threading local. - * - * Args: None. */ RuntimeContext(); /* attach: Sets the current 'Context' object. Returns a token * that can be used to reset to the previous Context. - * - * Args: - * context : the context to set. */ - static Token attach(Context context); + static Token Attach(Context context); /* get_current: Return the current context. - * - * Args: None */ - static Context get_current(); + static Context getCurrent(); /* detach: Resets the context to a previous value. - * - * Args: - * token: A reference to a previous context */ - static void detach(Token token); + static void Detach(Token token); }; - - +} From 7b369451684040024b406d80190ffd99d36d746e Mon Sep 17 00:00:00 2001 From: Sam Atac <65615762+satac2@users.noreply.github.com> Date: Mon, 8 Jun 2020 14:43:05 -0500 Subject: [PATCH 13/31] Update context.h --- api/include/opentelemetry/context/context.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index f42cc5826f..c08e216ea8 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -52,7 +52,7 @@ class RuntimeContext { /* get_current: Return the current context. */ - static Context getCurrent(); + static Context GetCurrent(); /* detach: Resets the context to a previous value. From 8e02c07959673c925a11101847e4bce3e96e8504 Mon Sep 17 00:00:00 2001 From: Sam Atac <65615762+satac2@users.noreply.github.com> Date: Mon, 8 Jun 2020 14:47:04 -0500 Subject: [PATCH 14/31] Update context.h --- api/include/opentelemetry/context/context.h | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index c08e216ea8..78e0134f31 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -22,7 +22,12 @@ class Context{ }; - +/* The token class provides an identifier that is used by + * the RuntimeContext attach and detach methods to keep track of context + * objects.*/ +class Token{ + Token(); +}; /* The RuntimeContext class provides a wrapper for @@ -30,15 +35,6 @@ class Context{ 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{ - Token(); - }; - - /* RuntimeContext: A constructor that will set the current * context to the threading local. */ From 115cd68a4e48f4073d193c2492f18ab602128ea9 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 16:28:43 +0000 Subject: [PATCH 15/31] context implementation --- api/include/opentelemetry/context/context.h | 229 +++++++++++++++++--- 1 file changed, 193 insertions(+), 36 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index f42cc5826f..73fcc09691 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,65 +1,222 @@ #pragma once + #include #include +#include + +#include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { + std::mutex context_id_mutex; + + /*The context class provides a context identifier*/ + class Context{ + + private: + + /*The identifier itself*/ + std::map ctx_; + + /*Used to track that last ContextKey identifier and create the next one */ + static int last_key_identifier_; + + /* Context: A constructor that accepts a key/value map*/ + Context(std::map ctx){ + ctx_ = ctx; + } + + 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_; + } + + /* ContextKey: 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: + + /* ContextKey: 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(); + } + + }; + + + /* Context: contructor, creates a context object with no key/value pairs + */ + Context(){ + ctx_ = std::map {}; + + } + + /* Context: contructor, creates a context object from a map + * of keys and identifiers + */ + Context(ContextKey key, int value){ + ctx_[key.GetIdentifier()] = value; + } + + + /* WriteValue: 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_; + + temp_map[key.GetIdentifier()] = value; + + return Context(temp_map); + } + + + /* GetValue: Returns the value associated with the passed in key + */ + int GetValue(ContextKey key){ + return ctx_[key.GetIdentifier()]; + } + + /* CreateKey: 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); + } + + + }; + + + + /* The token class provides an identifier that is used by + * the attach and detach methods to keep track of context + * objects.*/ + + class Token{ + private: + + Context ctx_; + + public: + + /* Token: A constructor that sets the token's Context object to the + * one that was passed in. + */ + Token(Context ctx){ + ctx_ = ctx; + } + + /* GetContext: Returns the stored context object */ + Context GetContext(){ + return ctx_; + } + + }; -/*The context class provides a context identifier*/ -class Context{ - private: - /*The identifier itself*/ - std::map ctx_; - public: - /* Context: contructor, creates a context object from a map - * of keys and identifiers - */ - Context(std::map ctx); + /* The RuntimeContext class provides a wrapper for + * propogating context through cpp*/ + class RuntimeContext { + private: + + static thread_local Context context_; -}; + public: + /* RuntimeContext: A default constructor that will set the context to + * an empty context object. + */ + RuntimeContext(){ + context_ = Context(); + } + /* RuntimeContext: A constructor that will set the context as + * the passed in context. + */ + RuntimeContext(Context context){ -/* The RuntimeContext class provides a wrapper for - * propogating context through cpp*/ -class RuntimeContext { - public: + context_ = context; + } + /* attach: Sets the current 'Context' object. Returns a token + * that can be used to reset to the previous Context. + */ + Token Attach(Context context){ - /* The token class provides an identifier that is used by - * the attach and detach methods to keep track of context - * objects.*/ - class Token{ - Token(); - }; + Token old_context_token = Token(context_); + context_ = context; - /* RuntimeContext: A constructor that will set the current - * context to the threading local. - */ - RuntimeContext(); + return old_context_token; - /* attach: Sets the current 'Context' object. Returns a token - * that can be used to reset to the previous Context. - */ - static Token Attach(Context context); + } - /* get_current: Return the current context. - */ - static Context getCurrent(); + /* GetCurrent: Return the current context. + */ + static Context GetCurrent(){ + Context context = context_; + return context_; + } - /* detach: Resets the context to a previous value. - */ - static void Detach(Token token); + /* Detach: Resets the context to a previous value stored in the + * passed in token. + */ + void Detach(Token token){ + context_ = token.GetContext(); + } + }; + + thread_local Context RuntimeContext::context_ = Context(); + int Context::last_key_identifier_ = 0; -}; } +OPENTELEMETRY_END_NAMESPACE From 0d9fea1a0de090691795cb38a965ce26426d6ea0 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 16:30:26 +0000 Subject: [PATCH 16/31] Adding tests --- api/test/context/BUILD | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 api/test/context/BUILD diff --git a/api/test/context/BUILD b/api/test/context/BUILD new file mode 100644 index 0000000000..f69f02509c --- /dev/null +++ b/api/test/context/BUILD @@ -0,0 +1,25 @@ +load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") + +cc_test( + name = "context_test", + srcs = [ + "context_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "runtimeContext_test", + srcs = [ + "runtimeContext_test.cc", + ], + copts = ["-Wall","-std=c++17"], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + From 565c40efc86ac24b021ae4c63a9d4312b4473a94 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 16:30:45 +0000 Subject: [PATCH 17/31] Adding tests --- api/test/context/context_test.cc | 49 +++++++++++++++++++++++++ api/test/context/runtimeContext_test.cc | 34 +++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 api/test/context/context_test.cc create mode 100644 api/test/context/runtimeContext_test.cc diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc new file mode 100644 index 0000000000..253543dab5 --- /dev/null +++ b/api/test/context/context_test.cc @@ -0,0 +1,49 @@ +#include "opentelemetry/context/context.h" + +#include + +using namespace opentelemetry::context; + +/* Tests whether the original context objects changes + * when you write to it. + */ +TEST(Context_test, is_context_immutable) +{ + + 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); + + EXPECT_EQ(new_test_context.GetValue(test_key), 7); + + 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) +{ + + + Context::ContextKey test_key = Context::ContextKey("test_key"); + + Context test_context = Context(test_key, 7); + + Context::ContextKey foo_key = test_context.CreateKey("foo_key"); + + Context new_test_context = test_context.WriteValue(foo_key,1); + + EXPECT_EQ(new_test_context.GetValue(test_key), 7); + + EXPECT_EQ(new_test_context.GetValue(foo_key), 1); +} + + + + + diff --git a/api/test/context/runtimeContext_test.cc b/api/test/context/runtimeContext_test.cc new file mode 100644 index 0000000000..2021125299 --- /dev/null +++ b/api/test/context/runtimeContext_test.cc @@ -0,0 +1,34 @@ +#include "opentelemetry/context/context.h" + +#include + +using namespace opentelemetry::context; + +/* 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)); + + test_runtime.Detach(test_token); + + 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)); +} + From 2d4a4110d39b06ac69a79cbf9b2f99886b91ab6c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 17:27:05 +0000 Subject: [PATCH 18/31] renamed some variables, added tests --- api/include/opentelemetry/context/context.h | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 73fcc09691..222872274c 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -19,14 +19,14 @@ namespace context private: /*The identifier itself*/ - std::map ctx_; + std::map ctx_map_; /*Used to track that last ContextKey identifier and create the next one */ static int last_key_identifier_; /* Context: A constructor that accepts a key/value map*/ - Context(std::map ctx){ - ctx_ = ctx; + Context(std::map ctx_map){ + ctx_map_ = ctx_map; } public: @@ -81,7 +81,7 @@ namespace context /* Context: contructor, creates a context object with no key/value pairs */ Context(){ - ctx_ = std::map {}; + ctx_map_ = std::map {}; } @@ -89,7 +89,7 @@ namespace context * of keys and identifiers */ Context(ContextKey key, int value){ - ctx_[key.GetIdentifier()] = value; + ctx_map_[key.GetIdentifier()] = value; } @@ -97,7 +97,7 @@ namespace context * context that contains both the original pairs and the new pair. */ Context WriteValue(ContextKey key, int value){ - std::map temp_map = ctx_; + std::map temp_map = ctx_map_; temp_map[key.GetIdentifier()] = value; @@ -108,7 +108,7 @@ namespace context /* GetValue: Returns the value associated with the passed in key */ int GetValue(ContextKey key){ - return ctx_[key.GetIdentifier()]; + return ctx_map_[key.GetIdentifier()]; } /* CreateKey: Returns a ContextKey that has the passed in name and the @@ -146,7 +146,7 @@ namespace context /* Token: A constructor that sets the token's Context object to the * one that was passed in. */ - Token(Context ctx){ + Token(Context &ctx){ ctx_ = ctx; } @@ -178,7 +178,7 @@ namespace context /* RuntimeContext: A constructor that will set the context as * the passed in context. */ - RuntimeContext(Context context){ + RuntimeContext(Context &context){ context_ = context; } @@ -186,7 +186,7 @@ namespace context /* attach: Sets the current 'Context' object. Returns a token * that can be used to reset to the previous Context. */ - Token Attach(Context context){ + Token Attach(Context &context){ Token old_context_token = Token(context_); @@ -208,7 +208,7 @@ namespace context /* Detach: Resets the context to a previous value stored in the * passed in token. */ - void Detach(Token token){ + void Detach(Token &token){ context_ = token.GetContext(); } From 9874c4cab7648c8d1268262fff52812ac2d5cc8d Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 17:27:36 +0000 Subject: [PATCH 19/31] renamed some variables, added tests --- api/test/CMakeLists.txt | 1 + api/test/context/context_test.cc | 4 --- api/test/context/runtimeContext_test.cc | 37 +++++++++++++++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/api/test/CMakeLists.txt b/api/test/CMakeLists.txt index 21b3e9a350..5c5a8590e4 100644 --- a/api/test/CMakeLists.txt +++ b/api/test/CMakeLists.txt @@ -1,4 +1,5 @@ add_subdirectory(core) +add_subdirectory(context) add_subdirectory(plugin) add_subdirectory(nostd) add_subdirectory(trace) diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 253543dab5..a8903be443 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -43,7 +43,3 @@ TEST(Context_test, context_write_new_object) EXPECT_EQ(new_test_context.GetValue(foo_key), 1); } - - - - diff --git a/api/test/context/runtimeContext_test.cc b/api/test/context/runtimeContext_test.cc index 2021125299..682443cf48 100644 --- a/api/test/context/runtimeContext_test.cc +++ b/api/test/context/runtimeContext_test.cc @@ -4,6 +4,25 @@ 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. */ @@ -18,17 +37,23 @@ TEST(runtimeContext_test, attach_detach_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)); + 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)); + 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)); test_runtime.Detach(test_token); - 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)); + 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)); } From 061e56486185ebcdf4e5d94345f94b15a33cd141 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 17:48:20 +0000 Subject: [PATCH 20/31] reversed public/private order --- api/include/opentelemetry/context/context.h | 80 ++++++++++----------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 222872274c..f0241a20f2 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -16,19 +16,6 @@ namespace context /*The context class provides a context identifier*/ class Context{ - 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_; - - /* Context: A constructor that accepts a key/value map*/ - Context(std::map ctx_map){ - ctx_map_ = ctx_map; - } - public: /*The ContextKey class is used to obscure access from the @@ -49,8 +36,8 @@ namespace context return identifier_; } - /* ContextKey: constructs a new ContextKey with the - * passed in name and identifier. + /* Constructs a new ContextKey with the passed in name and + * identifier. */ ContextKey(std::string key_name, int identifier){ key_name_ = key_name; @@ -59,9 +46,8 @@ namespace context public: - /* ContextKey: Consructs a new ContextKey with the passed in name - * and increments the identifier then assigns it to be the key's - * identifier. + /* 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; @@ -78,14 +64,14 @@ namespace context }; - /* Context: contructor, creates a context object with no key/value pairs + /* Creates a context object with no key/value pairs */ Context(){ ctx_map_ = std::map {}; } - /* Context: contructor, creates a context object from a map + /* Contructor, creates a context object from a map * of keys and identifiers */ Context(ContextKey key, int value){ @@ -93,7 +79,7 @@ namespace context } - /* WriteValue: accepts a new key/value pair and then returns a new + /* 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){ @@ -105,13 +91,12 @@ namespace context } - /* GetValue: Returns the value associated with the passed in key - */ + /* Returns the value associated with the passed in key */ int GetValue(ContextKey key){ return ctx_map_[key.GetIdentifier()]; } - /* CreateKey: Returns a ContextKey that has the passed in name and the + /* Returns a ContextKey that has the passed in name and the * next available identifier.*/ ContextKey CreateKey(std::string key_name){ int id; @@ -127,6 +112,19 @@ namespace context 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; + } + }; @@ -137,53 +135,48 @@ namespace context * objects.*/ class Token{ - private: - - Context ctx_; public: - /* Token: A constructor that sets the token's Context object to the + /* A constructor that sets the token's Context object to the * one that was passed in. */ Token(Context &ctx){ ctx_ = ctx; } - /* GetContext: Returns the stored context object */ + /* Returns the stored context object */ Context GetContext(){ return ctx_; } + private: + + Context ctx_; }; /* The RuntimeContext class provides a wrapper for * propogating context through cpp*/ class RuntimeContext { - private: - - static thread_local Context context_; public: - /* RuntimeContext: A default constructor that will set the context to + /* A default constructor that will set the context to * an empty context object. */ RuntimeContext(){ context_ = Context(); } - /* RuntimeContext: A constructor that will set the context as - * the passed in context. - */ + /* A constructor that will set the context as the passed in context.*/ RuntimeContext(Context &context){ context_ = context; } - /* attach: Sets the current 'Context' object. Returns a token + /* Sets the current 'Context' object. Returns a token * that can be used to reset to the previous Context. */ Token Attach(Context &context){ @@ -197,7 +190,7 @@ namespace context } - /* GetCurrent: Return the current context. + /* Return the current context. */ static Context GetCurrent(){ Context context = context_; @@ -205,13 +198,18 @@ namespace context } - /* Detach: Resets the context to a previous value stored in the - * passed in token. + /* Resets the context to a previous value stored in the + * passed in token. */ - void Detach(Token &token){ + int Detach(Token &token){ context_ = token.GetContext(); } + + private: + + static thread_local Context context_; + }; thread_local Context RuntimeContext::context_ = Context(); From b2717b31ae3ed37a4a89d91574c2192abab3b434 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 10 Jun 2020 18:16:15 +0000 Subject: [PATCH 21/31] Added detach test --- api/include/opentelemetry/context/context.h | 43 ++++++++++++++------- api/test/context/runtimeContext_test.cc | 4 +- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 3d03ed1a09..ddb6227095 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,6 +1,5 @@ #pragma once - #include #include #include @@ -13,7 +12,7 @@ namespace context std::mutex context_id_mutex; - /*The context class provides a context identifier*/ + /*The context class provides a context identifier */ class Context{ public: @@ -31,7 +30,7 @@ namespace context int identifier_; - /* GetIdentifier: returns the identifier*/ + /* GetIdentifier: returns the identifier */ int GetIdentifier(){ return identifier_; } @@ -64,8 +63,7 @@ namespace context }; - /* Creates a context object with no key/value pairs - */ + /* Creates a context object with no key/value pairs */ Context(){ ctx_map_ = std::map {}; @@ -90,6 +88,15 @@ namespace context 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){ @@ -114,13 +121,13 @@ namespace context private: - /*The identifier itself*/ + /* 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*/ + /* A constructor that accepts a key/value map */ Context(std::map ctx_map){ ctx_map_ = ctx_map; } @@ -132,7 +139,8 @@ namespace context /* The token class provides an identifier that is used by * the attach and detach methods to keep track of context - * objects.*/ + * objects. + */ class Token{ @@ -157,7 +165,7 @@ namespace context /* The RuntimeContext class provides a wrapper for - * propogating context through cpp*/ + * propogating context through cpp. */ class RuntimeContext { public: @@ -170,7 +178,7 @@ namespace context context_ = Context(); } - /* A constructor that will set the context as the passed in context.*/ + /* A constructor that will set the context as the passed in context. */ RuntimeContext(Context &context){ context_ = context; } @@ -188,8 +196,7 @@ namespace context } - /* Return the current context. - */ + /* Return the current context. */ static Context GetCurrent(){ Context context = context_; return context_; @@ -197,10 +204,18 @@ namespace context /* Resets the context to a previous value stored in the - * passed in token. + * passed in token. Returns zero if successful, -1 otherwise */ int Detach(Token &token){ - context_ = token.GetContext(); + + if(token.GetContext() == context_){ + + return -1; + } + + context_ = token.GetContext(); + + return 0; } diff --git a/api/test/context/runtimeContext_test.cc b/api/test/context/runtimeContext_test.cc index 682443cf48..e283071c1d 100644 --- a/api/test/context/runtimeContext_test.cc +++ b/api/test/context/runtimeContext_test.cc @@ -49,8 +49,10 @@ TEST(runtimeContext_test, attach_detach_context) EXPECT_EQ(test_runtime.GetCurrent().GetValue(foo_key), foo_context.GetValue(foo_key)); - test_runtime.Detach(test_token); + 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), From b7259cdf0379e99e6f66842694338486e68b8171 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 11 Jun 2020 14:20:29 +0000 Subject: [PATCH 22/31] 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 23/31] 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 24/31] 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 25/31] 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 26/31] 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 27/31] 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 28/31] 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 29/31] 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 30/31] 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 31/31] 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"); }