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 12 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.
72 changes: 72 additions & 0 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#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 <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 = context_map_;
context_map_copy[std::string(key)] = value;
Copy link
Member

@reyang reyang Jul 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if a library (compilation unit A) is making a call to this method, while the context object was constructed by another binary (compilation unit B), while A and B are using different version of STL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I solved the issue by putting the map into a KeyValueIterableView, and then copying the data over.

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 = context_map_;
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