From 78672966fcd345169aa888b180ef8fab2b11e4ed Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 8 Jun 2020 14:25:00 +0000 Subject: [PATCH 01/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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/93] 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"); } From 786f8a6e6ce555374a15876d6c45eca73cfb6c3b Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 16:53:14 +0000 Subject: [PATCH 32/93] style changes --- api/include/opentelemetry/context/context.h | 6 +--- api/test/context/context_test.cc | 33 +++++++++++++++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 30c650cdea..6bc1ebfa8b 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -20,7 +20,7 @@ namespace context { /* The context class provides a context identifier */ -template +template ::value> * = nullptr*/> class Context { @@ -66,8 +66,6 @@ class Context */ 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); } Context() {} @@ -77,8 +75,6 @@ class 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)); diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 50c8635553..2b7a8c0b53 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -1,11 +1,34 @@ #include #include "opentelemetry/context/context.h" +#include "opentelemetry/nostd/list.h" +#include "opentelemetry/nostd/pair.h" #include using namespace opentelemetry; +TEST(ContextTest, ContextPairList){ + + using M = nostd::list>; + + 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"); + + //EXPECT_EQ(nostd::get(test_context.GetValue(1)), 2); + //M m1 = {{1,2},{3,4}}; + M m1 = {{std::string(test_key.GetIdentifier()), "123"}, + {std::string(foo_key.GetIdentifier()), "456"}}; + + //context::Context foo_context = test_context.WriteValues(m1); + + //EXPECT_EQ(nostd::get(foo_context.GetValue(test_key)), "123"); + //EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "456"); +} + + /* Test ensurs that the context object doe not change when you write to it */ TEST(ContextTest, ContextImmutability) { @@ -16,7 +39,7 @@ TEST(ContextTest, ContextImmutability) context::Context::Key foo_key = test_context.CreateKey("test_key"); M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; + {std::string(foo_key.GetIdentifier()), "456"}}; context::Context foo_context = test_context.WriteValues(m1); @@ -38,7 +61,7 @@ TEST(ContextTest, ContextInheritance) context::Context::Key other_key = test_context.CreateKey("other_key"); M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; + {std::string(foo_key.GetIdentifier()), "456"}}; M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; context::Context foo_context = test_context.WriteValues(m1); @@ -62,7 +85,7 @@ TEST(ContextTest, ContextKeyOverwrite) context::Context::Key other_key = test_context.CreateKey("other_key"); M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; + {std::string(foo_key.GetIdentifier()), "456"}}; M m2 = {{std::string(test_key.GetIdentifier()), "000"}}; context::Context foo_context = test_context.WriteValues(m1); @@ -84,7 +107,7 @@ TEST(ContextTest, ContextCopy) context::Context::Key other_key = test_context.CreateKey("other_key"); M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; + {std::string(foo_key.GetIdentifier()), "456"}}; M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; context::Context foo_context = test_context.WriteValues(m1); @@ -107,7 +130,7 @@ TEST(ContextTest, ContextCompare) context::Context::Key other_key = test_context.CreateKey("other_key"); M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; + {std::string(foo_key.GetIdentifier()), "456"}}; M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; context::Context foo_context = test_context.WriteValues(m1); From 62b0c887f84ffee0b6c23874ec592deb1066f376 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 16:54:28 +0000 Subject: [PATCH 33/93] added nostd::list and nostd::pair --- api/include/opentelemetry/nostd/list.h | 241 +++++++++++++++++++++++++ api/include/opentelemetry/nostd/pair.h | 51 ++++++ api/test/nostd/list_test.cc | 128 +++++++++++++ api/test/nostd/pair_test.cc | 17 ++ 4 files changed, 437 insertions(+) create mode 100644 api/include/opentelemetry/nostd/list.h create mode 100644 api/include/opentelemetry/nostd/pair.h create mode 100644 api/test/nostd/list_test.cc create mode 100644 api/test/nostd/pair_test.cc diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h new file mode 100644 index 0000000000..c19ee561be --- /dev/null +++ b/api/include/opentelemetry/nostd/list.h @@ -0,0 +1,241 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace nostd +{ + /* Non-standard implementation of the list data structure. */ + template + class list { + public: + + typedef T value_type; + + /* Custom iterator class*/ + class iterator + { + public: + typedef std::forward_iterator_tag iterator_category; + iterator(T* ptr) : ptr_(ptr) {} + iterator operator++() {ptr_++; return ptr_; } + T& operator*() { return *ptr_; } + T* operator->() { return ptr_; } + bool operator==(const iterator& other) {return ptr_ == other.ptr_; } + bool operator!=(const iterator& other) {return ptr_ != other.ptr_; } + + private: + T* ptr_; + }; + + list(){ + size_ = 0; + data_ = NULL; + }; + + /* Constructs class with passed in element. */ + list(T element){ + size_ = 1; + + T * data_ = new T [size_]; + + std::memcpy(data_, &element, sizeof(T)); + }; + + /* Constructs class with the passed in initializer_list. */ + list(std::initializer_list init_list){ + size_ = init_list.size(); + + T * temp = new T[size_]; + + std::memcpy(temp, std::begin(init_list), init_list.size()*sizeof(T)); + + data_ = temp; + } + + /* Returns the element at the front of the list. */ + T front(){ + if(size_ == 0){ + return T(); + } + return data_[0]; + } + + /* Returns the element at the back of the list. */ + T back(){ + if(size_ == 0){ + return T(); + } + return data_[size_ - 1]; + } + + /* Returns an iterator pointing at the front of the list. */ + iterator begin(){ + return iterator(data_); + } + + /* Returns an iterator pointing at the back of the list. */ + iterator end(){ + return iterator(data_ + size_); + } + + /* Inserts the passed in element at the passed in + * iterator position. + */ + iterator insert(iterator position, const T& element){ + int previous_size = size_; + size_++; + T *temp = new T[size_]; + + /* Counter tracks the index of the passed in iterator position. */ + int counter = 0; + for(auto iter = begin(); iter != end(); ++iter){ + if(iter == position){ + break; + } + counter++; + } + + int insert_index = counter; + int next_index = counter+1; + int last_index = previous_size; + int remaining_elements = previous_size - counter; + + /* Copy the original data that comes before the insertion. */ + std::memcpy(temp, data_, insert_index*sizeof(T)); + /* Copy the inserted data. */ + std::memcpy(temp + insert_index, &element, sizeof(T)); + /* Copy the original data that comes after the insertion. */ + std::memcpy(temp + next_index, data_+counter , remaining_elements*sizeof(T)); + + T *old_data = data_; + + data_ = temp; + + auto iter_pos = begin(); + /* Iterate until the inserted element. */ + for(int i = 0; i < insert_index; i++){ + ++iter_pos; + } + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + /* Return an iterator at the inserted element. */ + return iter_pos; + } + + /* Insert an element at the end of the list. */ + void push_back(T element){ + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + std::memcpy(temp, data_, previous_size*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + data_[previous_size] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Insert an element at the front of the list. */ + void push_front(T element){ + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + /* Copy the original data, leaving space for one element + * at the front. */ + std::memcpy(temp + 1, data_, previous_size*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + data_[0] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Returns the last element and removes it from the list. */ + T pop_back(){ + T back_elem = data_[size_ - 1]; + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_, size_*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return back_elem; + } + + /* Returns the first element and removes it from the list. */ + T pop_front(){ + T front_elem = front(); + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_ + 1, size_*sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return front_elem; + } + + /* Returns the number of elements in the list. */ + int size(){ + return size_; + } + + void operator=(const list& other_list){ + data_ = other_list.data_; + size_ = other_list.size_; + } + + T& operator[](int index){ + return data_[index]; + } + + private: + T* data_; + size_t size_; + }; + +} +OPENTELEMETRY_END_NAMESPACE + + diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h new file mode 100644 index 0000000000..bf80805c30 --- /dev/null +++ b/api/include/opentelemetry/nostd/pair.h @@ -0,0 +1,51 @@ +#pragma once + +#include "opentelemetry/nostd/utility.h" +#include "opentelemetry/version.h" + + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace nostd +{ + +/* The nostd::pair class provides a data structure capable of hold + * two values of different or the same types, and provides access to each. + */ +template +class pair { + public: + + pair(){ + first_ = T1(); + second_ = T2(); + } + + /* Constructs a pair object with the passed in first and second values. */ + pair( T1 first, T2 second){ + first_ = first; + second_ = second; + } + + void operator=( const pair& other_pair){ + first_ = other_pair.first_; + second_ = other_pair.second_; + } + + /* Returns the first element. */ + T1 first(){ + return first_; + } + + /* Returns the second element. */ + T2 second(){ + return second_; + } + + private: + T1 first_; + T2 second_; + +}; + +} +OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc new file mode 100644 index 0000000000..b75962e4b6 --- /dev/null +++ b/api/test/nostd/list_test.cc @@ -0,0 +1,128 @@ +#include "opentelemetry/nostd/list.h" +#include "opentelemetry/nostd/pair.h" + +#include + +using opentelemetry::nostd::list; +using opentelemetry::nostd::pair; + +/* Tests that front() returns the first element and back() + * returns the last element. + */ +TEST(ListTest, FrontBack) +{ + list list_1; + + list_1.push_back(9); + list_1.push_back(8); + list_1.push_back(7); + list_1.push_back(6); + + EXPECT_EQ(list_1.front(), 9); + EXPECT_EQ(list_1.back(), 6); + +} + +/* Tests that the list object can handle data types of a size + * other than an int. */ +TEST(ListTest, PairList) +{ + list> pair_list; + + pair_list.push_back(pair(1,2)); + pair_list.push_back(pair(2,3)); + pair_list.push_back(pair(4,5)); + + pair temp_pair; + temp_pair = pair_list.front(); + + EXPECT_EQ(pair_list.front().first(), 1); + EXPECT_EQ(pair_list.front().second(), 2); + + EXPECT_EQ(pair_list.back().first(), 4); + EXPECT_EQ(pair_list.back().second(), 5); +} + +/* Tests taht a list object can be initialized with an initalizer list. */ +TEST(ListTest, InitializerList) +{ + + list pair_list = {1,2,3,4,5,6,7,8,9,10}; + + EXPECT_EQ(pair_list.front(), 1); + EXPECT_EQ(pair_list.back(), 10); +} + +/* Tests that the list object can handle an insert_iterator. */ +TEST(ListTest, IteratorInsertion) +{ + + list int_list = {1,2,3,4}; + list insert_list = {5,6,7,8}; + + std::insert_iterator> back(int_list, std::end(int_list)); + + for (auto iter = std::begin(insert_list); iter != std::end(insert_list); ++iter) + { + back = *iter; + } + + int counter = 1; + for(auto iter = std::begin(int_list); iter != std::end(int_list); ++iter){ + EXPECT_EQ(*iter,counter); + counter++; + } +} + +/* Tests that the push_front method added elements to the front of the list. */ +TEST(ListTest, PushFront){ + list> list_list; + list inner_list = {0,1,2,3,4}; + list mid_list = {9,9,9,9,9}; + list_list.push_front(inner_list); + list_list.push_back(mid_list); + list_list.push_front(inner_list); + + EXPECT_EQ(list_list.front().front(),0); + EXPECT_EQ(list_list.front().back(),4); + + EXPECT_EQ(list_list.back().front(), 9); + EXPECT_EQ(list_list.back().back(), 9); +} + +/* Tests that you can modify elements via the subscript operator. */ +TEST(ListTest, SubscriptOperator){ + list int_list = {0,1,2,3,4}; + + EXPECT_EQ(int_list[2],2); + int_list[2] = 9; + EXPECT_EQ(int_list[2],9); +} + +/* Tests that the pop_back method, returns and removes the back element + * from the list. + */ +TEST(ListTest, PopBack){ + list int_list = {0,1,2,3,4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.back(),4); + EXPECT_EQ(int_list.pop_back(), 4); + EXPECT_EQ(int_list.size(), previous_size - 1); + + EXPECT_EQ(int_list.back(), 3); +} + +/* Tests that the pop_front method, returns and removes the front element + * from the list. + */ +TEST(ListTest, PopFront){ + list int_list = {0,1,2,3,4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.front(),0); + EXPECT_EQ(int_list.pop_front(), 0); + EXPECT_EQ(int_list.size(), previous_size - 1); + + EXPECT_EQ(int_list.front(), 1); +} diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc new file mode 100644 index 0000000000..ad2dc379f3 --- /dev/null +++ b/api/test/nostd/pair_test.cc @@ -0,0 +1,17 @@ +#include "opentelemetry/nostd/pair.h" + +#include + +using opentelemetry::nostd::pair; + +/* Tests constructing a pair object. */ +TEST(PairTest, BasicConstruction) +{ + int val_1 = 1; + int val_2 = 9; + pair temp_pair = pair(val_1,val_2); + + EXPECT_EQ(temp_pair.first(), val_1); + EXPECT_EQ(temp_pair.second(), val_2); + +} From 9c395365b021a0fd5a2ace2f5871fdc2924f3548 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 16:58:59 +0000 Subject: [PATCH 34/93] ran auto formatter --- api/include/opentelemetry/nostd/list.h | 463 +++++++++++++------------ api/include/opentelemetry/nostd/pair.h | 72 ++-- api/test/nostd/list_test.cc | 88 ++--- api/test/nostd/pair_test.cc | 7 +- 4 files changed, 319 insertions(+), 311 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index c19ee561be..1d6e48f063 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -1,10 +1,10 @@ #pragma once +#include #include -#include #include -#include #include +#include #include "opentelemetry/nostd/utility.h" #include "opentelemetry/version.h" @@ -12,230 +12,239 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { - /* Non-standard implementation of the list data structure. */ - template - class list { - public: - - typedef T value_type; - - /* Custom iterator class*/ - class iterator - { - public: - typedef std::forward_iterator_tag iterator_category; - iterator(T* ptr) : ptr_(ptr) {} - iterator operator++() {ptr_++; return ptr_; } - T& operator*() { return *ptr_; } - T* operator->() { return ptr_; } - bool operator==(const iterator& other) {return ptr_ == other.ptr_; } - bool operator!=(const iterator& other) {return ptr_ != other.ptr_; } - - private: - T* ptr_; - }; - - list(){ - size_ = 0; - data_ = NULL; - }; - - /* Constructs class with passed in element. */ - list(T element){ - size_ = 1; - - T * data_ = new T [size_]; - - std::memcpy(data_, &element, sizeof(T)); - }; - - /* Constructs class with the passed in initializer_list. */ - list(std::initializer_list init_list){ - size_ = init_list.size(); - - T * temp = new T[size_]; - - std::memcpy(temp, std::begin(init_list), init_list.size()*sizeof(T)); - - data_ = temp; - } - - /* Returns the element at the front of the list. */ - T front(){ - if(size_ == 0){ - return T(); - } - return data_[0]; - } - - /* Returns the element at the back of the list. */ - T back(){ - if(size_ == 0){ - return T(); - } - return data_[size_ - 1]; - } - - /* Returns an iterator pointing at the front of the list. */ - iterator begin(){ - return iterator(data_); - } - - /* Returns an iterator pointing at the back of the list. */ - iterator end(){ - return iterator(data_ + size_); - } - - /* Inserts the passed in element at the passed in - * iterator position. - */ - iterator insert(iterator position, const T& element){ - int previous_size = size_; - size_++; - T *temp = new T[size_]; - - /* Counter tracks the index of the passed in iterator position. */ - int counter = 0; - for(auto iter = begin(); iter != end(); ++iter){ - if(iter == position){ - break; - } - counter++; - } - - int insert_index = counter; - int next_index = counter+1; - int last_index = previous_size; - int remaining_elements = previous_size - counter; - - /* Copy the original data that comes before the insertion. */ - std::memcpy(temp, data_, insert_index*sizeof(T)); - /* Copy the inserted data. */ - std::memcpy(temp + insert_index, &element, sizeof(T)); - /* Copy the original data that comes after the insertion. */ - std::memcpy(temp + next_index, data_+counter , remaining_elements*sizeof(T)); - - T *old_data = data_; - - data_ = temp; - - auto iter_pos = begin(); - /* Iterate until the inserted element. */ - for(int i = 0; i < insert_index; i++){ - ++iter_pos; - } - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - /* Return an iterator at the inserted element. */ - return iter_pos; - } - - /* Insert an element at the end of the list. */ - void push_back(T element){ - int previous_size = size_; - size_++; - - T *temp = new T[size_]; - - std::memcpy(temp, data_, previous_size*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - data_[previous_size] = element; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - }; - - /* Insert an element at the front of the list. */ - void push_front(T element){ - int previous_size = size_; - size_++; - - T *temp = new T[size_]; - - /* Copy the original data, leaving space for one element - * at the front. */ - std::memcpy(temp + 1, data_, previous_size*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - data_[0] = element; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - }; - - /* Returns the last element and removes it from the list. */ - T pop_back(){ - T back_elem = data_[size_ - 1]; - size_--; - - T *temp = new T[size_]; - - /* Copy the original data.*/ - std::memcpy(temp, data_, size_*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - return back_elem; - } - - /* Returns the first element and removes it from the list. */ - T pop_front(){ - T front_elem = front(); - size_--; - - T *temp = new T[size_]; - - /* Copy the original data.*/ - std::memcpy(temp, data_ + 1, size_*sizeof(T)); - - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - - data_ = temp; - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - return front_elem; - } - - /* Returns the number of elements in the list. */ - int size(){ - return size_; - } - - void operator=(const list& other_list){ - data_ = other_list.data_; - size_ = other_list.size_; - } - - T& operator[](int index){ - return data_[index]; - } - - private: - T* data_; - size_t size_; - }; - -} -OPENTELEMETRY_END_NAMESPACE +/* Non-standard implementation of the list data structure. */ +template +class list +{ +public: + typedef T value_type; + + /* Custom iterator class*/ + class iterator + { + public: + typedef std::forward_iterator_tag iterator_category; + iterator(T *ptr) : ptr_(ptr) {} + iterator operator++() + { + ptr_++; + return ptr_; + } + T &operator*() { return *ptr_; } + T *operator->() { return ptr_; } + bool operator==(const iterator &other) { return ptr_ == other.ptr_; } + bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } + + private: + T *ptr_; + }; + + list() + { + size_ = 0; + data_ = NULL; + }; + + /* Constructs class with passed in element. */ + list(T element) + { + size_ = 1; + + T *data_ = new T[size_]; + + std::memcpy(data_, &element, sizeof(T)); + }; + + /* Constructs class with the passed in initializer_list. */ + list(std::initializer_list init_list) + { + size_ = init_list.size(); + + T *temp = new T[size_]; + + std::memcpy(temp, std::begin(init_list), init_list.size() * sizeof(T)); + + data_ = temp; + } + + /* Returns the element at the front of the list. */ + T front() + { + if (size_ == 0) + { + return T(); + } + return data_[0]; + } + + /* Returns the element at the back of the list. */ + T back() + { + if (size_ == 0) + { + return T(); + } + return data_[size_ - 1]; + } + + /* Returns an iterator pointing at the front of the list. */ + iterator begin() { return iterator(data_); } + + /* Returns an iterator pointing at the back of the list. */ + iterator end() { return iterator(data_ + size_); } + + /* Inserts the passed in element at the passed in + * iterator position. + */ + iterator insert(iterator position, const T &element) + { + int previous_size = size_; + size_++; + T *temp = new T[size_]; + + /* Counter tracks the index of the passed in iterator position. */ + int counter = 0; + for (auto iter = begin(); iter != end(); ++iter) + { + if (iter == position) + { + break; + } + counter++; + } + + int insert_index = counter; + int next_index = counter + 1; + int last_index = previous_size; + int remaining_elements = previous_size - counter; + + /* Copy the original data that comes before the insertion. */ + std::memcpy(temp, data_, insert_index * sizeof(T)); + /* Copy the inserted data. */ + std::memcpy(temp + insert_index, &element, sizeof(T)); + /* Copy the original data that comes after the insertion. */ + std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); + + T *old_data = data_; + + data_ = temp; + + auto iter_pos = begin(); + /* Iterate until the inserted element. */ + for (int i = 0; i < insert_index; i++) + { + ++iter_pos; + } + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + /* Return an iterator at the inserted element. */ + return iter_pos; + } + + /* Insert an element at the end of the list. */ + void push_back(T element) + { + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + std::memcpy(temp, data_, previous_size * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + data_[previous_size] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Insert an element at the front of the list. */ + void push_front(T element) + { + int previous_size = size_; + size_++; + + T *temp = new T[size_]; + + /* Copy the original data, leaving space for one element + * at the front. */ + std::memcpy(temp + 1, data_, previous_size * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + data_ = temp; + data_[0] = element; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + }; + + /* Returns the last element and removes it from the list. */ + T pop_back() + { + T back_elem = data_[size_ - 1]; + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_, size_ * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return back_elem; + } + + /* Returns the first element and removes it from the list. */ + T pop_front() + { + T front_elem = front(); + size_--; + + T *temp = new T[size_]; + + /* Copy the original data.*/ + std::memcpy(temp, data_ + 1, size_ * sizeof(T)); + + /* Save old array pointer for later deallocation. */ + T *old_data = data_; + + data_ = temp; + + /* Deallocate memory used by previous array. */ + delete[] old_data; + + return front_elem; + } + + /* Returns the number of elements in the list. */ + int size() { return size_; } + + void operator=(const list &other_list) + { + data_ = other_list.data_; + size_ = other_list.size_; + } + + T &operator[](int index) { return data_[index]; } + +private: + T *data_; + size_t size_; +}; +} // namespace nostd +OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index bf80805c30..58b777ae72 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -3,49 +3,45 @@ #include "opentelemetry/nostd/utility.h" #include "opentelemetry/version.h" - OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { -/* The nostd::pair class provides a data structure capable of hold - * two values of different or the same types, and provides access to each. +/* The nostd::pair class provides a data structure capable of hold + * two values of different or the same types, and provides access to each. */ template -class pair { - public: - - pair(){ - first_ = T1(); - second_ = T2(); - } - - /* Constructs a pair object with the passed in first and second values. */ - pair( T1 first, T2 second){ - first_ = first; - second_ = second; - } - - void operator=( const pair& other_pair){ - first_ = other_pair.first_; - second_ = other_pair.second_; - } - - /* Returns the first element. */ - T1 first(){ - return first_; - } - - /* Returns the second element. */ - T2 second(){ - return second_; - } - - private: - T1 first_; - T2 second_; - +class pair +{ +public: + pair() + { + first_ = T1(); + second_ = T2(); + } + + /* Constructs a pair object with the passed in first and second values. */ + pair(T1 first, T2 second) + { + first_ = first; + second_ = second; + } + + void operator=(const pair &other_pair) + { + first_ = other_pair.first_; + second_ = other_pair.second_; + } + + /* Returns the first element. */ + T1 first() { return first_; } + + /* Returns the second element. */ + T2 second() { return second_; } + +private: + T1 first_; + T2 second_; }; - -} +} // namespace nostd OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index b75962e4b6..ce84b20c6f 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -6,8 +6,8 @@ using opentelemetry::nostd::list; using opentelemetry::nostd::pair; -/* Tests that front() returns the first element and back() - * returns the last element. +/* Tests that front() returns the first element and back() + * returns the last element. */ TEST(ListTest, FrontBack) { @@ -20,20 +20,19 @@ TEST(ListTest, FrontBack) EXPECT_EQ(list_1.front(), 9); EXPECT_EQ(list_1.back(), 6); - } -/* Tests that the list object can handle data types of a size +/* Tests that the list object can handle data types of a size * other than an int. */ TEST(ListTest, PairList) { - list> pair_list; - - pair_list.push_back(pair(1,2)); - pair_list.push_back(pair(2,3)); - pair_list.push_back(pair(4,5)); + list> pair_list; + + pair_list.push_back(pair(1, 2)); + pair_list.push_back(pair(2, 3)); + pair_list.push_back(pair(4, 5)); - pair temp_pair; + pair temp_pair; temp_pair = pair_list.front(); EXPECT_EQ(pair_list.front().first(), 1); @@ -47,7 +46,7 @@ TEST(ListTest, PairList) TEST(ListTest, InitializerList) { - list pair_list = {1,2,3,4,5,6,7,8,9,10}; + list pair_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; EXPECT_EQ(pair_list.front(), 1); EXPECT_EQ(pair_list.back(), 10); @@ -57,72 +56,77 @@ TEST(ListTest, InitializerList) TEST(ListTest, IteratorInsertion) { - list int_list = {1,2,3,4}; - list insert_list = {5,6,7,8}; + list int_list = {1, 2, 3, 4}; + list insert_list = {5, 6, 7, 8}; std::insert_iterator> back(int_list, std::end(int_list)); - + for (auto iter = std::begin(insert_list); iter != std::end(insert_list); ++iter) { back = *iter; } - - int counter = 1; - for(auto iter = std::begin(int_list); iter != std::end(int_list); ++iter){ - EXPECT_EQ(*iter,counter); + + int counter = 1; + for (auto iter = std::begin(int_list); iter != std::end(int_list); ++iter) + { + EXPECT_EQ(*iter, counter); counter++; } } /* Tests that the push_front method added elements to the front of the list. */ -TEST(ListTest, PushFront){ +TEST(ListTest, PushFront) +{ list> list_list; - list inner_list = {0,1,2,3,4}; - list mid_list = {9,9,9,9,9}; + list inner_list = {0, 1, 2, 3, 4}; + list mid_list = {9, 9, 9, 9, 9}; list_list.push_front(inner_list); list_list.push_back(mid_list); list_list.push_front(inner_list); - EXPECT_EQ(list_list.front().front(),0); - EXPECT_EQ(list_list.front().back(),4); + EXPECT_EQ(list_list.front().front(), 0); + EXPECT_EQ(list_list.front().back(), 4); EXPECT_EQ(list_list.back().front(), 9); EXPECT_EQ(list_list.back().back(), 9); } /* Tests that you can modify elements via the subscript operator. */ -TEST(ListTest, SubscriptOperator){ - list int_list = {0,1,2,3,4}; - - EXPECT_EQ(int_list[2],2); +TEST(ListTest, SubscriptOperator) +{ + list int_list = {0, 1, 2, 3, 4}; + + EXPECT_EQ(int_list[2], 2); int_list[2] = 9; - EXPECT_EQ(int_list[2],9); + EXPECT_EQ(int_list[2], 9); } /* Tests that the pop_back method, returns and removes the back element - * from the list. + * from the list. */ -TEST(ListTest, PopBack){ - list int_list = {0,1,2,3,4}; - - int previous_size = int_list.size(); - EXPECT_EQ(int_list.back(),4); +TEST(ListTest, PopBack) +{ + list int_list = {0, 1, 2, 3, 4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.back(), 4); EXPECT_EQ(int_list.pop_back(), 4); EXPECT_EQ(int_list.size(), previous_size - 1); - + EXPECT_EQ(int_list.back(), 3); } /* Tests that the pop_front method, returns and removes the front element - * from the list. + * from the list. */ -TEST(ListTest, PopFront){ - list int_list = {0,1,2,3,4}; - - int previous_size = int_list.size(); - EXPECT_EQ(int_list.front(),0); +TEST(ListTest, PopFront) +{ + list int_list = {0, 1, 2, 3, 4}; + + int previous_size = int_list.size(); + EXPECT_EQ(int_list.front(), 0); EXPECT_EQ(int_list.pop_front(), 0); EXPECT_EQ(int_list.size(), previous_size - 1); - + EXPECT_EQ(int_list.front(), 1); } diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index ad2dc379f3..cccaf40092 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -7,11 +7,10 @@ using opentelemetry::nostd::pair; /* Tests constructing a pair object. */ TEST(PairTest, BasicConstruction) { - int val_1 = 1; - int val_2 = 9; - pair temp_pair = pair(val_1,val_2); + int val_1 = 1; + int val_2 = 9; + pair temp_pair = pair(val_1, val_2); EXPECT_EQ(temp_pair.first(), val_1); EXPECT_EQ(temp_pair.second(), val_2); - } From 821bfb433c49d15d983ad608b3a20c71c8447575 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 24 Jun 2020 19:27:47 +0000 Subject: [PATCH 35/93] added tests to build file --- api/test/nostd/BUILD | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/api/test/nostd/BUILD b/api/test/nostd/BUILD index 35306e6a3c..e816b63dba 100644 --- a/api/test/nostd/BUILD +++ b/api/test/nostd/BUILD @@ -74,3 +74,25 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "list_test", + srcs = [ + "list_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "pair_test", + srcs = [ + "pair_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) From 25813c8932d2beadcea6ed684a7f3583f59e3d15 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 25 Jun 2020 03:04:20 +0000 Subject: [PATCH 36/93] Added tests, removed unnecessary code and simplified things --- api/include/opentelemetry/nostd/list.h | 135 ++++++++++++------------- api/include/opentelemetry/nostd/pair.h | 22 ++-- api/test/nostd/list_test.cc | 91 ++++++++++++----- api/test/nostd/pair_test.cc | 2 +- 4 files changed, 148 insertions(+), 102 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index 1d6e48f063..352aa84013 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "opentelemetry/nostd/utility.h" @@ -12,18 +13,17 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { -/* Non-standard implementation of the list data structure. */ +// Non-standard implementation of the list data structure. template class list { public: typedef T value_type; - /* Custom iterator class*/ + // Custom iterator class class iterator { public: - typedef std::forward_iterator_tag iterator_category; iterator(T *ptr) : ptr_(ptr) {} iterator operator++() { @@ -36,26 +36,27 @@ class list bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } private: + friend class list; T *ptr_; }; list() { size_ = 0; - data_ = NULL; + data_ = nullptr; }; - /* Constructs class with passed in element. */ + // Constructs class with passed in element. list(T element) { size_ = 1; - T *data_ = new T[size_]; + data_ = new T[size_]; std::memcpy(data_, &element, sizeof(T)); }; - /* Constructs class with the passed in initializer_list. */ + // Constructs class with the passed in initializer_list. list(std::initializer_list init_list) { size_ = init_list.size(); @@ -67,83 +68,61 @@ class list data_ = temp; } - /* Returns the element at the front of the list. */ - T front() + // Returns the element at the front of the list. + T& front() { - if (size_ == 0) - { - return T(); - } return data_[0]; } - /* Returns the element at the back of the list. */ - T back() + // Returns the element at the back of the list. + T& back() { + if (size_ == 0) { - return T(); + return data_[0]; } + return data_[size_ - 1]; } - /* Returns an iterator pointing at the front of the list. */ + // Returns an iterator pointing at the front of the list. iterator begin() { return iterator(data_); } - /* Returns an iterator pointing at the back of the list. */ + // Returns an iterator pointing at the back of the list. iterator end() { return iterator(data_ + size_); } - /* Inserts the passed in element at the passed in - * iterator position. - */ + // Inserts the passed in element at the passed in + // iterator position. iterator insert(iterator position, const T &element) { int previous_size = size_; size_++; T *temp = new T[size_]; - /* Counter tracks the index of the passed in iterator position. */ - int counter = 0; - for (auto iter = begin(); iter != end(); ++iter) - { - if (iter == position) - { - break; - } - counter++; - } + // Counter tracks the index of the passed in iterator position. + int counter = position.ptr_ - data_; int insert_index = counter; int next_index = counter + 1; - int last_index = previous_size; int remaining_elements = previous_size - counter; - /* Copy the original data that comes before the insertion. */ + // Copy the original data that comes before the insertion. std::memcpy(temp, data_, insert_index * sizeof(T)); - /* Copy the inserted data. */ + // Copy the inserted data. std::memcpy(temp + insert_index, &element, sizeof(T)); - /* Copy the original data that comes after the insertion. */ + // Copy the original data that comes after the insertion. std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); - T *old_data = data_; - + delete[] data_; + data_ = temp; - auto iter_pos = begin(); - /* Iterate until the inserted element. */ - for (int i = 0; i < insert_index; i++) - { - ++iter_pos; - } - - /* Deallocate memory used by previous array. */ - delete[] old_data; - - /* Return an iterator at the inserted element. */ - return iter_pos; + // Return an iterator at the inserted element. + return iterator(data_ + counter); } - /* Insert an element at the end of the list. */ + // Insert an element at the end of the list. void push_back(T element) { int previous_size = size_; @@ -153,18 +132,16 @@ class list std::memcpy(temp, data_, previous_size * sizeof(T)); - /* Save old array pointer for later deallocation. */ - T *old_data = data_; - + // Deallocate memory used by previous array. + delete[] data_; + data_ = temp; data_[previous_size] = element; - /* Deallocate memory used by previous array. */ - delete[] old_data; }; - /* Insert an element at the front of the list. */ + // Insert an element at the front of the list. void push_front(T element) { int previous_size = size_; @@ -172,44 +149,45 @@ class list T *temp = new T[size_]; - /* Copy the original data, leaving space for one element - * at the front. */ + // Copy the original data, leaving space for one element + // at the front. std::memcpy(temp + 1, data_, previous_size * sizeof(T)); - /* Save old array pointer for later deallocation. */ + // Save old array pointer for later deallocation. T *old_data = data_; data_ = temp; data_[0] = element; - /* Deallocate memory used by previous array. */ + // Deallocate memory used by previous array. delete[] old_data; }; - /* Returns the last element and removes it from the list. */ + // Returns the last element and removes it from the list. T pop_back() { + T back_elem = data_[size_ - 1]; size_--; T *temp = new T[size_]; - /* Copy the original data.*/ + // Copy the original data. std::memcpy(temp, data_, size_ * sizeof(T)); - /* Save old array pointer for later deallocation. */ + // Save old array pointer for later deallocation. T *old_data = data_; data_ = temp; - /* Deallocate memory used by previous array. */ + // Deallocate memory used by previous array. delete[] old_data; return back_elem; } - /* Returns the first element and removes it from the list. */ + // Returns the first element and removes it from the list. T pop_front() { T front_elem = front(); @@ -217,29 +195,46 @@ class list T *temp = new T[size_]; - /* Copy the original data.*/ + // Copy the original data. std::memcpy(temp, data_ + 1, size_ * sizeof(T)); - /* Save old array pointer for later deallocation. */ + // Save old array pointer for later deallocation. T *old_data = data_; data_ = temp; - /* Deallocate memory used by previous array. */ + // Deallocate memory used by previous array. delete[] old_data; return front_elem; } - /* Returns the number of elements in the list. */ + // Returns the number of elements in the list. // int size() { return size_; } void operator=(const list &other_list) { - data_ = other_list.data_; + delete[] data_; + data_ = new T[other_list.size_]; + memcpy(data_, other_list.data_, other_list.size_*sizeof(T)); size_ = other_list.size_; } + + // Returns true if the lists are the same, false if they are not + bool operator==(const list &other_list) + { + if(size_ != other_list.size_){ + return false; + } + if( memcmp(data_, other_list.data_, size_) == 0){ + return true; + } + else{ + return false; + } + } + T &operator[](int index) { return data_[index]; } private: diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index 58b777ae72..cf17f393e4 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -7,9 +7,9 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace nostd { -/* The nostd::pair class provides a data structure capable of hold - * two values of different or the same types, and provides access to each. - */ +// The nostd::pair class provides a data structure capable of hold +// two values of different or the same types, and provides access to each. + template class pair { @@ -20,7 +20,7 @@ class pair second_ = T2(); } - /* Constructs a pair object with the passed in first and second values. */ + // Constructs a pair object with the passed in first and second values. pair(T1 first, T2 second) { first_ = first; @@ -33,10 +33,20 @@ class pair second_ = other_pair.second_; } - /* Returns the first element. */ + bool operator==(const pair &other_pair) + { + if(first_ == other_pair.first_){ + if(second_ == other_pair.second_){ + return true; + } + } + return false; + } + + // Returns the first element. T1 first() { return first_; } - /* Returns the second element. */ + // Returns the second element. T2 second() { return second_; } private: diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index ce84b20c6f..36106d5fda 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -2,14 +2,15 @@ #include "opentelemetry/nostd/pair.h" #include +#include using opentelemetry::nostd::list; using opentelemetry::nostd::pair; -/* Tests that front() returns the first element and back() - * returns the last element. - */ -TEST(ListTest, FrontBack) +// Tests that front() returns the first element and back() +// returns the last element. + +TEST(ListTest, FrontBackReturnsProperElements) { list list_1; @@ -22,9 +23,9 @@ TEST(ListTest, FrontBack) EXPECT_EQ(list_1.back(), 6); } -/* Tests that the list object can handle data types of a size - * other than an int. */ -TEST(ListTest, PairList) +// Tests that the list object can handle data types of a size +// other than an int. +TEST(ListTest, ListAcceptsPairDatatype) { list> pair_list; @@ -42,8 +43,8 @@ TEST(ListTest, PairList) EXPECT_EQ(pair_list.back().second(), 5); } -/* Tests taht a list object can be initialized with an initalizer list. */ -TEST(ListTest, InitializerList) +// Tests that a list object can be initialized with an initalizer list. +TEST(ListTest, ListConstructorAcceptsInitializerList) { list pair_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; @@ -52,8 +53,8 @@ TEST(ListTest, InitializerList) EXPECT_EQ(pair_list.back(), 10); } -/* Tests that the list object can handle an insert_iterator. */ -TEST(ListTest, IteratorInsertion) +// Tests that the list object can handle an insert_iterator. +TEST(ListTest, InsertIteratorAddsToList) { list int_list = {1, 2, 3, 4}; @@ -72,14 +73,16 @@ TEST(ListTest, IteratorInsertion) EXPECT_EQ(*iter, counter); counter++; } + } -/* Tests that the push_front method added elements to the front of the list. */ -TEST(ListTest, PushFront) +// Tests that the push_front method added elements to the front of the list. +TEST(ListTest, PushFrontAddsDataAtFront) { list> list_list; list inner_list = {0, 1, 2, 3, 4}; - list mid_list = {9, 9, 9, 9, 9}; + list mid_list = {5, 6, 7, 8, 9}; + list_list.push_front(inner_list); list_list.push_back(mid_list); list_list.push_front(inner_list); @@ -87,12 +90,12 @@ TEST(ListTest, PushFront) EXPECT_EQ(list_list.front().front(), 0); EXPECT_EQ(list_list.front().back(), 4); - EXPECT_EQ(list_list.back().front(), 9); + EXPECT_EQ(list_list.back().front(), 5); EXPECT_EQ(list_list.back().back(), 9); } -/* Tests that you can modify elements via the subscript operator. */ -TEST(ListTest, SubscriptOperator) +// Tests that you can modify elements via the subscript operator. +TEST(ListTest, SubscriptOperatorAllowsForValueModification) { list int_list = {0, 1, 2, 3, 4}; @@ -101,10 +104,9 @@ TEST(ListTest, SubscriptOperator) EXPECT_EQ(int_list[2], 9); } -/* Tests that the pop_back method, returns and removes the back element - * from the list. - */ -TEST(ListTest, PopBack) +// Tests that the pop_back method returns and removes the back element +// from the list. +TEST(ListTest, PopBackReturnsAndRemovesFromBack) { list int_list = {0, 1, 2, 3, 4}; @@ -116,10 +118,9 @@ TEST(ListTest, PopBack) EXPECT_EQ(int_list.back(), 3); } -/* Tests that the pop_front method, returns and removes the front element - * from the list. - */ -TEST(ListTest, PopFront) +// Tests that the pop_front method returns and removes the front element +// from the list. +TEST(ListTest, PopFrontReturnsAndRemovesFromFront) { list int_list = {0, 1, 2, 3, 4}; @@ -130,3 +131,43 @@ TEST(ListTest, PopFront) EXPECT_EQ(int_list.front(), 1); } + +// Tests that after using the assignment operator, changing the new +// doesn't change the original +TEST(ListTest,AssignmentOperatorDoesNotChangeOriginal) +{ + + list first_list = {0, 1, 2, 3, 4}; + list first_list_copy = {0, 1, 2, 3, 4}; + + list second_list = {5, 6, 7, 8, 9}; + second_list = first_list; + second_list.pop_back(); + second_list.pop_back(); + second_list.pop_back(); + + EXPECT_EQ(first_list == first_list_copy, true); + EXPECT_EQ(first_list == second_list, false); + +} + +// Tests that two lists with the same contents return true when compared +// using the comparison operator +TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents){ + + list first_list = {0, 1, 2, 3, 4}; + list second_list = {0, 1, 2, 3, 4}; + list third_list = {0, 1, 2, 3, 5}; + EXPECT_EQ(first_list == second_list, true); + EXPECT_NE(first_list == third_list, false); +} + +// Tests that constructing a list with one element populates the list object +// with that element +TEST(ListTest, SingleElementConstruction){ + list single_list = list(5); + + EXPECT_EQ(single_list[0], 5); +} + + diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index cccaf40092..e4a6fd7eb7 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -4,7 +4,7 @@ using opentelemetry::nostd::pair; -/* Tests constructing a pair object. */ +// Tests constructing a pair object. TEST(PairTest, BasicConstruction) { int val_1 = 1; From ec12e39f55ae02595657d1c425e5a8485cbed524 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 25 Jun 2020 03:23:56 +0000 Subject: [PATCH 37/93] added assignment tests for pair and comparison test for pair --- api/include/opentelemetry/nostd/pair.h | 4 +- api/test/nostd/pair_test.cc | 62 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index cf17f393e4..116661b28d 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -44,10 +44,10 @@ class pair } // Returns the first element. - T1 first() { return first_; } + T1 & first() { return first_; } // Returns the second element. - T2 second() { return second_; } + T2 & second() { return second_; } private: T1 first_; diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index e4a6fd7eb7..e8171b2190 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -14,3 +14,65 @@ TEST(PairTest, BasicConstruction) EXPECT_EQ(temp_pair.first(), val_1); EXPECT_EQ(temp_pair.second(), val_2); } + +// Tests that the comparison operator returns true when pair values +// are the same and returns false when different. +TEST(PairTest, ComparisonSameValuesReturnTrue){ + + int val_1 = 1; + int val_2 = 2; + pair pair_1 = pair(val_1, val_2); + pair pair_2 = pair(val_1, val_2); + + EXPECT_EQ(pair_1 == pair_2, true); + + pair_1.first() = 8; + + EXPECT_EQ(pair_1 == pair_2, false); +} + +// Tests that the assignment operator sets the values as one would expect. +TEST(PairTest, AssignmentBasic){ + + int val_1 = 1; + int val_2 = 2; + pair pair_1 = pair(val_1, val_2); + + int val_3 = 3; + int val_4 = 4; + pair pair_2 = pair(val_3, val_4); + + EXPECT_EQ(pair_1 == pair_2, false); + pair_1 = pair_2; + + EXPECT_EQ(pair_1 == pair_2, true); + EXPECT_EQ(pair_1.first(), val_3); + EXPECT_EQ(pair_1.second(), val_4); + +} + +// Tests that after using the assignment operator, changing values of one +// object does not change both. +TEST(PairTest, AssignmentOtherObjectImmutable){ + + int val_1 = 1; + int val_2 = 2; + pair pair_1 = pair(val_1, val_2); + + int val_3 = 3; + int val_4 = 4; + pair pair_2 = pair(val_3, val_4); + + pair_1 = pair_2; + + pair_1.first() = 8; + pair_1.second() = 7; + + EXPECT_EQ(pair_1.first(), 8); + EXPECT_EQ(pair_1.second(), 7); + + EXPECT_EQ(pair_2.first(), val_3); + EXPECT_EQ(pair_2.second(), val_4); +} + + From 7ac58e26382ff595d474bdc04e362ecf8f49c4c6 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 25 Jun 2020 03:32:12 +0000 Subject: [PATCH 38/93] moved some delete statements --- api/include/opentelemetry/nostd/list.h | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index 352aa84013..bf90c9a7b2 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -153,15 +153,11 @@ class list // at the front. std::memcpy(temp + 1, data_, previous_size * sizeof(T)); - // Save old array pointer for later deallocation. - T *old_data = data_; + delete[] data_; data_ = temp; data_[0] = element; - - // Deallocate memory used by previous array. - delete[] old_data; }; // Returns the last element and removes it from the list. @@ -176,14 +172,10 @@ class list // Copy the original data. std::memcpy(temp, data_, size_ * sizeof(T)); - // Save old array pointer for later deallocation. - T *old_data = data_; + delete[] data_; data_ = temp; - // Deallocate memory used by previous array. - delete[] old_data; - return back_elem; } @@ -198,13 +190,10 @@ class list // Copy the original data. std::memcpy(temp, data_ + 1, size_ * sizeof(T)); - // Save old array pointer for later deallocation. - T *old_data = data_; + delete[] data_; data_ = temp; - // Deallocate memory used by previous array. - delete[] old_data; return front_elem; } @@ -234,7 +223,7 @@ class list return false; } } - + T &operator[](int index) { return data_[index]; } private: From cf403b7070475ce187b2ed308c16a88b2cd2c76c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Fri, 26 Jun 2020 19:29:35 +0000 Subject: [PATCH 39/93] fixed memory leak in list and pair --- api/include/opentelemetry/nostd/list.h | 96 +++++++++++++++---------- api/include/opentelemetry/nostd/pair.h | 11 +-- api/test/nostd/list_test.cc | 99 ++++++++++++++------------ api/test/nostd/pair_test.cc | 64 +++++++++++------ 4 files changed, 155 insertions(+), 115 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index bf90c9a7b2..59705bb0c4 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -3,8 +3,8 @@ #include #include #include -#include #include +#include #include #include "opentelemetry/nostd/utility.h" @@ -24,19 +24,25 @@ class list class iterator { public: - iterator(T *ptr) : ptr_(ptr) {} iterator operator++() { ptr_++; return ptr_; } + T &operator*() { return *ptr_; } + T *operator->() { return ptr_; } + bool operator==(const iterator &other) { return ptr_ == other.ptr_; } + bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } private: friend class list; + + iterator(T *ptr) : ptr_(ptr) {} + T *ptr_; }; @@ -61,28 +67,22 @@ class list { size_ = init_list.size(); - T *temp = new T[size_]; - - std::memcpy(temp, std::begin(init_list), init_list.size() * sizeof(T)); + data_ = new T[size_]; - data_ = temp; + std::memcpy(data_, std::begin(init_list), init_list.size() * sizeof(T)); } // Returns the element at the front of the list. - T& front() - { - return data_[0]; - } + T &front() { return data_[0]; } // Returns the element at the back of the list. - T& back() + T &back() { - if (size_ == 0) { return data_[0]; } - + return data_[size_ - 1]; } @@ -93,11 +93,12 @@ class list iterator end() { return iterator(data_ + size_); } // Inserts the passed in element at the passed in - // iterator position. + // iterator position. iterator insert(iterator position, const T &element) { int previous_size = size_; size_++; + T *temp = new T[size_]; // Counter tracks the index of the passed in iterator position. @@ -115,7 +116,7 @@ class list std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); delete[] data_; - + data_ = temp; // Return an iterator at the inserted element. @@ -125,45 +126,50 @@ class list // Insert an element at the end of the list. void push_back(T element) { + int previous_size = size_; size_++; T *temp = new T[size_]; - std::memcpy(temp, data_, previous_size * sizeof(T)); + // Copy the original data into the new array. + for (int i = 0; i < previous_size; i++) + { + temp[i] = data_[i]; + } // Deallocate memory used by previous array. delete[] data_; - + data_ = temp; data_[previous_size] = element; - }; // Insert an element at the front of the list. void push_front(T element) { - int previous_size = size_; size_++; T *temp = new T[size_]; - // Copy the original data, leaving space for one element - // at the front. - std::memcpy(temp + 1, data_, previous_size * sizeof(T)); + // Copy the original data into the new array leaving space + // for the new element at the front. + for (unsigned int i = 1; i < size_; i++) + { + temp[i] = data_[i - 1]; + } + // Deallocate memory used by previous array. delete[] data_; - data_ = temp; - + data_ = temp; data_[0] = element; }; // Returns the last element and removes it from the list. T pop_back() { - T back_elem = data_[size_ - 1]; size_--; @@ -172,6 +178,7 @@ class list // Copy the original data. std::memcpy(temp, data_, size_ * sizeof(T)); + // Deallocate memory used by previous array. delete[] data_; data_ = temp; @@ -190,45 +197,58 @@ class list // Copy the original data. std::memcpy(temp, data_ + 1, size_ * sizeof(T)); + // Deallocate memory used by previous array. delete[] data_; data_ = temp; - return front_elem; } // Returns the number of elements in the list. // int size() { return size_; } - void operator=(const list &other_list) + list &operator=(const list &other_list) { delete[] data_; + data_ = new T[other_list.size_]; - memcpy(data_, other_list.data_, other_list.size_*sizeof(T)); + + memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); + size_ = other_list.size_; + + return *this; } - - // Returns true if the lists are the same, false if they are not + + list(const list &other_list) + { + size_ = other_list.size_; + + data_ = new T[size_]; + + memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); + } + + // Returns true if the list contents are the same, false if they are not bool operator==(const list &other_list) { - if(size_ != other_list.size_){ + if (size_ != other_list.size_) + { return false; } - if( memcmp(data_, other_list.data_, size_) == 0){ - return true; - } - else{ - return false; - } + return (memcmp(data_, other_list.data_, size_) == 0); } T &operator[](int index) { return data_[index]; } + ~list() { delete[] data_; } + private: - T *data_; size_t size_; + + T *data_; }; } // namespace nostd OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index 116661b28d..87dd6f03f2 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -35,19 +35,14 @@ class pair bool operator==(const pair &other_pair) { - if(first_ == other_pair.first_){ - if(second_ == other_pair.second_){ - return true; - } - } - return false; + return (first_ == other_pair.first_ && second_ == other_pair.second_); } // Returns the first element. - T1 & first() { return first_; } + T1 &first() { return first_; } // Returns the second element. - T2 & second() { return second_; } + T2 &second() { return second_; } private: T1 first_; diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index 36106d5fda..4ede6fc2b1 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -1,30 +1,29 @@ #include "opentelemetry/nostd/list.h" #include "opentelemetry/nostd/pair.h" -#include #include +#include using opentelemetry::nostd::list; using opentelemetry::nostd::pair; - // Tests that front() returns the first element and back() // returns the last element. - + TEST(ListTest, FrontBackReturnsProperElements) { - list list_1; + list int_list; - list_1.push_back(9); - list_1.push_back(8); - list_1.push_back(7); - list_1.push_back(6); + int_list.push_back(9); + int_list.push_back(8); + int_list.push_back(7); + int_list.push_back(6); - EXPECT_EQ(list_1.front(), 9); - EXPECT_EQ(list_1.back(), 6); + EXPECT_EQ(int_list.front(), 9); + EXPECT_EQ(int_list.back(), 6); } // Tests that the list object can handle data types of a size -// other than an int. +// other than an int. TEST(ListTest, ListAcceptsPairDatatype) { list> pair_list; @@ -33,12 +32,8 @@ TEST(ListTest, ListAcceptsPairDatatype) pair_list.push_back(pair(2, 3)); pair_list.push_back(pair(4, 5)); - pair temp_pair; - temp_pair = pair_list.front(); - EXPECT_EQ(pair_list.front().first(), 1); EXPECT_EQ(pair_list.front().second(), 2); - EXPECT_EQ(pair_list.back().first(), 4); EXPECT_EQ(pair_list.back().second(), 5); } @@ -46,17 +41,15 @@ TEST(ListTest, ListAcceptsPairDatatype) // Tests that a list object can be initialized with an initalizer list. TEST(ListTest, ListConstructorAcceptsInitializerList) { + list int_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - list pair_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - EXPECT_EQ(pair_list.front(), 1); - EXPECT_EQ(pair_list.back(), 10); + EXPECT_EQ(int_list.front(), 1); + EXPECT_EQ(int_list.back(), 10); } // Tests that the list object can handle an insert_iterator. TEST(ListTest, InsertIteratorAddsToList) { - list int_list = {1, 2, 3, 4}; list insert_list = {5, 6, 7, 8}; @@ -68,12 +61,12 @@ TEST(ListTest, InsertIteratorAddsToList) } int counter = 1; + for (auto iter = std::begin(int_list); iter != std::end(int_list); ++iter) { EXPECT_EQ(*iter, counter); counter++; } - } // Tests that the push_front method added elements to the front of the list. @@ -82,16 +75,34 @@ TEST(ListTest, PushFrontAddsDataAtFront) list> list_list; list inner_list = {0, 1, 2, 3, 4}; list mid_list = {5, 6, 7, 8, 9}; - - list_list.push_front(inner_list); - list_list.push_back(mid_list); + list end_list = {10, 11, 12, 13, 14}; + + list_list.push_front(end_list); + list_list.push_front(mid_list); list_list.push_front(inner_list); EXPECT_EQ(list_list.front().front(), 0); EXPECT_EQ(list_list.front().back(), 4); + EXPECT_EQ(list_list.back().front(), 10); + EXPECT_EQ(list_list.back().back(), 14); +} - EXPECT_EQ(list_list.back().front(), 5); - EXPECT_EQ(list_list.back().back(), 9); +// Tests that the push_back method added elements at the back of the list. +TEST(ListTest, PushBackAddsDataAtBack) +{ + list> list_list; + list inner_list = {0, 1, 2, 3, 4}; + list mid_list = {5, 6, 7, 8, 9}; + list end_list = {10, 11, 12, 13, 14}; + + list_list.push_back(inner_list); + list_list.push_back(mid_list); + list_list.push_back(end_list); + + EXPECT_EQ(list_list.front().front(), 0); + EXPECT_EQ(list_list.front().back(), 4); + EXPECT_EQ(list_list.back().front(), 10); + EXPECT_EQ(list_list.back().back(), 14); } // Tests that you can modify elements via the subscript operator. @@ -109,12 +120,11 @@ TEST(ListTest, SubscriptOperatorAllowsForValueModification) TEST(ListTest, PopBackReturnsAndRemovesFromBack) { list int_list = {0, 1, 2, 3, 4}; + int previous_size = int_list.size(); - int previous_size = int_list.size(); EXPECT_EQ(int_list.back(), 4); EXPECT_EQ(int_list.pop_back(), 4); EXPECT_EQ(int_list.size(), previous_size - 1); - EXPECT_EQ(int_list.back(), 3); } @@ -123,51 +133,48 @@ TEST(ListTest, PopBackReturnsAndRemovesFromBack) TEST(ListTest, PopFrontReturnsAndRemovesFromFront) { list int_list = {0, 1, 2, 3, 4}; + int previous_size = int_list.size(); - int previous_size = int_list.size(); EXPECT_EQ(int_list.front(), 0); EXPECT_EQ(int_list.pop_front(), 0); EXPECT_EQ(int_list.size(), previous_size - 1); - EXPECT_EQ(int_list.front(), 1); } -// Tests that after using the assignment operator, changing the new +// Tests that after using the assignment operator, changing the new // doesn't change the original -TEST(ListTest,AssignmentOperatorDoesNotChangeOriginal) +TEST(ListTest, AssignmentOperatorDoesNotChangeOriginal) { - list first_list = {0, 1, 2, 3, 4}; + list first_list = {0, 1, 2, 3, 4}; list first_list_copy = {0, 1, 2, 3, 4}; - - list second_list = {5, 6, 7, 8, 9}; - second_list = first_list; + list second_list = first_list; + second_list.pop_back(); second_list.pop_back(); second_list.pop_back(); EXPECT_EQ(first_list == first_list_copy, true); EXPECT_EQ(first_list == second_list, false); - } -// Tests that two lists with the same contents return true when compared +// Tests that two lists with the same contents return true when compared // using the comparison operator -TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents){ - - list first_list = {0, 1, 2, 3, 4}; +TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents) +{ + list first_list = {0, 1, 2, 3, 4}; list second_list = {0, 1, 2, 3, 4}; - list third_list = {0, 1, 2, 3, 5}; + list third_list = {0, 1, 2, 3, 5}; + EXPECT_EQ(first_list == second_list, true); EXPECT_NE(first_list == third_list, false); } // Tests that constructing a list with one element populates the list object // with that element -TEST(ListTest, SingleElementConstruction){ +TEST(ListTest, SingleElementConstruction) +{ list single_list = list(5); - + EXPECT_EQ(single_list[0], 5); } - - diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index e8171b2190..342900a30e 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -1,7 +1,11 @@ #include "opentelemetry/nostd/pair.h" +#include "opentelemetry/nostd/list.h" + +#include #include +using opentelemetry::nostd::list; using opentelemetry::nostd::pair; // Tests constructing a pair object. @@ -17,62 +21,76 @@ TEST(PairTest, BasicConstruction) // Tests that the comparison operator returns true when pair values // are the same and returns false when different. -TEST(PairTest, ComparisonSameValuesReturnTrue){ +TEST(PairTest, ComparisonSameValuesReturnTrue) +{ - int val_1 = 1; - int val_2 = 2; + int val_1 = 1; + int val_2 = 2; pair pair_1 = pair(val_1, val_2); pair pair_2 = pair(val_1, val_2); EXPECT_EQ(pair_1 == pair_2, true); - + pair_1.first() = 8; - + EXPECT_EQ(pair_1 == pair_2, false); } // Tests that the assignment operator sets the values as one would expect. -TEST(PairTest, AssignmentBasic){ +TEST(PairTest, AssignmentBasic) +{ - int val_1 = 1; - int val_2 = 2; + int val_1 = 1; + int val_2 = 2; pair pair_1 = pair(val_1, val_2); - - int val_3 = 3; - int val_4 = 4; + + int val_3 = 3; + int val_4 = 4; pair pair_2 = pair(val_3, val_4); - EXPECT_EQ(pair_1 == pair_2, false); + // EXPECT_EQ(pair_1 == pair_2, false); + EXPECT_EQ(pair_1, pair_2); + pair_1 = pair_2; - + EXPECT_EQ(pair_1 == pair_2, true); EXPECT_EQ(pair_1.first(), val_3); EXPECT_EQ(pair_1.second(), val_4); - } -// Tests that after using the assignment operator, changing values of one +// Tests that after using the assignment operator, changing values of one // object does not change both. -TEST(PairTest, AssignmentOtherObjectImmutable){ +TEST(PairTest, AssignmentOtherObjectImmutable) +{ - int val_1 = 1; - int val_2 = 2; + int val_1 = 1; + int val_2 = 2; pair pair_1 = pair(val_1, val_2); - - int val_3 = 3; - int val_4 = 4; + + int val_3 = 3; + int val_4 = 4; pair pair_2 = pair(val_3, val_4); pair_1 = pair_2; - pair_1.first() = 8; + pair_1.first() = 8; pair_1.second() = 7; EXPECT_EQ(pair_1.first(), 8); EXPECT_EQ(pair_1.second(), 7); - EXPECT_EQ(pair_2.first(), val_3); EXPECT_EQ(pair_2.second(), val_4); } +// Tests that the pair class can accept containers +TEST(PairTest, PairAcceptContainers) +{ + list int_list = {1, 2, 3, 4}; + std::string test_string = "test"; + + pair, std::string> container_pair = pair, std::string>(int_list, test_string); + EXPECT_EQ(container_pair.second(), test_string); + EXPECT_EQ(container_pair.first().front(), int_list.front()); + EXPECT_EQ(container_pair.first().back(), int_list.back()); +} From 09b0d5083d222f7416aa6d84b91ec5ea7b9c4094 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Fri, 26 Jun 2020 19:47:07 +0000 Subject: [PATCH 40/93] fixed list comparison error --- api/include/opentelemetry/nostd/list.h | 4 ++-- api/test/nostd/list_test.cc | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h index 59705bb0c4..770a9dc335 100644 --- a/api/include/opentelemetry/nostd/list.h +++ b/api/include/opentelemetry/nostd/list.h @@ -237,8 +237,8 @@ class list { return false; } - - return (memcmp(data_, other_list.data_, size_) == 0); + + return (memcmp(data_, other_list.data_, size_*sizeof(T)) == 0); } T &operator[](int index) { return data_[index]; } diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc index 4ede6fc2b1..41cf8739df 100644 --- a/api/test/nostd/list_test.cc +++ b/api/test/nostd/list_test.cc @@ -6,9 +6,9 @@ using opentelemetry::nostd::list; using opentelemetry::nostd::pair; + // Tests that front() returns the first element and back() // returns the last element. - TEST(ListTest, FrontBackReturnsProperElements) { list int_list; @@ -154,8 +154,8 @@ TEST(ListTest, AssignmentOperatorDoesNotChangeOriginal) second_list.pop_back(); second_list.pop_back(); - EXPECT_EQ(first_list == first_list_copy, true); - EXPECT_EQ(first_list == second_list, false); + EXPECT_TRUE(first_list == first_list_copy); + EXPECT_FALSE(first_list == second_list); } // Tests that two lists with the same contents return true when compared @@ -166,8 +166,8 @@ TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents) list second_list = {0, 1, 2, 3, 4}; list third_list = {0, 1, 2, 3, 5}; - EXPECT_EQ(first_list == second_list, true); - EXPECT_NE(first_list == third_list, false); + EXPECT_TRUE(first_list == second_list); + EXPECT_FALSE(first_list == third_list); } // Tests that constructing a list with one element populates the list object From 6979e81f4c9b95fe0a7359d6f28190044612c753 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Sun, 28 Jun 2020 22:14:05 +0000 Subject: [PATCH 41/93] minor test --- api/include/opentelemetry/nostd/pair.h | 2 +- api/test/nostd/pair_test.cc | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h index 87dd6f03f2..fc3f59a12e 100644 --- a/api/include/opentelemetry/nostd/pair.h +++ b/api/include/opentelemetry/nostd/pair.h @@ -35,7 +35,7 @@ class pair bool operator==(const pair &other_pair) { - return (first_ == other_pair.first_ && second_ == other_pair.second_); + return ((first_ == other_pair.first_) && (second_ == other_pair.second_)); } // Returns the first element. diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc index 342900a30e..a916b5ef97 100644 --- a/api/test/nostd/pair_test.cc +++ b/api/test/nostd/pair_test.cc @@ -29,11 +29,11 @@ TEST(PairTest, ComparisonSameValuesReturnTrue) pair pair_1 = pair(val_1, val_2); pair pair_2 = pair(val_1, val_2); - EXPECT_EQ(pair_1 == pair_2, true); + EXPECT_TRUE(pair_1 == pair_2); pair_1.first() = 8; - EXPECT_EQ(pair_1 == pair_2, false); + EXPECT_FALSE(pair_1 == pair_2); } // Tests that the assignment operator sets the values as one would expect. @@ -48,12 +48,11 @@ TEST(PairTest, AssignmentBasic) int val_4 = 4; pair pair_2 = pair(val_3, val_4); - // EXPECT_EQ(pair_1 == pair_2, false); - EXPECT_EQ(pair_1, pair_2); + EXPECT_FALSE(pair_1 == pair_2); pair_1 = pair_2; - EXPECT_EQ(pair_1 == pair_2, true); + EXPECT_TRUE(pair_1 == pair_2); EXPECT_EQ(pair_1.first(), val_3); EXPECT_EQ(pair_1.second(), val_4); } From 0f870e00405b7faa1b2502455d3a0b14ef65ea29 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Sun, 28 Jun 2020 22:48:12 +0000 Subject: [PATCH 42/93] added CMakeLists.txt to context folder --- api/test/context/CMakeLists.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 api/test/context/CMakeLists.txt diff --git a/api/test/context/CMakeLists.txt b/api/test/context/CMakeLists.txt new file mode 100644 index 0000000000..fdbf8e7b07 --- /dev/null +++ b/api/test/context/CMakeLists.txt @@ -0,0 +1,9 @@ +include(GoogleTest) + +foreach(testname function_ref_test string_view_test unique_ptr_test + utility_test span_test shared_ptr_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) + gtest_add_tests(TARGET ${testname} TEST_PREFIX nostd. TEST_LIST ${testname}) +endforeach() From 3593653898395065c8e0f24cb04e40dc4dff02f8 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 7 Jul 2020 22:33:07 +0000 Subject: [PATCH 43/93] added linked list implementation --- api/include/opentelemetry/context/context.h | 195 ++++++++++-------- .../opentelemetry/context/runtime_context.h | 13 +- .../context/threadlocal_context.h | 20 +- api/test/context/context_test.cc | 122 +++++------ api/test/context/thread_local_test.cc | 12 +- 5 files changed, 190 insertions(+), 172 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 6bc1ebfa8b..1893f5cefe 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -2,9 +2,7 @@ #include #include -#include #include -#include #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/nostd/string_view.h" @@ -19,30 +17,36 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -/* The context class provides a context identifier */ -template ::value> * = nullptr*/> +// 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. - */ + + // 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*/ + + // Returns the key's identifier. nostd::string_view GetIdentifier() { return nostd::string_view(identifier_); } - /*Returns the key's name */ + + //Returns the key's name. nostd::string_view GetName() { return key_name_; } + Key(const Key &other){ + key_name_ = other.key_name_; + memcpy (identifier_, other.identifier_, 50); + } + private: friend class Context; - /* Constructs a new Key with the passed in name. Sets the identifier as - * the address of this object. */ + // 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; @@ -54,110 +58,135 @@ class Context } nostd::string_view key_name_; - + + char name_[50]; + char identifier_[50]; }; + + // A linked list to contain the keys and values of this context node + class DataList { + private: + friend class Context; + + DataList *next; + + char key_[50]; + + common::AttributeValue value_; + }; + - /* 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. - */ - Context(const T &attributes) : key_vals_(attributes) - { + // Creates a key with the passed in name and returns it. + static Key CreateKey(nostd::string_view key_name) { + Key *master = new Key(key_name); + return Key(*master); + } + + Context(){ + next_ = NULL; + head_ = NULL; } - Context() {} - - /* 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 + // Creates a context object from a map of keysand identifiers. + template ::value> * = nullptr> + Context(const T &attributes) { - - std::insert_iterator back(attributes, std::begin(attributes)); - - for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) + auto iter = std::begin(attributes); + + DataList *head = new DataList; + strcpy(head->key_, nostd::string_view(iter->first).data()); + head->value_ = iter->second; + ++iter; + + DataList *previous_node = head; + for (; iter != std::end(attributes); ++iter) { - back = *iter; + DataList *node = new DataList; + + node->next = NULL; + strcpy(node->key_, nostd::string_view(iter->first).data()); + node->value_ = iter->second; + + previous_node->next = node; + previous_node = node; } + + head_ = head; + } - return Context(attributes); + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + template ::value> * = nullptr> + Context WriteValues(T &attributes) noexcept + { + Context *master = new Context(attributes); + master->next_ = this; + return Context(*master); } - /* Returns the value associated with the passed in key */ + // Returns the value associated with the passed in key. common::AttributeValue GetValue(Key key) { - - for (auto iter = std::begin(key_vals_); iter != std::end(key_vals_); ++iter) - { - if (key.GetIdentifier() == iter->first) - { - return iter->second; + Context *context_trace = this; + + while(context_trace != NULL){ + DataList *data_trace = context_trace->head_; + + while(data_trace != NULL){ + if(strncmp(key.GetIdentifier().data(), data_trace->key_, 50) == 0){ + return data_trace->value_; + } + data_trace = data_trace->next; } + + context_trace = context_trace->next_; } - + return ""; } - /* Iterates through the current and comparing context objects - * to check for equality, */ + // 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(next_ != other.next_){ + return false; } + + DataList *trace = head_; + DataList *other_trace = other.head_; + while(trace != NULL){ - 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(other_trace == NULL){ + return false; } - - if (found == 0) - { + + // TODO add value comparison + if((trace->key_ != other_trace->key_) /*|| !((common::AttributeValue)trace->value_ == (common::AttributeValue)other_trace->value_)*/){ return false; } + trace = trace->next; + other_trace = other_trace->next; } return true; } - /* Copy Constructors. */ Context(const Context &other) = default; - Context &operator=(const Context &other) = default; + + Context &operator=(const Context &other){ + head_ = other.head_; + next_ = other.next_; + } private: - T key_vals_; + + // Head of the list which holds the keys and values of this context + DataList *head_; + + // Pointer to the next context object in the context list + Context *next_; }; } // namespace context diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h index b70f4ead1e..f2e93e4eb1 100644 --- a/api/include/opentelemetry/context/runtime_context.h +++ b/api/include/opentelemetry/context/runtime_context.h @@ -8,7 +8,6 @@ namespace context /* The RuntimeContext class provides a wrapper for * propogating context through cpp. */ -template class RuntimeContext { public: @@ -19,27 +18,27 @@ class RuntimeContext class Token { private: - friend class RuntimeContext; + friend class RuntimeContext; - 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; } + Token(Context &context) { context_ = context; } /* Returns the stored context object. */ - Context GetContext() { return context_; } + Context GetContext() { return context_; } }; /* Return the current context. */ - Context GetCurrent(); + Context GetCurrent(); /* 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); /* Resets the context to a previous value stored in the * passed in token. Returns zero if successful, -1 otherwise diff --git a/api/include/opentelemetry/context/threadlocal_context.h b/api/include/opentelemetry/context/threadlocal_context.h index 7ecf2b5709..cc328099cd 100644 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ b/api/include/opentelemetry/context/threadlocal_context.h @@ -5,11 +5,11 @@ 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 +class ThreadLocalContext : public RuntimeContext { public: /* The token class provides an identifier that is used by @@ -19,21 +19,21 @@ class ThreadLocalContext : public RuntimeContext class Token { private: - friend class ThreadLocalContext; + 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; } + Token(Context &context) { context_ = context; } /* Returns the stored context object. */ - Context GetContext() { return context_; } + Context GetContext() { return context_; } }; /* Return the current context. */ - static Context GetCurrent() { return GetInstance(); } + static Context GetCurrent() { return GetInstance(); } /* Resets the context to a previous value stored in the * passed in token. Returns zero if successful, -1 otherwise @@ -54,7 +54,7 @@ class ThreadLocalContext : public RuntimeContext /* 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; @@ -63,9 +63,9 @@ class ThreadLocalContext : public RuntimeContext private: /* Provides storage and access to the thread_local context object */ - static Context &GetInstance() + static Context &GetInstance() { - static thread_local Context instance; + static thread_local Context instance; return instance; } }; diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 2b7a8c0b53..c045c03e80 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -8,141 +8,131 @@ using namespace opentelemetry; -TEST(ContextTest, ContextPairList){ - - using M = nostd::list>; - - context::Context test_context = context::Context(); +// Test ensurs that the context object doe not change when you write to it +TEST(ContextTest, Common) +{ + using M = std::map; + context::Context::Key test_key = context::Context::CreateKey("test_key"); + context::Context::Key foo_key = context::Context::CreateKey("test_key"); - context::Context::Key test_key = test_context.CreateKey("test_key"); - context::Context::Key foo_key = test_context.CreateKey("test_key"); - - //EXPECT_EQ(nostd::get(test_context.GetValue(1)), 2); - //M m1 = {{1,2},{3,4}}; M m1 = {{std::string(test_key.GetIdentifier()), "123"}, {std::string(foo_key.GetIdentifier()), "456"}}; - - //context::Context foo_context = test_context.WriteValues(m1); - //EXPECT_EQ(nostd::get(foo_context.GetValue(test_key)), "123"); - //EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "456"); -} + context::Context test_context = context::Context(); + 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"); +} -/* Test ensurs that the context object doe not change when you write to it */ -TEST(ContextTest, ContextImmutability) +// Tests whether that a Context Object can return a value that was +// written to it. +TEST(ContextTest, ContextGetWrittenValue) { + using M = std::map; - context::Context test_context = context::Context(); + 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("foo_key"); M m1 = {{std::string(test_key.GetIdentifier()), "123"}, {std::string(foo_key.GetIdentifier()), "456"}}; - context::Context foo_context = test_context.WriteValues(m1); + 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"); + EXPECT_EQ(nostd::get(foo_context.GetValue(test_key)), "123"); + EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "456"); } -/* Tests whether the new Context Objects inherits the keys and values - * of the original context object. - */ +// Tests whether the new Context Objects inherits the keys and values +// of the original context object. TEST(ContextTest, ContextInheritance) { using M = std::map; - context::Context test_context = context::Context(); + 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"}}; - 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); 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. - */ +// 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; - context::Context test_context = context::Context(); + 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"}}; - 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); 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. */ +// Tests that copying a context copies the key value pairs properly. TEST(ContextTest, ContextCopy) { using M = std::map; - context::Context test_context = context::Context(); + 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"}}; - context::Context foo_context = test_context.WriteValues(m1); - context::Context other_context = foo_context.WriteValues(m2); - context::Context copied_context = other_context; + 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. */ +// Tests that the comparison compares properly. TEST(ContextTest, ContextCompare) { using M = std::map; - context::Context test_context = context::Context(); + 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"}}; - 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(); - } + context::Context foo_context = test_context.WriteValues(m1); + context::Context other_context = foo_context.WriteValues(m2); + context::Context copied_context = other_context; + + EXPECT_TRUE(copied_context == other_context); + EXPECT_FALSE(copied_context == foo_context); } diff --git a/api/test/context/thread_local_test.cc b/api/test/context/thread_local_test.cc index 8f20b75f2a..075a7b2282 100644 --- a/api/test/context/thread_local_test.cc +++ b/api/test/context/thread_local_test.cc @@ -11,18 +11,18 @@ TEST(ThreadLocalContextTest, ThreadLocalContextAttachDetach) 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"}}; - context::Context foo_context = test_context.WriteValues(m1); + 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); + context::ThreadLocalContext::Token test_token = test_thread_context.Attach(foo_context); EXPECT_EQ(nostd::get(test_thread_context.GetCurrent().GetValue(test_key)), "123"); From 67a7978bf85b98793f338e2a4fcb4084021f38c8 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 7 Jul 2020 22:36:43 +0000 Subject: [PATCH 44/93] fixed cmake file --- api/test/context/CMakeLists.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/api/test/context/CMakeLists.txt b/api/test/context/CMakeLists.txt index fdbf8e7b07..38d48ef0e8 100644 --- a/api/test/context/CMakeLists.txt +++ b/api/test/context/CMakeLists.txt @@ -1,9 +1,8 @@ include(GoogleTest) -foreach(testname function_ref_test string_view_test unique_ptr_test - utility_test span_test shared_ptr_test) +foreach(testname context_test) add_executable(${testname} "${testname}.cc") target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) - gtest_add_tests(TARGET ${testname} TEST_PREFIX nostd. TEST_LIST ${testname}) + gtest_add_tests(TARGET ${testname} TEST_PREFIX context. TEST_LIST ${testname}) endforeach() From cab9bc9fef5b94e17efb9236440fdb4d784699fb Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 7 Jul 2020 22:36:55 +0000 Subject: [PATCH 45/93] removed files --- api/include/opentelemetry/nostd/list.h | 254 ------------------------- api/include/opentelemetry/nostd/pair.h | 52 ----- api/test/nostd/BUILD | 22 --- api/test/nostd/list_test.cc | 180 ------------------ api/test/nostd/pair_test.cc | 95 --------- 5 files changed, 603 deletions(-) delete mode 100644 api/include/opentelemetry/nostd/list.h delete mode 100644 api/include/opentelemetry/nostd/pair.h delete mode 100644 api/test/nostd/list_test.cc delete mode 100644 api/test/nostd/pair_test.cc diff --git a/api/include/opentelemetry/nostd/list.h b/api/include/opentelemetry/nostd/list.h deleted file mode 100644 index 770a9dc335..0000000000 --- a/api/include/opentelemetry/nostd/list.h +++ /dev/null @@ -1,254 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#include "opentelemetry/nostd/utility.h" -#include "opentelemetry/version.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace nostd -{ -// Non-standard implementation of the list data structure. -template -class list -{ -public: - typedef T value_type; - - // Custom iterator class - class iterator - { - public: - iterator operator++() - { - ptr_++; - return ptr_; - } - - T &operator*() { return *ptr_; } - - T *operator->() { return ptr_; } - - bool operator==(const iterator &other) { return ptr_ == other.ptr_; } - - bool operator!=(const iterator &other) { return ptr_ != other.ptr_; } - - private: - friend class list; - - iterator(T *ptr) : ptr_(ptr) {} - - T *ptr_; - }; - - list() - { - size_ = 0; - data_ = nullptr; - }; - - // Constructs class with passed in element. - list(T element) - { - size_ = 1; - - data_ = new T[size_]; - - std::memcpy(data_, &element, sizeof(T)); - }; - - // Constructs class with the passed in initializer_list. - list(std::initializer_list init_list) - { - size_ = init_list.size(); - - data_ = new T[size_]; - - std::memcpy(data_, std::begin(init_list), init_list.size() * sizeof(T)); - } - - // Returns the element at the front of the list. - T &front() { return data_[0]; } - - // Returns the element at the back of the list. - T &back() - { - if (size_ == 0) - { - return data_[0]; - } - - return data_[size_ - 1]; - } - - // Returns an iterator pointing at the front of the list. - iterator begin() { return iterator(data_); } - - // Returns an iterator pointing at the back of the list. - iterator end() { return iterator(data_ + size_); } - - // Inserts the passed in element at the passed in - // iterator position. - iterator insert(iterator position, const T &element) - { - int previous_size = size_; - size_++; - - T *temp = new T[size_]; - - // Counter tracks the index of the passed in iterator position. - int counter = position.ptr_ - data_; - - int insert_index = counter; - int next_index = counter + 1; - int remaining_elements = previous_size - counter; - - // Copy the original data that comes before the insertion. - std::memcpy(temp, data_, insert_index * sizeof(T)); - // Copy the inserted data. - std::memcpy(temp + insert_index, &element, sizeof(T)); - // Copy the original data that comes after the insertion. - std::memcpy(temp + next_index, data_ + counter, remaining_elements * sizeof(T)); - - delete[] data_; - - data_ = temp; - - // Return an iterator at the inserted element. - return iterator(data_ + counter); - } - - // Insert an element at the end of the list. - void push_back(T element) - { - - int previous_size = size_; - size_++; - - T *temp = new T[size_]; - - // Copy the original data into the new array. - for (int i = 0; i < previous_size; i++) - { - temp[i] = data_[i]; - } - - // Deallocate memory used by previous array. - delete[] data_; - - data_ = temp; - - data_[previous_size] = element; - }; - - // Insert an element at the front of the list. - void push_front(T element) - { - size_++; - - T *temp = new T[size_]; - - // Copy the original data into the new array leaving space - // for the new element at the front. - for (unsigned int i = 1; i < size_; i++) - { - temp[i] = data_[i - 1]; - } - - // Deallocate memory used by previous array. - delete[] data_; - - data_ = temp; - data_[0] = element; - }; - - // Returns the last element and removes it from the list. - T pop_back() - { - T back_elem = data_[size_ - 1]; - size_--; - - T *temp = new T[size_]; - - // Copy the original data. - std::memcpy(temp, data_, size_ * sizeof(T)); - - // Deallocate memory used by previous array. - delete[] data_; - - data_ = temp; - - return back_elem; - } - - // Returns the first element and removes it from the list. - T pop_front() - { - T front_elem = front(); - size_--; - - T *temp = new T[size_]; - - // Copy the original data. - std::memcpy(temp, data_ + 1, size_ * sizeof(T)); - - // Deallocate memory used by previous array. - delete[] data_; - - data_ = temp; - - return front_elem; - } - - // Returns the number of elements in the list. // - int size() { return size_; } - - list &operator=(const list &other_list) - { - delete[] data_; - - data_ = new T[other_list.size_]; - - memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); - - size_ = other_list.size_; - - return *this; - } - - list(const list &other_list) - { - size_ = other_list.size_; - - data_ = new T[size_]; - - memcpy(data_, other_list.data_, other_list.size_ * sizeof(T)); - } - - // Returns true if the list contents are the same, false if they are not - bool operator==(const list &other_list) - { - if (size_ != other_list.size_) - { - return false; - } - - return (memcmp(data_, other_list.data_, size_*sizeof(T)) == 0); - } - - T &operator[](int index) { return data_[index]; } - - ~list() { delete[] data_; } - -private: - size_t size_; - - T *data_; -}; -} // namespace nostd -OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/nostd/pair.h b/api/include/opentelemetry/nostd/pair.h deleted file mode 100644 index fc3f59a12e..0000000000 --- a/api/include/opentelemetry/nostd/pair.h +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once - -#include "opentelemetry/nostd/utility.h" -#include "opentelemetry/version.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace nostd -{ - -// The nostd::pair class provides a data structure capable of hold -// two values of different or the same types, and provides access to each. - -template -class pair -{ -public: - pair() - { - first_ = T1(); - second_ = T2(); - } - - // Constructs a pair object with the passed in first and second values. - pair(T1 first, T2 second) - { - first_ = first; - second_ = second; - } - - void operator=(const pair &other_pair) - { - first_ = other_pair.first_; - second_ = other_pair.second_; - } - - bool operator==(const pair &other_pair) - { - return ((first_ == other_pair.first_) && (second_ == other_pair.second_)); - } - - // Returns the first element. - T1 &first() { return first_; } - - // Returns the second element. - T2 &second() { return second_; } - -private: - T1 first_; - T2 second_; -}; -} // namespace nostd -OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/nostd/BUILD b/api/test/nostd/BUILD index e816b63dba..35306e6a3c 100644 --- a/api/test/nostd/BUILD +++ b/api/test/nostd/BUILD @@ -74,25 +74,3 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) - -cc_test( - name = "list_test", - srcs = [ - "list_test.cc", - ], - deps = [ - "//api", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "pair_test", - srcs = [ - "pair_test.cc", - ], - deps = [ - "//api", - "@com_google_googletest//:gtest_main", - ], -) diff --git a/api/test/nostd/list_test.cc b/api/test/nostd/list_test.cc deleted file mode 100644 index 41cf8739df..0000000000 --- a/api/test/nostd/list_test.cc +++ /dev/null @@ -1,180 +0,0 @@ -#include "opentelemetry/nostd/list.h" -#include "opentelemetry/nostd/pair.h" - -#include -#include - -using opentelemetry::nostd::list; -using opentelemetry::nostd::pair; - -// Tests that front() returns the first element and back() -// returns the last element. -TEST(ListTest, FrontBackReturnsProperElements) -{ - list int_list; - - int_list.push_back(9); - int_list.push_back(8); - int_list.push_back(7); - int_list.push_back(6); - - EXPECT_EQ(int_list.front(), 9); - EXPECT_EQ(int_list.back(), 6); -} - -// Tests that the list object can handle data types of a size -// other than an int. -TEST(ListTest, ListAcceptsPairDatatype) -{ - list> pair_list; - - pair_list.push_back(pair(1, 2)); - pair_list.push_back(pair(2, 3)); - pair_list.push_back(pair(4, 5)); - - EXPECT_EQ(pair_list.front().first(), 1); - EXPECT_EQ(pair_list.front().second(), 2); - EXPECT_EQ(pair_list.back().first(), 4); - EXPECT_EQ(pair_list.back().second(), 5); -} - -// Tests that a list object can be initialized with an initalizer list. -TEST(ListTest, ListConstructorAcceptsInitializerList) -{ - list int_list = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - EXPECT_EQ(int_list.front(), 1); - EXPECT_EQ(int_list.back(), 10); -} - -// Tests that the list object can handle an insert_iterator. -TEST(ListTest, InsertIteratorAddsToList) -{ - list int_list = {1, 2, 3, 4}; - list insert_list = {5, 6, 7, 8}; - - std::insert_iterator> back(int_list, std::end(int_list)); - - for (auto iter = std::begin(insert_list); iter != std::end(insert_list); ++iter) - { - back = *iter; - } - - int counter = 1; - - for (auto iter = std::begin(int_list); iter != std::end(int_list); ++iter) - { - EXPECT_EQ(*iter, counter); - counter++; - } -} - -// Tests that the push_front method added elements to the front of the list. -TEST(ListTest, PushFrontAddsDataAtFront) -{ - list> list_list; - list inner_list = {0, 1, 2, 3, 4}; - list mid_list = {5, 6, 7, 8, 9}; - list end_list = {10, 11, 12, 13, 14}; - - list_list.push_front(end_list); - list_list.push_front(mid_list); - list_list.push_front(inner_list); - - EXPECT_EQ(list_list.front().front(), 0); - EXPECT_EQ(list_list.front().back(), 4); - EXPECT_EQ(list_list.back().front(), 10); - EXPECT_EQ(list_list.back().back(), 14); -} - -// Tests that the push_back method added elements at the back of the list. -TEST(ListTest, PushBackAddsDataAtBack) -{ - list> list_list; - list inner_list = {0, 1, 2, 3, 4}; - list mid_list = {5, 6, 7, 8, 9}; - list end_list = {10, 11, 12, 13, 14}; - - list_list.push_back(inner_list); - list_list.push_back(mid_list); - list_list.push_back(end_list); - - EXPECT_EQ(list_list.front().front(), 0); - EXPECT_EQ(list_list.front().back(), 4); - EXPECT_EQ(list_list.back().front(), 10); - EXPECT_EQ(list_list.back().back(), 14); -} - -// Tests that you can modify elements via the subscript operator. -TEST(ListTest, SubscriptOperatorAllowsForValueModification) -{ - list int_list = {0, 1, 2, 3, 4}; - - EXPECT_EQ(int_list[2], 2); - int_list[2] = 9; - EXPECT_EQ(int_list[2], 9); -} - -// Tests that the pop_back method returns and removes the back element -// from the list. -TEST(ListTest, PopBackReturnsAndRemovesFromBack) -{ - list int_list = {0, 1, 2, 3, 4}; - int previous_size = int_list.size(); - - EXPECT_EQ(int_list.back(), 4); - EXPECT_EQ(int_list.pop_back(), 4); - EXPECT_EQ(int_list.size(), previous_size - 1); - EXPECT_EQ(int_list.back(), 3); -} - -// Tests that the pop_front method returns and removes the front element -// from the list. -TEST(ListTest, PopFrontReturnsAndRemovesFromFront) -{ - list int_list = {0, 1, 2, 3, 4}; - int previous_size = int_list.size(); - - EXPECT_EQ(int_list.front(), 0); - EXPECT_EQ(int_list.pop_front(), 0); - EXPECT_EQ(int_list.size(), previous_size - 1); - EXPECT_EQ(int_list.front(), 1); -} - -// Tests that after using the assignment operator, changing the new -// doesn't change the original -TEST(ListTest, AssignmentOperatorDoesNotChangeOriginal) -{ - - list first_list = {0, 1, 2, 3, 4}; - list first_list_copy = {0, 1, 2, 3, 4}; - list second_list = first_list; - - second_list.pop_back(); - second_list.pop_back(); - second_list.pop_back(); - - EXPECT_TRUE(first_list == first_list_copy); - EXPECT_FALSE(first_list == second_list); -} - -// Tests that two lists with the same contents return true when compared -// using the comparison operator -TEST(ListTest, ComparisonOperatureReturnsTrueWithSameContents) -{ - list first_list = {0, 1, 2, 3, 4}; - list second_list = {0, 1, 2, 3, 4}; - list third_list = {0, 1, 2, 3, 5}; - - EXPECT_TRUE(first_list == second_list); - EXPECT_FALSE(first_list == third_list); -} - -// Tests that constructing a list with one element populates the list object -// with that element -TEST(ListTest, SingleElementConstruction) -{ - list single_list = list(5); - - EXPECT_EQ(single_list[0], 5); -} diff --git a/api/test/nostd/pair_test.cc b/api/test/nostd/pair_test.cc deleted file mode 100644 index a916b5ef97..0000000000 --- a/api/test/nostd/pair_test.cc +++ /dev/null @@ -1,95 +0,0 @@ -#include "opentelemetry/nostd/pair.h" -#include "opentelemetry/nostd/list.h" - -#include - -#include - -using opentelemetry::nostd::list; -using opentelemetry::nostd::pair; - -// Tests constructing a pair object. -TEST(PairTest, BasicConstruction) -{ - int val_1 = 1; - int val_2 = 9; - pair temp_pair = pair(val_1, val_2); - - EXPECT_EQ(temp_pair.first(), val_1); - EXPECT_EQ(temp_pair.second(), val_2); -} - -// Tests that the comparison operator returns true when pair values -// are the same and returns false when different. -TEST(PairTest, ComparisonSameValuesReturnTrue) -{ - - int val_1 = 1; - int val_2 = 2; - pair pair_1 = pair(val_1, val_2); - pair pair_2 = pair(val_1, val_2); - - EXPECT_TRUE(pair_1 == pair_2); - - pair_1.first() = 8; - - EXPECT_FALSE(pair_1 == pair_2); -} - -// Tests that the assignment operator sets the values as one would expect. -TEST(PairTest, AssignmentBasic) -{ - - int val_1 = 1; - int val_2 = 2; - pair pair_1 = pair(val_1, val_2); - - int val_3 = 3; - int val_4 = 4; - pair pair_2 = pair(val_3, val_4); - - EXPECT_FALSE(pair_1 == pair_2); - - pair_1 = pair_2; - - EXPECT_TRUE(pair_1 == pair_2); - EXPECT_EQ(pair_1.first(), val_3); - EXPECT_EQ(pair_1.second(), val_4); -} - -// Tests that after using the assignment operator, changing values of one -// object does not change both. -TEST(PairTest, AssignmentOtherObjectImmutable) -{ - - int val_1 = 1; - int val_2 = 2; - pair pair_1 = pair(val_1, val_2); - - int val_3 = 3; - int val_4 = 4; - pair pair_2 = pair(val_3, val_4); - - pair_1 = pair_2; - - pair_1.first() = 8; - pair_1.second() = 7; - - EXPECT_EQ(pair_1.first(), 8); - EXPECT_EQ(pair_1.second(), 7); - EXPECT_EQ(pair_2.first(), val_3); - EXPECT_EQ(pair_2.second(), val_4); -} - -// Tests that the pair class can accept containers -TEST(PairTest, PairAcceptContainers) -{ - list int_list = {1, 2, 3, 4}; - std::string test_string = "test"; - - pair, std::string> container_pair = pair, std::string>(int_list, test_string); - - EXPECT_EQ(container_pair.second(), test_string); - EXPECT_EQ(container_pair.first().front(), int_list.front()); - EXPECT_EQ(container_pair.first().back(), int_list.back()); -} From 6bf0821ab36241ace29013df23c93e35e9e12838 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 14:23:38 +0000 Subject: [PATCH 46/93] Added internal constructors and functions to context.h, and well as modified tests --- api/include/opentelemetry/context/context.h | 194 +++++++++++--------- api/test/context/context_test.cc | 168 +++++++---------- 2 files changed, 172 insertions(+), 190 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 1893f5cefe..d1e88ef757 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -5,6 +5,7 @@ #include #include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/utility.h" #include "opentelemetry/nostd/variant.h" @@ -17,127 +18,83 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -// The context class provides a context identifier. +// The context class provides a context identifier. Is built as a linked list +// of context nodes which each have access to the nodes before it, but not +// after. So each time a write is performed a new context is added to the list. +// Within each context node there is another linked list which holds the key +// and value data for each node. 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 - { - - 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_; } - - Key(const Key &other){ - key_name_ = other.key_name_; - memcpy (identifier_, other.identifier_, 50); - } - - 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(); - - memcpy(identifier_, temp_view.data(), temp_view.size()); - } - - nostd::string_view key_name_; - - char name_[50]; - - char identifier_[50]; - }; // A linked list to contain the keys and values of this context node class DataList { private: friend class Context; - DataList *next; + DataList *next_; char key_[50]; - common::AttributeValue value_; + ContextValue value_; }; - - // Creates a key with the passed in name and returns it. - static Key CreateKey(nostd::string_view key_name) { - Key *master = new Key(key_name); - return Key(*master); - } - Context(){ next_ = NULL; head_ = NULL; } - // Creates a context object from a map of keysand identifiers. + // Creates a context object from a map of keys and identifiers, this will + // be the head of the context object linked list. template ::value> * = nullptr> - Context(const T &attributes) + Context(const T &keys_and_values) { - auto iter = std::begin(attributes); - - DataList *head = new DataList; - strcpy(head->key_, nostd::string_view(iter->first).data()); - head->value_ = iter->second; - ++iter; - - DataList *previous_node = head; - for (; iter != std::end(attributes); ++iter) - { - DataList *node = new DataList; - - node->next = NULL; - strcpy(node->key_, nostd::string_view(iter->first).data()); - node->value_ = iter->second; - - previous_node->next = node; - previous_node = node; - } - - head_ = head; + head_ = BuildList(keys_and_values); + next_ = NULL; + } + + // Creates a context object from a key and value, this will be the head + // of the context object linked list. + Context(nostd::string_view key, ContextValue value) + { + head_ = BuildHead(key, value); + next_ = NULL; } // Accepts a new iterable and then returns a new context that // contains the new key and value data. template ::value> * = nullptr> - Context WriteValues(T &attributes) noexcept + Context* SetValues(T &values) noexcept + { + Context *new_node = new Context(values, this); + return new_node; + } + + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + Context* SetValue(nostd::string_view key, ContextValue value) noexcept { - Context *master = new Context(attributes); - master->next_ = this; - return Context(*master); + Context *new_node = new Context(key,value, this); + return new_node; } // Returns the value associated with the passed in key. - common::AttributeValue GetValue(Key key) + common::AttributeValue GetValue(nostd::string_view key) { Context *context_trace = this; - + + //Iterate through the context nodes while(context_trace != NULL){ DataList *data_trace = context_trace->head_; + //Iterate through the internal data nodes while(data_trace != NULL){ - if(strncmp(key.GetIdentifier().data(), data_trace->key_, 50) == 0){ + if(strncmp(key.data(), data_trace->key_, 50) == 0){ return data_trace->value_; } - data_trace = data_trace->next; + + data_trace = data_trace->next_; } context_trace = context_trace->next_; @@ -147,27 +104,33 @@ class Context } // Iterates through the current and comparing context objects - // to check for equality, + // to check for equality. bool operator==(const Context &other) { + // If the two contexts are not at the place in the list + // they cannot be equivalent if(next_ != other.next_){ return false; } DataList *trace = head_; DataList *other_trace = other.head_; + + // Iterate through the lists and compare the keys + // TODO should also compare the values but the nostd::variant + // seems to not have a comparison operator while(trace != NULL){ if(other_trace == NULL){ return false; } - // TODO add value comparison - if((trace->key_ != other_trace->key_) /*|| !((common::AttributeValue)trace->value_ == (common::AttributeValue)other_trace->value_)*/){ + if((trace->key_ != other_trace->key_)){ return false; } - trace = trace->next; - other_trace = other_trace->next; + + trace = trace->next_; + other_trace = other_trace->next_; } return true; @@ -187,6 +150,63 @@ class Context // Pointer to the next context object in the context list Context *next_; + + // Builds a data list off of a key and value iterable and returns the head + template ::value> * = nullptr> + DataList* BuildList(const T &keys_and_vals){ + auto iter = std::begin(keys_and_vals); + + // Create list head + DataList *head = new DataList; + strcpy(head->key_, nostd::string_view(iter->first).data()); + head->value_ = iter->second; + head->next_ = NULL; + ++iter; + + DataList *previous_node = head; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) + { + DataList *node = new DataList; + node->next_ = NULL; + strcpy(node->key_, nostd::string_view(iter->first).data()); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = node; + } + + return head; + } + + // Builds a data list with just a key and value, so it will just be the head + // and returns that head. + DataList* BuildHead(nostd::string_view key, ContextValue value){ + DataList *head = new DataList; + strcpy(head->key_, key.data()); + head->value_ = value; + head->next_ = NULL; + return head; + } + + + // Creates a context object from a map of keys and identifiers, an internal + // contructor only, will always create a node within the list, not at the + // head. + template ::value> * = nullptr> + Context(const T &attributes, Context* next_node) + { + head_ = BuildList(attributes); + next_ = next_node; + } + + // Creates a context object from a key and value. An internal + // contructor only, will always create a node within the list, not at the + // head. + Context(nostd::string_view key, ContextValue value, Context* next_node) + { + head_ = BuildHead(key, value); + next_ = next_node; + } }; } // namespace context diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index c045c03e80..8e5f04230a 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -1,138 +1,100 @@ -#include - #include "opentelemetry/context/context.h" -#include "opentelemetry/nostd/list.h" -#include "opentelemetry/nostd/pair.h" + +#include #include using namespace opentelemetry; -// Test ensurs that the context object doe not change when you write to it -TEST(ContextTest, Common) +// Tests that the context constructor accepts an std::map. +TEST(ContextTest, ContextIterableAcceptsMap) { - using M = std::map; - context::Context::Key test_key = context::Context::CreateKey("test_key"); - context::Context::Key foo_key = context::Context::CreateKey("test_key"); - - M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; - - context::Context test_context = context::Context(); - 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"); + std::map map_test = {{"test_key", "123"}}; + context::Context context_test = context::Context(map_test); } -// Tests whether that a Context Object can return a value that was -// written to it. -TEST(ContextTest, ContextGetWrittenValue) +// Tests that the GetValue method returns the expected value. +TEST(ContextTest, ContextGetValueReturnsExpectedValue) { + std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - 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"); - - M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; - - context::Context foo_context = test_context.WriteValues(m1); - - EXPECT_EQ(nostd::get(foo_context.GetValue(test_key)), "123"); - EXPECT_EQ(nostd::get(foo_context.GetValue(foo_key)), "456"); + context::Context context_test = context::Context(map_test); + EXPECT_EQ(nostd::get(context_test.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(context_test.GetValue("foo_key")), "456"); } -// Tests whether the new Context Objects inherits the keys and values -// of the original context object. -TEST(ContextTest, ContextInheritance) +// Tests that the SetValues method accepts an std::map. +TEST(ContextTest, ContextSetValuesAcceptsMap) { + std::map map_test = {{"test_key", "123"}}; + std::map map_test_write = {{"foo_key", "456"}}; + context::Context context_test = context::Context(map_test); + context::Context* foo_test = context_test.SetValues(map_test_write); + EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); +} - 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"); - - M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; - M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; +// Tests that the SetValues method accepts a nostd::string_view and +// context::ContextValue. +TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) +{ + nostd::string_view string_view_test = "string_view"; + context::ContextValue context_value_test = "123"; + context::Context context_test = context::Context(); + context::Context* context_foo = context_test.SetValue(string_view_test, context_value_test); + EXPECT_EQ(nostd::get(context_foo->GetValue(string_view_test)), "123"); +} - context::Context foo_context = test_context.WriteValues(m1); - context::Context other_context = foo_context.WriteValues(m2); +// Tests that the original context does not change when a value is +// written to it. +TEST(ContextTest, ContextImmutability) +{ + std::map map_test = {{"test_key", "123"}}; + context::Context context_test = context::Context(map_test); - EXPECT_EQ(nostd::get(other_context.GetValue(test_key)), "123"); - EXPECT_EQ(nostd::get(other_context.GetValue(foo_key)), "456"); + context::Context* context_foo = context_test.SetValue("foo_key", "456"); + + EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), + "456"); } -// Tests that when you add a key value pair where the key is already in -// existance, they key is overwritten. +// Tests that writing the same to a context overwrites the original value. TEST(ContextTest, ContextKeyOverwrite) { + std::map map_test = {{"test_key", "123"}}; + context::Context context_test = context::Context(map_test); + context::Context* context_foo = context_test.SetValue("test_key", "456"); - 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"); - - 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"); + EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); } -// Tests that copying a context copies the key value pairs properly. -TEST(ContextTest, ContextCopy) +// Tests that the new Context Objects inherits the keys and values +// of the original context object. +TEST(ContextTest, ContextInheritance) { - - using M = std::map; + 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"); - - M m1 = {{std::string(test_key.GetIdentifier()), "123"}, - {std::string(foo_key.GetIdentifier()), "456"}}; - M m2 = {{std::string(other_key.GetIdentifier()), "000"}}; + M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; + M m2 = {{"other_key", "789"}}; - context::Context foo_context = test_context.WriteValues(m1); - context::Context other_context = foo_context.WriteValues(m2); - context::Context copied_context = other_context; + context::Context* foo_context = test_context.SetValues(m1); + context::Context* other_context = foo_context->SetValues(m2); - EXPECT_EQ(nostd::get(copied_context.GetValue(test_key)), "123"); - EXPECT_EQ(nostd::get(copied_context.GetValue(foo_key)), "456"); + EXPECT_EQ(nostd::get(other_context->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(other_context->GetValue("foo_key")), "456"); } -// Tests that the comparison compares properly. -TEST(ContextTest, ContextCompare) +// Tests that copying a context copies the key value pairs as expected. +TEST(ContextTest, ContextCopyOperator) { + std::map test_map = { + {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; - 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 test_context = context::Context(test_map); + context::Context copied_context = test_context; - 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_TRUE(copied_context == other_context); - EXPECT_FALSE(copied_context == foo_context); + EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); } From ada7c68980effd894fc5b4dee0bc8c48e257c4c6 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 14:24:00 +0000 Subject: [PATCH 47/93] Added context_value.h --- .../opentelemetry/context/context_value.h | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 api/include/opentelemetry/context/context_value.h diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h new file mode 100644 index 0000000000..9527524fed --- /dev/null +++ b/api/include/opentelemetry/context/context_value.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include "opentelemetry/nostd/span.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/variant.h" +#include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace context +{ + using ContextValue = nostd::variant, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span>; +} // namespace context +OPENTELEMETRY_END_NAMESPACE From 134d6b40dc18ef87cb437d88bab39c3b28bb3c73 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 14:26:11 +0000 Subject: [PATCH 48/93] Temp removing files. --- .../opentelemetry/context/runtime_context.h | 49 ------------- .../context/threadlocal_context.h | 73 ------------------- api/test/context/thread_local_test.cc | 34 --------- 3 files changed, 156 deletions(-) delete mode 100644 api/include/opentelemetry/context/runtime_context.h delete mode 100644 api/include/opentelemetry/context/threadlocal_context.h delete mode 100644 api/test/context/thread_local_test.cc diff --git a/api/include/opentelemetry/context/runtime_context.h b/api/include/opentelemetry/context/runtime_context.h deleted file mode 100644 index f2e93e4eb1..0000000000 --- a/api/include/opentelemetry/context/runtime_context.h +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -#include "context.h" - -OPENTELEMETRY_BEGIN_NAMESPACE -namespace context -{ - -/* 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. - */ - 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 deleted file mode 100644 index cc328099cd..0000000000 --- a/api/include/opentelemetry/context/threadlocal_context.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include "opentelemetry/context/runtime_context.h" - -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. */ -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 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. */ - 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; - } - - /* 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/thread_local_test.cc b/api/test/context/thread_local_test.cc deleted file mode 100644 index 075a7b2282..0000000000 --- a/api/test/context/thread_local_test.cc +++ /dev/null @@ -1,34 +0,0 @@ - -#include "opentelemetry/context/threadlocal_context.h" - -#include - -using namespace opentelemetry; - -/* Tests whether the ThreadLocalContext attaches and detaches as expected */ -TEST(ThreadLocalContextTest, ThreadLocalContextAttachDetach) -{ - - 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 = {{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::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 00aee4c2ce367cf593155424ab4ac0ab80478abf Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 14:27:46 +0000 Subject: [PATCH 49/93] temp removed more files to pare down PR --- api/test/CMakeLists.txt | 5 -- api/test/context/BUILD | 23 ------- api/test/context/CMakeLists.txt | 8 --- api/test/context/context_test.cc | 100 ------------------------------- 4 files changed, 136 deletions(-) delete mode 100644 api/test/CMakeLists.txt delete mode 100644 api/test/context/BUILD delete mode 100644 api/test/context/CMakeLists.txt delete mode 100644 api/test/context/context_test.cc diff --git a/api/test/CMakeLists.txt b/api/test/CMakeLists.txt deleted file mode 100644 index 5c5a8590e4..0000000000 --- a/api/test/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -add_subdirectory(core) -add_subdirectory(context) -add_subdirectory(plugin) -add_subdirectory(nostd) -add_subdirectory(trace) diff --git a/api/test/context/BUILD b/api/test/context/BUILD deleted file mode 100644 index 68363db1c9..0000000000 --- a/api/test/context/BUILD +++ /dev/null @@ -1,23 +0,0 @@ -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 = "thread_local_test", - srcs = [ - "thread_local_test.cc", - ], - deps = [ - "//api", - "@com_google_googletest//:gtest_main", - ], -) diff --git a/api/test/context/CMakeLists.txt b/api/test/context/CMakeLists.txt deleted file mode 100644 index 38d48ef0e8..0000000000 --- a/api/test/context/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -include(GoogleTest) - -foreach(testname context_test) - add_executable(${testname} "${testname}.cc") - target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} - ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) - gtest_add_tests(TARGET ${testname} TEST_PREFIX context. TEST_LIST ${testname}) -endforeach() diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc deleted file mode 100644 index 8e5f04230a..0000000000 --- a/api/test/context/context_test.cc +++ /dev/null @@ -1,100 +0,0 @@ -#include "opentelemetry/context/context.h" - -#include - -#include - -using namespace opentelemetry; - -// Tests that the context constructor accepts an std::map. -TEST(ContextTest, ContextIterableAcceptsMap) -{ - std::map map_test = {{"test_key", "123"}}; - context::Context context_test = context::Context(map_test); -} - -// Tests that the GetValue method returns the expected value. -TEST(ContextTest, ContextGetValueReturnsExpectedValue) -{ - std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - - context::Context context_test = context::Context(map_test); - EXPECT_EQ(nostd::get(context_test.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(context_test.GetValue("foo_key")), "456"); -} - -// Tests that the SetValues method accepts an std::map. -TEST(ContextTest, ContextSetValuesAcceptsMap) -{ - std::map map_test = {{"test_key", "123"}}; - std::map map_test_write = {{"foo_key", "456"}}; - context::Context context_test = context::Context(map_test); - context::Context* foo_test = context_test.SetValues(map_test_write); - EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); -} - -// Tests that the SetValues method accepts a nostd::string_view and -// context::ContextValue. -TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) -{ - nostd::string_view string_view_test = "string_view"; - context::ContextValue context_value_test = "123"; - context::Context context_test = context::Context(); - context::Context* context_foo = context_test.SetValue(string_view_test, context_value_test); - EXPECT_EQ(nostd::get(context_foo->GetValue(string_view_test)), "123"); -} - -// Tests that the original context does not change when a value is -// written to it. -TEST(ContextTest, ContextImmutability) -{ - std::map map_test = {{"test_key", "123"}}; - context::Context context_test = context::Context(map_test); - - context::Context* context_foo = context_test.SetValue("foo_key", "456"); - - EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), - "456"); -} - -// Tests that writing the same to a context overwrites the original value. -TEST(ContextTest, ContextKeyOverwrite) -{ - std::map map_test = {{"test_key", "123"}}; - context::Context context_test = context::Context(map_test); - context::Context* context_foo = context_test.SetValue("test_key", "456"); - - EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); -} - -// Tests that the new Context Objects inherits the keys and values -// of the original context object. -TEST(ContextTest, ContextInheritance) -{ - using M = std::map; - context::Context test_context = context::Context(); - - M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; - M m2 = {{"other_key", "789"}}; - - context::Context* foo_context = test_context.SetValues(m1); - context::Context* other_context = foo_context->SetValues(m2); - - EXPECT_EQ(nostd::get(other_context->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(other_context->GetValue("foo_key")), "456"); -} - -// Tests that copying a context copies the key value pairs as expected. -TEST(ContextTest, ContextCopyOperator) -{ - std::map test_map = { - {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; - - context::Context test_context = context::Context(test_map); - context::Context copied_context = test_context; - - EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); - EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); -} From 9a42949cba2c7693a4601a4061136d365bc7781e Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 14:30:01 +0000 Subject: [PATCH 50/93] re-adding CMakeLists.txt --- api/test/CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 api/test/CMakeLists.txt diff --git a/api/test/CMakeLists.txt b/api/test/CMakeLists.txt new file mode 100644 index 0000000000..85f3032c8a --- /dev/null +++ b/api/test/CMakeLists.txt @@ -0,0 +1,4 @@ + add_subdirectory(core) + add_subdirectory(plugin) + add_subdirectory(nostd) + add_subdirectory(trace) From d8432dd8d4907abd00f0ed6a9e77b31c7545ca12 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 14:48:58 +0000 Subject: [PATCH 51/93] removed old includes --- api/include/opentelemetry/context/context.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index d1e88ef757..f98418e28e 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,18 +1,10 @@ #pragma once #include -#include -#include -#include "opentelemetry/common/attribute_value.h" #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/nostd/utility.h" -#include "opentelemetry/nostd/variant.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 From 00ed02aa7c539d5c52884a8c4204d9e5b2cc21c3 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 17:30:47 +0000 Subject: [PATCH 52/93] fixed destruction, disallowed copying and add DataList constructors --- api/include/opentelemetry/context/context.h | 370 ++++++++++---------- 1 file changed, 188 insertions(+), 182 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index f98418e28e..3ad8257796 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/string_view.h" @@ -10,196 +11,201 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -// The context class provides a context identifier. Is built as a linked list -// of context nodes which each have access to the nodes before it, but not -// after. So each time a write is performed a new context is added to the list. -// Within each context node there is another linked list which holds the key -// and value data for each node. -class Context -{ - -public: - - // A linked list to contain the keys and values of this context node - class DataList { - private: - friend class Context; - - DataList *next_; - - char key_[50]; - - ContextValue value_; - }; - - Context(){ - next_ = NULL; - head_ = NULL; - } - - // Creates a context object from a map of keys and identifiers, this will - // be the head of the context object linked list. - template ::value> * = nullptr> - Context(const T &keys_and_values) - { - head_ = BuildList(keys_and_values); - next_ = NULL; - } - - // Creates a context object from a key and value, this will be the head - // of the context object linked list. - Context(nostd::string_view key, ContextValue value) + // The context class provides a context identifier. Is built as a linked list + // of context nodes which each have access to the nodes before it, but not + // after. So each time a write is performed a new context is added to the list. + // Within each context node there is another linked list which holds the key + // and value data for each node. + class Context { - head_ = BuildHead(key, value); - next_ = NULL; - } - - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. - template ::value> * = nullptr> - Context* SetValues(T &values) noexcept - { - Context *new_node = new Context(values, this); - return new_node; - } - - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. - Context* SetValue(nostd::string_view key, ContextValue value) noexcept - { - Context *new_node = new Context(key,value, this); - return new_node; - } - // Returns the value associated with the passed in key. - common::AttributeValue GetValue(nostd::string_view key) - { - Context *context_trace = this; - - //Iterate through the context nodes - while(context_trace != NULL){ - DataList *data_trace = context_trace->head_; - - //Iterate through the internal data nodes - while(data_trace != NULL){ - if(strncmp(key.data(), data_trace->key_, 50) == 0){ - return data_trace->value_; + public: + + // A linked list to contain the keys and values of this context node + class DataList { + private: + friend class Context; + + DataList *next_; + + char key_[50]; + + ContextValue value_; + + DataList(){ + next_ = nullptr; + } + + // Builds a data list off of a key and value iterable and returns the head + template ::value> * = nullptr> + DataList(const T &keys_and_vals){ + auto iter = std::begin(keys_and_vals); + + // Create list head + strcpy(key_, nostd::string_view(iter->first).data()); + value_ = iter->second; + next_ = nullptr; + ++iter; + + DataList *previous_node = this; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) + { + DataList *node = new DataList(); + node->next_ = nullptr; + strcpy(node->key_, nostd::string_view(iter->first).data()); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = node; + } + } + + // Builds a data list with just a key and value, so it will just be the head + // and returns that head. + DataList(nostd::string_view key, ContextValue value){ + DataList *head = new DataList; + strcpy(key_, key.data()); + value_ = value; + next_ = nullptr; + } + }; + + Context(){ + next_ = nullptr; + head_ = nullptr; + } + + // Creates a context object from a map of keys and identifiers, this will + // be the head of the context object linked list. + template ::value> * = nullptr> + Context(const T &keys_and_values) + { + head_ = new DataList(keys_and_values); + next_ = nullptr; } - - data_trace = data_trace->next_; + + // Creates a context object from a key and value, this will be the head + // of the context object linked list. + Context(nostd::string_view key, ContextValue value) + { + head_ = new DataList(key, value); + next_ = nullptr; } - - context_trace = context_trace->next_; - } - - return ""; - } - - // Iterates through the current and comparing context objects - // to check for equality. - bool operator==(const Context &other) - { - // If the two contexts are not at the place in the list - // they cannot be equivalent - if(next_ != other.next_){ - return false; - } - - DataList *trace = head_; - DataList *other_trace = other.head_; - - // Iterate through the lists and compare the keys - // TODO should also compare the values but the nostd::variant - // seems to not have a comparison operator - while(trace != NULL){ - - if(other_trace == NULL){ - return false; + + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + template ::value> * = nullptr> + Context* SetValues(T &values) noexcept + { + return new Context(values, this); + } + + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + Context* SetValue(nostd::string_view key, ContextValue value) noexcept + { + return new Context(key, value, this); + } + + // Returns the value associated with the passed in key. + common::AttributeValue GetValue(nostd::string_view key) + { + Context *context_trace = this; + + //Iterate through the context nodes + while(context_trace != NULL){ + DataList *data_trace = context_trace->head_; + + //Iterate through the internal data nodes + while(data_trace != NULL){ + if(strncmp(key.data(), data_trace->key_, 50) == 0){ + return data_trace->value_; + } + + data_trace = data_trace->next_; + } + + context_trace = context_trace->next_; + } + + return ""; } + + // Iterates through the current and comparing context objects + // to check for equality. + bool operator==(const Context &other) + { + // If the two contexts are not at the place in the list + // they cannot be equivalent + if(next_ != other.next_){ + return false; + } + + DataList *trace = head_; + DataList *other_trace = other.head_; + + // Iterate through the lists and compare the keys + // TODO should also compare the values but the nostd::variant + // seems to not have a comparison operator + while(trace != NULL){ + + if(other_trace == NULL){ + return false; + } + + if((trace->key_ != other_trace->key_)){ + return false; + } + + trace = trace->next_; + other_trace = other_trace->next_; + } + + return true; + } + + Context(const Context &other) = delete; + + Context &operator=(const Context &other) = delete; - if((trace->key_ != other_trace->key_)){ - return false; + ~Context(){ + DataList *trace = head_; + + while(trace != nullptr){ + DataList * next = trace->next_; + delete trace; + trace = next; + } + } - trace = trace->next_; - other_trace = other_trace->next_; - } - - return true; - } - - Context(const Context &other) = default; - - Context &operator=(const Context &other){ - head_ = other.head_; - next_ = other.next_; - } - -private: - - // Head of the list which holds the keys and values of this context - DataList *head_; - - // Pointer to the next context object in the context list - Context *next_; - - // Builds a data list off of a key and value iterable and returns the head - template ::value> * = nullptr> - DataList* BuildList(const T &keys_and_vals){ - auto iter = std::begin(keys_and_vals); - - // Create list head - DataList *head = new DataList; - strcpy(head->key_, nostd::string_view(iter->first).data()); - head->value_ = iter->second; - head->next_ = NULL; - ++iter; - - DataList *previous_node = head; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) - { - DataList *node = new DataList; - node->next_ = NULL; - strcpy(node->key_, nostd::string_view(iter->first).data()); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = node; - } - - return head; - } - - // Builds a data list with just a key and value, so it will just be the head - // and returns that head. - DataList* BuildHead(nostd::string_view key, ContextValue value){ - DataList *head = new DataList; - strcpy(head->key_, key.data()); - head->value_ = value; - head->next_ = NULL; - return head; - } - - - // Creates a context object from a map of keys and identifiers, an internal - // contructor only, will always create a node within the list, not at the - // head. - template ::value> * = nullptr> - Context(const T &attributes, Context* next_node) - { - head_ = BuildList(attributes); - next_ = next_node; - } - - // Creates a context object from a key and value. An internal - // contructor only, will always create a node within the list, not at the - // head. - Context(nostd::string_view key, ContextValue value, Context* next_node) - { - head_ = BuildHead(key, value); - next_ = next_node; - } -}; + private: + + // Head of the list which holds the keys and values of this context + DataList *head_; + + // Pointer to the next context object in the context list + Context *next_; + + // Creates a context object from a map of keys and identifiers, an internal + // contructor only, will always create a node within the list, not at the + // head. + template ::value> * = nullptr> + Context(const T &attributes, Context* next_node) + { + head_ = new DataList(attributes); + next_ = next_node; + } + + // Creates a context object from a key and value. An internal + // contructor only, will always create a node within the list, not at the + // head. + Context(nostd::string_view key, ContextValue value, Context* next_node) + { + head_ = new DataList(key, value); + next_ = next_node; + } + }; } // namespace context OPENTELEMETRY_END_NAMESPACE From d3d6b30065d377fb49648e807c2733845166de7e Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 18:31:18 +0000 Subject: [PATCH 53/93] Stylistic changes --- api/include/opentelemetry/context/context.h | 174 ++++++++++---------- 1 file changed, 84 insertions(+), 90 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 3ad8257796..117dba31fd 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -21,59 +21,8 @@ namespace context public: - // A linked list to contain the keys and values of this context node - class DataList { - private: - friend class Context; - - DataList *next_; - - char key_[50]; - - ContextValue value_; - - DataList(){ - next_ = nullptr; - } - - // Builds a data list off of a key and value iterable and returns the head - template ::value> * = nullptr> - DataList(const T &keys_and_vals){ - auto iter = std::begin(keys_and_vals); - - // Create list head - strcpy(key_, nostd::string_view(iter->first).data()); - value_ = iter->second; - next_ = nullptr; - ++iter; - - DataList *previous_node = this; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) - { - DataList *node = new DataList(); - node->next_ = nullptr; - strcpy(node->key_, nostd::string_view(iter->first).data()); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = node; - } - } - // Builds a data list with just a key and value, so it will just be the head - // and returns that head. - DataList(nostd::string_view key, ContextValue value){ - DataList *head = new DataList; - strcpy(key_, key.data()); - value_ = value; - next_ = nullptr; - } - }; - - Context(){ - next_ = nullptr; - head_ = nullptr; - } + Context() = default; // Creates a context object from a map of keys and identifiers, this will // be the head of the context object linked list. @@ -81,7 +30,6 @@ namespace context Context(const T &keys_and_values) { head_ = new DataList(keys_and_values); - next_ = nullptr; } // Creates a context object from a key and value, this will be the head @@ -89,7 +37,6 @@ namespace context Context(nostd::string_view key, ContextValue value) { head_ = new DataList(key, value); - next_ = nullptr; } // Accepts a new iterable and then returns a new context that @@ -110,24 +57,15 @@ namespace context // Returns the value associated with the passed in key. common::AttributeValue GetValue(nostd::string_view key) { - Context *context_trace = this; - //Iterate through the context nodes - while(context_trace != NULL){ - DataList *data_trace = context_trace->head_; - + for(Context *context = this; context != nullptr; context = context->next_){ //Iterate through the internal data nodes - while(data_trace != NULL){ - if(strncmp(key.data(), data_trace->key_, 50) == 0){ - return data_trace->value_; + for(DataList *data = context->head_; data != nullptr; data = data->next_){ + if(strncmp(key.data(), data->key_, data->key_length_) == 0){ + return data->value_; } - - data_trace = data_trace->next_; } - - context_trace = context_trace->next_; } - return ""; } @@ -141,59 +79,109 @@ namespace context return false; } - DataList *trace = head_; - DataList *other_trace = other.head_; + DataList *other_data = other.head_; // Iterate through the lists and compare the keys // TODO should also compare the values but the nostd::variant // seems to not have a comparison operator - while(trace != NULL){ - - if(other_trace == NULL){ + for(DataList *data = head_; data != nullptr; data = data->next_){ + if(other_data == nullptr){ return false; } - if((trace->key_ != other_trace->key_)){ + if((data->key_ != other_data->key_)){ return false; } - trace = trace->next_; - other_trace = other_trace->next_; + data = data->next_; + other_data = other_data->next_; } return true; } - Context(const Context &other) = delete; + Context(const Context &other) = default; Context &operator=(const Context &other) = delete; - + ~Context(){ - DataList *trace = head_; - - while(trace != nullptr){ - DataList * next = trace->next_; - delete trace; - trace = next; + for(DataList* data = head_; data != nullptr;){ + DataList * next = data->next_; + delete data; + data = next; } - } private: - // Head of the list which holds the keys and values of this context - DataList *head_; - // Pointer to the next context object in the context list - Context *next_; + // A linked list to contain the keys and values of this context node + class DataList { + public: + + DataList *next_; + + char *key_; + + int key_length_; + + ContextValue value_; + + DataList(){ + next_ = nullptr; + } + + // Builds a data list off of a key and value iterable and returns the head + template ::value> * = nullptr> + DataList(const T &keys_and_vals){ + auto iter = std::begin(keys_and_vals); + + // Create list head + key_ = new char[nostd::string_view(iter->first).size()]; + key_length_ = nostd::string_view(iter->first).size(); + strncpy(key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size()); + value_ = iter->second; + next_ = nullptr; + ++iter; + + DataList *previous_node = this; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) + { + DataList *node = new DataList(); + node->next_ = nullptr; + node->key_ = new char[nostd::string_view(iter->first).size()]; + node->key_length_ = nostd::string_view(iter->first).size(); + strncpy(node->key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size()); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = node; + } + } + + // Builds a data list with just a key and value, so it will just be the head + // and returns that head. + DataList(nostd::string_view key, ContextValue value){ + DataList *head = new DataList; + key_ = new char[nostd::string_view(key).size()]; + key_length_ = nostd::string_view(key).size(); + strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); + value_ = value; + next_ = nullptr; + } + + ~DataList(){ + delete [] key_; + } + }; // Creates a context object from a map of keys and identifiers, an internal // contructor only, will always create a node within the list, not at the // head. template ::value> * = nullptr> - Context(const T &attributes, Context* next_node) + Context(const T &keys_and_vals, Context* next_node) { - head_ = new DataList(attributes); + head_ = new DataList(keys_and_vals); next_ = next_node; } @@ -205,6 +193,12 @@ namespace context head_ = new DataList(key, value); next_ = next_node; } + + // Head of the list which holds the keys and values of this context + DataList *head_ = nullptr; + + // Pointer to the next context object in the context list + Context *next_ = nullptr; }; } // namespace context From 5c59f5cc8fdd9c033d5d611eef4d94c508105697 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 18:31:30 +0000 Subject: [PATCH 54/93] Stylistic changes --- api/include/opentelemetry/context/context.h | 1 - 1 file changed, 1 deletion(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 117dba31fd..d3632523c9 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/string_view.h" From ac8503feebcf6888256b9fd1741f9e1a071528e6 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Wed, 15 Jul 2020 19:02:32 +0000 Subject: [PATCH 55/93] added copy and assignment constructors --- api/include/opentelemetry/context/context.h | 46 +++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index d3632523c9..5a98f7cf44 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -98,10 +98,50 @@ namespace context return true; } + + // Copy constructor links to the same next node but copies the other's + // data list into a new linked list + Context(const Context &other){ + next_ = other.next_; + head_ = new DataList; + DataList *data = head_; + // Copy the other data into a new linked list + for(DataList* other_data = other.head_; other_data != nullptr; other_data = other_data->next_){ + if(other_data->next_ != nullptr){ + data->next_ = new DataList; + } + else{ + data->next_ = nullptr ; + } + data->key_length_ = other_data->key_length_; + data->key_ = new char[data->key_length_]; + memcpy(data->key_, other_data->key_, data->key_length_); + data->value_ = other_data->value_; + data = data->next_; + } + } - Context(const Context &other) = default; - - Context &operator=(const Context &other) = delete; + // Assignment constructor links to the same next node but copies the other's + // data list into a new linked list + Context &operator=(const Context &other) { + next_ = other.next_; + head_ = new DataList; + DataList *data = head_; + // Copy the other data into a new linked list + for(DataList* other_data = other.head_; other_data != nullptr; other_data = other_data->next_){ + if(other_data->next_ != nullptr){ + data->next_ = new DataList; + } + else{ + data->next_ = nullptr ; + } + data->key_length_ = other_data->key_length_; + data->key_ = new char[data->key_length_]; + memcpy(data->key_, other_data->key_, data->key_length_); + data->value_ = other_data->value_; + data = data->next_; + } + } ~Context(){ for(DataList* data = head_; data != nullptr;){ From 91254779ce226f9a97358b73060962d05523bf6c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 16 Jul 2020 00:45:20 +0000 Subject: [PATCH 56/93] adding tests --- api/test/context/BUILD | 23 +++++++ api/test/context/context_test.cc | 100 +++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 api/test/context/BUILD create mode 100644 api/test/context/context_test.cc diff --git a/api/test/context/BUILD b/api/test/context/BUILD new file mode 100644 index 0000000000..4c4cb4795c --- /dev/null +++ b/api/test/context/BUILD @@ -0,0 +1,23 @@ +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 = "thread_local_test", + srcs = [ + "thread_local_test.cc", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], + ) diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc new file mode 100644 index 0000000000..68f36f91fc --- /dev/null +++ b/api/test/context/context_test.cc @@ -0,0 +1,100 @@ +#include "opentelemetry/context/context.h" + +#include + +#include + +using namespace opentelemetry; +/* +// Tests that the context constructor accepts an std::map. +TEST(ContextTest, ContextIterableAcceptsMap) +{ + std::map map_test = {{"test_key", "123"}}; + context::Context context_test = context::Context(map_test); +} +// Tests that the GetValue method returns the expected value. +TEST(ContextTest, ContextGetValueReturnsExpectedValue) +{ + std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; + + context::Context context_test = context::Context(map_test); + EXPECT_EQ(nostd::get(context_test.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(context_test.GetValue("foo_key")), "456"); +} + +// Tests that the SetValues method accepts an std::map. +TEST(ContextTest, ContextSetValuesAcceptsMap) +{ + std::map map_test = {{"test_key", "123"}}; + std::map map_test_write = {{"foo_key", "456"}}; + context::Context context_test = context::Context(map_test); + context::Context* foo_test = context_test.SetValues(map_test_write); + EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); +} + +// Tests that the SetValues method accepts a nostd::string_view and +// context::ContextValue. +TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) +{ + nostd::string_view string_view_test = "string_view"; + context::ContextValue context_value_test = "123"; + context::Context context_test = context::Context(); + context::Context* context_foo = context_test.SetValue(string_view_test, context_value_test); + EXPECT_EQ(nostd::get(context_foo->GetValue(string_view_test)), "123"); +} + +// Tests that the original context does not change when a value is +// written to it. +TEST(ContextTest, ContextImmutability) +{ + std::map map_test = {{"test_key", "123"}}; + context::Context context_test = context::Context(map_test); + + context::Context* context_foo = context_test.SetValue("foo_key", "456"); + + EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), + "456"); +} + +// Tests that writing the same to a context overwrites the original value. +TEST(ContextTest, ContextKeyOverwrite) +{ + std::map map_test = {{"test_key", "123"}}; + context::Context context_test = context::Context(map_test); + context::Context* context_foo = context_test.SetValue("test_key", "456"); + + EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); +} + +// Tests that the new Context Objects inherits the keys and values +// of the original context object. +TEST(ContextTest, ContextInheritance) +{ + using M = std::map; + context::Context test_context = context::Context(); + + M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; + M m2 = {{"other_key", "789"}}; + + context::Context* foo_context = test_context.SetValues(m1); + context::Context* other_context = foo_context->SetValues(m2); + + EXPECT_EQ(nostd::get(other_context->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(other_context->GetValue("foo_key")), "456"); +} +*/ +// Tests that copying a context copies the key value pairs as expected. +TEST(ContextTest, ContextCopyOperator) +{ + std::map test_map = { + {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; + + context::Context test_context = context::Context(test_map); + context::Context copied_context = test_context; +/* + EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); +*/ +} From 80bf6951caf4f3996497728d31a3cfde3badd510 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Thu, 16 Jul 2020 15:37:13 +0000 Subject: [PATCH 57/93] style change --- api/include/opentelemetry/context/context.h | 28 ++++++++++----------- api/test/context/context_test.cc | 8 +++--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 5a98f7cf44..bd26b2c8dd 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -57,9 +57,9 @@ namespace context common::AttributeValue GetValue(nostd::string_view key) { //Iterate through the context nodes - for(Context *context = this; context != nullptr; context = context->next_){ + for(Context* context = this; context != nullptr; context = context->next_){ //Iterate through the internal data nodes - for(DataList *data = context->head_; data != nullptr; data = data->next_){ + for(DataList* data = context->head_; data != nullptr; data = data->next_){ if(strncmp(key.data(), data->key_, data->key_length_) == 0){ return data->value_; } @@ -78,12 +78,12 @@ namespace context return false; } - DataList *other_data = other.head_; + DataList* other_data = other.head_; // Iterate through the lists and compare the keys // TODO should also compare the values but the nostd::variant // seems to not have a comparison operator - for(DataList *data = head_; data != nullptr; data = data->next_){ + for(DataList* data = head_; data != nullptr; data = data->next_){ if(other_data == nullptr){ return false; } @@ -104,7 +104,7 @@ namespace context Context(const Context &other){ next_ = other.next_; head_ = new DataList; - DataList *data = head_; + DataList* data = head_; // Copy the other data into a new linked list for(DataList* other_data = other.head_; other_data != nullptr; other_data = other_data->next_){ if(other_data->next_ != nullptr){ @@ -126,7 +126,7 @@ namespace context Context &operator=(const Context &other) { next_ = other.next_; head_ = new DataList; - DataList *data = head_; + DataList* data = head_; // Copy the other data into a new linked list for(DataList* other_data = other.head_; other_data != nullptr; other_data = other_data->next_){ if(other_data->next_ != nullptr){ @@ -145,7 +145,7 @@ namespace context ~Context(){ for(DataList* data = head_; data != nullptr;){ - DataList * next = data->next_; + DataList* next = data->next_; delete data; data = next; } @@ -158,9 +158,9 @@ namespace context class DataList { public: - DataList *next_; + DataList* next_; - char *key_; + char* key_; int key_length_; @@ -183,11 +183,11 @@ namespace context next_ = nullptr; ++iter; - DataList *previous_node = this; + DataList* previous_node = this; // Iterate over the keys and values iterable and add nodes for (; iter != std::end(keys_and_vals); ++iter) { - DataList *node = new DataList(); + DataList* node = new DataList(); node->next_ = nullptr; node->key_ = new char[nostd::string_view(iter->first).size()]; node->key_length_ = nostd::string_view(iter->first).size(); @@ -201,7 +201,7 @@ namespace context // Builds a data list with just a key and value, so it will just be the head // and returns that head. DataList(nostd::string_view key, ContextValue value){ - DataList *head = new DataList; + DataList* head = new DataList; key_ = new char[nostd::string_view(key).size()]; key_length_ = nostd::string_view(key).size(); strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); @@ -234,10 +234,10 @@ namespace context } // Head of the list which holds the keys and values of this context - DataList *head_ = nullptr; + DataList* head_ = nullptr; // Pointer to the next context object in the context list - Context *next_ = nullptr; + Context* next_ = nullptr; }; } // namespace context diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 68f36f91fc..5732491081 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -5,7 +5,7 @@ #include using namespace opentelemetry; -/* + // Tests that the context constructor accepts an std::map. TEST(ContextTest, ContextIterableAcceptsMap) { @@ -83,7 +83,7 @@ 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 copying a context copies the key value pairs as expected. TEST(ContextTest, ContextCopyOperator) { @@ -92,9 +92,9 @@ TEST(ContextTest, ContextCopyOperator) context::Context test_context = context::Context(test_map); context::Context copied_context = test_context; -/* + EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); -*/ + } From 9d3d643c8d09209365dbfd4b1dce469b3b4d068c Mon Sep 17 00:00:00 2001 From: Sam Atac <65615762+satac2@users.noreply.github.com> Date: Thu, 16 Jul 2020 10:38:52 -0500 Subject: [PATCH 58/93] Update api/include/opentelemetry/context/context.h Co-authored-by: Kirk Kelsey --- 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 bd26b2c8dd..4ca0b50131 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -60,7 +60,7 @@ namespace context for(Context* context = this; context != nullptr; context = context->next_){ //Iterate through the internal data nodes for(DataList* data = context->head_; data != nullptr; data = data->next_){ - if(strncmp(key.data(), data->key_, data->key_length_) == 0){ + if(key.length() == data->key_length_ && strncmp(key.data(), data->key_, data->key_length_) == 0){ return data->value_; } } From 4781d74eb524ff5af0096767eb12590d0fb8e42b Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Fri, 17 Jul 2020 13:25:20 +0000 Subject: [PATCH 59/93] added context CMakeLists.txt --- api/test/context/CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 api/test/context/CMakeLists.txt diff --git a/api/test/context/CMakeLists.txt b/api/test/context/CMakeLists.txt new file mode 100644 index 0000000000..cb7786ade3 --- /dev/null +++ b/api/test/context/CMakeLists.txt @@ -0,0 +1,8 @@ +include(GoogleTest) + +foreach(testname context_test ) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) + gtest_add_tests(TARGET ${testname} TEST_PREFIX context. TEST_LIST ${testname}) +endforeach() From 7d0a145570935a81f3158de6226e015a5a22ff92 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Fri, 17 Jul 2020 13:31:58 +0000 Subject: [PATCH 60/93] fixed minor context.h error, updated tests CMakeLists.txt to contain the context, and formatted code --- api/include/opentelemetry/context/context.h | 412 +++++++++--------- .../opentelemetry/context/context_value.h | 28 +- api/test/CMakeLists.txt | 9 +- api/test/context/BUILD | 19 +- api/test/context/CMakeLists.txt | 2 +- api/test/context/context_test.cc | 18 +- 6 files changed, 243 insertions(+), 245 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index bd26b2c8dd..3db8c0ebd5 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -10,235 +10,245 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - // The context class provides a context identifier. Is built as a linked list - // of context nodes which each have access to the nodes before it, but not - // after. So each time a write is performed a new context is added to the list. - // Within each context node there is another linked list which holds the key - // and value data for each node. - class Context - { +// The context class provides a context identifier. Is built as a linked list +// of context nodes which each have access to the nodes before it, but not +// after. So each time a write is performed a new context is added to the list. +// Within each context node there is another linked list which holds the key +// and value data for each node. +class Context +{ - public: +public: + Context() = default; + // Creates a context object from a map of keys and identifiers, this will + // be the head of the context object linked list. + template ::value> * = nullptr> + Context(const T &keys_and_values) + { + head_ = new DataList(keys_and_values); + } - Context() = default; + // Creates a context object from a key and value, this will be the head + // of the context object linked list. + Context(nostd::string_view key, ContextValue value) { head_ = new DataList(key, value); } - // Creates a context object from a map of keys and identifiers, this will - // be the head of the context object linked list. - template ::value> * = nullptr> - Context(const T &keys_and_values) - { - head_ = new DataList(keys_and_values); - } + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + template ::value> * = nullptr> + Context *SetValues(T &values) noexcept + { + return new Context(values, this); + } - // Creates a context object from a key and value, this will be the head - // of the context object linked list. - Context(nostd::string_view key, ContextValue value) - { - head_ = new DataList(key, value); - } + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + Context *SetValue(nostd::string_view key, ContextValue value) noexcept + { + return new Context(key, value, this); + } - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. - template ::value> * = nullptr> - Context* SetValues(T &values) noexcept + // Returns the value associated with the passed in key. + context::ContextValue GetValue(nostd::string_view key) + { + // Iterate through the context nodes + for (Context *context = this; context != nullptr; context = context->next_) + { + // Iterate through the internal data nodes + for (DataList *data = context->head_; data != nullptr; data = data->next_) + { + if (strncmp(key.data(), data->key_, data->key_length_) == 0) { - return new Context(values, this); + return data->value_; } + } + } + return ""; + } - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. - Context* SetValue(nostd::string_view key, ContextValue value) noexcept + // Iterates through the current and comparing context objects + // to check for equality. + bool operator==(const Context &other) + { + // If the two contexts are not at the place in the list + // they cannot be equivalent + if (next_ != other.next_) + { + return false; + } + + DataList *other_data = other.head_; + + // Iterate through the lists and compare the keys + // TODO should also compare the values but the nostd::variant + // seems to not have a comparison operator + for (DataList *data = head_; data != nullptr; data = data->next_) + { + if (other_data == nullptr) { - return new Context(key, value, this); + return false; } - // Returns the value associated with the passed in key. - common::AttributeValue GetValue(nostd::string_view key) + if ((data->key_ != other_data->key_)) { - //Iterate through the context nodes - for(Context* context = this; context != nullptr; context = context->next_){ - //Iterate through the internal data nodes - for(DataList* data = context->head_; data != nullptr; data = data->next_){ - if(strncmp(key.data(), data->key_, data->key_length_) == 0){ - return data->value_; - } - } - } - return ""; + return false; } - // Iterates through the current and comparing context objects - // to check for equality. - bool operator==(const Context &other) - { - // If the two contexts are not at the place in the list - // they cannot be equivalent - if(next_ != other.next_){ - return false; - } + data = data->next_; + other_data = other_data->next_; + } - DataList* other_data = other.head_; + return true; + } - // Iterate through the lists and compare the keys - // TODO should also compare the values but the nostd::variant - // seems to not have a comparison operator - for(DataList* data = head_; data != nullptr; data = data->next_){ - if(other_data == nullptr){ - return false; - } + // Copy constructor links to the same next node but copies the other's + // data list into a new linked list + Context(const Context &other) + { + next_ = other.next_; + head_ = new DataList; + DataList *data = head_; + // Copy the other data into a new linked list + for (DataList *other_data = other.head_; other_data != nullptr; other_data = other_data->next_) + { + if (other_data->next_ != nullptr) + { + data->next_ = new DataList; + } + else + { + data->next_ = nullptr; + } + data->key_length_ = other_data->key_length_; + data->key_ = new char[data->key_length_]; + memcpy(data->key_, other_data->key_, data->key_length_); + data->value_ = other_data->value_; + data = data->next_; + } + } + + // Assignment constructor links to the same next node but copies the other's + // data list into a new linked list + Context &operator=(const Context &other) + { + next_ = other.next_; + head_ = new DataList; + DataList *data = head_; + // Copy the other data into a new linked list + for (DataList *other_data = other.head_; other_data != nullptr; other_data = other_data->next_) + { + if (other_data->next_ != nullptr) + { + data->next_ = new DataList; + } + else + { + data->next_ = nullptr; + } + data->key_length_ = other_data->key_length_; + data->key_ = new char[data->key_length_]; + memcpy(data->key_, other_data->key_, data->key_length_); + data->value_ = other_data->value_; + data = data->next_; + } + } + + ~Context() + { + for (DataList *data = head_; data != nullptr;) + { + DataList *next = data->next_; + delete data; + data = next; + } + } + +private: + // A linked list to contain the keys and values of this context node + class DataList + { + public: + DataList *next_; - if((data->key_ != other_data->key_)){ - return false; - } + char *key_; - data = data->next_; - other_data = other_data->next_; - } + int key_length_; - return true; - } - - // Copy constructor links to the same next node but copies the other's - // data list into a new linked list - Context(const Context &other){ - next_ = other.next_; - head_ = new DataList; - DataList* data = head_; - // Copy the other data into a new linked list - for(DataList* other_data = other.head_; other_data != nullptr; other_data = other_data->next_){ - if(other_data->next_ != nullptr){ - data->next_ = new DataList; - } - else{ - data->next_ = nullptr ; - } - data->key_length_ = other_data->key_length_; - data->key_ = new char[data->key_length_]; - memcpy(data->key_, other_data->key_, data->key_length_); - data->value_ = other_data->value_; - data = data->next_; - } - } + ContextValue value_; - // Assignment constructor links to the same next node but copies the other's - // data list into a new linked list - Context &operator=(const Context &other) { - next_ = other.next_; - head_ = new DataList; - DataList* data = head_; - // Copy the other data into a new linked list - for(DataList* other_data = other.head_; other_data != nullptr; other_data = other_data->next_){ - if(other_data->next_ != nullptr){ - data->next_ = new DataList; - } - else{ - data->next_ = nullptr ; - } - data->key_length_ = other_data->key_length_; - data->key_ = new char[data->key_length_]; - memcpy(data->key_, other_data->key_, data->key_length_); - data->value_ = other_data->value_; - data = data->next_; - } - } + DataList() { next_ = nullptr; } - ~Context(){ - for(DataList* data = head_; data != nullptr;){ - DataList* next = data->next_; - delete data; - data = next; - } - } + // Builds a data list off of a key and value iterable and returns the head + template ::value> * = nullptr> + DataList(const T &keys_and_vals) + { + auto iter = std::begin(keys_and_vals); - private: - - - // A linked list to contain the keys and values of this context node - class DataList { - public: - - DataList* next_; - - char* key_; - - int key_length_; - - ContextValue value_; - - DataList(){ - next_ = nullptr; - } - - // Builds a data list off of a key and value iterable and returns the head - template ::value> * = nullptr> - DataList(const T &keys_and_vals){ - auto iter = std::begin(keys_and_vals); - - // Create list head - key_ = new char[nostd::string_view(iter->first).size()]; - key_length_ = nostd::string_view(iter->first).size(); - strncpy(key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size()); - value_ = iter->second; - next_ = nullptr; - ++iter; - - DataList* previous_node = this; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) - { - DataList* node = new DataList(); - node->next_ = nullptr; - node->key_ = new char[nostd::string_view(iter->first).size()]; - node->key_length_ = nostd::string_view(iter->first).size(); - strncpy(node->key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size()); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = node; - } - } - - // Builds a data list with just a key and value, so it will just be the head - // and returns that head. - DataList(nostd::string_view key, ContextValue value){ - DataList* head = new DataList; - key_ = new char[nostd::string_view(key).size()]; - key_length_ = nostd::string_view(key).size(); - strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); - value_ = value; - next_ = nullptr; - } - - ~DataList(){ - delete [] key_; - } - }; - - // Creates a context object from a map of keys and identifiers, an internal - // contructor only, will always create a node within the list, not at the - // head. - template ::value> * = nullptr> - Context(const T &keys_and_vals, Context* next_node) - { - head_ = new DataList(keys_and_vals); - next_ = next_node; - } + // Create list head + key_ = new char[nostd::string_view(iter->first).size()]; + key_length_ = nostd::string_view(iter->first).size(); + strncpy(key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size()); + value_ = iter->second; + next_ = nullptr; + ++iter; - // Creates a context object from a key and value. An internal - // contructor only, will always create a node within the list, not at the - // head. - Context(nostd::string_view key, ContextValue value, Context* next_node) + DataList *previous_node = this; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) { - head_ = new DataList(key, value); - next_ = next_node; + DataList *node = new DataList(); + node->next_ = nullptr; + node->key_ = new char[nostd::string_view(iter->first).size()]; + node->key_length_ = nostd::string_view(iter->first).size(); + strncpy(node->key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size()); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = node; } + } + + // Builds a data list with just a key and value, so it will just be the head + // and returns that head. + DataList(nostd::string_view key, ContextValue value) + { + DataList *head = new DataList; + key_ = new char[nostd::string_view(key).size()]; + key_length_ = nostd::string_view(key).size(); + strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); + value_ = value; + next_ = nullptr; + } + + ~DataList() { delete[] key_; } + }; - // Head of the list which holds the keys and values of this context - DataList* head_ = nullptr; + // Creates a context object from a map of keys and identifiers, an internal + // contructor only, will always create a node within the list, not at the + // head. + template ::value> * = nullptr> + Context(const T &keys_and_vals, Context *next_node) + { + head_ = new DataList(keys_and_vals); + next_ = next_node; + } + + // Creates a context object from a key and value. An internal + // contructor only, will always create a node within the list, not at the + // head. + Context(nostd::string_view key, ContextValue value, Context *next_node) + { + head_ = new DataList(key, value); + next_ = next_node; + } - // Pointer to the next context object in the context list - Context* next_ = nullptr; - }; + // Head of the list which holds the keys and values of this context + DataList *head_ = nullptr; + + // Pointer to the next context object in the context list + Context *next_ = nullptr; +}; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 9527524fed..bccdac46d1 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -10,19 +10,19 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - using ContextValue = nostd::variant, - nostd::span, - nostd::span, - nostd::span, - nostd::span, - nostd::span, - nostd::span>; +using ContextValue = nostd::variant, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span>; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/CMakeLists.txt b/api/test/CMakeLists.txt index 85f3032c8a..5c5a8590e4 100644 --- a/api/test/CMakeLists.txt +++ b/api/test/CMakeLists.txt @@ -1,4 +1,5 @@ - add_subdirectory(core) - add_subdirectory(plugin) - add_subdirectory(nostd) - add_subdirectory(trace) +add_subdirectory(core) +add_subdirectory(context) +add_subdirectory(plugin) +add_subdirectory(nostd) +add_subdirectory(trace) diff --git a/api/test/context/BUILD b/api/test/context/BUILD index 4c4cb4795c..b213710944 100644 --- a/api/test/context/BUILD +++ b/api/test/context/BUILD @@ -3,21 +3,10 @@ load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") cc_test( name = "context_test", srcs = [ - "context_test.cc", + "context_test.cc", ], deps = [ - "//api", - "@com_google_googletest//:gtest_main", + "//api", + "@com_google_googletest//:gtest_main", ], - ) - -cc_test( - name = "thread_local_test", - srcs = [ - "thread_local_test.cc", - ], - deps = [ - "//api", - "@com_google_googletest//:gtest_main", - ], - ) +) diff --git a/api/test/context/CMakeLists.txt b/api/test/context/CMakeLists.txt index cb7786ade3..38d48ef0e8 100644 --- a/api/test/context/CMakeLists.txt +++ b/api/test/context/CMakeLists.txt @@ -1,6 +1,6 @@ include(GoogleTest) -foreach(testname context_test ) +foreach(testname context_test) add_executable(${testname} "${testname}.cc") target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_api) diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 5732491081..07c2a9741e 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -28,7 +28,7 @@ TEST(ContextTest, ContextSetValuesAcceptsMap) std::map map_test = {{"test_key", "123"}}; std::map map_test_write = {{"foo_key", "456"}}; context::Context context_test = context::Context(map_test); - context::Context* foo_test = context_test.SetValues(map_test_write); + context::Context *foo_test = context_test.SetValues(map_test_write); EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); } @@ -40,7 +40,7 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) nostd::string_view string_view_test = "string_view"; context::ContextValue context_value_test = "123"; context::Context context_test = context::Context(); - context::Context* context_foo = context_test.SetValue(string_view_test, context_value_test); + context::Context *context_foo = context_test.SetValue(string_view_test, context_value_test); EXPECT_EQ(nostd::get(context_foo->GetValue(string_view_test)), "123"); } @@ -51,10 +51,9 @@ TEST(ContextTest, ContextImmutability) std::map map_test = {{"test_key", "123"}}; context::Context context_test = context::Context(map_test); - context::Context* context_foo = context_test.SetValue("foo_key", "456"); + context::Context *context_foo = context_test.SetValue("foo_key", "456"); - EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), - "456"); + EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), "456"); } // Tests that writing the same to a context overwrites the original value. @@ -62,7 +61,7 @@ TEST(ContextTest, ContextKeyOverwrite) { std::map map_test = {{"test_key", "123"}}; context::Context context_test = context::Context(map_test); - context::Context* context_foo = context_test.SetValue("test_key", "456"); + context::Context *context_foo = context_test.SetValue("test_key", "456"); EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); } @@ -77,8 +76,8 @@ TEST(ContextTest, ContextInheritance) M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; M m2 = {{"other_key", "789"}}; - context::Context* foo_context = test_context.SetValues(m1); - context::Context* other_context = foo_context->SetValues(m2); + context::Context *foo_context = test_context.SetValues(m1); + context::Context *other_context = foo_context->SetValues(m2); EXPECT_EQ(nostd::get(other_context->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(other_context->GetValue("foo_key")), "456"); @@ -88,7 +87,7 @@ TEST(ContextTest, ContextInheritance) TEST(ContextTest, ContextCopyOperator) { std::map test_map = { - {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; + {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; context::Context test_context = context::Context(test_map); context::Context copied_context = test_context; @@ -96,5 +95,4 @@ TEST(ContextTest, ContextCopyOperator) EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); - } From ea288a52b6dee9626908ec47fba2117a49b9b2fa Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 00:43:44 +0000 Subject: [PATCH 61/93] eliminated memory leaks and added abstraction for stability, also modified tests --- api/include/opentelemetry/context/context.h | 85 ++++++++++++--------- api/test/context/context_test.cc | 69 +++++++++-------- 2 files changed, 86 insertions(+), 68 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 7f74a8d11d..fe0c3df119 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -4,6 +4,7 @@ #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/trace/key_value_iterable_view.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -19,40 +20,52 @@ class Context { public: - Context() = default; - - // Creates a context object from a map of keys and identifiers, this will - // be the head of the context object linked list. + + nostd::shared_ptr* context_ptr_; + + // Creates a new context by calling a constructor with the passed in + // keyValueIterable and returns a shared_ptr pointing to it. template ::value> * = nullptr> - Context(const T &keys_and_values) - { - head_ = new DataList(keys_and_values); + static nostd::shared_ptr CreateContext( T &keys_and_values){ + nostd::shared_ptr temp = nostd::shared_ptr{ new Context(keys_and_values)}; + temp->context_ptr_ = &temp; + return temp; } - // Creates a context object from a key and value, this will be the head - // of the context object linked list. - Context(nostd::string_view key, ContextValue value) { head_ = new DataList(key, value); } + // Creates a new context by calling a constructor with the passed in + // key and value and returns a shared_ptr pointing to it. + static nostd::shared_ptr CreateContext(nostd::string_view key, ContextValue value){ + nostd::shared_ptr temp = nostd::shared_ptr{ new Context(key, value)}; + temp->context_ptr_ = &temp; + return temp; + } // Accepts a new iterable and then returns a new context that // contains the new key and value data. template ::value> * = nullptr> - Context *SetValues(T &values) noexcept + nostd::shared_ptr SetValues(T &values) noexcept { - return new Context(values, this); + nostd::shared_ptr temp = nostd::shared_ptr{ new Context(values)}; + temp->context_ptr_ = &temp; + temp->SetNext(*context_ptr_); + return temp; } // Accepts a new iterable and then returns a new context that // contains the new key and value data. - Context *SetValue(nostd::string_view key, ContextValue value) noexcept + nostd::shared_ptr SetValue(nostd::string_view key, ContextValue value) noexcept { - return new Context(key, value, this); + nostd::shared_ptr context = nostd::shared_ptr{new Context(key, value)}; + context->context_ptr_ = &context; + context->SetNext(*context_ptr_); + return context; } // Returns the value associated with the passed in key. context::ContextValue GetValue(nostd::string_view key) { // Iterate through the context nodes - for (Context *context = this; context != nullptr; context = context->next_) + for (auto context = *context_ptr_; context != nullptr; context = context->next_) { // Iterate through the internal data nodes for (DataList *data = context->head_; data != nullptr; data = data->next_) @@ -62,6 +75,7 @@ class Context return data->value_; } } + } return ""; } @@ -88,19 +102,16 @@ class Context { return false; } - if (data->key_ != other_data->key_) { return false; } - data = data->next_; other_data = other_data->next_; } - return true; } - + // Copy constructor links to the same next node but copies the other's // data list into a new linked list Context(const Context &other) @@ -163,7 +174,15 @@ class Context } } + // Used to set the next node in the list + void SetNext(nostd::shared_ptr next){ + next_ = next; + } + private: + + Context() = default; + // A linked list to contain the keys and values of this context node class DataList { @@ -184,7 +203,6 @@ class Context DataList(const T &keys_and_vals) { auto iter = std::begin(keys_and_vals); - // Create list head key_ = new char[nostd::string_view(iter->first).size()]; key_length_ = nostd::string_view(iter->first).size(); @@ -213,7 +231,6 @@ class Context // and returns that head. DataList(nostd::string_view key, ContextValue value) { - DataList *head = new DataList; key_ = new char[nostd::string_view(key).size()]; key_length_ = nostd::string_view(key).size(); strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); @@ -224,31 +241,23 @@ class Context ~DataList() { delete[] key_; } }; - // Creates a context object from a map of keys and identifiers, an internal - // contructor only, will always create a node within the list, not at the - // head. + // Creates a context object from a map of keys and identifiers, this will + // be the head of the context object linked list. template ::value> * = nullptr> - Context(const T &keys_and_vals, Context *next_node) - { - head_ = new DataList(keys_and_vals); - next_ = next_node; - } - - // Creates a context object from a key and value. An internal - // contructor only, will always create a node within the list, not at the - // head. - Context(nostd::string_view key, ContextValue value, Context *next_node) + Context(const T &keys_and_values) { - head_ = new DataList(key, value); - next_ = next_node; + head_ = new DataList(keys_and_values); } + // Creates a context object from a key and value, this will be the head + // of the context object linked list. + Context(nostd::string_view key, ContextValue value) { head_ = new DataList(key, value); } + // Head of the list which holds the keys and values of this context DataList *head_ = nullptr; // Pointer to the next context object in the context list - Context *next_ = nullptr; + nostd::shared_ptr next_; }; - } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 07c2a9741e..6731678dcc 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -10,16 +10,18 @@ using namespace opentelemetry; TEST(ContextTest, ContextIterableAcceptsMap) { std::map map_test = {{"test_key", "123"}}; - context::Context context_test = context::Context(map_test); + nostd::shared_ptr test_context = context::Context::CreateContext(map_test); } + // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - - context::Context context_test = context::Context(map_test); - EXPECT_EQ(nostd::get(context_test.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(context_test.GetValue("foo_key")), "456"); + + nostd::shared_ptr test_context = context::Context::CreateContext(map_test); + + EXPECT_EQ(nostd::get(test_context->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(test_context->GetValue("foo_key")), "456"); } // Tests that the SetValues method accepts an std::map. @@ -27,8 +29,10 @@ TEST(ContextTest, ContextSetValuesAcceptsMap) { std::map map_test = {{"test_key", "123"}}; std::map map_test_write = {{"foo_key", "456"}}; - context::Context context_test = context::Context(map_test); - context::Context *foo_test = context_test.SetValues(map_test_write); + + nostd::shared_ptr test_context = context::Context::CreateContext(map_test); + nostd::shared_ptr foo_test = test_context->SetValues(map_test_write); + EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); } @@ -39,9 +43,11 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) { nostd::string_view string_view_test = "string_view"; context::ContextValue context_value_test = "123"; - context::Context context_test = context::Context(); - context::Context *context_foo = context_test.SetValue(string_view_test, context_value_test); - EXPECT_EQ(nostd::get(context_foo->GetValue(string_view_test)), "123"); + + nostd::shared_ptr test_context = context::Context::CreateContext(string_view_test, context_value_test); + nostd::shared_ptr foo_context = test_context->SetValue(string_view_test, context_value_test); + + EXPECT_EQ(nostd::get(foo_context->GetValue(string_view_test)), "123"); } // Tests that the original context does not change when a value is @@ -49,20 +55,22 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) TEST(ContextTest, ContextImmutability) { std::map map_test = {{"test_key", "123"}}; - context::Context context_test = context::Context(map_test); - - context::Context *context_foo = context_test.SetValue("foo_key", "456"); - - EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), "456"); + std::map foo_map = {{"foo_key", "456"}}; + + nostd::shared_ptr context_test = context::Context::CreateContext(map_test); + nostd::shared_ptr context_foo = context_test->SetValue("foo_key","456"); + + EXPECT_NE(nostd::get(context_test->GetValue("foo_key")), "456"); } // Tests that writing the same to a context overwrites the original value. TEST(ContextTest, ContextKeyOverwrite) { std::map map_test = {{"test_key", "123"}}; - context::Context context_test = context::Context(map_test); - context::Context *context_foo = context_test.SetValue("test_key", "456"); - + + nostd::shared_ptr context_test = context::Context::CreateContext(map_test); + nostd::shared_ptr context_foo = context_test->SetValue("test_key", "456"); + EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); } @@ -71,28 +79,29 @@ TEST(ContextTest, ContextKeyOverwrite) TEST(ContextTest, ContextInheritance) { using M = std::map; - context::Context test_context = context::Context(); M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; M m2 = {{"other_key", "789"}}; - - context::Context *foo_context = test_context.SetValues(m1); - context::Context *other_context = foo_context->SetValues(m2); - - EXPECT_EQ(nostd::get(other_context->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(other_context->GetValue("foo_key")), "456"); + + nostd::shared_ptr test_context = context::Context::CreateContext(m1); + nostd::shared_ptr foo_context = test_context->SetValues(m2); + + EXPECT_EQ(nostd::get(foo_context->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(foo_context->GetValue("foo_key")), "456"); } + // Tests that copying a context copies the key value pairs as expected. TEST(ContextTest, ContextCopyOperator) { std::map test_map = { {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; - context::Context test_context = context::Context(test_map); - context::Context copied_context = test_context; + nostd::shared_ptr test_context = context::Context::CreateContext(test_map); + nostd::shared_ptr copied_context = test_context; - EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); - EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); + EXPECT_EQ(nostd::get(copied_context->GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(copied_context->GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(copied_context->GetValue("other_key")), "789"); } + From 0f1d48542c015da98c8e7c7296c866dd8727ab6f Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 00:47:23 +0000 Subject: [PATCH 62/93] auto formatted --- api/include/opentelemetry/context/context.h | 43 +++++++-------- .../opentelemetry/context/context_value.h | 15 ++---- api/test/context/context_test.cc | 52 +++++++++---------- 3 files changed, 49 insertions(+), 61 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index fe0c3df119..31ed806787 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -3,8 +3,8 @@ #include #include "opentelemetry/context/context_value.h" -#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/nostd/string_view.h" #include "opentelemetry/trace/key_value_iterable_view.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -20,23 +20,24 @@ class Context { public: - - nostd::shared_ptr* context_ptr_; - + nostd::shared_ptr *context_ptr_; + // Creates a new context by calling a constructor with the passed in // keyValueIterable and returns a shared_ptr pointing to it. template ::value> * = nullptr> - static nostd::shared_ptr CreateContext( T &keys_and_values){ - nostd::shared_ptr temp = nostd::shared_ptr{ new Context(keys_and_values)}; - temp->context_ptr_ = &temp; + static nostd::shared_ptr CreateContext(T &keys_and_values) + { + nostd::shared_ptr temp = nostd::shared_ptr{new Context(keys_and_values)}; + temp->context_ptr_ = &temp; return temp; } // Creates a new context by calling a constructor with the passed in // key and value and returns a shared_ptr pointing to it. - static nostd::shared_ptr CreateContext(nostd::string_view key, ContextValue value){ - nostd::shared_ptr temp = nostd::shared_ptr{ new Context(key, value)}; - temp->context_ptr_ = &temp; + static nostd::shared_ptr CreateContext(nostd::string_view key, ContextValue value) + { + nostd::shared_ptr temp = nostd::shared_ptr{new Context(key, value)}; + temp->context_ptr_ = &temp; return temp; } @@ -45,8 +46,8 @@ class Context template ::value> * = nullptr> nostd::shared_ptr SetValues(T &values) noexcept { - nostd::shared_ptr temp = nostd::shared_ptr{ new Context(values)}; - temp->context_ptr_ = &temp; + nostd::shared_ptr temp = nostd::shared_ptr{new Context(values)}; + temp->context_ptr_ = &temp; temp->SetNext(*context_ptr_); return temp; } @@ -56,7 +57,7 @@ class Context nostd::shared_ptr SetValue(nostd::string_view key, ContextValue value) noexcept { nostd::shared_ptr context = nostd::shared_ptr{new Context(key, value)}; - context->context_ptr_ = &context; + context->context_ptr_ = &context; context->SetNext(*context_ptr_); return context; } @@ -75,7 +76,6 @@ class Context return data->value_; } } - } return ""; } @@ -111,7 +111,7 @@ class Context } return true; } - + // Copy constructor links to the same next node but copies the other's // data list into a new linked list Context(const Context &other) @@ -175,14 +175,11 @@ class Context } // Used to set the next node in the list - void SetNext(nostd::shared_ptr next){ - next_ = next; - } + void SetNext(nostd::shared_ptr next) { next_ = next; } private: - Context() = default; - + // A linked list to contain the keys and values of this context node class DataList { @@ -231,8 +228,8 @@ class Context // and returns that head. DataList(nostd::string_view key, ContextValue value) { - key_ = new char[nostd::string_view(key).size()]; - key_length_ = nostd::string_view(key).size(); + key_ = new char[nostd::string_view(key).size()]; + key_length_ = nostd::string_view(key).size(); strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); value_ = value; next_ = nullptr; @@ -252,7 +249,7 @@ class Context // Creates a context object from a key and value, this will be the head // of the context object linked list. Context(nostd::string_view key, ContextValue value) { head_ = new DataList(key, value); } - + // Head of the list which holds the keys and values of this context DataList *head_ = nullptr; diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index bccdac46d1..9fb061ea89 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -10,19 +10,10 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -using ContextValue = nostd::variant, - nostd::span, - nostd::span, - nostd::span, - nostd::span, - nostd::span, - nostd::span>; + nostd::span, + nostd::span>>; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 6731678dcc..bd9de0d3c1 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -10,16 +10,16 @@ using namespace opentelemetry; TEST(ContextTest, ContextIterableAcceptsMap) { std::map map_test = {{"test_key", "123"}}; - nostd::shared_ptr test_context = context::Context::CreateContext(map_test); + nostd::shared_ptr test_context = context::Context::CreateContext(map_test); } // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - + nostd::shared_ptr test_context = context::Context::CreateContext(map_test); - + EXPECT_EQ(nostd::get(test_context->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(test_context->GetValue("foo_key")), "456"); } @@ -29,10 +29,10 @@ TEST(ContextTest, ContextSetValuesAcceptsMap) { std::map map_test = {{"test_key", "123"}}; std::map map_test_write = {{"foo_key", "456"}}; - + nostd::shared_ptr test_context = context::Context::CreateContext(map_test); - nostd::shared_ptr foo_test = test_context->SetValues(map_test_write); - + nostd::shared_ptr foo_test = test_context->SetValues(map_test_write); + EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); } @@ -43,10 +43,12 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) { nostd::string_view string_view_test = "string_view"; context::ContextValue context_value_test = "123"; - - nostd::shared_ptr test_context = context::Context::CreateContext(string_view_test, context_value_test); - nostd::shared_ptr foo_context = test_context->SetValue(string_view_test, context_value_test); - + + nostd::shared_ptr test_context = + context::Context::CreateContext(string_view_test, context_value_test); + nostd::shared_ptr foo_context = + test_context->SetValue(string_view_test, context_value_test); + EXPECT_EQ(nostd::get(foo_context->GetValue(string_view_test)), "123"); } @@ -55,11 +57,11 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) TEST(ContextTest, ContextImmutability) { std::map map_test = {{"test_key", "123"}}; - std::map foo_map = {{"foo_key", "456"}}; - - nostd::shared_ptr context_test = context::Context::CreateContext(map_test); - nostd::shared_ptr context_foo = context_test->SetValue("foo_key","456"); - + std::map foo_map = {{"foo_key", "456"}}; + + nostd::shared_ptr context_test = context::Context::CreateContext(map_test); + nostd::shared_ptr context_foo = context_test->SetValue("foo_key", "456"); + EXPECT_NE(nostd::get(context_test->GetValue("foo_key")), "456"); } @@ -67,10 +69,10 @@ TEST(ContextTest, ContextImmutability) TEST(ContextTest, ContextKeyOverwrite) { std::map map_test = {{"test_key", "123"}}; - - nostd::shared_ptr context_test = context::Context::CreateContext(map_test); - nostd::shared_ptr context_foo = context_test->SetValue("test_key", "456"); - + + nostd::shared_ptr context_test = context::Context::CreateContext(map_test); + nostd::shared_ptr context_foo = context_test->SetValue("test_key", "456"); + EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); } @@ -78,30 +80,28 @@ TEST(ContextTest, ContextKeyOverwrite) // of the original context object. TEST(ContextTest, ContextInheritance) { - using M = std::map; + using M = std::map; M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; M m2 = {{"other_key", "789"}}; - + nostd::shared_ptr test_context = context::Context::CreateContext(m1); - nostd::shared_ptr foo_context = test_context->SetValues(m2); - + nostd::shared_ptr foo_context = test_context->SetValues(m2); + EXPECT_EQ(nostd::get(foo_context->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(foo_context->GetValue("foo_key")), "456"); } - // Tests that copying a context copies the key value pairs as expected. TEST(ContextTest, ContextCopyOperator) { std::map test_map = { {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; - nostd::shared_ptr test_context = context::Context::CreateContext(test_map); + nostd::shared_ptr test_context = context::Context::CreateContext(test_map); nostd::shared_ptr copied_context = test_context; EXPECT_EQ(nostd::get(copied_context->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(copied_context->GetValue("foo_key")), "456"); EXPECT_EQ(nostd::get(copied_context->GetValue("other_key")), "789"); } - From 3b922a5ac0a43328e794ecae726d593fc9570a86 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 00:49:52 +0000 Subject: [PATCH 63/93] fixed pull request error --- 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 31ed806787..c49faae66b 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -140,7 +140,7 @@ class Context // Assignment constructor links to the same next node but copies the other's // data list into a new linked list - Context &operator=(const Context &other) + void &operator=(const Context &other) { next_ = other.next_; head_ = new DataList; From 39941d3320e4be7e059df4458583f86c429411ba Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 00:55:20 +0000 Subject: [PATCH 64/93] SpanContext should be found now --- api/include/opentelemetry/context/context_value.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 9fb061ea89..5b14680734 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -6,6 +6,7 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/version.h" +#include "opentelemetry/trace/span_context.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context @@ -13,7 +14,7 @@ namespace context using ContextValue = nostd::variant, + nostd::span, nostd::span>>; } // namespace context OPENTELEMETRY_END_NAMESPACE From e1f2cfa391f093eb459af417f6592dc4677165e1 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 00:57:46 +0000 Subject: [PATCH 65/93] fixed Cmake test failure --- 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 c49faae66b..900f24e1e2 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -140,7 +140,7 @@ class Context // Assignment constructor links to the same next node but copies the other's // data list into a new linked list - void &operator=(const Context &other) + void operator=(const Context &other) { next_ = other.next_; head_ = new DataList; From f4e27183c2e14fab403e0a6992a1fd6506a84225 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 01:19:08 +0000 Subject: [PATCH 66/93] should fix the cmake error --- .../opentelemetry/context/context_value.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 5b14680734..bccdac46d1 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -6,15 +6,23 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/version.h" -#include "opentelemetry/trace/span_context.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -using ContextValue = nostd::variant, - nostd::span>>; + nostd::string_view, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span, + nostd::span>; } // namespace context OPENTELEMETRY_END_NAMESPACE From f1f24804593722a4d37a7328a9c3001be2b9b4ad Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 13:49:59 +0000 Subject: [PATCH 67/93] removing warnings --- 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 900f24e1e2..f9b4f453f8 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -188,7 +188,7 @@ class Context char *key_; - int key_length_; + size_t key_length_; ContextValue value_; From ce6d9fa4218ac29fa47de9ff5c7c88580876f108 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 15:39:50 +0000 Subject: [PATCH 68/93] avoiding strncpy --- api/include/opentelemetry/context/context.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index f9b4f453f8..81b1cedd66 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -71,9 +71,12 @@ class Context // Iterate through the internal data nodes for (DataList *data = context->head_; data != nullptr; data = data->next_) { - if (strncmp(key.data(), data->key_, data->key_length_) == 0) + if (key.size() == data->key_length_) { - return data->value_; + if (memcmp(key.data(), data->key_, data->key_length_) == 0) + { + return data->value_; + } } } } @@ -203,7 +206,8 @@ class Context // Create list head key_ = new char[nostd::string_view(iter->first).size()]; key_length_ = nostd::string_view(iter->first).size(); - strncpy(key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size()); + memcpy(key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); value_ = iter->second; next_ = nullptr; ++iter; @@ -216,8 +220,8 @@ class Context node->next_ = nullptr; node->key_ = new char[nostd::string_view(iter->first).size()]; node->key_length_ = nostd::string_view(iter->first).size(); - strncpy(node->key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size()); + memcpy(node->key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); node->value_ = iter->second; previous_node->next_ = node; previous_node = node; @@ -230,7 +234,7 @@ class Context { key_ = new char[nostd::string_view(key).size()]; key_length_ = nostd::string_view(key).size(); - strncpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size()); + memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); value_ = value; next_ = nullptr; } From c7cbb21634aca07a146f17ca5361883ef6564347 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 16:59:45 +0000 Subject: [PATCH 69/93] initialized shared_ptr --- api/include/opentelemetry/context/context.h | 1 + 1 file changed, 1 insertion(+) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 81b1cedd66..ffd6b4d8fd 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -248,6 +248,7 @@ class Context Context(const T &keys_and_values) { head_ = new DataList(keys_and_values); + next_ = nostd::shared_ptr{nullptr}; } // Creates a context object from a key and value, this will be the head From f2542fd3b3040d8a60835a1d075207bd82411347 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 17:33:56 +0000 Subject: [PATCH 70/93] removed getValue to try and pass tests --- api/test/context/context_test.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index bd9de0d3c1..547c3cdda4 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -20,10 +20,10 @@ TEST(ContextTest, ContextGetValueReturnsExpectedValue) nostd::shared_ptr test_context = context::Context::CreateContext(map_test); - EXPECT_EQ(nostd::get(test_context->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(test_context->GetValue("foo_key")), "456"); + // EXPECT_EQ(nostd::get(test_context->GetValue("test_key")), "123"); + // EXPECT_EQ(nostd::get(test_context->GetValue("foo_key")), "456"); } - +/* // Tests that the SetValues method accepts an std::map. TEST(ContextTest, ContextSetValuesAcceptsMap) { @@ -104,4 +104,4 @@ TEST(ContextTest, ContextCopyOperator) EXPECT_EQ(nostd::get(copied_context->GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(copied_context->GetValue("foo_key")), "456"); EXPECT_EQ(nostd::get(copied_context->GetValue("other_key")), "789"); -} +}*/ From ba9da8e6d3f4851c412c4555ae6b121d7c1a5ab5 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 17:52:26 +0000 Subject: [PATCH 71/93] restricted GetValue --- api/include/opentelemetry/context/context.h | 2 ++ api/test/context/context_test.cc | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index ffd6b4d8fd..d2a038618e 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -65,6 +65,7 @@ class Context // Returns the value associated with the passed in key. context::ContextValue GetValue(nostd::string_view key) { +/* // Iterate through the context nodes for (auto context = *context_ptr_; context != nullptr; context = context->next_) { @@ -80,6 +81,7 @@ class Context } } } + */ return ""; } diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 547c3cdda4..fdd79327ad 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -19,7 +19,7 @@ TEST(ContextTest, ContextGetValueReturnsExpectedValue) std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; nostd::shared_ptr test_context = context::Context::CreateContext(map_test); - + nostd::get(test_context->GetValue("test_key")); // EXPECT_EQ(nostd::get(test_context->GetValue("test_key")), "123"); // EXPECT_EQ(nostd::get(test_context->GetValue("foo_key")), "456"); } From a9c4eeabc0f41772d2566ab16da8b5c404669c0c Mon Sep 17 00:00:00 2001 From: root Date: Mon, 20 Jul 2020 18:13:07 +0000 Subject: [PATCH 72/93] now deferencing shared_ptr --- api/include/opentelemetry/context/context.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index d2a038618e..47aa00b59c 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -65,10 +65,11 @@ class Context // Returns the value associated with the passed in key. context::ContextValue GetValue(nostd::string_view key) { -/* + // Iterate through the context nodes for (auto context = *context_ptr_; context != nullptr; context = context->next_) { + /* // Iterate through the internal data nodes for (DataList *data = context->head_; data != nullptr; data = data->next_) { @@ -79,9 +80,9 @@ class Context return data->value_; } } - } + }*/ } - */ + return ""; } From a7fbbd01505828a105cb40e69d01c77c5dd72933 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 18:14:43 +0000 Subject: [PATCH 73/93] formatted --- 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 47aa00b59c..b3d1c2a3f9 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -82,7 +82,7 @@ class Context } }*/ } - + return ""; } From dd3f297c9bddeb0957f1437dbdfd29fcf747438c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 19:39:03 +0000 Subject: [PATCH 74/93] shared Datalist instead of context list --- api/include/opentelemetry/context/context.h | 360 +++++++------------- api/test/context/context_test.cc | 66 ++-- 2 files changed, 157 insertions(+), 269 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index b3d1c2a3f9..6d1195d77d 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/shared_ptr.h" @@ -11,258 +12,145 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -// The context class provides a context identifier. Is built as a linked list -// of context nodes which each have access to the nodes before it, but not -// after. So each time a write is performed a new context is added to the list. -// Within each context node there is another linked list which holds the key -// and value data for each node. -class Context -{ - -public: - nostd::shared_ptr *context_ptr_; - - // Creates a new context by calling a constructor with the passed in - // keyValueIterable and returns a shared_ptr pointing to it. - template ::value> * = nullptr> - static nostd::shared_ptr CreateContext(T &keys_and_values) - { - nostd::shared_ptr temp = nostd::shared_ptr{new Context(keys_and_values)}; - temp->context_ptr_ = &temp; - return temp; - } - - // Creates a new context by calling a constructor with the passed in - // key and value and returns a shared_ptr pointing to it. - static nostd::shared_ptr CreateContext(nostd::string_view key, ContextValue value) - { - nostd::shared_ptr temp = nostd::shared_ptr{new Context(key, value)}; - temp->context_ptr_ = &temp; - return temp; - } - - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. - template ::value> * = nullptr> - nostd::shared_ptr SetValues(T &values) noexcept - { - nostd::shared_ptr temp = nostd::shared_ptr{new Context(values)}; - temp->context_ptr_ = &temp; - temp->SetNext(*context_ptr_); - return temp; - } - - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. - nostd::shared_ptr SetValue(nostd::string_view key, ContextValue value) noexcept + // The context class provides a context identifier. Is built as a linked list + // of context nodes which each have access to the nodes before it, but not + // after. So each time a write is performed a new context is added to the list. + // Within each context node there is another linked list which holds the key + // and value data for each node. + class Context { - nostd::shared_ptr context = nostd::shared_ptr{new Context(key, value)}; - context->context_ptr_ = &context; - context->SetNext(*context_ptr_); - return context; - } - // Returns the value associated with the passed in key. - context::ContextValue GetValue(nostd::string_view key) - { + public: - // Iterate through the context nodes - for (auto context = *context_ptr_; context != nullptr; context = context->next_) - { - /* - // Iterate through the internal data nodes - for (DataList *data = context->head_; data != nullptr; data = data->next_) - { - if (key.size() == data->key_length_) + // Creates a context object from a map of keys and identifiers, this will + // be the head of the context object linked list. + template ::value> * = nullptr> + Context(const T &keys_and_values) { - if (memcmp(key.data(), data->key_, data->key_length_) == 0) - { - return data->value_; - } + head_ = nostd::shared_ptr{new DataList(keys_and_values)}; + } + + Context(nostd::string_view key, ContextValue value) + { + head_ = nostd::shared_ptr{new DataList(key, value)}; } - }*/ - } - - return ""; - } - - // Iterates through the current and comparing context objects - // to check for equality. - bool operator==(const Context &other) - { - // If the two contexts are not at the place in the list - // they cannot be equivalent - if (next_ != other.next_) - { - return false; - } - - DataList *other_data = other.head_; - // Iterate through the lists and compare the keys - // TODO should also compare the values but the nostd::variant - // seems to not have a comparison operator - for (DataList *data = head_; data != nullptr; data = data->next_) - { - if (other_data == nullptr) + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + template ::value> * = nullptr> + Context SetValues(T &values) noexcept + { + Context context = Context(values); + context.head_->down_ = head_; + return context; + } + + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. + Context SetValue(nostd::string_view key, ContextValue value) noexcept { - return false; + Context context = Context(key, value); + context.head_->down_ = head_; + return context; } - if (data->key_ != other_data->key_) + + // Returns the value associated with the passed in key. + context::ContextValue GetValue(nostd::string_view key) { - return false; - } - data = data->next_; - other_data = other_data->next_; - } - return true; - } - // Copy constructor links to the same next node but copies the other's - // data list into a new linked list - Context(const Context &other) - { - next_ = other.next_; - head_ = new DataList; - DataList *data = head_; - // Copy the other data into a new linked list - for (DataList *other_data = other.head_; other_data != nullptr; other_data = other_data->next_) - { - if (other_data->next_ != nullptr) - { - data->next_ = new DataList; - } - else - { - data->next_ = nullptr; - } - data->key_length_ = other_data->key_length_; - data->key_ = new char[data->key_length_]; - memcpy(data->key_, other_data->key_, data->key_length_); - data->value_ = other_data->value_; - data = data->next_; - } - } - - // Assignment constructor links to the same next node but copies the other's - // data list into a new linked list - void operator=(const Context &other) - { - next_ = other.next_; - head_ = new DataList; - DataList *data = head_; - // Copy the other data into a new linked list - for (DataList *other_data = other.head_; other_data != nullptr; other_data = other_data->next_) - { - if (other_data->next_ != nullptr) - { - data->next_ = new DataList; - } - else - { - data->next_ = nullptr; + DataList * head = &*head_; + for(DataList* down = &*(head->down_); head != nullptr; down = &*(head->down_)){ + for (DataList* data = head; data != nullptr; data = data->next_) + { + if (key.size() == data->key_length_) + { + if (memcmp(key.data(), data->key_, data->key_length_) == 0) + { + return data->value_; + } + } + } + head = down; + if(head == nullptr){ + break; + } + } + return ""; } - data->key_length_ = other_data->key_length_; - data->key_ = new char[data->key_length_]; - memcpy(data->key_, other_data->key_, data->key_length_); - data->value_ = other_data->value_; - data = data->next_; - } - } - - ~Context() - { - for (DataList *data = head_; data != nullptr;) - { - DataList *next = data->next_; - delete data; - data = next; - } - } - - // Used to set the next node in the list - void SetNext(nostd::shared_ptr next) { next_ = next; } - -private: - Context() = default; - - // A linked list to contain the keys and values of this context node - class DataList - { - public: - DataList *next_; - - char *key_; - - size_t key_length_; - - ContextValue value_; + + private: + Context() = default; - DataList() { next_ = nullptr; } - - // Builds a data list off of a key and value iterable and returns the head - template ::value> * = nullptr> - DataList(const T &keys_and_vals) + // A linked list to contain the keys and values of this context node + class DataList { - auto iter = std::begin(keys_and_vals); - // Create list head - key_ = new char[nostd::string_view(iter->first).size()]; - key_length_ = nostd::string_view(iter->first).size(); - memcpy(key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - value_ = iter->second; - next_ = nullptr; - ++iter; + public: + DataList *next_; + + nostd::shared_ptr down_; + + char *key_; + + size_t key_length_; + + ContextValue value_; + + DataList() { next_ = nullptr; } + + // Builds a data list off of a key and value iterable and returns the head + template ::value> * = nullptr> + DataList(const T &keys_and_vals) + { + auto iter = std::begin(keys_and_vals); + // Create list head + key_ = new char[nostd::string_view(iter->first).size()]; + key_length_ = nostd::string_view(iter->first).size(); + memcpy(key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); + value_ = iter->second; + next_ = nullptr; + down_ = nostd::shared_ptr{nullptr}; + ++iter; + + DataList *previous_node = this; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) + { + DataList *node = new DataList(); + node->next_ = nullptr; + node->down_ = nostd::shared_ptr{nullptr}; + node->key_ = new char[nostd::string_view(iter->first).size()]; + node->key_length_ = nostd::string_view(iter->first).size(); + memcpy(node->key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = node; + } + } + + // Builds a data list with just a key and value, so it will just be the head + // and returns that head. + DataList(nostd::string_view key, ContextValue value) + { + key_ = new char[nostd::string_view(key).size()]; + key_length_ = nostd::string_view(key).size(); + memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); + value_ = value; + next_ = nullptr; + down_ = nostd::shared_ptr{nullptr}; + } - DataList *previous_node = this; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) - { - DataList *node = new DataList(); - node->next_ = nullptr; - node->key_ = new char[nostd::string_view(iter->first).size()]; - node->key_length_ = nostd::string_view(iter->first).size(); - memcpy(node->key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = node; - } - } + ~DataList() { + delete[] key_; + delete next_; + } + }; - // Builds a data list with just a key and value, so it will just be the head - // and returns that head. - DataList(nostd::string_view key, ContextValue value) - { - key_ = new char[nostd::string_view(key).size()]; - key_length_ = nostd::string_view(key).size(); - memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); - value_ = value; - next_ = nullptr; - } + // Head of the list which holds the keys and values of this context + nostd::shared_ptr head_; - ~DataList() { delete[] key_; } }; - - // Creates a context object from a map of keys and identifiers, this will - // be the head of the context object linked list. - template ::value> * = nullptr> - Context(const T &keys_and_values) - { - head_ = new DataList(keys_and_values); - next_ = nostd::shared_ptr{nullptr}; - } - - // Creates a context object from a key and value, this will be the head - // of the context object linked list. - Context(nostd::string_view key, ContextValue value) { head_ = new DataList(key, value); } - - // Head of the list which holds the keys and values of this context - DataList *head_ = nullptr; - - // Pointer to the next context object in the context list - nostd::shared_ptr next_; -}; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index fdd79327ad..0eef6d2273 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -10,31 +10,30 @@ using namespace opentelemetry; TEST(ContextTest, ContextIterableAcceptsMap) { std::map map_test = {{"test_key", "123"}}; - nostd::shared_ptr test_context = context::Context::CreateContext(map_test); + context::Context test_context = context::Context(map_test); } // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - - nostd::shared_ptr test_context = context::Context::CreateContext(map_test); - nostd::get(test_context->GetValue("test_key")); - // EXPECT_EQ(nostd::get(test_context->GetValue("test_key")), "123"); - // EXPECT_EQ(nostd::get(test_context->GetValue("foo_key")), "456"); + context::Context test_context = context::Context(map_test); + EXPECT_EQ(nostd::get(test_context.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(test_context.GetValue("foo_key")), "456"); } -/* + + // Tests that the SetValues method accepts an std::map. TEST(ContextTest, ContextSetValuesAcceptsMap) { std::map map_test = {{"test_key", "123"}}; std::map map_test_write = {{"foo_key", "456"}}; - nostd::shared_ptr test_context = context::Context::CreateContext(map_test); - nostd::shared_ptr foo_test = test_context->SetValues(map_test_write); - - EXPECT_EQ(nostd::get(foo_test->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(foo_test->GetValue("foo_key")), "456"); + context::Context test_context = context::Context(map_test); + context::Context foo_context = test_context.SetValues(map_test_write); + + EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), "456"); } // Tests that the SetValues method accepts a nostd::string_view and @@ -44,12 +43,12 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) nostd::string_view string_view_test = "string_view"; context::ContextValue context_value_test = "123"; - nostd::shared_ptr test_context = - context::Context::CreateContext(string_view_test, context_value_test); - nostd::shared_ptr foo_context = - test_context->SetValue(string_view_test, context_value_test); + context::Context test_context = + context::Context(string_view_test, context_value_test); + context::Context foo_context = + test_context.SetValue(string_view_test, context_value_test); - EXPECT_EQ(nostd::get(foo_context->GetValue(string_view_test)), "123"); + EXPECT_EQ(nostd::get(foo_context.GetValue(string_view_test)), "123"); } // Tests that the original context does not change when a value is @@ -59,10 +58,10 @@ TEST(ContextTest, ContextImmutability) std::map map_test = {{"test_key", "123"}}; std::map foo_map = {{"foo_key", "456"}}; - nostd::shared_ptr context_test = context::Context::CreateContext(map_test); - nostd::shared_ptr context_foo = context_test->SetValue("foo_key", "456"); + context::Context context_test = context::Context(map_test); + context::Context context_foo = context_test.SetValue("foo_key", "456"); - EXPECT_NE(nostd::get(context_test->GetValue("foo_key")), "456"); + EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), "456"); } // Tests that writing the same to a context overwrites the original value. @@ -70,12 +69,13 @@ TEST(ContextTest, ContextKeyOverwrite) { std::map map_test = {{"test_key", "123"}}; - nostd::shared_ptr context_test = context::Context::CreateContext(map_test); - nostd::shared_ptr context_foo = context_test->SetValue("test_key", "456"); + context::Context context_test = context::Context(map_test); + context::Context context_foo = context_test.SetValue("test_key", "456"); - EXPECT_EQ(nostd::get(context_foo->GetValue("test_key")), "456"); + EXPECT_EQ(nostd::get(context_foo.GetValue("test_key")), "456"); } + // Tests that the new Context Objects inherits the keys and values // of the original context object. TEST(ContextTest, ContextInheritance) @@ -85,11 +85,11 @@ TEST(ContextTest, ContextInheritance) M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; M m2 = {{"other_key", "789"}}; - nostd::shared_ptr test_context = context::Context::CreateContext(m1); - nostd::shared_ptr foo_context = test_context->SetValues(m2); + context::Context test_context = context::Context(m1); + context::Context foo_context = test_context.SetValues(m2); - EXPECT_EQ(nostd::get(foo_context->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(foo_context->GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), "456"); } // Tests that copying a context copies the key value pairs as expected. @@ -98,10 +98,10 @@ TEST(ContextTest, ContextCopyOperator) std::map test_map = { {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; - nostd::shared_ptr test_context = context::Context::CreateContext(test_map); - nostd::shared_ptr copied_context = test_context; + context::Context test_context = context::Context(test_map); + context::Context copied_context = test_context; - EXPECT_EQ(nostd::get(copied_context->GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(copied_context->GetValue("foo_key")), "456"); - EXPECT_EQ(nostd::get(copied_context->GetValue("other_key")), "789"); -}*/ + EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); + EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); +} From 82920126e014304890c6f5211772345316e58957 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 20:11:02 +0000 Subject: [PATCH 75/93] one long list --- api/include/opentelemetry/context/context.h | 47 +++++++++------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 6d1195d77d..c226014f8e 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -13,10 +13,7 @@ namespace context { // The context class provides a context identifier. Is built as a linked list - // of context nodes which each have access to the nodes before it, but not - // after. So each time a write is performed a new context is added to the list. - // Within each context node there is another linked list which holds the key - // and value data for each node. + // of DataList node class Context { @@ -41,7 +38,12 @@ namespace context Context SetValues(T &values) noexcept { Context context = Context(values); - context.head_->down_ = head_; + + nostd::shared_ptr last_node; + for(nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_){ + last_node = data; + } + last_node->next_ = head_; return context; } @@ -50,7 +52,11 @@ namespace context Context SetValue(nostd::string_view key, ContextValue value) noexcept { Context context = Context(key, value); - context.head_->down_ = head_; + nostd::shared_ptr last_node; + for(nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_){ + last_node = data; + } + last_node->next_ = head_; return context; } @@ -58,9 +64,7 @@ namespace context context::ContextValue GetValue(nostd::string_view key) { - DataList * head = &*head_; - for(DataList* down = &*(head->down_); head != nullptr; down = &*(head->down_)){ - for (DataList* data = head; data != nullptr; data = data->next_) + for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) { if (key.size() == data->key_length_) { @@ -70,11 +74,6 @@ namespace context } } } - head = down; - if(head == nullptr){ - break; - } - } return ""; } @@ -85,9 +84,7 @@ namespace context class DataList { public: - DataList *next_; - - nostd::shared_ptr down_; + nostd::shared_ptr next_; char *key_; @@ -109,24 +106,22 @@ namespace context memcpy(key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size() * sizeof(char)); value_ = iter->second; - next_ = nullptr; - down_ = nostd::shared_ptr{nullptr}; + next_ = nostd::shared_ptr{nullptr}; ++iter; - DataList *previous_node = this; + DataList* previous_node = this; // Iterate over the keys and values iterable and add nodes for (; iter != std::end(keys_and_vals); ++iter) { - DataList *node = new DataList(); - node->next_ = nullptr; - node->down_ = nostd::shared_ptr{nullptr}; + nostd::shared_ptr node = nostd::shared_ptr{new DataList()}; + node->next_ = nostd::shared_ptr{nullptr}; node->key_ = new char[nostd::string_view(iter->first).size()]; node->key_length_ = nostd::string_view(iter->first).size(); memcpy(node->key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size() * sizeof(char)); node->value_ = iter->second; previous_node->next_ = node; - previous_node = node; + previous_node = &*node; } } @@ -138,13 +133,11 @@ namespace context key_length_ = nostd::string_view(key).size(); memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); value_ = value; - next_ = nullptr; - down_ = nostd::shared_ptr{nullptr}; + next_ = nostd::shared_ptr{nullptr}; } ~DataList() { delete[] key_; - delete next_; } }; From 7534cbca942fdfa2cc06237b4e5a934e30f3e58d Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 20:16:09 +0000 Subject: [PATCH 76/93] updated some comments --- api/include/opentelemetry/context/context.h | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index c226014f8e..e95bb9ec99 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/shared_ptr.h" @@ -13,32 +12,36 @@ namespace context { // The context class provides a context identifier. Is built as a linked list - // of DataList node + // of DataList nodes and each context holds a shared_ptr to a place within + // the list that determines which keys and values it has access to. All that + // come before and none that come after. class Context { public: // Creates a context object from a map of keys and identifiers, this will - // be the head of the context object linked list. + // hold a shared_ptr to the head of the DataList linked list template ::value> * = nullptr> Context(const T &keys_and_values) { head_ = nostd::shared_ptr{new DataList(keys_and_values)}; } + // Creates a context object from a key and value, this will + // hold a shared_ptr to the head of the DataList linked list Context(nostd::string_view key, ContextValue value) { head_ = nostd::shared_ptr{new DataList(key, value)}; } // Accepts a new iterable and then returns a new context that - // contains the new key and value data. + // contains the new key and value data. It attaches the + // exisiting list to the end of the new list. template ::value> * = nullptr> Context SetValues(T &values) noexcept { Context context = Context(values); - nostd::shared_ptr last_node; for(nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_){ last_node = data; @@ -48,7 +51,8 @@ namespace context } // Accepts a new iterable and then returns a new context that - // contains the new key and value data. + // contains the new key and value data. It attaches the + // exisiting list to the end of the new list. Context SetValue(nostd::string_view key, ContextValue value) noexcept { Context context = Context(key, value); @@ -63,7 +67,6 @@ namespace context // Returns the value associated with the passed in key. context::ContextValue GetValue(nostd::string_view key) { - for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) { if (key.size() == data->key_length_) From c925962d82335d40d05c1e7901c7c48ae8e37893 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 20:18:05 +0000 Subject: [PATCH 77/93] removed unnecessary complexity from SetValue --- api/include/opentelemetry/context/context.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index e95bb9ec99..fc9cb7fba0 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -56,11 +56,7 @@ namespace context Context SetValue(nostd::string_view key, ContextValue value) noexcept { Context context = Context(key, value); - nostd::shared_ptr last_node; - for(nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_){ - last_node = data; - } - last_node->next_ = head_; + context.head_->next_ = head_; return context; } From b2e53b23f2c714007d67e9e7f90ab6e686f13fe9 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Mon, 20 Jul 2020 20:24:41 +0000 Subject: [PATCH 78/93] auto formatted --- api/include/opentelemetry/context/context.h | 241 ++++++++++---------- api/test/context/context_test.cc | 16 +- 2 files changed, 125 insertions(+), 132 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index fc9cb7fba0..20366e370e 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -11,138 +11,135 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { - // The context class provides a context identifier. Is built as a linked list - // of DataList nodes and each context holds a shared_ptr to a place within - // the list that determines which keys and values it has access to. All that - // come before and none that come after. - class Context - { +// The context class provides a context identifier. Is built as a linked list +// of DataList nodes and each context holds a shared_ptr to a place within +// the list that determines which keys and values it has access to. All that +// come before and none that come after. +class Context +{ - public: +public: + // Creates a context object from a map of keys and identifiers, this will + // hold a shared_ptr to the head of the DataList linked list + template ::value> * = nullptr> + Context(const T &keys_and_values) + { + head_ = nostd::shared_ptr{new DataList(keys_and_values)}; + } - // Creates a context object from a map of keys and identifiers, this will - // hold a shared_ptr to the head of the DataList linked list - template ::value> * = nullptr> - Context(const T &keys_and_values) - { - head_ = nostd::shared_ptr{new DataList(keys_and_values)}; - } - - // Creates a context object from a key and value, this will - // hold a shared_ptr to the head of the DataList linked list - Context(nostd::string_view key, ContextValue value) - { - head_ = nostd::shared_ptr{new DataList(key, value)}; - } + // Creates a context object from a key and value, this will + // hold a shared_ptr to the head of the DataList linked list + Context(nostd::string_view key, ContextValue value) + { + head_ = nostd::shared_ptr{new DataList(key, value)}; + } + + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. It attaches the + // exisiting list to the end of the new list. + template ::value> * = nullptr> + Context SetValues(T &values) noexcept + { + Context context = Context(values); + nostd::shared_ptr last_node; + for (nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_) + { + last_node = data; + } + last_node->next_ = head_; + return context; + } + + // Accepts a new iterable and then returns a new context that + // contains the new key and value data. It attaches the + // exisiting list to the end of the new list. + Context SetValue(nostd::string_view key, ContextValue value) noexcept + { + Context context = Context(key, value); + context.head_->next_ = head_; + return context; + } - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. It attaches the - // exisiting list to the end of the new list. - template ::value> * = nullptr> - Context SetValues(T &values) noexcept + // Returns the value associated with the passed in key. + context::ContextValue GetValue(nostd::string_view key) + { + for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) + { + if (key.size() == data->key_length_) + { + if (memcmp(key.data(), data->key_, data->key_length_) == 0) { - Context context = Context(values); - nostd::shared_ptr last_node; - for(nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_){ - last_node = data; - } - last_node->next_ = head_; - return context; + return data->value_; } - - // Accepts a new iterable and then returns a new context that - // contains the new key and value data. It attaches the - // exisiting list to the end of the new list. - Context SetValue(nostd::string_view key, ContextValue value) noexcept - { - Context context = Context(key, value); - context.head_->next_ = head_; - return context; } - - // Returns the value associated with the passed in key. - context::ContextValue GetValue(nostd::string_view key) - { - for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) - { - if (key.size() == data->key_length_) - { - if (memcmp(key.data(), data->key_, data->key_length_) == 0) - { - return data->value_; - } - } - } - return ""; - } - - private: - Context() = default; + } + return ""; + } - // A linked list to contain the keys and values of this context node - class DataList - { - public: - nostd::shared_ptr next_; - - char *key_; - - size_t key_length_; - - ContextValue value_; - - DataList() { next_ = nullptr; } - - // Builds a data list off of a key and value iterable and returns the head - template ::value> * = nullptr> - DataList(const T &keys_and_vals) - { - auto iter = std::begin(keys_and_vals); - // Create list head - key_ = new char[nostd::string_view(iter->first).size()]; - key_length_ = nostd::string_view(iter->first).size(); - memcpy(key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - value_ = iter->second; - next_ = nostd::shared_ptr{nullptr}; - ++iter; - - DataList* previous_node = this; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) - { - nostd::shared_ptr node = nostd::shared_ptr{new DataList()}; - node->next_ = nostd::shared_ptr{nullptr}; - node->key_ = new char[nostd::string_view(iter->first).size()]; - node->key_length_ = nostd::string_view(iter->first).size(); - memcpy(node->key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = &*node; - } - } - - // Builds a data list with just a key and value, so it will just be the head - // and returns that head. - DataList(nostd::string_view key, ContextValue value) - { - key_ = new char[nostd::string_view(key).size()]; - key_length_ = nostd::string_view(key).size(); - memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); - value_ = value; - next_ = nostd::shared_ptr{nullptr}; - } +private: + Context() = default; + + // A linked list to contain the keys and values of this context node + class DataList + { + public: + nostd::shared_ptr next_; + + char *key_; - ~DataList() { - delete[] key_; - } - }; + size_t key_length_; - // Head of the list which holds the keys and values of this context - nostd::shared_ptr head_; + ContextValue value_; + DataList() { next_ = nullptr; } + + // Builds a data list off of a key and value iterable and returns the head + template ::value> * = nullptr> + DataList(const T &keys_and_vals) + { + auto iter = std::begin(keys_and_vals); + // Create list head + key_ = new char[nostd::string_view(iter->first).size()]; + key_length_ = nostd::string_view(iter->first).size(); + memcpy(key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); + value_ = iter->second; + next_ = nostd::shared_ptr{nullptr}; + ++iter; + + DataList *previous_node = this; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) + { + nostd::shared_ptr node = nostd::shared_ptr{new DataList()}; + node->next_ = nostd::shared_ptr{nullptr}; + node->key_ = new char[nostd::string_view(iter->first).size()]; + node->key_length_ = nostd::string_view(iter->first).size(); + memcpy(node->key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = &*node; + } + } + + // Builds a data list with just a key and value, so it will just be the head + // and returns that head. + DataList(nostd::string_view key, ContextValue value) + { + key_ = new char[nostd::string_view(key).size()]; + key_length_ = nostd::string_view(key).size(); + memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); + value_ = value; + next_ = nostd::shared_ptr{nullptr}; + } + + ~DataList() { delete[] key_; } }; + + // Head of the list which holds the keys and values of this context + nostd::shared_ptr head_; +}; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 0eef6d2273..f073a39c8b 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -10,19 +10,18 @@ using namespace opentelemetry; TEST(ContextTest, ContextIterableAcceptsMap) { std::map map_test = {{"test_key", "123"}}; - context::Context test_context = context::Context(map_test); + context::Context test_context = context::Context(map_test); } // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - context::Context test_context = context::Context(map_test); + context::Context test_context = context::Context(map_test); EXPECT_EQ(nostd::get(test_context.GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(test_context.GetValue("foo_key")), "456"); } - // Tests that the SetValues method accepts an std::map. TEST(ContextTest, ContextSetValuesAcceptsMap) { @@ -30,8 +29,8 @@ TEST(ContextTest, ContextSetValuesAcceptsMap) std::map map_test_write = {{"foo_key", "456"}}; context::Context test_context = context::Context(map_test); - context::Context foo_context = test_context.SetValues(map_test_write); - + context::Context foo_context = test_context.SetValues(map_test_write); + EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), "123"); EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), "456"); } @@ -43,10 +42,8 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) nostd::string_view string_view_test = "string_view"; context::ContextValue context_value_test = "123"; - context::Context test_context = - context::Context(string_view_test, context_value_test); - context::Context foo_context = - test_context.SetValue(string_view_test, context_value_test); + context::Context test_context = context::Context(string_view_test, context_value_test); + context::Context foo_context = test_context.SetValue(string_view_test, context_value_test); EXPECT_EQ(nostd::get(foo_context.GetValue(string_view_test)), "123"); } @@ -75,7 +72,6 @@ TEST(ContextTest, ContextKeyOverwrite) EXPECT_EQ(nostd::get(context_foo.GetValue("test_key")), "456"); } - // Tests that the new Context Objects inherits the keys and values // of the original context object. TEST(ContextTest, ContextInheritance) From 067e7dd259e884fdb93bbc80688ef99d59bc62ad Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 00:07:51 +0000 Subject: [PATCH 79/93] stylistic changes updated ContextValue, add HasKey method --- api/include/opentelemetry/context/context.h | 85 ++++++++++++------- .../opentelemetry/context/context_value.h | 18 +--- api/test/context/context_test.cc | 70 +++++++++------ 3 files changed, 102 insertions(+), 71 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 20366e370e..82463edde7 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -21,7 +21,7 @@ class Context public: // Creates a context object from a map of keys and identifiers, this will // hold a shared_ptr to the head of the DataList linked list - template ::value> * = nullptr> + template Context(const T &keys_and_values) { head_ = nostd::shared_ptr{new DataList(keys_and_values)}; @@ -37,7 +37,7 @@ class Context // Accepts a new iterable and then returns a new context that // contains the new key and value data. It attaches the // exisiting list to the end of the new list. - template ::value> * = nullptr> + template Context SetValues(T &values) noexcept { Context context = Context(values); @@ -73,7 +73,23 @@ class Context } } } - return ""; + return (int64_t)0; + } + + // Checks for key and returns true if found + bool HasKey(nostd::string_view key) + { + for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) + { + if (key.size() == data->key_length_) + { + if (memcmp(key.data(), data->key_, data->key_length_) == 0) + { + return true; + } + } + } + return false; } private: @@ -94,33 +110,38 @@ class Context DataList() { next_ = nullptr; } // Builds a data list off of a key and value iterable and returns the head - template ::value> * = nullptr> + template DataList(const T &keys_and_vals) { - auto iter = std::begin(keys_and_vals); - // Create list head - key_ = new char[nostd::string_view(iter->first).size()]; - key_length_ = nostd::string_view(iter->first).size(); - memcpy(key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - value_ = iter->second; - next_ = nostd::shared_ptr{nullptr}; - ++iter; - - DataList *previous_node = this; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) + if (std::begin(keys_and_vals) != std::end(keys_and_vals)) { - nostd::shared_ptr node = nostd::shared_ptr{new DataList()}; - node->next_ = nostd::shared_ptr{nullptr}; - node->key_ = new char[nostd::string_view(iter->first).size()]; - node->key_length_ = nostd::string_view(iter->first).size(); - memcpy(node->key_, nostd::string_view(iter->first).data(), + auto iter = std::begin(keys_and_vals); + // Create list head + key_ = new char[nostd::string_view(iter->first).size()]; + key_length_ = nostd::string_view(iter->first).size(); + memcpy(key_, nostd::string_view(iter->first).data(), nostd::string_view(iter->first).size() * sizeof(char)); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = &*node; + value_ = iter->second; + next_ = nostd::shared_ptr{nullptr}; + ++iter; + + DataList *previous_node = this; + // Iterate over the keys and values iterable and add nodes + for (; iter != std::end(keys_and_vals); ++iter) + { + nostd::shared_ptr node = nostd::shared_ptr{new DataList()}; + node->key_ = new char[nostd::string_view(iter->first).size()]; + node->key_length_ = nostd::string_view(iter->first).size(); + memcpy(node->key_, nostd::string_view(iter->first).data(), + nostd::string_view(iter->first).size() * sizeof(char)); + node->value_ = iter->second; + previous_node->next_ = node; + previous_node = &*node; + } + } + else + { + key_ = nullptr; } } @@ -129,13 +150,19 @@ class Context DataList(nostd::string_view key, ContextValue value) { key_ = new char[nostd::string_view(key).size()]; - key_length_ = nostd::string_view(key).size(); - memcpy(key_, nostd::string_view(key).data(), nostd::string_view(key).size() * sizeof(char)); + key_length_ = key.size(); + memcpy(key_, key.data(), key.size() * sizeof(char)); value_ = value; next_ = nostd::shared_ptr{nullptr}; } - ~DataList() { delete[] key_; } + ~DataList() + { + if (key_ != nullptr) + { + delete[] key_; + } + } }; // Head of the list which holds the keys and values of this context diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index bccdac46d1..f6db04280f 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -3,26 +3,14 @@ #include #include "opentelemetry/nostd/span.h" -#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -using ContextValue = nostd::variant, - nostd::span, - nostd::span, - nostd::span, - nostd::span, - nostd::span, - nostd::span>; +using ContextValue = nostd::variant; } // namespace context OPENTELEMETRY_END_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index f073a39c8b..2dc26acc21 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -9,30 +9,30 @@ using namespace opentelemetry; // Tests that the context constructor accepts an std::map. TEST(ContextTest, ContextIterableAcceptsMap) { - std::map map_test = {{"test_key", "123"}}; + std::map map_test = {{"test_key", 123}}; context::Context test_context = context::Context(map_test); } // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { - std::map map_test = {{"test_key", "123"}, {"foo_key", "456"}}; - context::Context test_context = context::Context(map_test); - EXPECT_EQ(nostd::get(test_context.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(test_context.GetValue("foo_key")), "456"); + std::map map_test = {{"test_key", 123}, {"foo_key", 456}}; + context::Context test_context = context::Context(map_test); + EXPECT_EQ(nostd::get(test_context.GetValue("test_key")), 123); + EXPECT_EQ(nostd::get(test_context.GetValue("foo_key")), 456); } // Tests that the SetValues method accepts an std::map. TEST(ContextTest, ContextSetValuesAcceptsMap) { - std::map map_test = {{"test_key", "123"}}; - std::map map_test_write = {{"foo_key", "456"}}; + std::map map_test = {{"test_key", 123}}; + std::map map_test_write = {{"foo_key", 456}}; context::Context test_context = context::Context(map_test); context::Context foo_context = test_context.SetValues(map_test_write); - EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), 123); + EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), 456); } // Tests that the SetValues method accepts a nostd::string_view and @@ -40,36 +40,35 @@ TEST(ContextTest, ContextSetValuesAcceptsMap) TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) { nostd::string_view string_view_test = "string_view"; - context::ContextValue context_value_test = "123"; + context::ContextValue context_value_test = 123; context::Context test_context = context::Context(string_view_test, context_value_test); context::Context foo_context = test_context.SetValue(string_view_test, context_value_test); - EXPECT_EQ(nostd::get(foo_context.GetValue(string_view_test)), "123"); + EXPECT_EQ(nostd::get(foo_context.GetValue(string_view_test)), 123); } // Tests that the original context does not change when a value is // written to it. TEST(ContextTest, ContextImmutability) { - std::map map_test = {{"test_key", "123"}}; - std::map foo_map = {{"foo_key", "456"}}; + std::map map_test = {{"test_key", 123}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("foo_key", "456"); + context::Context context_foo = context_test.SetValue("foo_key", 456); - EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), "456"); + EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), 456); } // Tests that writing the same to a context overwrites the original value. TEST(ContextTest, ContextKeyOverwrite) { - std::map map_test = {{"test_key", "123"}}; + std::map map_test = {{"test_key", 123}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("test_key", "456"); + context::Context context_foo = context_test.SetValue("test_key", 456); - EXPECT_EQ(nostd::get(context_foo.GetValue("test_key")), "456"); + EXPECT_EQ(nostd::get(context_foo.GetValue("test_key")), 456); } // Tests that the new Context Objects inherits the keys and values @@ -78,26 +77,43 @@ TEST(ContextTest, ContextInheritance) { using M = std::map; - M m1 = {{"test_key", "123"}, {"foo_key", "456"}}; - M m2 = {{"other_key", "789"}}; + M m1 = {{"test_key", 123}, {"foo_key", 456}}; + M m2 = {{"other_key", 789}}; context::Context test_context = context::Context(m1); context::Context foo_context = test_context.SetValues(m2); - EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), "456"); + EXPECT_EQ(nostd::get(foo_context.GetValue("test_key")), 123); + EXPECT_EQ(nostd::get(foo_context.GetValue("foo_key")), 456); } // Tests that copying a context copies the key value pairs as expected. TEST(ContextTest, ContextCopyOperator) { - std::map test_map = { - {"test_key", "123"}, {"foo_key", "456"}, {"other_key", "789"}}; + std::map test_map = { + {"test_key", 123}, {"foo_key", 456}, {"other_key", 789}}; context::Context test_context = context::Context(test_map); context::Context copied_context = test_context; - EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), "123"); - EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), "456"); - EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), "789"); + EXPECT_EQ(nostd::get(copied_context.GetValue("test_key")), 123); + EXPECT_EQ(nostd::get(copied_context.GetValue("foo_key")), 456); + EXPECT_EQ(nostd::get(copied_context.GetValue("other_key")), 789); +} + +// Tests that the Context accepts an empty map. +TEST(ContextTest, ContextEmptyMap) +{ + std::map map_test = {}; + context::Context test_context = context::Context(map_test); +} + +// Tests that if a key exists within a context has key will return true +// false if not. +TEST(ContextTest, ContextHasKey) +{ + std::map map_test = {{"test_key", 123}}; + context::Context context_test = context::Context(map_test); + EXPECT_TRUE(context_test.HasKey("test_key")); + EXPECT_FALSE(context_test.HasKey("foo_key")); } From 8396792334b9f2ee800a8b652e53b1cffb89f057 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 00:47:58 +0000 Subject: [PATCH 80/93] removed reference to span_context.h --- api/include/opentelemetry/context/context_value.h | 1 - 1 file changed, 1 deletion(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index f6db04280f..f9e97e6100 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -5,7 +5,6 @@ #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE From 86b032cfca48a4f4938354e8569cf27c57d37214 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 03:18:44 +0000 Subject: [PATCH 81/93] explicit conversion to contextValue in tests --- api/test/context/context_test.cc | 37 ++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index 2dc26acc21..cb147a075a 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -9,14 +9,16 @@ using namespace opentelemetry; // Tests that the context constructor accepts an std::map. TEST(ContextTest, ContextIterableAcceptsMap) { - std::map map_test = {{"test_key", 123}}; - context::Context test_context = context::Context(map_test); + std::map map_test = { + {"test_key", context::ContextValue(123)}}; + context::Context test_context = context::Context(map_test); } // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { - std::map map_test = {{"test_key", 123}, {"foo_key", 456}}; + std::map map_test = {{"test_key", context::ContextValue(123)}, + {"foo_key", context::ContextValue(456)}}; context::Context test_context = context::Context(map_test); EXPECT_EQ(nostd::get(test_context.GetValue("test_key")), 123); EXPECT_EQ(nostd::get(test_context.GetValue("foo_key")), 456); @@ -25,8 +27,10 @@ TEST(ContextTest, ContextGetValueReturnsExpectedValue) // Tests that the SetValues method accepts an std::map. TEST(ContextTest, ContextSetValuesAcceptsMap) { - std::map map_test = {{"test_key", 123}}; - std::map map_test_write = {{"foo_key", 456}}; + std::map map_test = { + {"test_key", context::ContextValue(123)}}; + std::map map_test_write = { + {"foo_key", context::ContextValue(456)}}; context::Context test_context = context::Context(map_test); context::Context foo_context = test_context.SetValues(map_test_write); @@ -52,10 +56,11 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) // written to it. TEST(ContextTest, ContextImmutability) { - std::map map_test = {{"test_key", 123}}; + std::map map_test = { + {"test_key", context::ContextValue(123)}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("foo_key", 456); + context::Context context_foo = context_test.SetValue("foo_key", context::ContextValue(456)); EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), 456); } @@ -63,10 +68,11 @@ TEST(ContextTest, ContextImmutability) // Tests that writing the same to a context overwrites the original value. TEST(ContextTest, ContextKeyOverwrite) { - std::map map_test = {{"test_key", 123}}; + std::map map_test = { + {"test_key", context::ContextValue(123)}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("test_key", 456); + context::Context context_foo = context_test.SetValue("test_key", context::ContextValue(456)); EXPECT_EQ(nostd::get(context_foo.GetValue("test_key")), 456); } @@ -77,8 +83,8 @@ TEST(ContextTest, ContextInheritance) { using M = std::map; - M m1 = {{"test_key", 123}, {"foo_key", 456}}; - M m2 = {{"other_key", 789}}; + M m1 = {{"test_key", context::ContextValue(123)}, {"foo_key", context::ContextValue(456)}}; + M m2 = {{"other_key", context::ContextValue(789)}}; context::Context test_context = context::Context(m1); context::Context foo_context = test_context.SetValues(m2); @@ -91,7 +97,9 @@ TEST(ContextTest, ContextInheritance) TEST(ContextTest, ContextCopyOperator) { std::map test_map = { - {"test_key", 123}, {"foo_key", 456}, {"other_key", 789}}; + {"test_key", context::ContextValue(123)}, + {"foo_key", context::ContextValue(456)}, + {"other_key", context::ContextValue(789)}}; context::Context test_context = context::Context(test_map); context::Context copied_context = test_context; @@ -112,8 +120,9 @@ TEST(ContextTest, ContextEmptyMap) // false if not. TEST(ContextTest, ContextHasKey) { - std::map map_test = {{"test_key", 123}}; - context::Context context_test = context::Context(map_test); + std::map map_test = { + {"test_key", context::ContextValue(123)}}; + context::Context context_test = context::Context(map_test); EXPECT_TRUE(context_test.HasKey("test_key")); EXPECT_FALSE(context_test.HasKey("foo_key")); } From f78ab7425421eadf99d83f0c51305498b8d953d1 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 14:01:17 +0000 Subject: [PATCH 82/93] explicit type case in tests --- api/test/context/context_test.cc | 38 +++++++++++++------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index cb147a075a..b912c6f1ad 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -9,16 +9,15 @@ using namespace opentelemetry; // Tests that the context constructor accepts an std::map. TEST(ContextTest, ContextIterableAcceptsMap) { - std::map map_test = { - {"test_key", context::ContextValue(123)}}; - context::Context test_context = context::Context(map_test); + std::map map_test = {{"test_key", (int64_t)123}}; + context::Context test_context = context::Context(map_test); } // Tests that the GetValue method returns the expected value. TEST(ContextTest, ContextGetValueReturnsExpectedValue) { - std::map map_test = {{"test_key", context::ContextValue(123)}, - {"foo_key", context::ContextValue(456)}}; + std::map map_test = {{"test_key", (int64_t)123}, + {"foo_key", (int64_t)456}}; context::Context test_context = context::Context(map_test); EXPECT_EQ(nostd::get(test_context.GetValue("test_key")), 123); EXPECT_EQ(nostd::get(test_context.GetValue("foo_key")), 456); @@ -27,10 +26,8 @@ TEST(ContextTest, ContextGetValueReturnsExpectedValue) // Tests that the SetValues method accepts an std::map. TEST(ContextTest, ContextSetValuesAcceptsMap) { - std::map map_test = { - {"test_key", context::ContextValue(123)}}; - std::map map_test_write = { - {"foo_key", context::ContextValue(456)}}; + std::map map_test = {{"test_key", (int64_t)123}}; + std::map map_test_write = {{"foo_key", (int64_t)456}}; context::Context test_context = context::Context(map_test); context::Context foo_context = test_context.SetValues(map_test_write); @@ -56,11 +53,10 @@ TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) // written to it. TEST(ContextTest, ContextImmutability) { - std::map map_test = { - {"test_key", context::ContextValue(123)}}; + std::map map_test = {{"test_key", (int64_t)123}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("foo_key", context::ContextValue(456)); + context::Context context_foo = context_test.SetValue("foo_key", 456); EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), 456); } @@ -68,11 +64,10 @@ TEST(ContextTest, ContextImmutability) // Tests that writing the same to a context overwrites the original value. TEST(ContextTest, ContextKeyOverwrite) { - std::map map_test = { - {"test_key", context::ContextValue(123)}}; + std::map map_test = {{"test_key", (int64_t)123}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("test_key", context::ContextValue(456)); + context::Context context_foo = context_test.SetValue("test_key", (int64_t)456); EXPECT_EQ(nostd::get(context_foo.GetValue("test_key")), 456); } @@ -83,8 +78,8 @@ TEST(ContextTest, ContextInheritance) { using M = std::map; - M m1 = {{"test_key", context::ContextValue(123)}, {"foo_key", context::ContextValue(456)}}; - M m2 = {{"other_key", context::ContextValue(789)}}; + M m1 = {{"test_key", (int64_t)123}, {"foo_key", (int64_t)456}}; + M m2 = {{"other_key", (int64_t)789}}; context::Context test_context = context::Context(m1); context::Context foo_context = test_context.SetValues(m2); @@ -97,9 +92,7 @@ TEST(ContextTest, ContextInheritance) TEST(ContextTest, ContextCopyOperator) { std::map test_map = { - {"test_key", context::ContextValue(123)}, - {"foo_key", context::ContextValue(456)}, - {"other_key", context::ContextValue(789)}}; + {"test_key", (int64_t)123}, {"foo_key", (int64_t)456}, {"other_key", (int64_t)789}}; context::Context test_context = context::Context(test_map); context::Context copied_context = test_context; @@ -120,9 +113,8 @@ TEST(ContextTest, ContextEmptyMap) // false if not. TEST(ContextTest, ContextHasKey) { - std::map map_test = { - {"test_key", context::ContextValue(123)}}; - context::Context context_test = context::Context(map_test); + std::map map_test = {{"test_key", (int64_t)123}}; + context::Context context_test = context::Context(map_test); EXPECT_TRUE(context_test.HasKey("test_key")); EXPECT_FALSE(context_test.HasKey("foo_key")); } From 77c4c9a711f2dfbe648368320745549b0dcf545c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 14:18:22 +0000 Subject: [PATCH 83/93] added nostd::shared_ptr to ContextValue --- api/include/opentelemetry/context/context_value.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index f9e97e6100..8fd2fabe8c 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -2,14 +2,17 @@ #include +#include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -using ContextValue = nostd::variant; +using ContextValue = + nostd::variant>; } // namespace context OPENTELEMETRY_END_NAMESPACE From f63d595461ad7811eefe3a518c40a58aadba66fd Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 14:34:25 +0000 Subject: [PATCH 84/93] removed span_context.h include, so tests would pass, not sure why --- api/include/opentelemetry/context/context_value.h | 1 - 1 file changed, 1 deletion(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 8fd2fabe8c..082c17f26b 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -6,7 +6,6 @@ #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE From 5f25cf1251f0296340d7595a1826369ac5727e16 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 14:36:55 +0000 Subject: [PATCH 85/93] readded span_context.h --- api/include/opentelemetry/context/context_value.h | 1 + 1 file changed, 1 insertion(+) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 082c17f26b..7bf5e877ff 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -5,6 +5,7 @@ #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" +#include "opentelemetry/trace/span_context.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/version.h" From 86eacca5f5a0b44c0eaf0ee6daa4641eed931efb Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 14:41:01 +0000 Subject: [PATCH 86/93] re-added span_context include --- api/include/opentelemetry/context/context_value.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 7bf5e877ff..8fd2fabe8c 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -5,8 +5,8 @@ #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" -#include "opentelemetry/trace/span_context.h" #include "opentelemetry/nostd/variant.h" +#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE From 7a2ea4c457f35da47136f206e8ede8d755efdeb2 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 16:12:43 +0000 Subject: [PATCH 87/93] testing for the include failure --- api/include/opentelemetry/context/context_value.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index 8fd2fabe8c..e16464c319 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -6,13 +6,14 @@ #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" -#include "opentelemetry/trace/span_context.h" +//#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { using ContextValue = - nostd::variant>; + nostd::variant*/>; } // namespace context OPENTELEMETRY_END_NAMESPACE From 961f7ed19f461ae115c8c22f10fde3567f89531c Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 16:30:17 +0000 Subject: [PATCH 88/93] removed key_value_iterable_include --- api/include/opentelemetry/context/context_value.h | 1 - api/test/context/context_test.cc | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index e16464c319..dcb987d3d5 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -7,7 +7,6 @@ #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" //#include "opentelemetry/trace/span_context.h" -#include "opentelemetry/trace/key_value_iterable_view.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index b912c6f1ad..26fc5110bf 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -41,7 +41,7 @@ TEST(ContextTest, ContextSetValuesAcceptsMap) TEST(ContextTest, ContextSetValuesAcceptsStringViewContextValue) { nostd::string_view string_view_test = "string_view"; - context::ContextValue context_value_test = 123; + context::ContextValue context_value_test = (int64_t)123; context::Context test_context = context::Context(string_view_test, context_value_test); context::Context foo_context = test_context.SetValue(string_view_test, context_value_test); @@ -56,7 +56,7 @@ TEST(ContextTest, ContextImmutability) std::map map_test = {{"test_key", (int64_t)123}}; context::Context context_test = context::Context(map_test); - context::Context context_foo = context_test.SetValue("foo_key", 456); + context::Context context_foo = context_test.SetValue("foo_key", (int64_t)456); EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), 456); } From 4358d350e84eef92a2405037750c7be11fff6596 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 17:46:39 +0000 Subject: [PATCH 89/93] removed unncessary shared_ptr during iteration --- 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 82463edde7..84957e288c 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -63,7 +63,7 @@ class Context // Returns the value associated with the passed in key. context::ContextValue GetValue(nostd::string_view key) { - for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) + for (DataList *data = &*head_; data != nullptr; data = &*(data->next_)) { if (key.size() == data->key_length_) { From 80cbb965720a30f135ccc5cb7b15ec146ab40527 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 18:18:45 +0000 Subject: [PATCH 90/93] added shared_ptr to context_value --- api/include/opentelemetry/context/context_value.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index dcb987d3d5..8fd2fabe8c 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -6,13 +6,13 @@ #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" -//#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { using ContextValue = - nostd::variant*/>; + nostd::variant>; } // namespace context OPENTELEMETRY_END_NAMESPACE From feb3f17a0dc6eb9d14e4d0a4444b76e28e3fc829 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 18:54:01 +0000 Subject: [PATCH 91/93] added shared_ptr and include --- api/include/opentelemetry/context/context_value.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index dcb987d3d5..8fd2fabe8c 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -6,13 +6,13 @@ #include "opentelemetry/nostd/span.h" #include "opentelemetry/nostd/unique_ptr.h" #include "opentelemetry/nostd/variant.h" -//#include "opentelemetry/trace/span_context.h" +#include "opentelemetry/trace/span_context.h" #include "opentelemetry/version.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context { using ContextValue = - nostd::variant*/>; + nostd::variant>; } // namespace context OPENTELEMETRY_END_NAMESPACE From bf94af8bcbef7751b3951b4564fb6b91decc4fd9 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 19:57:35 +0000 Subject: [PATCH 92/93] simplifications --- api/include/opentelemetry/context/context.h | 71 ++++++++++----------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 84957e288c..9099a770a6 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -40,13 +40,13 @@ class Context template Context SetValues(T &values) noexcept { - Context context = Context(values); - nostd::shared_ptr last_node; - for (nostd::shared_ptr data = context.head_; data != nullptr; data = data->next_) + Context context = Context(values); + nostd::shared_ptr &last = context.head_; + while (last->next_ != nullptr) { - last_node = data; + last = last->next_; } - last_node->next_ = head_; + last->next_ = head_; return context; } @@ -61,9 +61,9 @@ class Context } // Returns the value associated with the passed in key. - context::ContextValue GetValue(nostd::string_view key) + context::ContextValue GetValue(const nostd::string_view key) noexcept { - for (DataList *data = &*head_; data != nullptr; data = &*(data->next_)) + for (DataList *data = head_.get(); data != nullptr; data = data->next_.get()) { if (key.size() == data->key_length_) { @@ -77,9 +77,9 @@ class Context } // Checks for key and returns true if found - bool HasKey(nostd::string_view key) + bool HasKey(const nostd::string_view key) noexcept { - for (nostd::shared_ptr data = head_; data != nullptr; data = data->next_) + for (DataList *data = head_.get(); data != nullptr; data = data->next_.get()) { if (key.size() == data->key_length_) { @@ -111,37 +111,22 @@ class Context // Builds a data list off of a key and value iterable and returns the head template - DataList(const T &keys_and_vals) + DataList(const T &keys_and_vals) : key_{nullptr} { - if (std::begin(keys_and_vals) != std::end(keys_and_vals)) + bool first = true; + auto *node = this; + for (auto &iter : keys_and_vals) { - auto iter = std::begin(keys_and_vals); - // Create list head - key_ = new char[nostd::string_view(iter->first).size()]; - key_length_ = nostd::string_view(iter->first).size(); - memcpy(key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - value_ = iter->second; - next_ = nostd::shared_ptr{nullptr}; - ++iter; - - DataList *previous_node = this; - // Iterate over the keys and values iterable and add nodes - for (; iter != std::end(keys_and_vals); ++iter) + if (first) { - nostd::shared_ptr node = nostd::shared_ptr{new DataList()}; - node->key_ = new char[nostd::string_view(iter->first).size()]; - node->key_length_ = nostd::string_view(iter->first).size(); - memcpy(node->key_, nostd::string_view(iter->first).data(), - nostd::string_view(iter->first).size() * sizeof(char)); - node->value_ = iter->second; - previous_node->next_ = node; - previous_node = &*node; + *node = std::move(DataList(iter.first, iter.second)); + first = false; + } + else + { + node->next_ = nostd::shared_ptr(new DataList(iter.first, iter.second)); + node = node->next_.get(); } - } - else - { - key_ = nullptr; } } @@ -149,13 +134,25 @@ class Context // and returns that head. DataList(nostd::string_view key, ContextValue value) { - key_ = new char[nostd::string_view(key).size()]; + key_ = new char[key.size()]; key_length_ = key.size(); memcpy(key_, key.data(), key.size() * sizeof(char)); value_ = value; next_ = nostd::shared_ptr{nullptr}; } + DataList &operator=(DataList &&other) + { + key_length_ = other.key_length_; + value_ = std::move(other.value_); + next_ = std::move(other.next_); + + key_ = other.key_; + other.key_ = nullptr; + + return *this; + } + ~DataList() { if (key_ != nullptr) From 5eddd2fcb07c7903690c44f90328e8ec6ffc4ad8 Mon Sep 17 00:00:00 2001 From: Sam Atac Date: Tue, 21 Jul 2020 21:36:30 +0000 Subject: [PATCH 93/93] removed unecessary includes --- api/include/opentelemetry/context/context.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 9099a770a6..1f7caf0ee1 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -1,11 +1,8 @@ #pragma once -#include - #include "opentelemetry/context/context_value.h" #include "opentelemetry/nostd/shared_ptr.h" #include "opentelemetry/nostd/string_view.h" -#include "opentelemetry/trace/key_value_iterable_view.h" OPENTELEMETRY_BEGIN_NAMESPACE namespace context