Skip to content

Commit 56f986c

Browse files
Set service.version in ECS-reformatting (#2603)
1 parent d81ce11 commit 56f986c

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

CHANGELOG.asciidoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ endif::[]
2323
[[release-notes-1.30.2]]
2424
==== 1.30.2 - YYYY/MM/DD
2525
26+
[float]
27+
===== Potentially breaking changes
28+
* Starting this version, when using <<config-log-ecs-reformatting>>, the agent will automatically set the `service.version` field.
29+
If you are using ECS-logging and set the `service.version` through a custom field, the behaviour is not strictly defined. Remove the
30+
custom `service.name` field setting and either allow the agent to automatically discover and set it, or use the
31+
<<config-service-version>> config option to set it manually.
32+
2633
[float]
2734
===== Refactorings
2835
* Vert.x 3.x instrumentation was refactored to remove constructor instrumentation as well as wrapping of response handler. In addition,
@@ -32,6 +39,7 @@ allows for spans representing asynchronous handling of requests for which the co
3239
3340
[float]
3441
===== Features
42+
* Set the service version when using the ECS reformatting of the application logs: {pull}2603[#2603]
3543
3644
[float]
3745
===== Bug fixes

apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ protected String getAppenderName(WriterAppender appender) {
5656
}
5757

5858
@Override
59-
protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName,
60-
@Nullable Map<String, String> additionalFields, Layout originalFormatter) {
59+
protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion,
60+
@Nullable String serviceNodeName, @Nullable Map<String, String> additionalFields,
61+
Layout originalFormatter) {
6162
EcsLayout ecsLayout = new EcsLayout();
6263
ecsLayout.setServiceName(serviceName);
64+
ecsLayout.setServiceVersion(serviceVersion);
6365
ecsLayout.setServiceNodeName(serviceNodeName);
6466
ecsLayout.setEventDataset(eventDataset);
6567
if (additionalFields != null) {

apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ protected String getAppenderName(Appender appender) {
6262
}
6363

6464
@Override
65-
protected Layout<? extends Serializable> createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName,
66-
@Nullable Map<String, String> additionalFields, Layout<? extends Serializable> originalFormatter) {
65+
protected Layout<? extends Serializable> createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion,
66+
@Nullable String serviceNodeName, @Nullable Map<String, String> additionalFields,
67+
Layout<? extends Serializable> originalFormatter) {
6768
EcsLayout.Builder builder = EcsLayout.newBuilder()
6869
.setServiceName(serviceName)
70+
.setServiceVersion(serviceVersion)
6971
.setServiceNodeName(serviceNodeName)
7072
.setEventDataset(eventDataset)
7173
.setIncludeMarkers(true)

apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ protected String getAppenderName(OutputStreamAppender<ILoggingEvent> appender) {
7676
}
7777

7878
@Override
79-
protected Encoder<ILoggingEvent> createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName,
80-
@Nullable Map<String, String> additionalFields, Encoder<ILoggingEvent> originalFormatter) {
79+
protected Encoder<ILoggingEvent> createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion,
80+
@Nullable String serviceNodeName,
81+
@Nullable Map<String, String> additionalFields,
82+
Encoder<ILoggingEvent> originalFormatter) {
8183
EcsEncoder ecsEncoder = new EcsEncoder();
8284
ecsEncoder.setServiceName(serviceName);
85+
ecsEncoder.setServiceVersion(serviceVersion);
8386
ecsEncoder.setServiceNodeName(serviceNodeName);
8487
ecsEncoder.setEventDataset(eventDataset);
8588
ecsEncoder.setIncludeMarkers(true);

apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public abstract class AbstractEcsReformattingHelper<A, F> {
149149
private final LoggingConfiguration loggingConfiguration;
150150

151151
@Nullable
152-
private final String configuredServiceName;
152+
private final String globalServiceName;
153+
154+
@Nullable
155+
private final String globalServiceVersion;
153156

154157
@Nullable
155158
private final String configuredServiceNodeName;
@@ -166,7 +169,8 @@ public AbstractEcsReformattingHelper() {
166169
"",
167170
tracer.getConfig(ServerlessConfiguration.class)
168171
);
169-
configuredServiceName = service.getName();
172+
globalServiceName = service.getName();
173+
globalServiceVersion = service.getVersion();
170174
if (service.getNode() != null) {
171175
configuredServiceNodeName = service.getNode().getName();
172176
} else {
@@ -230,8 +234,8 @@ private void startOverriding(A originalAppender) {
230234
mappedFormatter = originalFormatter;
231235
String serviceName = getServiceName();
232236
F createdEcsFormatter = createEcsFormatter(
233-
getEventDataset(originalAppender, serviceName), serviceName, configuredServiceNodeName,
234-
additionalFields, originalFormatter
237+
getEventDataset(originalAppender, serviceName), serviceName, getServiceVersion(),
238+
configuredServiceNodeName, additionalFields, originalFormatter
235239
);
236240
setFormatter(originalAppender, createdEcsFormatter);
237241
ecsFormatter = createdEcsFormatter;
@@ -313,8 +317,8 @@ private Object createAndMapShadeAppenderFor(final A originalAppender) {
313317
if (shouldApplyEcsReformatting(originalAppender)) {
314318
String serviceName = getServiceName();
315319
F ecsFormatter = createEcsFormatter(
316-
getEventDataset(originalAppender, serviceName), serviceName, configuredServiceNodeName,
317-
additionalFields, getFormatterFrom(originalAppender)
320+
getEventDataset(originalAppender, serviceName), serviceName, getServiceVersion(),
321+
configuredServiceNodeName, additionalFields, getFormatterFrom(originalAppender)
318322
);
319323
ecsAppender = createAndStartEcsAppender(originalAppender, ECS_SHADE_APPENDER_NAME, ecsFormatter);
320324
if (ecsAppender == null) {
@@ -422,21 +426,34 @@ private boolean isEcsFormatter(F formatter) {
422426
@Nullable
423427
protected abstract A createAndStartEcsAppender(A originalAppender, String ecsAppenderName, F ecsFormatter);
424428

425-
protected abstract F createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName,
426-
@Nullable Map<String, String> additionalFields, F originalFormatter);
429+
protected abstract F createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion,
430+
@Nullable String serviceNodeName, @Nullable Map<String, String> additionalFields,
431+
F originalFormatter);
427432

428433
/**
429434
* We currently get the same service name that is reported in the metadata document.
430-
* This may mismatch automatically-discovered service names (if not configured). However, we only set it
431-
* once when configuring our appender, so we can have only one service name. In addition, if we use the
432-
* in-context service name (eg through MDC), all log events that will not occur within a traced transaction
433-
* will get a the global service name.
435+
* This would mismatch automatically-discovered service names (if not configured) when relying on multi-service auto-discovery.
436+
* However, we only set it once when configuring our appender, so we can have only one service name. In addition, if we use the
437+
* in-context service name (eg through MDC), all log events that will not occur within a traced transaction will get the global
438+
* service name.
434439
*
435440
* @return the configured service name or the globally-automatically-discovered one (not one that is context-dependent)
436441
*/
437442
@Nullable
438443
private String getServiceName() {
439-
return configuredServiceName;
444+
return globalServiceName;
445+
}
446+
447+
/**
448+
* We currently get the same service version that is reported in the metadata document.
449+
* This would mismatch automatically-discovered service version (if not configured) when relying on multi-service auto-discovery.
450+
* However, we only set it once when configuring our appender, so we can have only one service version.
451+
*
452+
* @return the configured service version or the globally-automatically-discovered one (not one that is context-dependent)
453+
*/
454+
@Nullable
455+
private String getServiceVersion() {
456+
return globalServiceVersion;
440457
}
441458

442459
/**

apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ public abstract class LoggingInstrumentationTest extends AbstractInstrumentation
6363
public static final String WARN_MESSAGE = "Warn-this";
6464
public static final String ERROR_MESSAGE = "Error-this";
6565

66+
private static final String SERVICE_VERSION = "v42";
67+
6668
private static final String SERVICE_NODE_NAME = "my-service-node";
67-
private static final Map<String, String> ADDITIONAL_FIELDS = Map.of("service.version", "v42", "some.field", "some-value");
69+
private static final Map<String, String> ADDITIONAL_FIELDS = Map.of("some.field", "some-value", "another.field", "another-value");
6870

6971
private final LoggerFacade logger;
7072
private final ObjectMapper objectMapper;
@@ -86,6 +88,7 @@ public LoggingInstrumentationTest() {
8688

8789
@BeforeEach
8890
public void setup() throws Exception {
91+
doReturn(SERVICE_VERSION).when(config.getConfig(CoreConfiguration.class)).getServiceVersion();
8992
doReturn(SERVICE_NODE_NAME).when(config.getConfig(CoreConfiguration.class)).getServiceNodeName();
9093

9194
loggingConfig = config.getConfig(LoggingConfiguration.class);
@@ -303,6 +306,7 @@ private void verifyEcsLogLine(JsonNode ecsLogLineTree) {
303306
assertThat(ecsLogLineTree.get("event.dataset").textValue()).isEqualTo(serviceName + ".FILE");
304307
assertThat(ecsLogLineTree.get("service.version").textValue()).isEqualTo("v42");
305308
assertThat(ecsLogLineTree.get("some.field").textValue()).isEqualTo("some-value");
309+
assertThat(ecsLogLineTree.get("another.field").textValue()).isEqualTo("another-value");
306310
assertThat(ecsLogLineTree.get(AbstractLogCorrelationHelper.TRACE_ID_MDC_KEY).textValue()).isEqualTo(transaction.getTraceContext().getTraceId().toString());
307311
assertThat(ecsLogLineTree.get(AbstractLogCorrelationHelper.TRANSACTION_ID_MDC_KEY).textValue()).isEqualTo(transaction.getTraceContext().getTransactionId().toString());
308312
verifyErrorCaptureAndCorrelation(isErrorLine, ecsLogLineTree);

apm-agent-plugins/apm-logging-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<properties>
1616
<apm-agent-parent.base.dir>${project.basedir}/../..</apm-agent-parent.base.dir>
17-
<version.ecs.logging>1.3.2</version.ecs.logging>
17+
<version.ecs.logging>1.4.0</version.ecs.logging>
1818
</properties>
1919

2020
<modules>

0 commit comments

Comments
 (0)