Skip to content

Adding service version #132

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 30 additions & 5 deletions docs/tab-widgets/ecs-encoder.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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

|`eventDataset`
|String
Expand Down Expand Up @@ -125,7 +130,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

|`eventDataset`
|String
Expand Down Expand Up @@ -205,7 +215,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

|`eventDataset`
|String
Expand Down Expand Up @@ -259,7 +274,12 @@ co.elastic.logging.jul.EcsFormatter.serviceName=my-app
|`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

|`eventDataset`
|String
Expand Down Expand Up @@ -313,7 +333,12 @@ $WILDFLY_HOME/bin/jboss-cli.sh -c '/subsystem=logging/custom-formatter=ECS:add(m
|`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

|`eventDataset`
|String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* 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
Expand Down Expand Up @@ -91,6 +91,12 @@ public static void serializeServiceName(StringBuilder builder, String serviceNam
}
}

public static void serializeServiceVersion(StringBuilder builder, String serviceVersion) {
if (serviceVersion != null) {
builder.append("\"service.version\":\"").append(serviceVersion).append("\",");
}
}

public static void serializeEventDataset(StringBuilder builder, String eventDataset) {
if (eventDataset != null) {
builder.append("\"event.dataset\":\"").append(eventDataset).append("\",");
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("1.0.0");
assertThat(Instant.parse(getAndValidateLastLogLine().get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES));
assertThat(getAndValidateLastLogLine().get("log.level").textValue()).isIn("DEBUG", "FINE");
assertThat(getAndValidateLastLogLine().get("log.logger")).isNotNull();
Expand Down
9 changes: 9 additions & 0 deletions ecs-logging-core/src/test/resources/spec/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@
"When an APM agent is active, they should auto-configure it if not already set."
]
},
"service.version": {
"type": "string",
"required": false,
"url": "https://www.elastic.co/guide/en/ecs/current/ecs-service.html",
"comment": [
"Configurable by users.",
"When an APM agent is active, they should auto-configure it if not already set."
]
},
"event.dataset": {
"type": "string",
"required": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
public class EcsFormatter extends ExtFormatter {

private String serviceName;
private String serviceVersion;
private String eventDataset;
private List<AdditionalField> additionalFields = Collections.emptyList();
private boolean includeOrigin;
private boolean stackTraceAsArray;

public EcsFormatter() {
serviceName = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceName", null);
serviceVersion = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.serviceVersion", null);
eventDataset = getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.eventDataset", null);
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jboss.logmanager.EcsFormatter.includeOrigin", "false"));
Expand All @@ -58,6 +60,7 @@ public String format(ExtLogRecord record) {
EcsJsonSerializer.serializeFormattedMessage(builder, record.getFormattedMessage());
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, record.getThreadName());
EcsJsonSerializer.serializeLoggerName(builder, record.getLoggerName());
Expand Down Expand Up @@ -91,6 +94,10 @@ public void setServiceName(final String serviceName) {
eventDataset = EcsJsonSerializer.computeEventDataset(eventDataset, serviceName);
}

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

public void setStackTraceAsArray(final boolean stackTraceAsArray) {
this.stackTraceAsArray = stackTraceAsArray;
}
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("1.0.0");
formatter.setEventDataset("testdataset.log");
formatter.setAdditionalFields("key1=value1,key2=value2");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* 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
Expand All @@ -37,9 +37,10 @@ public class EcsFormatter extends Formatter {

private static final String UNKNOWN_FILE = "<Unknown>";
private static final MdcSupplier mdcSupplier = MdcSupplier.Resolver.INSTANCE.resolve();

private boolean stackTraceAsArray;
private String serviceName;
private String serviceVersion;
private boolean includeOrigin;
private String eventDataset;
private List<AdditionalField> additionalFields = Collections.emptyList();
Expand All @@ -49,6 +50,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);
includeOrigin = Boolean.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.includeOrigin", "false"));
stackTraceAsArray = Boolean
.getBoolean(getProperty("co.elastic.logging.jul.EcsFormatter.stackTraceAsArray", "false"));
Expand All @@ -67,6 +69,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.serializeEventDataset(builder, eventDataset);
if (Thread.currentThread().getId() == record.getThreadID()) {
EcsJsonSerializer.serializeThreadName(builder, Thread.currentThread().getName());
Expand All @@ -93,10 +96,14 @@ protected void setServiceName(final String serviceName) {
this.serviceName = serviceName;
}

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

protected void setStackTraceAsArray(final boolean stackTraceAsArray) {
this.stackTraceAsArray = stackTraceAsArray;
}

public void setEventDataset(String eventDataset) {
this.eventDataset = eventDataset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* 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
Expand Down Expand Up @@ -68,9 +68,9 @@ public void publish(LogRecord record) {
}

private final EcsFormatter formatter = new EcsFormatter();

private final Logger logger = Logger.getLogger("");

private final ByteArrayOutputStream out = new ByteArrayOutputStream();

private LogRecord record;
Expand Down Expand Up @@ -100,7 +100,7 @@ public void debug(String message, Object... logParams) {
public void error(String message, Throwable t) {
logger.log(Level.SEVERE, message, t);
}

@Override
public JsonNode getLastLogLine() throws IOException {
return objectMapper.readTree(out.toString());
Expand All @@ -109,15 +109,16 @@ public JsonNode getLastLogLine() throws IOException {
@BeforeEach
void setUp() {
clearHandlers();

formatter.setIncludeOrigin(true);
formatter.setServiceName("test");
formatter.setServiceVersion("1.0.0");
formatter.setEventDataset("testdataset.log");
formatter.setAdditionalFields("key1=value1,key2=value2");

Handler handler = new InMemoryStreamHandler(out, formatter);
handler.setLevel(Level.ALL);

logger.addHandler(handler);
logger.setLevel(Level.ALL);
}
Expand All @@ -130,7 +131,7 @@ void testLogOrigin() throws Exception {
//No file.line for JUL
}


private void clearHandlers() {
for (Handler handler : logger.getHandlers()) {
logger.removeHandler(handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* 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
Expand All @@ -40,6 +40,7 @@ public class EcsLayout extends Layout {

private boolean stackTraceAsArray = false;
private String serviceName;
private String serviceVersion;
private boolean includeOrigin;
private String eventDataset;
private List<AdditionalField> additionalFields = new ArrayList<AdditionalField>();
Expand All @@ -52,6 +53,7 @@ public String format(LoggingEvent event) {
EcsJsonSerializer.serializeFormattedMessage(builder, event.getRenderedMessage());
EcsJsonSerializer.serializeEcsVersion(builder);
EcsJsonSerializer.serializeServiceName(builder, serviceName);
EcsJsonSerializer.serializeServiceVersion(builder, serviceVersion);
EcsJsonSerializer.serializeEventDataset(builder, eventDataset);
EcsJsonSerializer.serializeThreadName(builder, event.getThreadName());
EcsJsonSerializer.serializeLoggerName(builder, event.categoryName);
Expand Down Expand Up @@ -99,6 +101,10 @@ public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

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

public void setIncludeOrigin(boolean includeOrigin) {
this.includeOrigin = includeOrigin;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
* 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
Expand Down Expand Up @@ -51,6 +51,7 @@ void setUp() {
logger.addAppender(appender);
ecsLayout = new EcsLayout();
ecsLayout.setServiceName("test");
ecsLayout.setServiceVersion("1.0.0");
ecsLayout.setIncludeOrigin(true);
ecsLayout.setEventDataset("testdataset.log");
ecsLayout.activateOptions();
Expand Down
3 changes: 2 additions & 1 deletion 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="1.0.0"/>
<param name="eventDataset" value="testdataset.log"/>
<param name="includeOrigin" value="true"/>
<param name="additionalField" value="key1=value1"/>
Expand All @@ -14,4 +15,4 @@
<priority value="DEBUG"/>
<appender-ref ref="List"/>
</root>
</log4j:configuration>
</log4j:configuration>
Loading