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 api dummy methods #155

Closed
wants to merge 18 commits into from
Closed
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
Empty file.
92 changes: 92 additions & 0 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#pragma once

#include "opentelemetry/common/attribute_value.h"
#include "opentelemetry/context/context_value.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/trace/key_value_iterable_view.h"

#include <iostream>
#include <map>

OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{

// The context class provides a context identifier.
// This is a dummy class that is meant to be overridden,
// the methods return default values.
class Context
{

public:
Context() = default;

// Contructor, creates a context object from a map of keys
// and identifiers.
template <class T, nostd::enable_if_t<trace::detail::is_key_value_iterable<T>::value> * = nullptr>
Context(const T &keys_and_values)
{
trace::KeyValueIterableView<T> iterable{keys_and_values};
iterable.ForEachKeyValue([&](nostd::string_view key, context::ContextValue value) noexcept {
context_map_[std::string(key)] = value;
return true;
});
}

// Accepts a key and a value and then returns a new context that
// contains both the original pairs and the new pair.
template <class T>
Context SetValue(nostd::string_view key, T &value) noexcept
{
std::map<std::string, context::ContextValue> context_map_copy;
trace::KeyValueIterableView<std::map<std::string, context::ContextValue>> context_map_iterable{
context_map_};

context_map_iterable.ForEachKeyValue([&](nostd::string_view key,
context::ContextValue value) noexcept {
context_map_copy[std::string(key)] = value;
return true;
});

context_map_copy[std::string(key)] = value;

return Context(context_map_copy);
}

// Accepts a new iterable and then returns a new context that
// contains both the original pairs and the new pair.
template <class T, nostd::enable_if_t<trace::detail::is_key_value_iterable<T>::value> * = nullptr>
Context SetValues(T &keys_and_values) noexcept
{
std::map<std::string, context::ContextValue> context_map_copy;
trace::KeyValueIterableView<std::map<std::string, context::ContextValue>> context_map_iterable{
context_map_};

context_map_iterable.ForEachKeyValue([&](nostd::string_view key,
context::ContextValue value) noexcept {
context_map_copy[std::string(key)] = value;
return true;
});

trace::KeyValueIterableView<T> iterable{keys_and_values};

iterable.ForEachKeyValue([&](nostd::string_view key, context::ContextValue value) noexcept {
context_map_copy[std::string(key)] = value;
return true;
});

return Context(context_map_copy);
}

// Returns the value associated with the passed in key.
context::ContextValue GetValue(nostd::string_view key) { return context_map_[std::string(key)]; }

// Copy Constructors.
Context(const Context &other) = default;
Context &operator=(const Context &other) = default;

private:
std::map<std::string, context::ContextValue> context_map_;
};
} // namespace context
OPENTELEMETRY_END_NAMESPACE
28 changes: 28 additions & 0 deletions api/include/opentelemetry/context/context_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <cstdint>

#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<bool,
int,
int64_t,
unsigned int,
uint64_t,
double,
nostd::string_view,
nostd::span<const bool>,
nostd::span<const int>,
nostd::span<const int64_t>,
nostd::span<const unsigned int>,
nostd::span<const uint64_t>,
nostd::span<const double>,
nostd::span<const nostd::string_view>>;
} // namespace context
OPENTELEMETRY_END_NAMESPACE
10 changes: 10 additions & 0 deletions api/test/context/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cc_test(
name = "context_test",
srcs = [
"context_test.cc",
],
deps = [
"//api",
"@com_google_googletest//:gtest_main",
],
)
8 changes: 8 additions & 0 deletions api/test/context/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
104 changes: 104 additions & 0 deletions api/test/context/context_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "opentelemetry/context/context.h"

#include <map>

#include <gtest/gtest.h>

using namespace opentelemetry;

// Tests that the context constructor accepts an std::map.
TEST(ContextTest, ContextIterableAcceptsMap)
{
std::map<std::string, context::ContextValue> 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<std::string, std::string> map_test = {{"test_key", "123"}, {"foo_key", "456"}};

context::Context context_test = context::Context(map_test);
EXPECT_EQ(nostd::get<nostd::string_view>(context_test.GetValue("test_key")), "123");
EXPECT_EQ(nostd::get<nostd::string_view>(context_test.GetValue("foo_key")), "456");
}

// Tests that the SetValues method accepts an std::map.
TEST(ContextTest, ContextSetValuesAcceptsMap)
{
std::map<std::string, context::ContextValue> map_test = {{"test_key", "123"}};
std::map<std::string, context::ContextValue> 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<nostd::string_view>(foo_test.GetValue("test_key")), "123");
EXPECT_EQ(nostd::get<nostd::string_view>(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<nostd::string_view>(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<std::string, context::ContextValue> map_test = {{"test_key", "123"}};
context::Context context_test = context::Context(map_test);

context::Context context_foo = context_test.SetValue("foo_key", "456");
#if __EXCEPTIONS
EXPECT_THROW(nostd::get<nostd::string_view>(context_test.GetValue("foo_key")),
nostd::bad_variant_access);
#else
EXPECT_DEATH({ nostd::get<nostd::string_view>(context_test.GetValue("foo_key")); }, "");
#endif
}

// Tests that writing the same to a context overwrites the original value.
TEST(ContextTest, ContextKeyOverwrite)
{
std::map<std::string, context::ContextValue> 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<nostd::string_view>(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<std::string, context::ContextValue>;
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<nostd::string_view>(other_context.GetValue("test_key")), "123");
EXPECT_EQ(nostd::get<nostd::string_view>(other_context.GetValue("foo_key")), "456");
}

// Tests that copying a context copies the key value pairs as expected.
TEST(ContextTest, ContextCopyOperator)
{
std::map<std::string, std::string> 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<nostd::string_view>(copied_context.GetValue("test_key")), "123");
EXPECT_EQ(nostd::get<nostd::string_view>(copied_context.GetValue("foo_key")), "456");
EXPECT_EQ(nostd::get<nostd::string_view>(copied_context.GetValue("other_key")), "789");
}