Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 64 additions & 50 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once


#include <map>
#include <string>
#include <mutex>
Expand All @@ -13,22 +12,9 @@ namespace context

std::mutex context_id_mutex;

/*The context class provides a context identifier*/
/*The context class provides a context identifier */
class Context{

private:

/*The identifier itself*/
std::map<int, int> 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<int,int> ctx_map){
ctx_map_ = ctx_map;
}

public:

/*The ContextKey class is used to obscure access from the
Expand All @@ -44,13 +30,13 @@ namespace context
int identifier_;


/* GetIdentifier: returns the identifier*/
/* GetIdentifier: returns the identifier */
int GetIdentifier(){
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;
Expand All @@ -59,9 +45,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;
Expand All @@ -78,22 +63,21 @@ 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<int,int> {};

}

/* 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){
ctx_map_[key.GetIdentifier()] = value;
}


/* 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){
Expand All @@ -104,14 +88,22 @@ 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;
}
}

/* 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;
Expand All @@ -127,60 +119,71 @@ namespace context
return ContextKey(key_name,id);
}

private:

/* The identifier itself */
std::map<int, int> 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<int,int> ctx_map){
ctx_map_ = ctx_map;
}


};



/* The token class provides an identifier that is used by
* the attach and detach methods to keep track of context
* objects.*/
* 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*/
* propogating context through cpp. */
class RuntimeContext {
private:

static thread_local Context context_;

public:


/* 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){
Expand All @@ -193,22 +196,33 @@ namespace context
}


/* GetCurrent: Return the current context.
*/
/* Return the current context. */
static Context GetCurrent(){
Context context = context_;
return 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. Returns zero if successful, -1 otherwise
*/
void Detach(Token &token){
context_ = token.GetContext();
int Detach(Token &token){

if(token.GetContext() == context_){

return -1;
}

context_ = token.GetContext();

return 0;
}


private:

static thread_local Context context_;

};

thread_local Context RuntimeContext::context_ = Context();
Expand Down
4 changes: 3 additions & 1 deletion api/test/context/runtimeContext_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down