Repository: envoyproxy/envoy
File: source/common/stats/thread_local_store.cc (C++)

Code before:
    sinked_histograms_.clear();
    for (auto& histogram : histogram_set_) {
      if (sink_predicates_->includeHistogram(*histogram)) {
        sinked_histograms_.insert(histogram);
      }
    }
  }
}

void ThreadLocalStoreImpl::extractAndAppendTags(StatName name, StatNamePool& pool,
                                                StatNameTagVector& stat_tags) {
  extractAndAppendTags(symbolTable().toString(name), pool, stat_tags);
}

void ThreadLocalStoreImpl::extractAndAppendTags(absl::string_view name, StatNamePool& pool,
                                                StatNameTagVector& stat_tags) {
  TagVector tags;
  tagProducer().produceTags(name, tags);
  for (const auto& tag : tags) {
    stat_tags.emplace_back(pool.add(tag.name_), pool.add(tag.value_));
  }
}

void ThreadLocalStoreImpl::ensureOverflowStats(const ScopeStatsLimitSettings& limits) {
  Thread::LockGuard lock(lock_);
  if (limits.max_counters != 0 && counters_overflow_ == nullptr) {
    StatNamePool pool(symbolTable());
    StatName name = pool.add("server.stats_overflow.counter");
    counters_overflow_ = alloc_.makeCounter(name, name, {});
  }
  if (limits.max_gauges != 0 && gauges_overflow_ == nullptr) {
    StatNamePool pool(symbolTable());
    StatName name = pool.add("server.stats_overflow.gauge");
    gauges_overflow_ = alloc_.makeCounter(name, name, {});
  }
  if (limits.max_histograms != 0 && histograms_overflow_ == nullptr) {
    StatNamePool pool(symbolTable());
    StatName name = pool.add("server.stats_overflow.histogram");
    histograms_overflow_ = alloc_.makeCounter(name, name, {});
  }
}

} // namespace Stats
} // namespace Envoy


Review comment (suggestion):
Maybe move lock guard into the `if` so as regular Scopes don't grab the lock.

Code after:
  ASSERT(sink_predicates != nullptr);
  if (sink_predicates != nullptr) {
    sink_predicates_.emplace(*sink_predicates);
    alloc_.setSinkPredicates(std::move(sink_predicates));
    // Add histograms to the set of sinked histograms.
    Thread::LockGuard lock(hist_mutex_);
    sinked_histograms_.clear();
    for (auto& histogram : histogram_set_) {
      if (sink_predicates_->includeHistogram(*histogram)) {
        sinked_histograms_.insert(histogram);
      }
    }
  }
}

void ThreadLocalStoreImpl::extractAndAppendTags(StatName name, StatNamePool& pool,
                                                StatNameTagVector& stat_tags) {
  extractAndAppendTags(symbolTable().toString(name), pool, stat_tags);
}

void ThreadLocalStoreImpl::extractAndAppendTags(absl::string_view name, StatNamePool& pool,
                                                StatNameTagVector& stat_tags) {
  TagVector tags;
  tagProducer().produceTags(name, tags);
  for (const auto& tag : tags) {
    stat_tags.emplace_back(pool.add(tag.name_), pool.add(tag.value_));
  }
}

void ThreadLocalStoreImpl::ensureOverflowStats(const ScopeStatsLimitSettings& limits) {
  const bool need_counter_overflow_stat = limits.max_counters != 0;
  const bool need_gauge_overflow_stat = limits.max_gauges != 0;
  const bool need_histogram_overflow_stat = limits.max_histograms != 0;

  if (!need_counter_overflow_stat && !need_gauge_overflow_stat && !need_histogram_overflow_stat) {
    return;
  }

  Thread::LockGuard lock(lock_);
  if (need_counter_overflow_stat && counters_overflow_ == nullptr) {
    StatNamePool pool(symbolTable());
    StatName name = pool.add("server.stats_overflow.counter");
    counters_overflow_ = alloc_.makeCounter(name, name, {});
  }
  if (need_gauge_overflow_stat && gauges_overflow_ == nullptr) {
    StatNamePool pool(symbolTable());
    StatName name = pool.add("server.stats_overflow.gauge");
    gauges_overflow_ = alloc_.makeCounter(name, name, {});
  }
  if (need_histogram_overflow_stat && histograms_overflow_ == nullptr) {
    StatNamePool pool(symbolTable());