Skip to content
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

Add support for service.environment #184

Merged
merged 4 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions docs/tab-widgets/ecs-encoder.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ All you have to do is to use the `co.elastic.logging.logback.EcsEncoder` instead
<encoder class="co.elastic.logging.logback.EcsEncoder">
<serviceName>my-application</serviceName>
<serviceVersion>my-application-version</serviceVersion>
<serviceEnvironment>my-application-environment</serviceEnvironment>
<serviceNodeName>my-application-cluster-node</serviceNodeName>
</encoder>
----
Expand All @@ -61,6 +62,11 @@ All you have to do is to use the `co.elastic.logging.logback.EcsEncoder` instead
|
|Sets the `service.version` field so you can filter your logs by a particular service version

|`serviceEnvironment`
|String
|
|Sets the `service.environment` field so you can filter your logs by a particular service environment

|`serviceNodeName`
|String
|
Expand Down Expand Up @@ -116,10 +122,10 @@ For example:
<Configuration status="DEBUG">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<EcsLayout serviceName="my-app" serviceVersion="my-app-version" serviceNodeName="my-app-cluster-node"/>
<EcsLayout serviceName="my-app" serviceVersion="my-app-version" serviceEnvironment="my-app-environment" serviceNodeName="my-app-cluster-node"/>
</Console>
<File name="LogToFile" fileName="logs/app.log">
<EcsLayout serviceName="my-app" serviceVersion="my-app-version" serviceNodeName="my-app-cluster-node"/>
<EcsLayout serviceName="my-app" serviceVersion="my-app-version" serviceEnvironment="my-app-environment" serviceNodeName="my-app-cluster-node"/>
</File>
</Appenders>
<Loggers>
Expand All @@ -146,6 +152,11 @@ For example:
|
|Sets the `service.version` field so you can filter your logs by a particular service version

|`serviceEnvironment`
|String
|
|Sets the `service.environment` field so you can filter your logs by a particular service environment

|`serviceNodeName`
|String
|
Expand Down Expand Up @@ -238,6 +249,11 @@ For example:
|
|Sets the `service.version` field so you can filter your logs by a particular service version

|`serviceEnvironment`
|String
|
|Sets the `service.environment` field so you can filter your logs by a particular service environment

|`serviceNodeName`
|String
|
Expand Down Expand Up @@ -286,6 +302,7 @@ java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = co.elastic.logging.jul.EcsFormatter
co.elastic.logging.jul.EcsFormatter.serviceName=my-app
co.elastic.logging.jul.EcsFormatter.serviceVersion=my-app-version
co.elastic.logging.jul.EcsFormatter.serviceEnvironment=my-app-environment
co.elastic.logging.jul.EcsFormatter.serviceNodeName=my-app-cluster-node
----

Expand All @@ -304,6 +321,11 @@ co.elastic.logging.jul.EcsFormatter.serviceNodeName=my-app-cluster-node
|
|Sets the `service.version` field so you can filter your logs by a particular service version

|`serviceEnvironment`
|String
|
|Sets the `service.environment` field so you can filter your logs by a particular service environment

|`serviceNodeName`
|String
|
Expand Down Expand Up @@ -350,7 +372,7 @@ Add the formatter to a handler in the logging subsystem:
[source,bash]
----
$WILDFLY_HOME/bin/jboss-cli.sh -c '/subsystem=logging/custom-formatter=ECS:add(module=co.elastic.logging.jboss-logmanager-ecs-formatter,
class=co.elastic.logging.jboss.logmanager.EcsFormatter, properties={serviceName=my-app,serviceVersion=my-app-version,serviceNodeName=my-app-cluster-node}),\
class=co.elastic.logging.jboss.logmanager.EcsFormatter, properties={serviceName=my-app,serviceVersion=my-app-version,serviceEnvironment=my-app-environment,serviceNodeName=my-app-cluster-node}),\
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter,value=ECS)'
----

