Skip to content

Commit

Permalink
Added support for service.version (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstadler authored Apr 28, 2022
1 parent 05e5383 commit f8be7d2
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 13 deletions.
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` 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 @@ -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 @@ -95,6 +95,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 @@ -59,6 +59,7 @@ void testMetadata() throws Exception {
System.out.println(logLine.toPrettyString());
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.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 @@ -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 @@ public void setServiceName(final String serviceName) {
this.serviceName = 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 @@ -120,6 +120,7 @@ private void setUpFormatter() {
formatter = new EcsFormatter();
formatter.setIncludeOrigin(true);
formatter.setServiceName("test");
formatter.setServiceVersion("test-version");
formatter.setServiceNodeName("test-node");
formatter.setEventDataset("testdataset");

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,17 +71,19 @@ 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 PatternFormatter[] exceptionPatternFormatter;
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, String exceptionPattern, 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 @@ -140,6 +142,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 @@ -354,6 +357,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 @@ -389,6 +394,10 @@ public String getServiceName() {
return serviceName;
}

public String getServiceVersion() {
return serviceVersion;
}

public String getServiceNodeName() {
return serviceNodeName;
}
Expand Down Expand Up @@ -428,6 +437,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 @@ -460,7 +474,7 @@ public EcsLayout.Builder setExceptionPattern(String exceptionPattern) {

@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, exceptionPattern, stackTraceAsArray);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/*-
* #%L
* Java ECS logging
* %%
* Copyright (C) 2019 - 2022 Elastic and contributors
* %%
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. 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.
* #L%
*/
package co.elastic.logging.log4j2;

import org.apache.logging.log4j.core.LogEvent;
Expand Down
Loading

0 comments on commit f8be7d2

Please sign in to comment.