Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context helper functions #225

Merged
merged 47 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
4128a69
added context header with dummy methods
satac2 Jul 6, 2020
cba6d5f
Build and CMake files
satac2 Jul 6, 2020
b2e876e
auto formatted
satac2 Jul 6, 2020
a7480c8
changed variable name from attributes to values
satac2 Jul 9, 2020
96cd32f
Update api/include/opentelemetry/context/context.h
satac2 Jul 9, 2020
5f0634b
changed to follow the spec and added context_value type
satac2 Jul 13, 2020
5d77daf
Merge branch 'context_api_dummy_methods' of github.com:satac2/opentel…
satac2 Jul 13, 2020
68a1a70
removed tests for this PR
satac2 Jul 13, 2020
5e68574
removed line
satac2 Jul 13, 2020
16a492e
removed unnecessary include
satac2 Jul 13, 2020
497eb67
changed a function to pass by reference
satac2 Jul 13, 2020
479dead
formatted
satac2 Jul 13, 2020
2a78c48
Added tests.
satac2 Jul 13, 2020
f51748f
avoiding ABI compatibility issues
satac2 Jul 13, 2020
7d73219
Merge branch 'master' into context_api_dummy_methods
maxgolov Jul 13, 2020
3f85aa5
added throw capture
satac2 Jul 14, 2020
0ec83e8
Merge branch 'context_api_dummy_methods' of github.com:satac2/opentel…
satac2 Jul 14, 2020
21ea872
minor exception syntax error
satac2 Jul 14, 2020
f016689
Merge pull request #13 from satac2/context_api_dummy_methods
satac2 Jul 14, 2020
1f5e5fd
added runtime and threadlocal contexts
satac2 Jul 14, 2020
bbf9559
add comparison operator to context api
satac2 Jul 14, 2020
c8b7ed6
added stack for local storage of context objects
satac2 Jul 16, 2020
8d8b91f
removed old context.h
satac2 Jul 16, 2020
4b78ac8
changed runtime context interface to match
satac2 Jul 16, 2020
d3cea59
Merge remote-tracking branch 'origin/master' into runtime_and_threadl…
satac2 Jul 22, 2020
25bec5e
Merge remote-tracking branch 'origin/master' into runtime_and_threadl…
satac2 Jul 22, 2020
d77e445
formatted and added some size checks to prevent seg faults, also adde…
satac2 Jul 22, 2020
0583e3c
added tests
satac2 Jul 22, 2020
e02fadb
add a context_config file to choose which implementation the runtime …
satac2 Jul 23, 2020
c06edc5
readding string_view_test
satac2 Jul 23, 2020
fc42d91
threadlocalcontext is once again a derived class of RuntimeContext an…
satac2 Jul 23, 2020
ce78585
protected virtual members
satac2 Jul 23, 2020
02dbbdf
reset string_view_test
satac2 Jul 23, 2020
c1ebc16
Merge branch 'master' into runtime_and_threadlocal_context
reyang Jul 23, 2020
5a5df6e
added destructor that detaches the context to the Token
satac2 Jul 24, 2020
a2c81b3
Merge branch 'runtime_and_threadlocal_context' of github.com:satac2/o…
satac2 Jul 24, 2020
1d7549f
minor changes and switch from RuntimeContext accepting a ptr
satac2 Jul 25, 2020
d803674
added SetValue and GetValue static methods to Runtime Context
satac2 Jul 27, 2020
224f3ec
removed commented out line
satac2 Jul 27, 2020
5e5033a
removed unnecessary line
satac2 Jul 27, 2020
4605bb1
providing default value for parameters
satac2 Jul 27, 2020
090fbe7
fixed nits
satac2 Jul 28, 2020
2ba7e02
added const to Top
satac2 Jul 28, 2020
dfe09be
Merge branch 'runtime_and_threadlocal_context' into context_helper_fu…
satac2 Jul 28, 2020
6779bf9
added more comments
satac2 Jul 28, 2020
1141e41
Merge remote-tracking branch 'origin/master' into context_helper_func…
satac2 Jul 28, 2020
1d08b99
Merge branch 'master' into context_helper_functions
reyang Jul 29, 2020
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
40 changes: 40 additions & 0 deletions api/include/opentelemetry/context/runtime_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,46 @@ class RuntimeContext

static RuntimeContext *context_handler_;

// Sets the Key and Value into the passed in context or if a context is not
// passed in, the RuntimeContext.
// Should be used to SetValues to the current RuntimeContext, is essentially
// equivalent to RuntimeContext::GetCurrent().SetValue(key,value). Keep in
// mind that the current RuntimeContext will not be changed, and the new
// context will be returned.
static Context SetValue(nostd::string_view key,
ContextValue value,
Context *context = nullptr) noexcept
{
Context temp_context;
if (context == nullptr)
{
temp_context = GetCurrent();
}
else
{
temp_context = *context;
}
return temp_context.SetValue(key, value);
}

// Returns the value associated with the passed in key and either the
// passed in context* or the runtime context if a context is not passed in.
// Should be used to get values from the current RuntimeContext, is
// essentially equivalent to RuntimeContext::GetCurrent().GetValue(key).
static ContextValue GetValue(nostd::string_view key, Context *context = nullptr) noexcept
{
Context temp_context;
if (context == nullptr)
{
temp_context = GetCurrent();
}
else
{
temp_context = *context;
}
return temp_context.GetValue(key);
}

protected:
// Provides a token with the passed in context
Token CreateToken(Context context) noexcept { return Token(context); }
Expand Down
41 changes: 41 additions & 0 deletions api/test/context/runtime_context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,44 @@ TEST(RuntimeContextTest, ThreeAttachDetach)
EXPECT_TRUE(context::RuntimeContext::Detach(foo_context_token));
EXPECT_TRUE(context::RuntimeContext::Detach(test_context_token));
}

// Tests that SetValue returns a context with the passed in data and the
// RuntimeContext data when a context is not passed into the
// RuntimeContext::SetValue method.
TEST(RuntimeContextTest, SetValueRuntimeContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
context::RuntimeContext::Token old_context_token = context::RuntimeContext::Attach(foo_context);
context::Context test_context = context::RuntimeContext::SetValue("test_key", (int64_t)123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
}

// Tests that SetValue returns a context with the passed in data and the
// passed in context data when a context* is passed into the
// RuntimeContext::SetValue method.
TEST(RuntimeContextTest, SetValueOtherContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
context::Context test_context =
context::RuntimeContext::SetValue("test_key", (int64_t)123, &foo_context);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
}

// Tests that SetValue returns the ContextValue associated with the
// passed in string and the current Runtime Context
TEST(RuntimeContextTest, GetValueRuntimeContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
context::RuntimeContext::Token old_context_token = context::RuntimeContext::Attach(foo_context);
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key")), 596);
}

// Tests that SetValue returns the ContextValue associated with the
// passed in string and the passed in context
TEST(RuntimeContextTest, GetValueOtherContext)
{
context::Context foo_context = context::Context("foo_key", (int64_t)596);
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key", &foo_context)), 596);
}