Skip to content

Commit 2d6a0a6

Browse files
authored
Use proxy.config.log.hostname for rotated log filenames (#7943)
When ATS rotates logs it includes the hostname in the rotated log filename. The user has the ability to set the hostname via 'proxy.config.log.hostname', however ATS did not respected this configuration parameter when naming the rotated log file. Rather, ATS always used the result of gethostname(). This fixes the log rotation mechanism so that if the user sets 'proxy.config.log.hostname' to a non-default value, then ATS will use that when naming the rotated log filename.
1 parent bf3fa57 commit 2d6a0a6

File tree

5 files changed

+55
-24
lines changed

5 files changed

+55
-24
lines changed

proxy/logging/LogConfig.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "tscore/ink_platform.h"
2525
#include "tscore/I_Layout.h"
26+
#include "I_Machine.h"
2627

2728
#ifdef HAVE_SYS_PARAM_H
2829
#include <sys/param.h>
@@ -68,13 +69,7 @@
6869
void
6970
LogConfig::setup_default_values()
7071
{
71-
const unsigned int bufSize = 512;
72-
char name[bufSize];
73-
if (!gethostname(name, bufSize)) {
74-
ink_strlcpy(name, "unknown_host_name", sizeof(name));
75-
}
76-
hostname = ats_strdup(name);
77-
72+
hostname = ats_strdup(Machine::instance()->hostname);
7873
log_buffer_size = static_cast<int>(10 * LOG_KILOBYTE);
7974
max_secs_per_buffer = 5;
8075
max_space_mb_for_logs = 100;
@@ -160,7 +155,7 @@ LogConfig::read_configuration_variables()
160155
ats_free(ptr);
161156

162157
ptr = REC_ConfigReadString("proxy.config.log.hostname");
163-
if (ptr != nullptr) {
158+
if (ptr != nullptr && std::string_view(ptr) != "localhost") {
164159
ats_free(hostname);
165160
hostname = ptr;
166161
}

proxy/logging/LogFile.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ LogFile::LogFile(const char *name, const char *header, LogFileFormat format, uin
7373
{
7474
if (m_file_format != LOG_FILE_PIPE) {
7575
m_log = new BaseLogFile(name, m_signature);
76-
m_log->set_hostname(Machine::instance()->hostname);
76+
// Use Log::config->hostname rather than Machine::instance()->hostname
77+
// because the former is reloadable.
78+
m_log->set_hostname(Log::config->hostname);
7779
} else {
7880
m_log = nullptr;
7981
}

proxy/shared/UglyLogStubs.cc

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,31 @@ ConfigUpdateCbTable::invoke(const char * /* name ATS_UNUSED */)
6666
}
6767

6868
struct Machine {
69+
Machine();
70+
~Machine();
6971
static Machine *instance();
72+
char *hostname = nullptr;
73+
74+
private:
75+
static Machine _instance;
7076
};
77+
78+
Machine Machine::_instance;
79+
80+
Machine::Machine()
81+
{
82+
hostname = ats_strdup("test.host.com");
83+
}
84+
85+
Machine::~Machine()
86+
{
87+
ats_free(hostname);
88+
}
89+
7190
Machine *
7291
Machine::instance()
7392
{
74-
ink_release_assert(false);
75-
return nullptr;
93+
return &_instance;
7694
}
7795

7896
NetAccept *

src/traffic_server/traffic_server.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,13 @@ main(int /* argc ATS_UNUSED */, const char **argv)
19431943
} else if (HttpConfig::m_master.inbound_ip6.isValid()) {
19441944
machine_addr.assign(HttpConfig::m_master.inbound_ip6);
19451945
}
1946-
Machine::init(nullptr, &machine_addr.sa);
1946+
char *hostname = REC_ConfigReadString("proxy.config.log.hostname");
1947+
if (hostname != nullptr && std::string_view(hostname) == "localhost") {
1948+
// The default value was used. Let Machine::init derive the hostname.
1949+
hostname = nullptr;
1950+
}
1951+
Machine::init(hostname, &machine_addr.sa);
1952+
ats_free(hostname);
19471953

19481954
RecRegisterStatString(RECT_PROCESS, "proxy.process.version.server.uuid", (char *)Machine::instance()->uuid.getString(),
19491955
RECP_NON_PERSISTENT);

tests/gold_tests/logging/log_retention.test.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# limitations under the License.
1919

2020
import os
21+
import socket
2122

2223
Test.Summary = '''
2324
Test the enforcement of proxy.config.log.max_space_mb_for_logs.
@@ -46,7 +47,7 @@ class TestLogRetention:
4647
}
4748

4849
__server = None
49-
__ts_counter = 1
50+
__ts_counter = 0
5051
__server_is_started = False
5152

5253
def __init__(self, records_config, run_description, command="traffic_manager"):
@@ -144,14 +145,17 @@ def get_command_to_rotate_thrice(self):
144145

145146

146147
#
147-
# Run 1: Verify that log deletion happens when no min_count is specified.
148+
# Test 0: Verify that log deletion happens when no min_count is specified.
148149
#
150+
specified_hostname = 'my_hostname'
149151
twelve_meg_log_space = {
150152
# The following configures a 12 MB log cap with a required 2 MB head room.
151153
# Thus the rotated log of just over 10 MB should be deleted because it
152154
# will not leave enough head room.
153155
'proxy.config.log.max_space_mb_headroom': 2,
154156
'proxy.config.log.max_space_mb_for_logs': 12,
157+
# Verify that setting a hostname changes the hostname used in rolled logs.
158+
'proxy.config.log.hostname': specified_hostname,
155159
}
156160
test = TestLogRetention(twelve_meg_log_space,
157161
"Verify log rotation and deletion of the configured log file with no min_count.")
@@ -187,7 +191,7 @@ def get_command_to_rotate_thrice(self):
187191
"Verify manager.log auto-delete configuration")
188192
# Verify test_deletion was rotated and deleted.
189193
test.ts.Streams.stderr += Testers.ContainsExpression(
190-
"The rolled logfile.*test_deletion.log_.*was auto-deleted.*bytes were reclaimed",
194+
f"The rolled logfile.*test_deletion.log_{specified_hostname}.*was auto-deleted.*bytes were reclaimed",
191195
"Verify that space was reclaimed")
192196

193197
test.tr.Processes.Default.Command = test.get_command_to_rotate_once()
@@ -198,7 +202,7 @@ def get_command_to_rotate_thrice(self):
198202

199203

200204
#
201-
# Test 2: Verify log deletion happens with a min_count of 1.
205+
# Test 1: Verify log deletion happens with a min_count of 1.
202206
#
203207
test = TestLogRetention(twelve_meg_log_space,
204208
"Verify log rotation and deletion of the configured log file with a min_count of 1.")
@@ -236,7 +240,7 @@ def get_command_to_rotate_thrice(self):
236240
"Verify manager.log auto-delete configuration")
237241
# Verify test_deletion was rotated and deleted.
238242
test.ts.Streams.stderr += Testers.ContainsExpression(
239-
"The rolled logfile.*test_deletion.log_.*was auto-deleted.*bytes were reclaimed",
243+
f"The rolled logfile.*test_deletion.log_{specified_hostname}.*was auto-deleted.*bytes were reclaimed",
240244
"Verify that space was reclaimed")
241245

242246
test.tr.Processes.Default.Command = test.get_command_to_rotate_once()
@@ -246,7 +250,7 @@ def get_command_to_rotate_thrice(self):
246250

247251

248252
#
249-
# Test 3: Verify log deletion happens for a plugin's logs.
253+
# Test 2: Verify log deletion happens for a plugin's logs.
250254
#
251255
test = TestLogRetention(twelve_meg_log_space,
252256
"Verify log rotation and deletion of plugin logs.")
@@ -279,7 +283,7 @@ def get_command_to_rotate_thrice(self):
279283
test.tr.StillRunningAfter = test.server
280284

281285
#
282-
# Test 4: Verify log deletion priority behavior.
286+
# Test 3: Verify log deletion priority behavior.
283287
#
284288
twenty_two_meg_log_space = {
285289
# The following configures a 22 MB log cap with a required 2 MB head room.
@@ -332,8 +336,12 @@ def get_command_to_rotate_thrice(self):
332336
test.ts.Streams.stderr += Testers.ExcludesExpression(
333337
"The rolled logfile.*test_low_priority_deletion.log_.*was auto-deleted.*bytes were reclaimed",
334338
"Verify that space was reclaimed from test_high_priority_deletion")
339+
340+
# Verify that ATS derives the hostname correctly if the user does not specify a
341+
# hostname via 'proxy.config.log.hostname'.
342+
hostname = socket.gethostname()
335343
test.ts.Streams.stderr += Testers.ContainsExpression(
336-
"The rolled logfile.*test_high_priority_deletion.log_.*was auto-deleted.*bytes were reclaimed",
344+
f"The rolled logfile.*test_high_priority_deletion.log_{hostname}.*was auto-deleted.*bytes were reclaimed",
337345
"Verify that space was reclaimed from test_high_priority_deletion")
338346

339347
test.tr.Processes.Default.Command = test.get_command_to_rotate_once()
@@ -342,7 +350,7 @@ def get_command_to_rotate_thrice(self):
342350
test.tr.StillRunningAfter = test.server
343351

344352
#
345-
# Test 5: Verify min_count configuration overrides.
353+
# Test 4: Verify min_count configuration overrides.
346354
#
347355
various_min_count_overrides = {
348356
'proxy.config.log.max_space_mb_for_logs': 22,
@@ -381,11 +389,13 @@ def get_command_to_rotate_thrice(self):
381389

382390

383391
#
384-
# Test 6: Verify log deletion does not happen when it is disabled.
392+
# Test 5: Verify log deletion does not happen when it is disabled.
385393
#
386394
auto_delete_disabled = twelve_meg_log_space.copy()
387395
auto_delete_disabled.update({
388396
'proxy.config.log.auto_delete_rolled_files': 0,
397+
# Verify that setting a hostname changes the hostname used in rolled logs.
398+
'proxy.config.log.hostname': 'my_hostname',
389399
})
390400
test = TestLogRetention(auto_delete_disabled,
391401
"Verify log deletion does not happen when auto-delet is disabled.")
@@ -432,7 +442,7 @@ def get_command_to_rotate_thrice(self):
432442
test.tr.StillRunningAfter = test.server
433443

434444
#
435-
# Test 7: Verify that max_roll_count is respected.
445+
# Test 6: Verify that max_roll_count is respected.
436446
#
437447
max_roll_count_of_2 = {
438448
'proxy.config.diags.debug.tags': 'log-file',
@@ -472,7 +482,7 @@ def get_command_to_rotate_thrice(self):
472482
test.tr.StillRunningAfter = test.server
473483

474484
#
475-
# Test 8: Verify log deletion happens after a config reload.
485+
# Test 7: Verify log deletion happens after a config reload.
476486
#
477487
test = TestLogRetention(twelve_meg_log_space,
478488
"Verify log rotation and deletion after a config reload.")

0 commit comments

Comments
 (0)