summaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/ut/logger_ut.cpp
blob: 54e25a6181b91fa7a8d0606775569af829791bb0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <library/cpp/unified_agent_client/logger.h>

#include <library/cpp/logger/log.h>
#include <library/cpp/logger/stream.h>
#include <library/cpp/monlib/dynamic_counters/counters.h>
#include <library/cpp/testing/gtest/gtest.h>

#include <sstream>
#include <thread>

using namespace NUnifiedAgent;
using namespace NMonitoring;

// Helper to capture log output
class TTestLogBackend : public TLogBackend {
public:
    explicit TTestLogBackend(std::ostringstream* stream)
        : Stream(stream)
    {
    }

    void WriteData(const TLogRecord& rec) override {
        (*Stream) << rec.Data;
    }

    void ReopenLog() override {
    }

private:
    std::ostringstream* Stream;
};

class TLoggerTest : public ::testing::Test {
protected:
    void SetUp() override {
        LogStream = std::make_unique<std::ostringstream>();
        Log = std::make_unique<TLog>(MakeHolder<TTestLogBackend>(LogStream.get()));

        // Create counters for testing
        Counters = MakeIntrusive<TDynamicCounters>();
        ReceivedCounter = Counters->GetCounter("ReceivedCounter", true).Get();
        DroppedCounter = Counters->GetCounter("DroppedCounter", true).Get();

        // Create logger with throttling: 20 logs per 1 second
        LoggerImpl = std::make_unique<TLogger>(*Log, Nothing(), 20, 1,
                                               TLogger::TCounters{.RecordsReceived = ReceivedCounter,
                                                                  .RecordsDropped = DroppedCounter});
        Logger = LoggerImpl->Child("");
    }

    std::string GetLogOutput() {
        return LogStream->str();
    }

    void ClearLogOutput() {
        LogStream->str("");
        LogStream->clear();
    }

    std::unique_ptr<std::ostringstream> LogStream;
    std::unique_ptr<TLog> Log;
    std::unique_ptr<TLogger> LoggerImpl;
    TScopeLogger Logger;
    TIntrusivePtr<TDynamicCounters> Counters;
    TDeprecatedCounter* ReceivedCounter;
    TDeprecatedCounter* DroppedCounter;
};

TEST_F(TLoggerTest, BasicThrottling) {
    // Log 40 messages using YLOG_T macro
    for (int i = 0; i < 40; ++i) {
        YLOG_ERROR_T("Test message {}", i);
    }

    const auto output = GetLogOutput();
    const int count = std::count(output.begin(), output.end(), '\n');

    // Should log exactly 20 messages (first slot)
    EXPECT_EQ(count, 20);

    // Check counters: 40 received, 20 dropped
    EXPECT_EQ(ReceivedCounter->Val(), 40);
    EXPECT_EQ(DroppedCounter->Val(), 20);
}

TEST_F(TLoggerTest, SuppressedCounter) {
    for (int i = 20 + 13; i >= 0; --i) {
        if (i == 0) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
        YLOG_ERROR_T("Test message");
    }

    const auto output = GetLogOutput();

    // Should log 20 messages in first slot + 1 in second slot with suppressed count
    const int count = std::count(output.begin(), output.end(), '\n');
    EXPECT_EQ(count, 21);

    // Should contain "suppressed" in the output (from the message after sleep)
    EXPECT_NE(output.find("[+13 suppressed]"), std::string::npos);

    // Check counters: 34 received (33 + 1 after sleep), 13 dropped
    EXPECT_EQ(ReceivedCounter->Val(), 34);
    EXPECT_EQ(DroppedCounter->Val(), 13);
}

TEST_F(TLoggerTest, IndependentThrottlingPerLocation) {
    // Each log location should have independent throttling
    for (int i = 0; i < 40; ++i) {
        YLOG_ERROR_T("Location A");  // Line 1
        YLOG_ERROR_T("Location B");  // Line 2 - different location
    }

    const auto output = GetLogOutput();

    // Both locations should log ~20 messages each
    const int locACount = std::count(output.begin(), output.end(), 'A');
    const int locBCount = std::count(output.begin(), output.end(), 'B');

    EXPECT_EQ(locACount, 20);
    EXPECT_EQ(locBCount, 20);

    // Total counters: 80 received, 40 dropped
    EXPECT_EQ(ReceivedCounter->Val(), 80);
    EXPECT_EQ(DroppedCounter->Val(), 40);
}

TEST_F(TLoggerTest, FormatMessage) {
    YLOG_ERROR_T("Simple message");
    YLOG_ERROR_T("Formatted: {}, {}", 42, "test");

    const auto output = GetLogOutput();

    EXPECT_NE(output.find("Simple message"), std::string::npos);
    EXPECT_NE(output.find("Formatted: 42, test"), std::string::npos);
}

TEST_F(TLoggerTest, DifferentLogLevels) {
    YLOG_INFO_T("Info message");
    YLOG_WARNING_T("Warning message");
    YLOG_ERROR_T("Error message");

    const auto output = GetLogOutput();

    EXPECT_NE(output.find("Info message"), std::string::npos);
    EXPECT_NE(output.find("Warning message"), std::string::npos);
    EXPECT_NE(output.find("Error message"), std::string::npos);
}

TEST_F(TLoggerTest, DisabledThrottling) {
    LoggerImpl = std::make_unique<TLogger>(*Log, Nothing(), /*maxLogsPerSlot=*/0, /*slotSeconds=*/0,
        TLogger::TCounters{.RecordsReceived = ReceivedCounter,
                        .RecordsDropped = DroppedCounter});

    Logger = LoggerImpl->Child("");

    // Log 40 messages using YLOG_T macro
    for (int i = 0; i < 40; ++i) {
        YLOG_ERROR_T("Test message {}", i);
    }

    const auto output = GetLogOutput();
    const int count = std::count(output.begin(), output.end(), '\n');

    // Should log exactly 40 messages (throttler disabled)
    EXPECT_EQ(count, 40);

    // Check counters: 40 received, 0 dropped
    EXPECT_EQ(ReceivedCounter->Val(), 40);
    EXPECT_EQ(DroppedCounter->Val(), 0);
}