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

Added support for service.version #168

Merged
merged 4 commits into from
Apr 28, 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
43 changes: 35 additions & 8 deletions docs/tab-widgets/ecs-encoder.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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>
<serviceNodeName>my-application-cluster-node</serviceNodeName>
</encoder>
----
Expand All @@ -53,7 +54,12 @@ All you have to do is to use the `co.elastic.logging.logback.EcsEncoder` instead
|`serviceName`
|String
|
|Sets the `service.name` field so you can filter your logs by a particular service
|Sets the `service.name` fieldso you can filter your logs by a particular service name
eyalkoren marked this conversation as resolved.
Show resolved Hide resolved

|`serviceVersion`
|String
|
|Sets the `service.version` fieldso you can filter your logs by a particular service version
eyalkoren marked this conversation as resolved.
Show resolved Hide resolved

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

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

|`serviceNodeName`
|String
Expand Down Expand Up @@ -220,7 +231,12 @@ For example:
|`serviceName`
|String
|
|Sets the `service.name` field so you can filter your logs by a particular service
|Sets the `service.name` field so you can filter your logs by a particular service name

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

|`serviceNodeName`
|String
Expand Down Expand Up @@ -269,6 +285,7 @@ For example, in `$CATALINA_HOME/conf/logging.properties`:
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.serviceNodeName=my-app-cluster-node
----

Expand All @@ -280,7 +297,12 @@ co.elastic.logging.jul.EcsFormatter.serviceNodeName=my-app-cluster-node
|`serviceName`
|String
|
|Sets the `service.name` field so you can filter your logs by a particular service
|Sets the `service.name` field so you can filter your logs by a particular service name

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

|`serviceNodeName`
|String
Expand Down Expand Up @@ -328,7 +350,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,serviceNodeName=my-app-cluster-node}),\
class=co.elastic.logging.jboss.logmanager.EcsFormatter, properties={serviceName=my-app,serviceVersion=my-app-version,serviceNodeName=my-app-cluster-node}),\
/subsystem=logging/console-handler=CONSOLE:write-attribute(name=named-formatter,value=ECS)'
----

