Skip to content

Commit 50104e9

Browse files
Compatibility test JDK 17 (#2184)
* enable jdk 17 + disable 15,16 * Adding utility to mock environment variables * update war plugin * update maven to 3.8.3 * update javadoc plugin version * log4j1 broken on Java 17, just disable tests * add log4j1 instrumentation for test * fix log4j1 to keep testing it * fix typo * fix log correlaction plugin * fix logging activation listener test * fix the url connection plugin test Co-authored-by: eyalkoren <[email protected]>
1 parent 15e6803 commit 50104e9

File tree

15 files changed

+217
-120
lines changed

15 files changed

+217
-120
lines changed

.mvn/wrapper/MavenWrapperDownloader.java

100755100644
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
11
/*
2-
Licensed to the Apache Software Foundation (ASF) under one
3-
or more contributor license agreements. See the NOTICE file
4-
distributed with this work for additional information
5-
regarding copyright ownership. The ASF licenses this file
6-
to you under the Apache License, Version 2.0 (the
7-
"License"); you may not use this file except in compliance
8-
with the License. You may obtain a copy of the License at
9-
10-
http://www.apache.org/licenses/LICENSE-2.0
11-
12-
Unless required by applicable law or agreed to in writing,
13-
software distributed under the License is distributed on an
14-
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
KIND, either express or implied. See the License for the
16-
specific language governing permissions and limitations
17-
under the License.
18-
*/
19-
2+
* Copyright 2007-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
2016
import java.net.*;
2117
import java.io.*;
2218
import java.nio.channels.*;
2319
import java.util.Properties;
2420

2521
public class MavenWrapperDownloader {
2622

23+
private static final String WRAPPER_VERSION = "0.5.6";
2724
/**
2825
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
2926
*/
30-
private static final String DEFAULT_DOWNLOAD_URL =
31-
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.0/maven-wrapper-0.4.0.jar";
27+
private static final String DEFAULT_DOWNLOAD_URL = "https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/"
28+
+ WRAPPER_VERSION + "/maven-wrapper-" + WRAPPER_VERSION + ".jar";
3229

3330
/**
3431
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
@@ -76,13 +73,13 @@ public static void main(String args[]) {
7673
}
7774
}
7875
}
79-
System.out.println("- Downloading from: : " + url);
76+
System.out.println("- Downloading from: " + url);
8077

8178
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
8279
if(!outputFile.getParentFile().exists()) {
8380
if(!outputFile.getParentFile().mkdirs()) {
8481
System.out.println(
85-
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
82+
"- ERROR creating output directory '" + outputFile.getParentFile().getAbsolutePath() + "'");
8683
}
8784
}
8885
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
@@ -98,6 +95,16 @@ public static void main(String args[]) {
9895
}
9996

10097
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
98+
if (System.getenv("MVNW_USERNAME") != null && System.getenv("MVNW_PASSWORD") != null) {
99+
String username = System.getenv("MVNW_USERNAME");
100+
char[] password = System.getenv("MVNW_PASSWORD").toCharArray();
101+
Authenticator.setDefault(new Authenticator() {
102+
@Override
103+
protected PasswordAuthentication getPasswordAuthentication() {
104+
return new PasswordAuthentication(username, password);
105+
}
106+
});
107+
}
101108
URL website = new URL(urlString);
102109
ReadableByteChannel rbc;
103110
rbc = Channels.newChannel(website.openStream());

.mvn/wrapper/maven-wrapper.jar

100755100644
2.32 KB
Binary file not shown.

.mvn/wrapper/maven-wrapper.properties

100755100644
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.3/apache-maven-3.8.3-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

Jenkinsfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ pipeline {
333333
axis {
334334
// the list of support java versions can be found in the infra repo (ansible/roles/java/defaults/main.yml)
335335
name 'JAVA_VERSION'
336-
values 'openjdk12', 'openjdk13', 'openjdk14', 'openjdk15', 'openjdk16'
336+
values 'openjdk12', 'openjdk13', 'openjdk14', 'openjdk17'
337+
337338
}
338339
}
339340
stages {

apm-agent-core/src/main/java/co/elastic/apm/agent/bci/bytebuddy/ErrorLoggingListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class ErrorLoggingListener extends AgentBuilder.Listener.Adapter {
3030
@Override
3131
public void onError(String typeName, ClassLoader classLoader, JavaModule module, boolean loaded, Throwable throwable) {
3232
if (throwable instanceof MinimumClassFileVersionValidator.UnsupportedClassFileVersionException) {
33-
logger.warn("{} uses an unsupported class file version (pre Java {}}) and can't be instrumented. " +
33+
logger.warn("{} uses an unsupported class file version (pre Java {})) and can't be instrumented. " +
3434
"Consider updating to a newer version of that library.",
3535
typeName,
3636
((MinimumClassFileVersionValidator.UnsupportedClassFileVersionException)throwable).getMinVersion());
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.apm.agent.testinstr;
20+
21+
import co.elastic.apm.agent.sdk.ElasticApmInstrumentation;
22+
import net.bytebuddy.asm.Advice;
23+
import net.bytebuddy.description.method.MethodDescription;
24+
import net.bytebuddy.description.type.TypeDescription;
25+
import net.bytebuddy.matcher.ElementMatcher;
26+
27+
import javax.annotation.Nullable;
28+
import java.util.Collection;
29+
import java.util.Collections;
30+
31+
import static net.bytebuddy.matcher.ElementMatchers.named;
32+
33+
/**
34+
* Instruments {@link org.apache.log4j.helpers.OptionConverter} to make {@link org.apache.log4j.helpers.Loader} work as
35+
* expected on Java 17+. As log4j1 is now EOL http://logging.apache.org/log4j/1.2/ it's the best way to keep our tests
36+
* active and relevant on this feature.
37+
*/
38+
public class OptionConverterInstrumentation extends ElasticApmInstrumentation {
39+
40+
@Override
41+
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
42+
return named("org.apache.log4j.helpers.OptionConverter");
43+
}
44+
45+
@Override
46+
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
47+
return named("getSystemProperty");
48+
}
49+
50+
@Override
51+
public Collection<String> getInstrumentationGroupNames() {
52+
return Collections.emptyList();
53+
}
54+
55+
public static class AdviceClass {
56+
57+
@Advice.AssignReturned.ToReturned
58+
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false)
59+
public static String onExit(@Advice.Argument(0) String key,
60+
@Advice.Return @Nullable String returnValue) {
61+
62+
if (returnValue == null || !"java.version".equals(key)) {
63+
return returnValue;
64+
}
65+
66+
if (returnValue.indexOf('.') < 0) {
67+
// just convert '17' to '17.0' to make Log4j simple version parsing work and not assume Java 1.x
68+
return returnValue + ".0";
69+
} else {
70+
return returnValue;
71+
}
72+
}
73+
}
74+
}

apm-agent-plugins/apm-log-correlation-plugin/src/test/java/co/elastic/apm/agent/mdc/MdcActivationListenerIT.java

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,15 @@
1818
*/
1919
package co.elastic.apm.agent.mdc;
2020

21-
import co.elastic.apm.agent.MockReporter;
22-
import co.elastic.apm.agent.bci.ElasticApmAgent;
23-
import co.elastic.apm.agent.configuration.SpyConfiguration;
24-
import co.elastic.apm.agent.errorlogging.Log4j2LoggerErrorCapturingInstrumentation;
25-
import co.elastic.apm.agent.errorlogging.Slf4jLoggerErrorCapturingInstrumentation;
26-
import co.elastic.apm.agent.impl.ElasticApmTracerBuilder;
27-
import co.elastic.apm.agent.impl.ElasticApmTracer;
21+
import co.elastic.apm.agent.AbstractInstrumentationTest;
2822
import co.elastic.apm.agent.impl.transaction.Transaction;
2923
import co.elastic.apm.agent.logging.LoggingConfiguration;
30-
import net.bytebuddy.agent.ByteBuddyAgent;
3124
import org.apache.logging.log4j.ThreadContext;
32-
import org.junit.jupiter.api.AfterAll;
33-
import org.junit.jupiter.api.Assumptions;
34-
import org.junit.jupiter.api.BeforeAll;
3525
import org.junit.jupiter.api.BeforeEach;
3626
import org.junit.jupiter.api.Test;
3727
import org.mockito.stubbing.Answer;
3828
import org.slf4j.Logger;
3929
import org.slf4j.MDC;
40-
import org.stagemonitor.configuration.ConfigurationRegistry;
41-
42-
import java.util.Arrays;
4330

4431
import static org.assertj.core.api.Assertions.assertThat;
4532
import static org.mockito.ArgumentMatchers.any;
@@ -48,31 +35,12 @@
4835
import static org.mockito.Mockito.mock;
4936
import static org.mockito.Mockito.when;
5037

51-
class MdcActivationListenerIT {
38+
class MdcActivationListenerIT extends AbstractInstrumentationTest {
5239

53-
protected static ElasticApmTracer tracer;
54-
protected static MockReporter reporter;
55-
protected static ConfigurationRegistry config;
5640
private LoggingConfiguration loggingConfiguration;
5741

58-
@BeforeAll
59-
static void beforeAll() {
60-
reporter = new MockReporter();
61-
config = SpyConfiguration.createSpyConfig();
62-
tracer = new ElasticApmTracerBuilder()
63-
.configurationRegistry(config)
64-
.reporter(reporter)
65-
.buildAndStart();
66-
ElasticApmAgent.initInstrumentation(tracer, ByteBuddyAgent.install(), Arrays.asList(new Slf4jLoggerErrorCapturingInstrumentation(), new Log4j2LoggerErrorCapturingInstrumentation()));
67-
}
68-
69-
@AfterAll
70-
static void afterAll() {
71-
ElasticApmAgent.reset();
72-
}
73-
7442
@BeforeEach
75-
void setUp() throws Exception {
43+
void setUp() {
7644
MDC.clear();
7745
org.apache.log4j.MDC.clear();
7846
ThreadContext.clearAll();
@@ -114,10 +82,6 @@ void testVerifyThatWithEnabledCorrelationAndLoggedErrorMdcErrorIdIsNotBlankWithS
11482

11583
@Test
11684
void testVerifyThatWithEnabledCorrelationAndLoggedErrorMdcErrorIdIsNotBlankWithLog4j() {
117-
Assumptions.assumeTrue(() -> {
118-
org.apache.log4j.MDC.put("test", true);
119-
return org.apache.log4j.MDC.get("test") == Boolean.TRUE;
120-
}, "Log4j MDC is not working, this happens with some versions of Java 10 where log4j thinks it's Java 1");
12185
when(loggingConfiguration.isLogCorrelationEnabled()).thenReturn(true);
12286
org.apache.logging.log4j.Logger logger = mock(org.apache.logging.log4j.Logger.class);
12387
doAnswer(invocation -> assertMdcErrorIdIsNotEmpty()).when(logger).error(anyString(), any(Exception.class));
@@ -135,10 +99,6 @@ void testVerifyThatWithEnabledCorrelationAndLoggedErrorMdcErrorIdIsNotBlankWithL
13599

136100
@Test
137101
void testVerifyThatWithEnabledCorrelationAndLoggedErrorMdcErrorIdIsNotBlankWithLog4jNotInTransaction() {
138-
Assumptions.assumeTrue(() -> {
139-
org.apache.log4j.MDC.put("test", true);
140-
return org.apache.log4j.MDC.get("test") == Boolean.TRUE;
141-
}, "Log4j MDC is not working, this happens with some versions of Java 10 where log4j thinks it's Java 1");
142102
when(loggingConfiguration.isLogCorrelationEnabled()).thenReturn(true);
143103
org.apache.logging.log4j.Logger logger = mock(org.apache.logging.log4j.Logger.class);
144104
doAnswer(invocation -> assertMdcErrorIdIsNotEmpty()).when(logger).error(anyString(), any(Exception.class));

apm-agent-plugins/apm-log-correlation-plugin/src/test/java/co/elastic/apm/agent/mdc/MdcActivationListenerTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class MdcActivationListenerTest extends AbstractInstrumentationTest {
5454
void setUp() {
5555
org.apache.log4j.MDC.put("test", true);
5656
log4jMdcWorking = (Boolean) org.apache.log4j.MDC.get("test");
57+
assertThat(log4jMdcWorking)
58+
.describedAs("MDC not working as expected")
59+
.isNotNull();
5760

5861
forAllMdc(MdcImpl::clear);
5962
loggingConfiguration = config.getConfig(LoggingConfiguration.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Allows to make Log4j 1.x work on Java 17 even if not supported
2+
co.elastic.apm.agent.testinstr.OptionConverterInstrumentation

apm-agent-plugins/apm-log-shader-plugin/apm-log-shader-plugin-common/src/test/java/co/elastic/apm/agent/log/shader/LogShadingInstrumentationTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,12 @@ private void verifyEcsFormat(String[] splitRawLogLine, JsonNode ecsLogLineTree,
344344
assertThat(ecsLogLineTree.get("event.dataset").textValue()).isEqualTo(serviceName + ".FILE");
345345
assertThat(ecsLogLineTree.get("service.version").textValue()).isEqualTo("v42");
346346
assertThat(ecsLogLineTree.get("some.field").textValue()).isEqualTo("some-value");
347+
JsonNode jsonTraceId = ecsLogLineTree.get("trace.id");
347348
if (traceId != null) {
348-
assertThat(ecsLogLineTree.get("trace.id").textValue()).isEqualTo(traceId);
349+
assertThat(jsonTraceId).isNotNull();
350+
assertThat(jsonTraceId.asText()).isEqualTo(traceId);
349351
} else {
350-
assertThat(ecsLogLineTree.get("trace.id")).isNull();
352+
assertThat(jsonTraceId).isNull();
351353
}
352354
}
353355

0 commit comments

Comments
 (0)