Skip to content

Commit 4e2aeee

Browse files
authored
Context helper functions (open-telemetry#225)
1 parent 9195cf7 commit 4e2aeee

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

api/include/opentelemetry/context/runtime_context.h

+40
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,46 @@ class RuntimeContext
4747

4848
static RuntimeContext *context_handler_;
4949

50+
// Sets the Key and Value into the passed in context or if a context is not
51+
// passed in, the RuntimeContext.
52+
// Should be used to SetValues to the current RuntimeContext, is essentially
53+
// equivalent to RuntimeContext::GetCurrent().SetValue(key,value). Keep in
54+
// mind that the current RuntimeContext will not be changed, and the new
55+
// context will be returned.
56+
static Context SetValue(nostd::string_view key,
57+
ContextValue value,
58+
Context *context = nullptr) noexcept
59+
{
60+
Context temp_context;
61+
if (context == nullptr)
62+
{
63+
temp_context = GetCurrent();
64+
}
65+
else
66+
{
67+
temp_context = *context;
68+
}
69+
return temp_context.SetValue(key, value);
70+
}
71+
72+
// Returns the value associated with the passed in key and either the
73+
// passed in context* or the runtime context if a context is not passed in.
74+
// Should be used to get values from the current RuntimeContext, is
75+
// essentially equivalent to RuntimeContext::GetCurrent().GetValue(key).
76+
static ContextValue GetValue(nostd::string_view key, Context *context = nullptr) noexcept
77+
{
78+
Context temp_context;
79+
if (context == nullptr)
80+
{
81+
temp_context = GetCurrent();
82+
}
83+
else
84+
{
85+
temp_context = *context;
86+
}
87+
return temp_context.GetValue(key);
88+
}
89+
5090
protected:
5191
// Provides a token with the passed in context
5292
Token CreateToken(Context context) noexcept { return Token(context); }

api/test/context/runtime_context_test.cc

+41
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,44 @@ TEST(RuntimeContextTest, ThreeAttachDetach)
5959
EXPECT_TRUE(context::RuntimeContext::Detach(foo_context_token));
6060
EXPECT_TRUE(context::RuntimeContext::Detach(test_context_token));
6161
}
62+
63+
// Tests that SetValue returns a context with the passed in data and the
64+
// RuntimeContext data when a context is not passed into the
65+
// RuntimeContext::SetValue method.
66+
TEST(RuntimeContextTest, SetValueRuntimeContext)
67+
{
68+
context::Context foo_context = context::Context("foo_key", (int64_t)596);
69+
context::RuntimeContext::Token old_context_token = context::RuntimeContext::Attach(foo_context);
70+
context::Context test_context = context::RuntimeContext::SetValue("test_key", (int64_t)123);
71+
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
72+
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
73+
}
74+
75+
// Tests that SetValue returns a context with the passed in data and the
76+
// passed in context data when a context* is passed into the
77+
// RuntimeContext::SetValue method.
78+
TEST(RuntimeContextTest, SetValueOtherContext)
79+
{
80+
context::Context foo_context = context::Context("foo_key", (int64_t)596);
81+
context::Context test_context =
82+
context::RuntimeContext::SetValue("test_key", (int64_t)123, &foo_context);
83+
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("test_key")), 123);
84+
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("foo_key")), 596);
85+
}
86+
87+
// Tests that SetValue returns the ContextValue associated with the
88+
// passed in string and the current Runtime Context
89+
TEST(RuntimeContextTest, GetValueRuntimeContext)
90+
{
91+
context::Context foo_context = context::Context("foo_key", (int64_t)596);
92+
context::RuntimeContext::Token old_context_token = context::RuntimeContext::Attach(foo_context);
93+
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key")), 596);
94+
}
95+
96+
// Tests that SetValue returns the ContextValue associated with the
97+
// passed in string and the passed in context
98+
TEST(RuntimeContextTest, GetValueOtherContext)
99+
{
100+
context::Context foo_context = context::Context("foo_key", (int64_t)596);
101+
EXPECT_EQ(nostd::get<int64_t>(context::RuntimeContext::GetValue("foo_key", &foo_context)), 596);
102+
}

0 commit comments

Comments
 (0)