Expand All @@ -340,7 +362,12 @@ class=co.elastic.logging.jboss.logmanager.EcsFormatter, properties={serviceName=
|`serviceName`
|String
|
|Sets the `service.name` field so you can filter your logs by a particular service
|Sets the `service.name` field so you can filter your logs by a particular service name

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

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

public static void serializeServiceVersion(StringBuilder builder, String serviceVersion) {
if (serviceVersion != null) {
builder.append("\"service.version\":\"");
JsonUtils.quoteAsString(serviceVersion, 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 @@ -57,6 +57,7 @@ void testMetadata() throws Exception {
debug("test");
assertThat(getAndValidateLastLogLine().get("process.thread.name").textValue()).isEqualTo(Thread.currentThread().getName());
assertThat(getAndValidateLastLogLine().get("service.name").textValue()).isEqualTo("test");
assertThat(getAndValidateLastLogLine().get("service.version").textValue()).isEqualTo("test-version");
assertThat(getAndValidateLastLogLine().get("service.node.name").textValue()).isEqualTo("test-node");
assertThat(Instant.parse(getAndValidateLastLogLine().get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES));
assertThat(getAndValidateLastLogLine().get("log.level").textValue()).isIn("DEBUG", "FINE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void serializeExceptionAsString() throws IOException {
void testEscaping() throws IOException {
String loggerName = "logger\"";
String serviceName = "test\"";
String serviceVersion = "test-version\"";
String serviceNodeName = "test-node\"";
String eventDataset = "event-dataset\"";
String threadName = "event-dataset\"";
Expand All @@ -75,6 +76,7 @@ void testEscaping() throws IOException {
jsonBuilder.append('{');
EcsJsonSerializer.serializeLoggerName(jsonBuilder, loggerName);
EcsJsonSerializer.serializeServiceName(jsonBuilder, serviceName);
EcsJsonSerializer.serializeServiceVersion(jsonBuilder, serviceVersion);
EcsJsonSerializer.serializeServiceNodeName(jsonBuilder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(jsonBuilder, eventDataset);
EcsJsonSerializer.serializeThreadName(jsonBuilder, threadName);
Expand All @@ -85,6 +87,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.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 @@ -24,19 +24,19 @@
*/
package co.elastic.logging.jboss.logmanager;

import co.elastic.logging.EcsJsonSerializer;
import co.elastic.logging.AdditionalField;
import co.elastic.logging.EcsJsonSerializer;
import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.LogManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EcsFormatter extends ExtFormatter {

private String serviceName;
private String serviceVersion;
private String serviceNodeName;
private String eventDataset;
private List<AdditionalField> additionalFields = Collections.emptyList();
Expand All @@ -45,6 +45,7 @@ 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);
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 @@ -60,6 +61,7 @@ public String format(ExtLogRecord record) {
EcsJsonSerializer.serializeFormattedMessage(builder, record.getFormattedMessage());
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, record.getThreadName());
Expand Down Expand Up @@ -94,6 +96,10 @@ public void setServiceName(final String serviceName) {
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
}

public void setServiceVersion(final String serviceVersion) {
this.serviceVersion = serviceVersion;
}

public void setServiceNodeName(final String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public JsonNode getLastLogLine() throws IOException {
void setUp() {
formatter.setIncludeOrigin(true);
formatter.setServiceName("test");
formatter.setServiceVersion("test-version");
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 @@ -40,6 +40,7 @@ public class EcsFormatter extends Formatter {

private boolean stackTraceAsArray;
private String serviceName;
private String serviceVersion;
private String serviceNodeName;
private boolean includeOrigin;
private String eventDataset;
Expand All @@ -50,6 +51,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);
serviceNodeName = getProperty("co.elastic.logging.jul.EcsFormatter.serviceNodeName", null);
includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
stackTraceAsArray = Boolean
Expand All @@ -69,6 +71,7 @@ public String format(final LogRecord record) {
EcsJsonSerializer.serializeAdditionalFields(builder, additionalFields);
EcsJsonSerializer.serializeMDC(builder, mdcSupplier.getMDC());
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
if (Thread.currentThread().getId() == record.getThreadID()) {
Expand Down Expand Up @@ -96,6 +99,10 @@ protected void setServiceName(final String serviceName) {
this.serviceName = serviceName;
}

protected void setServiceVersion(final String serviceVersion) {
eyalkoren marked this conversation as resolved.
Show resolved Hide resolved
this.serviceVersion = serviceVersion;
}

protected void setServiceNodeName(final String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void setUp() {

formatter.setIncludeOrigin(true);
formatter.setServiceName("test");
formatter.setServiceVersion("test-version");
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 @@ -40,6 +40,7 @@ public class EcsLayout extends Layout {

private boolean stackTraceAsArray = false;
private String serviceName;
private String serviceVersion;
private String serviceNodeName;
private boolean includeOrigin;
private String eventDataset;
Expand All @@ -53,6 +54,7 @@ public String format(LoggingEvent event) {
EcsJsonSerializer.serializeFormattedMessage(builder, event.getRenderedMessage());
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, event.getThreadName());
Expand Down Expand Up @@ -101,6 +103,10 @@ public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}

public void setServiceNodeName(String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void setUp() {
logger.addAppender(appender);
ecsLayout = new EcsLayout();
ecsLayout.setServiceName("test");
ecsLayout.setServiceVersion("test-version");
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 @@ -4,6 +4,7 @@
<appender name="List" class="co.elastic.logging.log4j.ListAppender">
<layout class="co.elastic.logging.log4j.EcsLayout">
<param name="serviceName" value="test"/>
<param name="serviceVersion" value="test-version"/>
<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 @@ -71,16 +71,18 @@ public class EcsLayout extends AbstractStringLayout {
private final PatternFormatter[][] fieldValuePatternFormatter;
private final boolean stackTraceAsArray;
private final String serviceName;
private final String serviceVersion;
private final String serviceNodeName;
private final String eventDataset;
private final boolean includeMarkers;
private final boolean includeOrigin;
private final ConcurrentMap<Class<? extends MultiformatMessage>, Boolean> supportsJson = new ConcurrentHashMap<Class<? extends MultiformatMessage>, Boolean>();

private EcsLayout(Configuration config, String serviceName, String serviceNodeName, String eventDataset, boolean includeMarkers,
private EcsLayout(Configuration config, String serviceName, String serviceVersion, String serviceNodeName, String eventDataset, boolean includeMarkers,
KeyValuePair[] additionalFields, boolean includeOrigin, boolean stackTraceAsArray) {
super(config, UTF_8, null, null);
this.serviceName = serviceName;
this.serviceVersion = serviceVersion;
this.serviceNodeName = serviceNodeName;
this.eventDataset = eventDataset;
this.includeMarkers = includeMarkers;
Expand Down Expand Up @@ -131,6 +133,7 @@ private StringBuilder toText(LogEvent event, StringBuilder builder, boolean gcFr
serializeMessage(builder, gcFree, event.getMessage(), event.getThrown());
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeServiceNodeName(builder, serviceNodeName);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, event.getThreadName());
Expand Down Expand Up @@ -331,6 +334,8 @@ public static class Builder implements org.apache.logging.log4j.core.util.Builde
private Configuration configuration;
@PluginBuilderAttribute("serviceName")
private String serviceName;
@PluginBuilderAttribute("serviceVersion")
private String serviceVersion;
@PluginBuilderAttribute("serviceNodeName")
private String serviceNodeName;
@PluginBuilderAttribute("eventDataset")
Expand Down Expand Up @@ -364,6 +369,10 @@ public String getServiceName() {
return serviceName;
}

public String getServiceVersion() {
return serviceVersion;
}

public String getServiceNodeName() {
return serviceNodeName;
}
Expand Down Expand Up @@ -395,6 +404,11 @@ public EcsLayout.Builder setServiceName(final String serviceName) {
return this;
}

public EcsLayout.Builder setServiceVersion(final String serviceVersion) {
this.serviceVersion = serviceVersion;
return this;
}

public EcsLayout.Builder setServiceNodeName(final String serviceNodeName) {
this.serviceNodeName = serviceNodeName;
return this;
Expand Down Expand Up @@ -422,7 +436,7 @@ public EcsLayout.Builder setStackTraceAsArray(boolean stackTraceAsArray) {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void setUp() {
EcsLayout ecsLayout = EcsLayout.newBuilder()
.setConfiguration(ctx.getConfiguration())
.setServiceName("test")
.setServiceVersion("test-version")
.setServiceNodeName("test-node")
.setIncludeMarkers(true)
.setIncludeOrigin(true)
Expand Down
2 changes: 1 addition & 1 deletion log4j2-ecs-layout/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</Properties>
<Appenders>
<List name="TestAppender">
<EcsLayout serviceName="test" serviceNodeName="test-node" includeMarkers="true" includeOrigin="true"
<EcsLayout serviceName="test" serviceVersion="test-version" serviceNodeName="test-node" includeMarkers="true" includeOrigin="true"
eventDataset="testdataset">
<KeyValuePair key="cluster.uuid" value="9fe9134b-20b0-465e-acf9-8cc09ac9053b"/>
<KeyValuePair key="node.id" value="${node.id}"/>
Expand Down
Loading