LCOV - code coverage report
Current view: top level - source/extensions/common/wasm - stats_handler.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 1 8 12.5 %
Date: 2024-01-05 06:35:25 Functions: 1 2 50.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <memory>
       4             : 
       5             : #include "envoy/server/lifecycle_notifier.h"
       6             : #include "envoy/stats/scope.h"
       7             : #include "envoy/stats/stats.h"
       8             : #include "envoy/upstream/cluster_manager.h"
       9             : 
      10             : #include "source/common/common/logger.h"
      11             : #include "source/common/stats/symbol_table.h"
      12             : 
      13             : namespace Envoy {
      14             : namespace Extensions {
      15             : namespace Common {
      16             : namespace Wasm {
      17             : 
      18             : // The custom stat namespace which prepends all the user-defined metrics.
      19             : // Note that the prefix is removed from the final output of /stats endpoints.
      20             : constexpr absl::string_view CustomStatNamespace = "wasmcustom";
      21             : 
      22             : #define CREATE_WASM_STATS(COUNTER, GAUGE)                                                          \
      23           0 :   COUNTER(remote_load_cache_hits)                                                                  \
      24           0 :   COUNTER(remote_load_cache_negative_hits)                                                         \
      25           0 :   COUNTER(remote_load_cache_misses)                                                                \
      26           0 :   COUNTER(remote_load_fetch_successes)                                                             \
      27           0 :   COUNTER(remote_load_fetch_failures)                                                              \
      28           0 :   GAUGE(remote_load_cache_entries, NeverImport)
      29             : 
      30             : struct CreateWasmStats {
      31             :   CREATE_WASM_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT)
      32             : };
      33             : 
      34             : #define LIFECYCLE_STATS(COUNTER, GAUGE)                                                            \
      35             :   COUNTER(created)                                                                                 \
      36             :   GAUGE(active, NeverImport)
      37             : 
      38             : struct LifecycleStats {
      39             :   LIFECYCLE_STATS(GENERATE_COUNTER_STRUCT, GENERATE_GAUGE_STRUCT)
      40             : };
      41             : 
      42             : using ScopeWeakPtr = std::weak_ptr<Stats::Scope>;
      43             : 
      44             : enum class WasmEvent : int {
      45             :   Ok,
      46             :   RemoteLoadCacheHit,
      47             :   RemoteLoadCacheNegativeHit,
      48             :   RemoteLoadCacheMiss,
      49             :   RemoteLoadCacheFetchSuccess,
      50             :   RemoteLoadCacheFetchFailure,
      51             :   UnableToCreateVm,
      52             :   UnableToCloneVm,
      53             :   MissingFunction,
      54             :   UnableToInitializeCode,
      55             :   StartFailed,
      56             :   ConfigureFailed,
      57             :   RuntimeError,
      58             :   VmCreated,
      59             :   VmShutDown,
      60             : };
      61             : 
      62             : class CreateStatsHandler : Logger::Loggable<Logger::Id::wasm> {
      63             : public:
      64           1 :   CreateStatsHandler() = default;
      65             :   ~CreateStatsHandler() = default;
      66             : 
      67             :   void initialize();
      68             : 
      69             :   void onEvent(WasmEvent event);
      70             :   void onRemoteCacheEntriesChanged(int remote_cache_entries);
      71             :   void createStats(const Stats::ScopeSharedPtr& scope) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
      72             :   void resetStats() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Delete stats pointers
      73             : 
      74             :   // NB: the Scope can become invalid if, for example, the owning FilterChain is deleted. When that
      75             :   // happens the stats must be recreated. This hook verifies the Scope of any existing stats and if
      76             :   // necessary recreates the stats with the newly provided scope.
      77             :   // This call takes out the mutex_ and calls createStats and possibly resetStats().
      78             :   Stats::ScopeSharedPtr lockAndCreateStats(const Stats::ScopeSharedPtr& scope);
      79             : 
      80             :   void resetStatsForTesting();
      81             : 
      82             : protected:
      83             :   absl::Mutex mutex_;
      84             :   ScopeWeakPtr scope_;
      85             :   std::unique_ptr<CreateWasmStats> create_wasm_stats_;
      86             : };
      87             : 
      88             : CreateStatsHandler& getCreateStatsHandler();
      89             : 
      90             : class LifecycleStatsHandler {
      91             : public:
      92             :   LifecycleStatsHandler(const Stats::ScopeSharedPtr& scope, std::string runtime)
      93             :       : lifecycle_stats_(LifecycleStats{
      94             :             LIFECYCLE_STATS(POOL_COUNTER_PREFIX(*scope, absl::StrCat("wasm.", runtime, ".")),
      95           0 :                             POOL_GAUGE_PREFIX(*scope, absl::StrCat("wasm.", runtime, ".")))}){};
      96             :   ~LifecycleStatsHandler() = default;
      97             : 
      98             :   void onEvent(WasmEvent event);
      99             :   static int64_t getActiveVmCount();
     100             : 
     101             : protected:
     102             :   LifecycleStats lifecycle_stats_;
     103             : };
     104             : 
     105             : } // namespace Wasm
     106             : } // namespace Common
     107             : } // namespace Extensions
     108             : } // namespace Envoy

Generated by: LCOV version 1.15