diff --git a/source/extensions/common/wasm/BUILD b/source/extensions/common/wasm/BUILD index cfeafcc591..cdc13181b1 100644 --- a/source/extensions/common/wasm/BUILD +++ b/source/extensions/common/wasm/BUILD @@ -28,6 +28,7 @@ envoy_cc_library( "//include/envoy/server:wasm_interface", "//include/envoy/upstream:cluster_manager_interface", "//source/common/common:stack_array", + "//source/common/stats:symbol_table_lib", "//source/extensions/filters/http:well_known_names", "@envoy_api//envoy/config/filter/http/wasm/v2:wasm_cc", ], diff --git a/source/extensions/common/wasm/wasm.cc b/source/extensions/common/wasm/wasm.cc index 621ca981fd..93e0adf8fd 100644 --- a/source/extensions/common/wasm/wasm.cc +++ b/source/extensions/common/wasm/wasm.cc @@ -1680,23 +1680,21 @@ void Context::onGrpcReceiveTrailingMetadata(uint32_t token, Http::HeaderMapPtr&& } uint32_t Context::defineMetric(MetricType type, absl::string_view name) { + auto stat_name = wasm_->stat_name_set_.getStatName(name); if (type == MetricType::Counter) { auto id = wasm_->nextCounterMetricId(); - wasm_->counters_.emplace(id, &wasm_->scope_.counter(std::string( - name))); // This is inefficient, but it is the Scope API. + auto c = &wasm_->scope_.counterFromStatName(stat_name); + wasm_->counters_.emplace(id, c); return id; } else if (type == MetricType::Gauge) { auto id = wasm_->nextGaugeMetricId(); - wasm_->gauges_.emplace( - id, - &wasm_->scope_.gauge( - std::string(name), - Stats::Gauge::ImportMode::Accumulate)); // This is inefficient, but it is the Scope API. + auto g = &wasm_->scope_.gaugeFromStatName(stat_name, Stats::Gauge::ImportMode::Accumulate); + wasm_->gauges_.emplace(id, g); return id; } else if (type == MetricType::Histogram) { auto id = wasm_->nextHistogramMetricId(); - wasm_->histograms_.emplace(id, &wasm_->scope_.histogram(std::string( - name))); // This is inefficient, but it is the Scope API. + auto h = &wasm_->scope_.histogramFromStatName(stat_name); + wasm_->histograms_.emplace(id, h); return id; } return 0; @@ -1766,7 +1764,8 @@ Wasm::Wasm(absl::string_view vm, absl::string_view id, absl::string_view vm_conf Stats::ScopeSharedPtr owned_scope) : cluster_manager_(cluster_manager), dispatcher_(dispatcher), scope_(scope), local_info_(local_info), listener_metadata_(listener_metadata), owned_scope_(owned_scope), - time_source_(dispatcher.timeSource()), vm_configuration_(vm_configuration) { + time_source_(dispatcher.timeSource()), vm_configuration_(vm_configuration), + stat_name_set_(scope_.symbolTable()) { wasm_vm_ = Common::Wasm::createWasmVm(vm); id_ = std::string(id); } @@ -1946,7 +1945,7 @@ Wasm::Wasm(const Wasm& wasm, Event::Dispatcher& dispatcher) : std::enable_shared_from_this(wasm), cluster_manager_(wasm.cluster_manager_), dispatcher_(dispatcher), scope_(wasm.scope_), local_info_(wasm.local_info_), listener_metadata_(wasm.listener_metadata_), id_(wasm.id_), owned_scope_(wasm.owned_scope_), - time_source_(dispatcher.timeSource()) { + time_source_(dispatcher.timeSource()), stat_name_set_(scope_.symbolTable()) { wasm_vm_ = wasm.wasmVm()->clone(); vm_context_ = std::make_shared(this); getFunctions(); diff --git a/source/extensions/common/wasm/wasm.h b/source/extensions/common/wasm/wasm.h index 6ea8f1bc0f..937432962f 100644 --- a/source/extensions/common/wasm/wasm.h +++ b/source/extensions/common/wasm/wasm.h @@ -18,6 +18,7 @@ #include "common/common/c_smart_ptr.h" #include "common/common/logger.h" #include "common/common/stack_array.h" +#include "common/stats/symbol_table_impl.h" #include "extensions/common/wasm/well_known_names.h" #include "extensions/filters/http/well_known_names.h" @@ -761,6 +762,11 @@ class Wasm : public Envoy::Server::Wasm, std::unique_ptr> global_Infinity_; // Stats/Metrics + // TODO(jplevyak): replace the use of Stats::StatNameSet with something more efficient. + // By having a separate StatNameSet per Wasm we are duplicating all the strings but + // avoiding locks. Consider lock-free hash tables or pre-registering stats. + Stats::StatNameSet stat_name_set_; + absl::flat_hash_map stat_names_; uint32_t next_counter_metric_id_ = kMetricTypeCounter; uint32_t next_gauge_metric_id_ = kMetricTypeGauge; uint32_t next_histogram_metric_id_ = kMetricTypeHistogram; diff --git a/tools/check_format.py b/tools/check_format.py index 77fd8bbfcb..e28ca04160 100755 --- a/tools/check_format.py +++ b/tools/check_format.py @@ -49,8 +49,7 @@ # https://github.com/envoyproxy/envoy/pull/7573 and others. # # TODO(#4196): Eliminate this list completely and then merge #4980. -STAT_FROM_STRING_WHITELIST = ("./source/extensions/common/wasm/wasm.cc", - "./source/extensions/filters/http/fault/fault_filter.cc", +STAT_FROM_STRING_WHITELIST = ("./source/extensions/filters/http/fault/fault_filter.cc", "./source/extensions/filters/http/ip_tagging/ip_tagging_filter.cc", "./source/extensions/filters/network/mongo_proxy/proxy.cc", "./source/extensions/stat_sinks/common/statsd/statsd.cc",