LCOV - code coverage report
Current view: top level - source/common/runtime - runtime_protos.h (source / functions) Hit Total Coverage
Test: coverage.dat Lines: 9 32 28.1 %
Date: 2024-01-05 06:35:25 Functions: 7 14 50.0 %

          Line data    Source code
       1             : #pragma once
       2             : 
       3             : #include <string>
       4             : 
       5             : #include "envoy/config/core/v3/base.pb.h"
       6             : #include "envoy/runtime/runtime.h"
       7             : #include "envoy/type/v3/percent.pb.h"
       8             : 
       9             : #include "source/common/protobuf/utility.h"
      10             : 
      11             : namespace Envoy {
      12             : namespace Runtime {
      13             : 
      14             : // Helper class for runtime-derived uint32.
      15             : class UInt32 : Logger::Loggable<Logger::Id::runtime> {
      16             : public:
      17             :   UInt32(const envoy::config::core::v3::RuntimeUInt32& uint32_proto, Runtime::Loader& runtime)
      18             :       : runtime_key_(uint32_proto.runtime_key()), default_value_(uint32_proto.default_value()),
      19           0 :         runtime_(runtime) {}
      20             : 
      21           0 :   const std::string& runtimeKey() const { return runtime_key_; }
      22             : 
      23           0 :   uint32_t value() const {
      24           0 :     uint64_t raw_value = runtime_.snapshot().getInteger(runtime_key_, default_value_);
      25           0 :     if (raw_value > std::numeric_limits<uint32_t>::max()) {
      26           0 :       ENVOY_LOG_EVERY_POW_2(
      27           0 :           warn,
      28           0 :           "parsed runtime value:{} of {} is larger than uint32 max, returning default instead",
      29           0 :           raw_value, runtime_key_);
      30           0 :       return default_value_;
      31           0 :     }
      32           0 :     return static_cast<uint32_t>(raw_value);
      33           0 :   }
      34             : 
      35             : private:
      36             :   const std::string runtime_key_;
      37             :   const uint32_t default_value_;
      38             :   Runtime::Loader& runtime_;
      39             : };
      40             : 
      41             : // Helper class for runtime-derived boolean feature flags.
      42             : class FeatureFlag {
      43             : public:
      44             :   FeatureFlag(const envoy::config::core::v3::RuntimeFeatureFlag& feature_flag_proto,
      45             :               Runtime::Loader& runtime)
      46             :       : runtime_key_(feature_flag_proto.runtime_key()),
      47             :         default_value_(PROTOBUF_GET_WRAPPED_OR_DEFAULT(feature_flag_proto, default_value, true)),
      48          69 :         runtime_(runtime) {}
      49             : 
      50          56 :   bool enabled() const { return runtime_.snapshot().getBoolean(runtime_key_, default_value_); }
      51             : 
      52             : private:
      53             :   const std::string runtime_key_;
      54             :   const bool default_value_;
      55             :   Runtime::Loader& runtime_;
      56             : };
      57             : 
      58             : // Helper class for runtime-derived doubles.
      59             : class Double {
      60             : public:
      61             :   Double(const envoy::config::core::v3::RuntimeDouble& double_proto, Runtime::Loader& runtime)
      62             :       : runtime_key_(double_proto.runtime_key()), default_value_(double_proto.default_value()),
      63          33 :         runtime_(runtime) {}
      64             :   Double(std::string runtime_key, double default_value, Runtime::Loader& runtime)
      65           0 :       : runtime_key_(std::move(runtime_key)), default_value_(default_value), runtime_(runtime) {}
      66          66 :   virtual ~Double() = default;
      67             : 
      68           0 :   const std::string& runtimeKey() const { return runtime_key_; }
      69             : 
      70        3550 :   virtual double value() const {
      71        3550 :     return runtime_.snapshot().getDouble(runtime_key_, default_value_);
      72        3550 :   }
      73             : 
      74             : protected:
      75             :   const std::string runtime_key_;
      76             :   const double default_value_;
      77             :   Runtime::Loader& runtime_;
      78             : };
      79             : 
      80             : // Helper class for runtime-derived fractional percent flags.
      81             : class FractionalPercent {
      82             : public:
      83             :   FractionalPercent(
      84             :       const envoy::config::core::v3::RuntimeFractionalPercent& fractional_percent_proto,
      85             :       Runtime::Loader& runtime)
      86             :       : runtime_key_(fractional_percent_proto.runtime_key()),
      87           6 :         default_value_(fractional_percent_proto.default_value()), runtime_(runtime) {}
      88             : 
      89           5 :   bool enabled() const { return runtime_.snapshot().featureEnabled(runtime_key_, default_value_); }
      90             : 
      91             : private:
      92             :   const std::string runtime_key_;
      93             :   const envoy::type::v3::FractionalPercent default_value_;
      94             :   Runtime::Loader& runtime_;
      95             : };
      96             : 
      97             : // Helper class for runtime-derived percentages.
      98             : class Percentage : public Double {
      99             : public:
     100             :   Percentage(const envoy::config::core::v3::RuntimePercent& percent_proto, Runtime::Loader& runtime)
     101           0 :       : Double(percent_proto.runtime_key(), percent_proto.default_value().value(), runtime) {}
     102             : 
     103           0 :   double value() const override {
     104           0 :     const auto val = Double::value();
     105           0 :     if (val <= 100.0 && val >= 0.0) {
     106           0 :       return val;
     107           0 :     }
     108           0 :     return default_value_;
     109           0 :   }
     110             : };
     111             : 
     112             : } // namespace Runtime
     113             : } // namespace Envoy

Generated by: LCOV version 1.15