Line data Source code
1 : #include "source/extensions/common/wasm/stats_handler.h" 2 : 3 : #include "source/extensions/common/wasm/context.h" 4 : #include "source/extensions/common/wasm/wasm.h" 5 : #include "source/extensions/common/wasm/wasm_vm.h" 6 : 7 : namespace Envoy { 8 : namespace Extensions { 9 : namespace Common { 10 : namespace Wasm { 11 : 12 0 : Stats::ScopeSharedPtr CreateStatsHandler::lockAndCreateStats(const Stats::ScopeSharedPtr& scope) { 13 0 : absl::MutexLock l(&mutex_); 14 0 : Stats::ScopeSharedPtr lock; 15 0 : if (!(lock = scope_.lock())) { 16 0 : resetStats(); 17 0 : createStats(scope); 18 0 : scope_ = ScopeWeakPtr(scope); 19 0 : return scope; 20 0 : } 21 0 : createStats(scope); 22 0 : return lock; 23 0 : } 24 : 25 0 : void CreateStatsHandler::resetStatsForTesting() { 26 0 : absl::MutexLock l(&mutex_); 27 0 : resetStats(); 28 0 : } 29 : 30 0 : void CreateStatsHandler::onEvent(WasmEvent event) { 31 0 : switch (event) { 32 0 : case WasmEvent::RemoteLoadCacheHit: 33 0 : create_wasm_stats_->remote_load_cache_hits_.inc(); 34 0 : break; 35 0 : case WasmEvent::RemoteLoadCacheNegativeHit: 36 0 : create_wasm_stats_->remote_load_cache_negative_hits_.inc(); 37 0 : break; 38 0 : case WasmEvent::RemoteLoadCacheMiss: 39 0 : create_wasm_stats_->remote_load_cache_misses_.inc(); 40 0 : break; 41 0 : case WasmEvent::RemoteLoadCacheFetchSuccess: 42 0 : create_wasm_stats_->remote_load_fetch_successes_.inc(); 43 0 : break; 44 0 : case WasmEvent::RemoteLoadCacheFetchFailure: 45 0 : create_wasm_stats_->remote_load_fetch_failures_.inc(); 46 0 : break; 47 0 : default: 48 0 : break; 49 0 : } 50 0 : } 51 : 52 0 : void CreateStatsHandler::onRemoteCacheEntriesChanged(int entries) { 53 0 : create_wasm_stats_->remote_load_cache_entries_.set(entries); 54 0 : } 55 : 56 0 : void CreateStatsHandler::createStats(const Stats::ScopeSharedPtr& scope) { 57 0 : if (!create_wasm_stats_) { 58 0 : create_wasm_stats_.reset(new CreateWasmStats{CREATE_WASM_STATS( // NOLINT 59 0 : POOL_COUNTER_PREFIX(*scope, "wasm."), POOL_GAUGE_PREFIX(*scope, "wasm."))}); 60 0 : } 61 0 : } 62 : 63 0 : void CreateStatsHandler::resetStats() { create_wasm_stats_.reset(); } 64 : 65 4 : CreateStatsHandler& getCreateStatsHandler() { MUTABLE_CONSTRUCT_ON_FIRST_USE(CreateStatsHandler); } 66 : 67 : std::atomic<int64_t> active_wasms; 68 : 69 0 : void LifecycleStatsHandler::onEvent(WasmEvent event) { 70 0 : switch (event) { 71 0 : case WasmEvent::VmShutDown: 72 0 : lifecycle_stats_.active_.set(--active_wasms); 73 0 : break; 74 0 : case WasmEvent::VmCreated: 75 0 : lifecycle_stats_.active_.set(++active_wasms); 76 0 : lifecycle_stats_.created_.inc(); 77 0 : break; 78 0 : default: 79 0 : break; 80 0 : } 81 0 : } 82 : 83 0 : int64_t LifecycleStatsHandler::getActiveVmCount() { return active_wasms; }; 84 : 85 : } // namespace Wasm 86 : } // namespace Common 87 : } // namespace Extensions 88 : } // namespace Envoy