Skip to content

Context api#4

Open
satac2 wants to merge 37 commits into
masterfrom
context_api
Open

Context api#4
satac2 wants to merge 37 commits into
masterfrom
context_api

Conversation

@satac2
Copy link
Copy Markdown
Owner

@satac2 satac2 commented Jun 8, 2020

Just the class declarations.

@satac2 satac2 requested a review from 0x4b June 8, 2020 19:47
Comment thread api/include/opentelemetry/context/context.h Outdated

/* The RuntimeContext class provides a wrapper for
* propogating context through cpp*/
class RuntimeContext {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you have any thoughts on having two separate classes, versus moving the RuntimeContext methods into context itself?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I don't really have thoughts either way, I am happy to merge them

Comment thread api/include/opentelemetry/context/context.h Outdated
Comment thread api/include/opentelemetry/context/context.h Outdated
Comment thread api/include/opentelemetry/context/context.h Outdated
Comment thread api/include/opentelemetry/context/context.h Outdated
Comment thread api/test/context/runtimeContext_test.cc Outdated

#include <gtest/gtest.h>

using namespace opentelemetry::context;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider putting the tests in the namespace rather than "using" it.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

That seems to be contrary to the tests that already exist, should I do it anyway?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No, stick with the pattern they have then.

* user to the context map. The identifier is used as a key
* to the context map.
*/
class ContextKey{
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider naming this "Key" since it's already scoped to the Context. In places like the test it's currently called Context::ContextKey, which is redundant.

Suggested change
class ContextKey{
class Key{

Comment thread api/test/context/context_test.cc Outdated
/* Tests whether the original context objects changes
* when you write to it.
*/
TEST(Context_test, is_context_immutable)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Avoid underscores in the test name (I'm having a hard time finding public documentation on it, but maybe https://chromium.googlesource.com/external/github.com/google/googletest/+/refs/tags/release-1.8.0/googletest/docs/FAQ.md#why-should-not-test-case-names-and-test-names-contain-underscore)

Suggested change
TEST(Context_test, is_context_immutable)
TEST(ContextTest, ContextIsImmutable)

FWIW, some of the existing files (e.g. https://github.com/satac2/opentelemetry-cpp/blob/master/api/test/trace/key_value_iterable_view_test.cc) do have a mix.


EXPECT_EQ(new_test_context.GetValue(foo_key), 1);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Two other tests I can think of are

  • you can override a key (maybe in the same context and in a derived context)
  • that two keys created with the same name set / get different values

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

What is the expected result when you attempt to override a key?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The spec doesn't seem to specify.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you call SetValue / WriteValue with the same key I would expect it the context to have only the new value, like a map's key/value pairs.

namespace context
{

std::mutex context_id_mutex;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It feels like the mutex should have the same storage class as the thing(s) it's guarding, so in this case it should also be a static class field like last_key_identifier_.

* and increments the identifier then assigns it to be the key's
* identifier.
*/
ContextKey(std::string key_name){
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a public method, right? (ContextKey is a public class and this is a public method)

Is there a use case for calling this anywhere other than within the Context implementation?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Well, that is an interesting question, I implemented it because I wanted to test the use case for constructing a context with a Key, so I needed the key before I had the context object.

identifier_ = identifier;
}

public:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you want public copy constructor and assignment operator?

https://google.github.io/styleguide/cppguide.html#Copyable_Movable_Types

Comment on lines +83 to +86
Context(){
ctx_map_ = std::map<int,int> {};

}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since ctx_map_ is concrete (not e.g. a pointer) this assignment is equivalent to the state of the map when Context is default constructed.

Suggested change
Context(){
ctx_map_ = std::map<int,int> {};
}
Context() = default;

Comment on lines +117 to +123
int id;

context_id_mutex.lock();

last_key_identifier_++;

id = last_key_identifier_;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a minor style point, but declare variables as close to their use as possible

Suggested change
int id;
context_id_mutex.lock();
last_key_identifier_++;
id = last_key_identifier_;
context_id_mutex.lock();
last_key_identifier_++;
int id = last_key_identifier_;


Context ctx_;

public:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The public methods looks like things that a user shouldn't call (they make the Token transparent rather than opqaue as the specification demands).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants