diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index afd533e021..91f6c3c008 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -23,6 +23,13 @@ endif::[] [[release-notes-1.30.2]] ==== 1.30.2 - YYYY/MM/DD +[float] +===== Potentially breaking changes +* Starting this version, when using <>, the agent will automatically set the `service.version` field. +If you are using ECS-logging and set the `service.version` through a custom field, the behaviour is not strictly defined. Remove the +custom `service.name` field setting and either allow the agent to automatically discover and set it, or use the +<> config option to set it manually. + [float] ===== Refactorings * 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 [float] ===== Features +* Set the service version when using the ECS reformatting of the application logs: {pull}2603[#2603] [float] ===== Bug fixes diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java index b83f819bbb..3ac8effb20 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java @@ -56,10 +56,12 @@ protected String getAppenderName(WriterAppender appender) { } @Override - protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName, - @Nullable Map additionalFields, Layout originalFormatter) { + protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, + @Nullable String serviceNodeName, @Nullable Map additionalFields, + Layout originalFormatter) { EcsLayout ecsLayout = new EcsLayout(); ecsLayout.setServiceName(serviceName); + ecsLayout.setServiceVersion(serviceVersion); ecsLayout.setServiceNodeName(serviceNodeName); ecsLayout.setEventDataset(eventDataset); if (additionalFields != null) { diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java index eebd270e0f..1f76c8ff38 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java @@ -62,10 +62,12 @@ protected String getAppenderName(Appender appender) { } @Override - protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName, - @Nullable Map additionalFields, Layout originalFormatter) { + protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, + @Nullable String serviceNodeName, @Nullable Map additionalFields, + Layout originalFormatter) { EcsLayout.Builder builder = EcsLayout.newBuilder() .setServiceName(serviceName) + .setServiceVersion(serviceVersion) .setServiceNodeName(serviceNodeName) .setEventDataset(eventDataset) .setIncludeMarkers(true) diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java index 5ca5271dec..1ae954b856 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java @@ -76,10 +76,13 @@ protected String getAppenderName(OutputStreamAppender appender) { } @Override - protected Encoder createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName, - @Nullable Map additionalFields, Encoder originalFormatter) { + protected Encoder createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, + @Nullable String serviceNodeName, + @Nullable Map additionalFields, + Encoder originalFormatter) { EcsEncoder ecsEncoder = new EcsEncoder(); ecsEncoder.setServiceName(serviceName); + ecsEncoder.setServiceVersion(serviceVersion); ecsEncoder.setServiceNodeName(serviceNodeName); ecsEncoder.setEventDataset(eventDataset); ecsEncoder.setIncludeMarkers(true); diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java index 1c9b11d79e..11fa6e4665 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java @@ -149,7 +149,10 @@ public abstract class AbstractEcsReformattingHelper { private final LoggingConfiguration loggingConfiguration; @Nullable - private final String configuredServiceName; + private final String globalServiceName; + + @Nullable + private final String globalServiceVersion; @Nullable private final String configuredServiceNodeName; @@ -166,7 +169,8 @@ public AbstractEcsReformattingHelper() { "", tracer.getConfig(ServerlessConfiguration.class) ); - configuredServiceName = service.getName(); + globalServiceName = service.getName(); + globalServiceVersion = service.getVersion(); if (service.getNode() != null) { configuredServiceNodeName = service.getNode().getName(); } else { @@ -230,8 +234,8 @@ private void startOverriding(A originalAppender) { mappedFormatter = originalFormatter; String serviceName = getServiceName(); F createdEcsFormatter = createEcsFormatter( - getEventDataset(originalAppender, serviceName), serviceName, configuredServiceNodeName, - additionalFields, originalFormatter + getEventDataset(originalAppender, serviceName), serviceName, getServiceVersion(), + configuredServiceNodeName, additionalFields, originalFormatter ); setFormatter(originalAppender, createdEcsFormatter); ecsFormatter = createdEcsFormatter; @@ -313,8 +317,8 @@ private Object createAndMapShadeAppenderFor(final A originalAppender) { if (shouldApplyEcsReformatting(originalAppender)) { String serviceName = getServiceName(); F ecsFormatter = createEcsFormatter( - getEventDataset(originalAppender, serviceName), serviceName, configuredServiceNodeName, - additionalFields, getFormatterFrom(originalAppender) + getEventDataset(originalAppender, serviceName), serviceName, getServiceVersion(), + configuredServiceNodeName, additionalFields, getFormatterFrom(originalAppender) ); ecsAppender = createAndStartEcsAppender(originalAppender, ECS_SHADE_APPENDER_NAME, ecsFormatter); if (ecsAppender == null) { @@ -422,21 +426,34 @@ private boolean isEcsFormatter(F formatter) { @Nullable protected abstract A createAndStartEcsAppender(A originalAppender, String ecsAppenderName, F ecsFormatter); - protected abstract F createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceNodeName, - @Nullable Map additionalFields, F originalFormatter); + protected abstract F createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, + @Nullable String serviceNodeName, @Nullable Map additionalFields, + F originalFormatter); /** * We currently get the same service name that is reported in the metadata document. - * This may mismatch automatically-discovered service names (if not configured). However, we only set it - * once when configuring our appender, so we can have only one service name. In addition, if we use the - * in-context service name (eg through MDC), all log events that will not occur within a traced transaction - * will get a the global service name. + * This would mismatch automatically-discovered service names (if not configured) when relying on multi-service auto-discovery. + * However, we only set it once when configuring our appender, so we can have only one service name. In addition, if we use the + * in-context service name (eg through MDC), all log events that will not occur within a traced transaction will get the global + * service name. * * @return the configured service name or the globally-automatically-discovered one (not one that is context-dependent) */ @Nullable private String getServiceName() { - return configuredServiceName; + return globalServiceName; + } + + /** + * We currently get the same service version that is reported in the metadata document. + * This would mismatch automatically-discovered service version (if not configured) when relying on multi-service auto-discovery. + * However, we only set it once when configuring our appender, so we can have only one service version. + * + * @return the configured service version or the globally-automatically-discovered one (not one that is context-dependent) + */ + @Nullable + private String getServiceVersion() { + return globalServiceVersion; } /** diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java index 9b7456738d..05f96f45fa 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java @@ -63,8 +63,10 @@ public abstract class LoggingInstrumentationTest extends AbstractInstrumentation public static final String WARN_MESSAGE = "Warn-this"; public static final String ERROR_MESSAGE = "Error-this"; + private static final String SERVICE_VERSION = "v42"; + private static final String SERVICE_NODE_NAME = "my-service-node"; - private static final Map ADDITIONAL_FIELDS = Map.of("service.version", "v42", "some.field", "some-value"); + private static final Map ADDITIONAL_FIELDS = Map.of("some.field", "some-value", "another.field", "another-value"); private final LoggerFacade logger; private final ObjectMapper objectMapper; @@ -86,6 +88,7 @@ public LoggingInstrumentationTest() { @BeforeEach public void setup() throws Exception { + doReturn(SERVICE_VERSION).when(config.getConfig(CoreConfiguration.class)).getServiceVersion(); doReturn(SERVICE_NODE_NAME).when(config.getConfig(CoreConfiguration.class)).getServiceNodeName(); loggingConfig = config.getConfig(LoggingConfiguration.class); @@ -303,6 +306,7 @@ private void verifyEcsLogLine(JsonNode ecsLogLineTree) { assertThat(ecsLogLineTree.get("event.dataset").textValue()).isEqualTo(serviceName + ".FILE"); assertThat(ecsLogLineTree.get("service.version").textValue()).isEqualTo("v42"); assertThat(ecsLogLineTree.get("some.field").textValue()).isEqualTo("some-value"); + assertThat(ecsLogLineTree.get("another.field").textValue()).isEqualTo("another-value"); assertThat(ecsLogLineTree.get(AbstractLogCorrelationHelper.TRACE_ID_MDC_KEY).textValue()).isEqualTo(transaction.getTraceContext().getTraceId().toString()); assertThat(ecsLogLineTree.get(AbstractLogCorrelationHelper.TRANSACTION_ID_MDC_KEY).textValue()).isEqualTo(transaction.getTraceContext().getTransactionId().toString()); verifyErrorCaptureAndCorrelation(isErrorLine, ecsLogLineTree); diff --git a/apm-agent-plugins/apm-logging-plugin/pom.xml b/apm-agent-plugins/apm-logging-plugin/pom.xml index 6396eeb271..26e81ecbeb 100644 --- a/apm-agent-plugins/apm-logging-plugin/pom.xml +++ b/apm-agent-plugins/apm-logging-plugin/pom.xml @@ -14,7 +14,7 @@ ${project.basedir}/../.. - 1.3.2 + 1.4.0