-
Notifications
You must be signed in to change notification settings - Fork 24.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logging: Configure the node name when we have it #32983
Changes from 16 commits
cedc062
20fc7cc
fe9f0d7
7d8ce29
2ef93a0
1b4eb20
85f722e
d9bf742
de97aad
8c01b28
bd42188
3aa19f7
2fdfe51
04eca10
3685ddd
6013c50
9ba6da6
c374a3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,23 @@ | ||
// This file is intentionally blank. All configuration of the | ||
// distribution is done in the parent project. | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
integTestRunner { | ||
systemProperty 'tests.logfile', | ||
"${ -> integTest.nodes[0].homeDir}/logs/${ -> integTest.nodes[0].clusterName }.log" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.unconfigurednodename; | ||
|
||
import org.elasticsearch.common.logging.NodeNameInLogsIntegTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.BufferedReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
|
||
public class NodeNameInLogsIT extends NodeNameInLogsIntegTestCase { | ||
@Override | ||
protected BufferedReader openReader(Path logFile) throws IOException { | ||
return AccessController.doPrivileged((PrivilegedAction<BufferedReader>) () -> { | ||
try { | ||
return Files.newBufferedReader(logFile, StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
grant { | ||
// Needed to read the log file | ||
permission java.io.FilePermission "${tests.logfile}", "read"; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
apply plugin: 'elasticsearch.standalone-rest-test' | ||
apply plugin: 'elasticsearch.rest-test' | ||
|
||
integTestCluster { | ||
setting 'node.name', null | ||
} | ||
|
||
integTestRunner { | ||
systemProperty 'tests.logfile', | ||
"${ -> integTest.nodes[0].homeDir}/logs/${ -> integTest.nodes[0].clusterName }.log" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.unconfigurednodename; | ||
|
||
import org.elasticsearch.common.logging.NodeNameInLogsIntegTestCase; | ||
|
||
import java.io.IOException; | ||
import java.io.BufferedReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
|
||
public class NodeNameInLogsIT extends NodeNameInLogsIntegTestCase { | ||
@Override | ||
protected BufferedReader openReader(Path logFile) throws IOException { | ||
return AccessController.doPrivileged((PrivilegedAction<BufferedReader>) () -> { | ||
try { | ||
return Files.newBufferedReader(logFile, StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
grant { | ||
// Needed to read the log file | ||
permission java.io.FilePermission "${tests.logfile}", "read"; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,20 +30,22 @@ | |
|
||
/** | ||
* Converts {@code %node_name} in log4j patterns into the current node name. | ||
* We *could* use a system property lookup instead but this is very explicit | ||
* and fails fast if we try to use the logger without initializing the node | ||
* name. As a bonus it ought to be ever so slightly faster because it doesn't | ||
* have to look up the system property every time. | ||
* We can't use a system property for this because the node name system | ||
* property is only set if the node name is explicitly defined in | ||
* elasticsearch.yml. | ||
*/ | ||
@Plugin(category = PatternConverter.CATEGORY, name = "NodeNamePatternConverter") | ||
@ConverterKeys({"node_name"}) | ||
public class NodeNamePatternConverter extends LogEventPatternConverter { | ||
public final class NodeNamePatternConverter extends LogEventPatternConverter { | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not happy with this at all but it is another thing that I think we'll be able to remove it master in a follow up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jasontedor could you take a look at this when you get a chance? I'm well aware it isn't good but I'm not clear on whether it is disastrous or simply "something we shouldn't keep around for long". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I talked with @jasontedor and he'd prefer the volatile read over this so I'm switching back to it. |
||
* The name of this node. | ||
*/ | ||
private static final SetOnce<String> NODE_NAME = new SetOnce<>(); | ||
|
||
/** | ||
* Set the name of this node. | ||
*/ | ||
public static void setNodeName(String nodeName) { | ||
static void setNodeName(String nodeName) { | ||
NODE_NAME.set(nodeName); | ||
} | ||
|
||
|
@@ -55,18 +57,21 @@ public static NodeNamePatternConverter newInstance(final String[] options) { | |
throw new IllegalArgumentException("no options supported but options provided: " | ||
+ Arrays.toString(options)); | ||
} | ||
return new NodeNamePatternConverter(NODE_NAME.get()); | ||
return new NodeNamePatternConverter(); | ||
} | ||
|
||
private final String nodeName; | ||
|
||
private NodeNamePatternConverter(String nodeName) { | ||
private NodeNamePatternConverter() { | ||
super("NodeName", "node_name"); | ||
this.nodeName = nodeName; | ||
} | ||
|
||
@Override | ||
public void format(LogEvent event, StringBuilder toAppendTo) { | ||
toAppendTo.append(nodeName); | ||
/* | ||
* We're not thrilled about this volatile read on every line logged but | ||
* the alternatives are slightly terrifying and/or don't work with the | ||
* security manager. | ||
*/ | ||
String nodeName = NODE_NAME.get(); | ||
toAppendTo.append(nodeName == null ? "unknown" : nodeName); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One nit, so sorry, can you change the suffix on this package name to
unconfigured_node_name
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always thought package names with underscores was somehow discouraged but I checked around and that doesn't seem to be the case. Adding the underscores makes it easier to read too so I'm just going to have to get used to them. I'll switch it like you ask.