@@ -43,12 +43,11 @@ class StatsAccessLoggerTest : public testing::Test {
4343 }
4444
4545 void initialize (const envoy::extensions::access_loggers::stats::v3::Config& config) {
46- // ON_CALL(context_, statsScope()).WillByDefault(testing::ReturnRef(root_scope_));
4746 ON_CALL (context_, statsScope ()).WillByDefault (testing::ReturnRef (store_.mockScope ()));
4847 EXPECT_CALL (store_.mockScope (), createScope_ (_))
4948 .WillOnce (Invoke ([this ](const std::string& name) {
5049 Stats::StatNameDynamicStorage storage (name, context_.store_ .symbolTable ());
51- scope_ = std::make_shared<Stats::MockScope>(storage.statName (), store_);
50+ scope_ = std::make_shared<NiceMock< Stats::MockScope> >(storage.statName (), store_);
5251 return scope_;
5352 }));
5453
@@ -217,6 +216,24 @@ TEST_F(StatsAccessLoggerTest, NumberStringValueFormatted) {
217216 logger_->log (formatter_context_, stream_info_);
218217}
219218
219+ TEST_F (StatsAccessLoggerTest, CounterValueFixed) {
220+ const std::string yaml = R"EOF(
221+ stat_prefix: test_stat_prefix
222+ counters:
223+ - stat:
224+ name: counter
225+ value_fixed: 42
226+ )EOF" ;
227+
228+ initialize (yaml);
229+
230+ absl::optional<std::string> a_number{" 42" };
231+ EXPECT_CALL (stream_info_, responseCodeDetails ()).WillRepeatedly (testing::ReturnRef (a_number));
232+ EXPECT_CALL (store_, counter (_));
233+ EXPECT_CALL (store_.counter_ , add (42 ));
234+ logger_->log (formatter_context_, stream_info_);
235+ }
236+
220237// Histogram values are in the range 0-1.0, so ensure that fractional values work.
221238TEST_F (StatsAccessLoggerTest, HistogramPercent) {
222239 const std::string yaml = R"EOF(
@@ -248,6 +265,38 @@ TEST_F(StatsAccessLoggerTest, HistogramPercent) {
248265 logger_->log (formatter_context_, stream_info_);
249266}
250267
268+ // Test that a tag formatter that doesn't have a value becomes an empty string.
269+ TEST_F (StatsAccessLoggerTest, EmptyTagFormatter) {
270+ const std::string yaml = R"EOF(
271+ stat_prefix: test_stat_prefix
272+ counters:
273+ - stat:
274+ name: counter
275+ tags:
276+ - name: tag
277+ value_format: '%RESPONSE_CODE_DETAILS%:%RESPONSE_CODE%'
278+ value_fixed: 1
279+ )EOF" ;
280+
281+ initialize (yaml);
282+
283+ absl::optional<std::string> nullopt {absl::nullopt };
284+ EXPECT_CALL (stream_info_, responseCodeDetails ()).WillRepeatedly (testing::ReturnRef (nullopt ));
285+ EXPECT_CALL (stream_info_, responseCode ())
286+ .WillRepeatedly (testing::Return (absl::optional<uint32_t >{200 }));
287+ EXPECT_CALL (*scope_, counterFromStatNameWithTags (_, _))
288+ .WillOnce (
289+ testing::Invoke ([this ](const Stats::StatName& name,
290+ Stats::StatNameTagVectorOptConstRef tags) -> Stats::Counter& {
291+ EXPECT_EQ (" counter" , scope_->symbolTable ().toString (name));
292+ EXPECT_EQ (1 , tags->get ().size ());
293+ EXPECT_EQ (" :200" , scope_->symbolTable ().toString (tags->get ().front ().second ));
294+
295+ return scope_->counterFromStatNameWithTags_ (name, tags);
296+ }));
297+ logger_->log (formatter_context_, stream_info_);
298+ }
299+
251300} // namespace StatsAccessLog
252301} // namespace AccessLoggers
253302} // namespace Extensions
0 commit comments