Repository navigation
stats: support default stats tags with fixed value - #2357
Conversation
The default tags will be added to all stats. Typical use case is identifying Envoy instances with the tag. Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
| * all metrics. | ||
| * @param bootstrap bootstrap proto. | ||
| */ | ||
| static std::vector<Stats::Tag> createTags(const envoy::api::v2::Bootstrap& bootstrap); |
There was a problem hiding this comment.
Should this be internal to utility.cc and just live in the anonymous namespace?
There was a problem hiding this comment.
I'm not familiar but since this function needs to be called from server/server.cc, I thought this function needs to be published. Is that wrong?
Possibly will be refactored with #2357 (comment).
There was a problem hiding this comment.
Refactored with e059698 not to expose these functions. Good point!
| } | ||
|
|
||
| std::string ThreadLocalStoreImpl::getTagsForName(const std::string& name, std::vector<Tag>& tags) { | ||
| if (default_tags_ != nullptr) { |
There was a problem hiding this comment.
Since we have to initialize stats stores before we ready the default_tags, stats stores have their default_tags_ fields as a point (const std::vector<Tag>*). Pointers can be null, so I thought we need check nullability of the pointer. The tag_extractors_ also checks the nullability below. Still I'm not sure it's not wrong.
There was a problem hiding this comment.
Refactored with e059698 to stop using pointer approach. Instead, use null object pattern to ensure they always exist.
| commit = "f268484d221509b175c46cce6c4cc3593a0ede5e", | ||
| remote = "https://github.com/envoyproxy/data-plane-api", | ||
| commit = "07b79a51679558d6460fd2785b7206d102869f4d", | ||
| remote = "https://github.com/taiki45/data-plane-api", |
There was a problem hiding this comment.
Looks like this snuck in by accident.
There was a problem hiding this comment.
I'm assuming this was purposeful to align with envoyproxy/data-plane-api#416 to pass CI. What's the expected workflow for synchronized changes to both repos?
There was a problem hiding this comment.
Ah, yes, that makes sense. Normally you'd get your change reviewed and merged over at data-plane-api, then update the hash in your PR here (if someone else updates it first past your changes in data-plane-api, just take the newer hash when you merge master).
It's fine to push a PR for review pointing at the author's repo to get early feedback, but obviously it needs to get updated before the PR can be merged. It's probably good practice to comment on your own PR to mark it as needing work.
There was a problem hiding this comment.
Yes, I read the workflow at https://github.com/envoyproxy/data-plane-api/blob/master/CONTRIBUTING.md#api-changes but leaving the comment encourages reviews. I will do that, thanks!
|
|
||
| if (!bootstrap.stats_config().has_use_all_default_tags() || | ||
| bootstrap.stats_config().use_all_default_tags().value()) { | ||
| for (const std::pair<std::string, std::string>& default_tag : |
There was a problem hiding this comment.
Using const auto& for this might make it easier to read.
| } | ||
| } | ||
|
|
||
| for (const envoy::api::v2::TagSpecifier& tag_specifier : bootstrap.stats_config().stats_tags()) { |
There was a problem hiding this comment.
const auto& is also probably a good choice here.
| }; | ||
| std::vector<Stats::TagExtractorPtr> | ||
| Utility::createTagExtractors(const envoy::api::v2::Bootstrap& bootstrap) { | ||
| std::vector<Stats::TagExtractorPtr> tag_extractors; |
There was a problem hiding this comment.
It looks like you know how many items will go into tag_extractors. You can use this with reserve to avoid any costly reallocations:
tag_extractors.reserve(default_tag_size + bootstrap_tags_size)
There was a problem hiding this comment.
Nice, I didn't know that. I roughly estimate the size but should I get precise size?
d3b5e7f
| std::vector<Stats::Tag> Utility::createTags(const envoy::api::v2::Bootstrap& bootstrap) { | ||
| std::vector<Stats::Tag> tags; | ||
|
|
||
| for (const envoy::api::v2::TagSpecifier& tag_specifier : bootstrap.stats_config().stats_tags()) { |
There was a problem hiding this comment.
Use const auto& for these long type names.
| * Envoy can have both pre-defined tag names in well_known_names and user | ||
| * provided tag names through bootstrap configuration. Check the confilict of | ||
| * all the tag names to avoid unexpected tag name overwriting. If found a | ||
| * confilict, throw an exception. |
There was a problem hiding this comment.
There's a typo ("confilict"). It also might be more readable to alter the text a bit:
"Check all tag names for conflicts to avoid unexpected tag name overwriting. If a conflict is found, throw an exception."
| * all the tag names to avoid unexpected tag name overwriting. If found a | ||
| * confilict, throw an exception. | ||
| * @param bootstrap bootstrap proto. | ||
| */ |
There was a problem hiding this comment.
It might not be consistently applied across the Envoy code base but this is a good opportunity to use the @throws doxygen tag to specify what exception type and when it is thrown.
| void setTagExtractors(const std::vector<TagExtractorPtr>& tag_extractors) override { | ||
| tag_extractors_ = &tag_extractors; | ||
| } | ||
| void setDefaultTags(const std::vector<Tag>& tags) override { default_tags_ = &tags; } |
There was a problem hiding this comment.
This seems risky. What's the lifespan of tags guaranteed to be? I think it is OK because tags has the lifespan of the server. But any time we are saving a pointer to a value passed by reference it's a good idea to document what controls the lifecycle of that memory.
There was a problem hiding this comment.
Yeah, a server hold the value so the lifespan will be same as the server's one. Looks good with this comment? a94dbda
There was a problem hiding this comment.
Finally stopped this approach and change thread local stores to have ownership of those pointers in e059698.
|
@mrice32 Can you also take a look at this? Thanks. |
| std::vector<Stats::TagExtractorPtr> tag_extractors; | ||
|
|
||
| // Ensure no tag names are repeated. | ||
| void Utility::detectTagNameConflict(const envoy::api::v2::Bootstrap& bootstrap) { |
There was a problem hiding this comment.
Not sure what others think about this, but is there a way we could get the name conflict detection as a part of the creation of TagExtractors? We could do that in a few ways:
- We could create a stateful class for doing this initial config processing, keeping track of
TagExtractors, and taking care of doing stat creation-time processing (likegetTagsForName()inThreadLocalStore). This is probably the elegant and more maintainable solution because it has the benefit of reducing complexity in other classes, too. - We could pass mutable state between these utility methods. This would be a bit ugly, but it would get rid of the nearly repeated code.
There was a problem hiding this comment.
Agree. I was also thiking how we generalize tag extractors and default tags. It's off-topic but I started with creating tag extrators which do match every stat names but it was not good idea.
(1) sounds good to me, so I'll try (1). Please tell me the possible name of the class if you have ideas. The class will consume bootstrap configurations and build it-self, then create proper tags when a stats name will be given. Maybe TagProvider or TagProducer?
There was a problem hiding this comment.
Yeah, I thought about the idea of a generalized tag extractor as well, and for performance reasons, I agree with you that it's probably best to not do it that way.
Great! Yeah, I'm not too great with naming, but both of those seem reasonable to me.
There was a problem hiding this comment.
Implemented (1) in e059698. Please note I didn't move bootrap config consuming task from Config::Utility bacause (a). the TagProducer (and its factory) living in the stats component shouldn't depend on Bootrap config (b) the consuming task is not so big from the both points of code size and responsibility. If you have a better place for moving the task, I'm glad to implement that.
| ScopePtr default_scope_; | ||
| std::list<std::reference_wrapper<Sink>> timer_sinks_; | ||
| const std::vector<TagExtractorPtr>* tag_extractors_{}; | ||
| const std::vector<Tag>* default_tags_{}; |
There was a problem hiding this comment.
Adding a class as mentioned in 1 above would mean that this class doesn't have to keep track of two separate sets of related data and know how to process it.
|
|
||
| Config::Utility::detectTagNameConflict(bootstrap); | ||
| tag_extractors_ = Config::Utility::createTagExtractors(bootstrap); | ||
| Config::Utility::createTags(bootstrap); |
There was a problem hiding this comment.
nit: just for consistency, you can remove tag_extractors_ from this file as well since it appears that it isn't being used.
| EXPECT_THROW_WITH_MESSAGE(Utility::detectTagNameConflict(bootstrap), EnvoyException, | ||
| fmt::format("Tag name '{}' specified twice.", "test.x")); | ||
|
|
||
| // Also should raise an error When user defined tag name conflicts with Envoy's default tag names. |
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Collect responsibility of the stats tag management to the newly created class, TagProducer. A set of TagExtractor and a set of default Tag will be handled via TagProducer class. I'm also looking proper places to extract bootstrap config handling around stats tag from Config::Utility. Since the code size and responsibility are limited, leave the config handling as it is for this time. For tests, I moved some of test cases from utility unit test to stats integration test. This is because we can't (or shouldn't) test the internal functions inside Config::Utility. In addition, some of them are actually testing tag extracor behaviors, so extract them from utility unit test. Signed-off-by: Taiki Ono <[email protected]>
| return tag_extractors; | ||
| } | ||
|
|
||
| Stats::TagProducerPtr Utility::createTagProducer(const envoy::api::v2::Bootstrap& bootstrap) { |
There was a problem hiding this comment.
Do you think it makes sense to encapsulate all of this config interpretation logic in the constructor for TagProducerImpl?
There was a problem hiding this comment.
I don't think that. In my opinion, logics which handle a bootstrap config should be in Config or Server. I think it makes sense to extract these logics to somewhere in Config namespace. WDYT?
There was a problem hiding this comment.
If you look at virtual clusters or routes and others, there are a bunch of examples of v2 api objects being passed to to parameterize their representation in Envoy. I think this method of encapsulation is preferred in Envoy. Also, just a note, we should only need to pass the StatsConfig object rather than the entire bootstrap config.
I prefer this approach as well, especially when there's only one class in Envoy that represents a particular config object (in this case StatsConfig only configures TagProducer). I think it allows us to contain the component-specific logic within specific classes and directories pertaining to that component rather than polluting the more general areas of the codebase with it. Also, it gives more flexibility when maintaining state - for instance, in this case you could write the default Tags and TagExtractors to member vectors rather than be forced to use the in/out semantics of a c-style function.
This is just my opinion, though. Others may feel differently.
There was a problem hiding this comment.
+1, passing StatsConfig message or pieces of bootstrap config makes sense to me. How about the refacoring plan: Utility extracts stats proto messages from bootstrap message, then pass them to a factory of TagProducer (or direcly to TagProducerImpl).
There was a problem hiding this comment.
Just a thought: I'm wondering doing the stateful initalization in the TagProducer will change it to a mutable object. I think it's better Entity objects keep their immutablility, so "building data in the other places and passing them into an Entiry object" approach isn't always bad.
hmm... after some thinking, using a stateful object to build tag extroctors and default tags seems better to me in this case. I'm just thiking to get both immutability of TagProducer and that stateful object for the config processing task.
There was a problem hiding this comment.
I'm not exactly sure what you mean when you say mutable object? The object could still be passed around as const and all of TagProducer's interface methods can be const, so it won't allow anyone to modify any fields after construction.
If you're referring to making all the fields inside TagProducerImpl const, I'm not sure that's a big concern here, especially considering the added complexity that would be necessary to make this work.
There was a problem hiding this comment.
Sorry for the confustion. I misunderstood I have to change the constbility of the object but it's wrong. Then the refactoring plan sounds great to me. Implemented it with f736311. Cloud this match to your idea?
There was a problem hiding this comment.
Yep, I think this new design is great! I'll take another pass over the code and make comments.
It seems we don't need it. Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
For refactoring. Signed-off-by: Taiki Ono <[email protected]>
mrice32
left a comment
There was a problem hiding this comment.
Looks great! LGTM after comments.
| void addDefaultExtractors(const envoy::api::v2::StatsConfig& config, | ||
| std::unordered_set<std::string>& names); | ||
|
|
||
| ExtractorsPtr tag_extractors_; |
There was a problem hiding this comment.
nit: Can we just make these vectors instead of pointers to vectors? They will always be non-nullptr right?
| EXPECT_THROW_WITH_MESSAGE( | ||
| Utility::createTagExtractors(bootstrap), EnvoyException, | ||
| "No regex specified for tag specifier and no default regex for name: 'test_extractor'"); | ||
| Utility::createTagProducer(bootstrap); |
There was a problem hiding this comment.
This test seems to just exist now to show that this method doesn't throw. Do you think we could add in a little logic to ensure the returned pointer is not nullptr and can be used (by maybe calling produceTags() on it or something)?
There was a problem hiding this comment.
Yeah, feeling I'm missing something here. +1 to add some tests against the returned value.
| TagProducerImpl::~TagProducerImpl() {} | ||
|
|
||
| std::string TagProducerImpl::produceTags(const std::string& name, std::vector<Tag>& tags) const { | ||
| if (!default_tags_->empty()) { |
There was a problem hiding this comment.
nit: do we need this if()? It should be fine to insert an empty vector, right?
There was a problem hiding this comment.
Right. My concern was the cost of inserting an emptry vector.
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
mattklein123
left a comment
There was a problem hiding this comment.
very nice, just a few nits.
|
|
||
| return tag_extractors; | ||
| Stats::TagProducerPtr Utility::createTagProducer(const envoy::api::v2::Bootstrap& bootstrap) { | ||
| return Stats::TagProducerPtr{new Stats::TagProducerImpl(bootstrap.stats_config())}; |
There was a problem hiding this comment.
I see. Always prefer std::make_unique? Maybe same here https://github.com/envoyproxy/envoy/pull/2357/files#diff-4112b28a18267da2a53245cfbacaeb94R16.
There was a problem hiding this comment.
We didn't have make_unique until we switched to c++14, so there are tons of places where it could be used. I wouldn't go out of your way, but in new cases I would use it.
| } | ||
| } | ||
|
|
||
| TagProducerImpl::TagProducerImpl() {} |
There was a problem hiding this comment.
nit: del empty constructor and destructor (if default constructor is needed for some reason (?) just define in header file)
| throw EnvoyException(fmt::format("Tag name '{}' specified twice.", tag_specifier.tag_name())); | ||
| } | ||
|
|
||
| // If no tag value are found, fallback to default regex to keep backward compatibility. |
| void addDefaultExtractors(const envoy::api::v2::StatsConfig& config, | ||
| std::unordered_set<std::string>& names); | ||
|
|
||
| std::vector<TagExtractorPtr> tag_extractors_{}; |
There was a problem hiding this comment.
nit {} not needed here and next line
| void setTagExtractors(const std::vector<TagExtractorPtr>& tag_extractors) override { | ||
| tag_extractors_ = &tag_extractors; | ||
| void setTagProducer(TagProducerPtr&& tag_producer) override { | ||
| tag_producer_ = std::move(tag_producer); |
There was a problem hiding this comment.
If this is generally meant to be called once, I would add an ASSERT(tag_producer_ == nullptr); here.
There was a problem hiding this comment.
We expects this to be called one. But tag_producer_ will never be nullptr due to this construcor https://github.com/envoyproxy/envoy/pull/2357/files#diff-4112b28a18267da2a53245cfbacaeb94R16.
There was a problem hiding this comment.
OK no problem just skip it.
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
Signed-off-by: Taiki Ono <[email protected]>
| tag_specifier.tag_name(), tag_specifier.regex())); | ||
|
|
||
| } else if (tag_specifier.tag_value_case() == envoy::api::v2::TagSpecifier::kFixedValue) { | ||
| default_tags_.emplace_back(Stats::Tag{tag_specifier.tag_name(), tag_specifier.fixed_value()}); |
There was a problem hiding this comment.
nit: Can we use this notation: Stats::Tag{.name_ = tag_specifier.tag_name(), .value_ = tag_specifier.fixed_value()}?
Signed-off-by: Taiki Ono <[email protected]>
mattklein123
left a comment
There was a problem hiding this comment.
sorry tiny nit that I just saw, otherwise LGTM
| public: | ||
| virtual ~TagProducer() {} | ||
|
|
||
| /* |
| /** | ||
| * Creates the set of stats tag extractors requested by the config and transfers ownership to the | ||
| * caller. | ||
| /* |
| TagProducerImpl(const envoy::api::v2::StatsConfig& config); | ||
| TagProducerImpl() {} | ||
|
|
||
| /* |
Signed-off-by: Taiki Ono <[email protected]>
|
Thanks! |
* Always link against libc++. Signed-off-by: Piotr Sikora <[email protected]> * review: don't use libc++ with TSan until upstream is fixed. Signed-off-by: Piotr Sikora <[email protected]>
Description:
Support default stats tag which will be added to all metrics. Typical use case is
identifying Envoy instances with the tag.
The previous disscussions were:
So just implement fixed stats tag value option and leave CLI override feature to #2269.
Risk Level: Medium
New features that are not enabled by default.
Testing:
unit test and integration test should cover this. Also munally tested with with statsd_exporter v0.5.0 and Prometheus v2.0.0.
Docs Changes:
envoyproxy/data-plane-api#416
Release Notes:
Added.
API Changes:
envoyproxy/data-plane-api#416
Fixes #1975