Expand All @@ -369,6 +391,11 @@ class=co.elastic.logging.jboss.logmanager.EcsFormatter, properties={serviceName=
|
|Sets the `service.version` field so you can filter your logs by a particular service version

|`serviceEnvironment`
|String
|
|Sets the `service.environment` field so you can filter your logs by a particular service environment

|`serviceNodeName`
|String
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ public static void serializeServiceVersion(StringBuilder builder, String service
}
}

public static void serializeServiceEnvironment(StringBuilder builder, String serviceEnvironment) {
if (serviceEnvironment != null) {
builder.append("\"service.environment\":\"");
JsonUtils.quoteAsString(serviceEnvironment, builder);
builder.append("\",");
}
}

public static void serializeServiceNodeName(StringBuilder builder, String serviceNodeName) {
if (serviceNodeName != null) {
builder.append("\"service.node.name\":\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ void testMetadata() throws Exception {
assertThat(logLine.get("process.thread.name").textValue()).isEqualTo(Thread.currentThread().getName());
assertThat(logLine.get("service.name").textValue()).isEqualTo("test");
assertThat(logLine.get("service.version").textValue()).isEqualTo("test-version");
assertThat(logLine.get("service.environment").textValue()).isEqualTo("test-environment");
assertThat(logLine.get("service.node.name").textValue()).isEqualTo("test-node");
assertThat(Instant.parse(logLine.get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES));
assertThat(logLine.get("log.level").textValue()).isIn("DEBUG", "FINE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void testEscaping() throws IOException {
String loggerName = "logger\"";
String serviceName = "test\"";
String serviceVersion = "test-version\"";
String serviceEnvironment = "test-environment\"";
String serviceNodeName = "test-node\"";
String eventDataset = "event-dataset\"";
String threadName = "event-dataset\"";
Expand All @@ -77,6 +78,7 @@ void testEscaping() throws IOException {
EcsJsonSerializer.serializeLoggerName(jsonBuilder, loggerName);
EcsJsonSerializer.serializeServiceName(jsonBuilder, serviceName);
EcsJsonSerializer.serializeServiceVersion(jsonBuilder, serviceVersion);
EcsJsonSerializer.serializeServiceEnvironment(jsonBuilder, serviceEnvironment);
EcsJsonSerializer.serializeServiceNodeName(jsonBuilder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(jsonBuilder, eventDataset);
EcsJsonSerializer.serializeThreadName(jsonBuilder, threadName);
Expand All @@ -88,6 +90,7 @@ void testEscaping() throws IOException {
assertThat(jsonNode.get("log.logger").textValue()).isEqualTo(loggerName);
assertThat(jsonNode.get("service.name").textValue()).isEqualTo(serviceName);
assertThat(jsonNode.get("service.version").textValue()).isEqualTo(serviceVersion);
assertThat(jsonNode.get("service.environment").textValue()).isEqualTo(serviceEnvironment);
assertThat(jsonNode.get("service.node.name").textValue()).isEqualTo(serviceNodeName);
assertThat(jsonNode.get("event.dataset").textValue()).isEqualTo(eventDataset);
assertThat(jsonNode.get("process.thread.name").textValue()).isEqualTo(eventDataset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class EcsFormatter extends ExtFormatter {

private String serviceName;
private String serviceVersion;
private String serviceEnvironment;
private String serviceNodeName;
private String eventDataset;
private List<AdditionalField> additionalFields = Collections.emptyList();
Expand All @@ -45,7 +46,8 @@ public class EcsFormatter extends ExtFormatter {

public EcsFormatter() {
serviceName = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceName", null);
serviceVersion = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceversion", null);
serviceVersion = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceVersion", null);
serviceEnvironment = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceEnvironment", null);
serviceNodeName = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceNodeName", null);
eventDataset = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.eventDataset", null);
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
Expand All @@ -62,6 +64,7 @@ public String format(ExtLogRecord record) {
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceEnvironment(builder, serviceEnvironment);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, record.getThreadName());
Expand Down Expand Up @@ -100,6 +103,10 @@ public void setServiceVersion(final String serviceVersion) {
this.serviceVersion = serviceVersion;
}

public void setServiceEnvironment(final String serviceEnvironment) {
this.serviceEnvironment = serviceEnvironment;
}

public void setServiceNodeName(final String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public void printStackTrace(PrintWriter pw) {
assertThat(result.get("error.type").textValue()).isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1");
assertThat(result.get("error.message").textValue()).isEqualTo("Example Exception Message");
assertThat(result.get("error.stack_trace").textValue())
.isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1: Example Exception Message\n" +
"\tat co.elastic.logging.jboss.logmanager.EcsFormatterTest.testExceptionLogging(EcsFormatterTest.java:125)\n");
.isEqualTo("co.elastic.logging.jboss.logmanager.EcsFormatterTest$1: Example Exception Message" + System.lineSeparator() +
"\tat co.elastic.logging.jboss.logmanager.EcsFormatterTest.testExceptionLogging(EcsFormatterTest.java:125)" + System.lineSeparator());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void setUp() {
formatter.setIncludeOrigin(true);
formatter.setServiceName("test");
formatter.setServiceVersion("test-version");
formatter.setServiceEnvironment("test-environment");
formatter.setServiceNodeName("test-node");
formatter.setEventDataset("testdataset");
formatter.setAdditionalFields("key1=value1,key2=value2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class EcsFormatter extends Formatter {
private boolean stackTraceAsArray;
private String serviceName;
private String serviceVersion;

odin568 marked this conversation as resolved.
Show resolved Hide resolved
private String serviceEnvironment;
private String serviceNodeName;
private boolean includeOrigin;
private String eventDataset;
Expand All @@ -52,6 +54,7 @@ public class EcsFormatter extends Formatter {
public EcsFormatter() {
serviceName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceName", null);
serviceVersion= getProperty("co.elastic.logging.jul.EcsFormatter.serviceVersion", null);
serviceEnvironment= getProperty("co.elastic.logging.jul.EcsFormatter.serviceEnvironment", null);
serviceNodeName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceNodeName", null);
includeOrigin = Boolean.parseBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
stackTraceAsArray = Boolean.parseBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false"));
Expand All @@ -71,6 +74,7 @@ public String format(final LogRecord record) {
EcsJsonSerializer.serializeMDC(builder, mdcSupplier.getMDC());
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceEnvironment(builder, serviceEnvironment);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
if (Thread.currentThread().getId() == record.getThreadID()) {
Expand Down Expand Up @@ -102,6 +106,10 @@ public void setServiceVersion(final String serviceVersion) {
this.serviceVersion = serviceVersion;
}

public void setServiceEnvironment(final String serviceEnvironment) {
this.serviceEnvironment = serviceEnvironment;
}

public void setServiceNodeName(final String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ protected EcsFormatter createEcsFormatter() {
ret.setIncludeOrigin(true);
ret.setServiceName("test");
ret.setServiceVersion("test-version");
ret.setServiceEnvironment("test-environment");
ret.setServiceNodeName("test-node");
ret.setEventDataset("testdataset");
return ret;
Expand Down
1 change: 1 addition & 0 deletions jul-ecs-formatter/src/test/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ECS formatter
co.elastic.logging.jul.EcsFormatter.serviceName=test
co.elastic.logging.jul.EcsFormatter.serviceVersion=test-version
co.elastic.logging.jul.EcsFormatter.serviceEnvironment=test-environment
co.elastic.logging.jul.EcsFormatter.serviceNodeName=test-node
co.elastic.logging.jul.EcsFormatter.eventDataset=testdataset
co.elastic.logging.jul.EcsFormatter.includeOrigin=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class EcsLayout extends Layout {
private boolean stackTraceAsArray = false;
private String serviceName;
private String serviceVersion;

odin568 marked this conversation as resolved.
Show resolved Hide resolved
private String serviceEnvironment;
private String serviceNodeName;
private boolean includeOrigin;
private String eventDataset;
Expand All @@ -55,6 +57,7 @@ public String format(LoggingEvent event) {
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceEnvironment(builder, serviceEnvironment);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, event.getThreadName());
Expand Down Expand Up @@ -107,6 +110,10 @@ public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}

public void setServiceEnvironment(String serviceEnvironment) {
this.serviceEnvironment = serviceEnvironment;
}

public void setServiceNodeName(String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void setUp() {
ecsLayout = new EcsLayout();
ecsLayout.setServiceName("test");
ecsLayout.setServiceVersion("test-version");
ecsLayout.setServiceEnvironment("test-environment");
ecsLayout.setServiceNodeName("test-node");
ecsLayout.setIncludeOrigin(true);
ecsLayout.setEventDataset("testdataset");
Expand Down
1 change: 1 addition & 0 deletions log4j-ecs-layout/src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<layout class="co.elastic.logging.log4j.EcsLayout">
<param name="serviceName" value="test"/>
<param name="serviceVersion" value="test-version"/>
<param name="serviceEnvironment" value="test-environment"/>
<param name="serviceNodeName" value="test-node"/>
<param name="eventDataset" value="testdataset"/>
<param name="includeOrigin" value="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ public class EcsLayout extends AbstractStringLayout {
private final boolean stackTraceAsArray;
private final String serviceName;
private final String serviceVersion;

odin568 marked this conversation as resolved.
Show resolved Hide resolved
private final String serviceEnvironment;
private final String serviceNodeName;
private final String eventDataset;
private final boolean includeMarkers;
private final boolean includeOrigin;
private final PatternFormatter[] exceptionPatternFormatter;
private final ConcurrentMap<Class<? extends MultiformatMessage>, Boolean> supportsJson = new ConcurrentHashMap<Class<? extends MultiformatMessage>, Boolean>();

private EcsLayout(Configuration config, String serviceName, String serviceVersion, String serviceNodeName, String eventDataset, boolean includeMarkers,
private EcsLayout(Configuration config, String serviceName, String serviceVersion, String serviceEnvironment, String serviceNodeName, String eventDataset, boolean includeMarkers,
KeyValuePair[] additionalFields, boolean includeOrigin, String exceptionPattern, boolean stackTraceAsArray) {
super(config, UTF_8, null, null);
this.serviceName = serviceName;
this.serviceVersion = serviceVersion;
this.serviceEnvironment = serviceEnvironment;
this.serviceNodeName = serviceNodeName;
this.eventDataset = eventDataset;
this.includeMarkers = includeMarkers;
Expand Down Expand Up @@ -143,6 +146,7 @@ private StringBuilder toText(LogEvent event, StringBuilder builder, boolean gcFr
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceEnvironment(builder, serviceEnvironment);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, event.getThreadName());
Expand Down Expand Up @@ -358,6 +362,8 @@ public static class Builder implements org.apache.logging.log4j.core.util.Builde
private String serviceName;
@PluginBuilderAttribute("serviceVersion")
private String serviceVersion;
@PluginBuilderAttribute("serviceEnvironment")
private String serviceEnvironment;
@PluginBuilderAttribute("serviceNodeName")
private String serviceNodeName;
@PluginBuilderAttribute("eventDataset")
Expand Down Expand Up @@ -397,6 +403,8 @@ public String getServiceVersion() {
return serviceVersion;
}

public String getServiceEnvironment() { return serviceEnvironment; }

public String getServiceNodeName() {
return serviceNodeName;
}
Expand Down Expand Up @@ -441,6 +449,11 @@ public EcsLayout.Builder setServiceVersion(final String serviceVersion) {
return this;
}

public EcsLayout.Builder setServiceEnvironment(final String serviceEnvironment) {
this.serviceEnvironment = serviceEnvironment;
return this;
}

public EcsLayout.Builder setServiceNodeName(final String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
return this;
Expand Down Expand Up @@ -473,7 +486,8 @@ public EcsLayout.Builder setExceptionPattern(String exceptionPattern) {

@Override
public EcsLayout build() {
return new EcsLayout(getConfiguration(), serviceName, serviceVersion, serviceNodeName, EcsJsonSerializer.computeEventDataset(eventDataset, serviceName),
return new EcsLayout(getConfiguration(), serviceName, serviceVersion, serviceEnvironment, serviceNodeName,
EcsJsonSerializer.computeEventDataset(eventDataset, serviceName),
includeMarkers, additionalFields, includeOrigin, exceptionPattern, stackTraceAsArray);
}
}
Expand Down
Loading