Skip to content

Commit ccaacf5

Browse files
authored
Merge pull request #156 from DataDog/jb/merge_upstream
Merging changes from the upstream repo
2 parents 9d9160a + 1f4b69f commit ccaacf5

File tree

286 files changed

+13472
-2897
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

286 files changed

+13472
-2897
lines changed

.circleci/config.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ jobs:
129129
- image: memcached
130130
# This is used by rabbitmq instrumentation tests
131131
- image: rabbitmq
132+
# This is used by aerospike instrumentation tests
133+
- image: aerospike
132134

133135
parameters:
134136
testTask:
@@ -239,10 +241,13 @@ jobs:
239241
command: >-
240242
SKIP_BUILDSCAN="true"
241243
GRADLE_OPTS="-Dorg.gradle.jvmargs='-Xmx4G -Xms64M' -Ddatadog.forkedMaxHeapSize=4G -Ddatadog.forkedMinHeapSize=64M"
242-
./gradlew `circleci tests split workspace/build/muzzleTasks | xargs`
244+
./gradlew `circleci tests split --split-by=timings workspace/build/muzzleTasks | xargs`
243245
<< pipeline.parameters.gradle_flags >>
244246
--max-workers=16
245247
248+
- store_test_results:
249+
path: workspace/build/muzzle-test-results
250+
246251
workflows:
247252
build_test_deploy:
248253
jobs:

buildSrc/src/main/groovy/MuzzlePlugin.groovy

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,34 +109,60 @@ class MuzzlePlugin implements Plugin<Project> {
109109
return
110110
}
111111

112+
// We only get here if we are running muzzle, so let's start timing things
113+
long startTime = System.currentTimeMillis()
114+
112115
final RepositorySystem system = newRepositorySystem()
113116
final RepositorySystemSession session = newRepositorySystemSession(system)
114117

115118
project.afterEvaluate {
116119
// use runAfter to set up task finalizers in version order
117120
Task runAfter = project.tasks.muzzle
121+
// runLast is the last task to finish, so we can time the execution
122+
Task runLast = runAfter
118123

119124
for (MuzzleDirective muzzleDirective : project.muzzle.directives) {
120125
project.getLogger().info("configured $muzzleDirective")
121126

122127
if (muzzleDirective.coreJdk) {
123-
runAfter = addMuzzleTask(muzzleDirective, null, project, runAfter, bootstrapProject, toolingProject)
128+
runLast = runAfter = addMuzzleTask(muzzleDirective, null, project, runAfter, bootstrapProject, toolingProject)
124129
} else {
125-
muzzleDirectiveToArtifacts(muzzleDirective, system, session).collect() { Artifact singleVersion ->
130+
runLast = muzzleDirectiveToArtifacts(muzzleDirective, system, session).inject(runLast) { last, Artifact singleVersion ->
126131
runAfter = addMuzzleTask(muzzleDirective, singleVersion, project, runAfter, bootstrapProject, toolingProject)
127132
}
128133
if (muzzleDirective.assertInverse) {
129-
inverseOf(muzzleDirective, system, session).collect() { MuzzleDirective inverseDirective ->
130-
muzzleDirectiveToArtifacts(inverseDirective, system, session).collect() { Artifact singleVersion ->
134+
runLast = inverseOf(muzzleDirective, system, session).inject(runLast) { last1, MuzzleDirective inverseDirective ->
135+
muzzleDirectiveToArtifacts(inverseDirective, system, session).inject(last1) { last2, Artifact singleVersion ->
131136
runAfter = addMuzzleTask(inverseDirective, singleVersion, project, runAfter, bootstrapProject, toolingProject)
132137
}
133138
}
134139
}
135140
}
136141
}
142+
def timingTask = project.task("muzzle-end") {
143+
doLast {
144+
long endTime = System.currentTimeMillis()
145+
generateResultsXML(project, endTime - startTime)
146+
}
147+
}
148+
runLast.finalizedBy(timingTask)
137149
}
138150
}
139151

152+
private static void generateResultsXML(Project project, long millis) {
153+
def seconds = (millis * 1.0) / 1000
154+
def name = "${project.path}:muzzle"
155+
def dirname = name.replaceFirst('^:', '').replace(':', '_')
156+
def dir = project.file("${project.rootProject.buildDir}/muzzle-test-results/$dirname")
157+
dir.mkdirs()
158+
def file = project.file("$dir/results.xml")
159+
file.text = """|<?xml version="1.0" encoding="UTF-8"?>
160+
|<testsuite name="${name}" tests="1" id="0" time="${seconds}">
161+
| <testcase name="${name}" time="${seconds}">
162+
| </testcase>
163+
|</testsuite>\n""".stripMargin()
164+
}
165+
140166
private static ClassLoader getOrCreateToolingLoader(Project toolingProject) {
141167
synchronized (TOOLING_LOADER) {
142168
final ClassLoader toolingLoader = TOOLING_LOADER.get()
@@ -216,14 +242,12 @@ class MuzzlePlugin implements Plugin<Project> {
216242
rangeRequest.setRepositories(MUZZLE_REPOS)
217243
rangeRequest.setArtifact(directiveArtifact)
218244
final VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest)
219-
final Set<Version> versions = rangeResult.versions.toSet()
220-
221-
limitLargeRanges(rangeResult, versions, muzzleDirective.skipVersions)
245+
final Set<Version> versions = filterAndLimitVersions(rangeResult, muzzleDirective.skipVersions)
222246

223247
// println "Range Request: " + rangeRequest
224248
// println "Range Result: " + rangeResult
225249

226-
final Set<Artifact> allVersionArtifacts = filterVersion(versions, muzzleDirective.skipVersions).collect { version ->
250+
final Set<Artifact> allVersionArtifacts = versions.collect { version ->
227251
new DefaultArtifact(muzzleDirective.group, muzzleDirective.module, "jar", version.toString())
228252
}.toSet()
229253

@@ -238,50 +262,51 @@ class MuzzlePlugin implements Plugin<Project> {
238262
* Create a list of muzzle directives which assert the opposite of the given MuzzleDirective.
239263
*/
240264
private static Set<MuzzleDirective> inverseOf(MuzzleDirective muzzleDirective, RepositorySystem system, RepositorySystemSession session) {
241-
Set<MuzzleDirective> inverseDirectives = new HashSet<>()
242-
243-
final Artifact allVerisonsArtifact = new DefaultArtifact(muzzleDirective.group, muzzleDirective.module, "jar", "[,)")
265+
final Artifact allVersionsArtifact = new DefaultArtifact(muzzleDirective.group, muzzleDirective.module, "jar", "[,)")
244266
final Artifact directiveArtifact = new DefaultArtifact(muzzleDirective.group, muzzleDirective.module, "jar", muzzleDirective.versions)
245267

246-
247268
final VersionRangeRequest allRangeRequest = new VersionRangeRequest()
248269
allRangeRequest.setRepositories(MUZZLE_REPOS)
249-
allRangeRequest.setArtifact(allVerisonsArtifact)
270+
allRangeRequest.setArtifact(allVersionsArtifact)
250271
final VersionRangeResult allRangeResult = system.resolveVersionRange(session, allRangeRequest)
251272

252273
final VersionRangeRequest rangeRequest = new VersionRangeRequest()
253274
rangeRequest.setRepositories(MUZZLE_REPOS)
254275
rangeRequest.setArtifact(directiveArtifact)
255276
final VersionRangeResult rangeResult = system.resolveVersionRange(session, rangeRequest)
256277
final Set<Version> versions = rangeResult.versions.toSet()
278+
allRangeResult.versions.removeAll(versions)
279+
280+
return filterAndLimitVersions(allRangeResult, muzzleDirective.skipVersions).collect { version ->
281+
final MuzzleDirective inverseDirective = new MuzzleDirective()
282+
inverseDirective.group = muzzleDirective.group
283+
inverseDirective.module = muzzleDirective.module
284+
inverseDirective.versions = "$version"
285+
inverseDirective.assertPass = !muzzleDirective.assertPass
286+
inverseDirective
287+
}.toSet()
288+
}
257289

258-
filterVersion(allRangeResult.versions.toSet(), muzzleDirective.skipVersions).collect { version ->
259-
if (!versions.contains(version)) {
260-
final MuzzleDirective inverseDirective = new MuzzleDirective()
261-
inverseDirective.group = muzzleDirective.group
262-
inverseDirective.module = muzzleDirective.module
263-
inverseDirective.versions = "$version"
264-
inverseDirective.assertPass = !muzzleDirective.assertPass
265-
inverseDirectives.add(inverseDirective)
266-
}
267-
}
268-
269-
return inverseDirectives
290+
private static Set<Version> filterAndLimitVersions(VersionRangeResult result, Set<String> skipVersions) {
291+
return limitLargeRanges(result, filterVersion(result.versions.toSet(), skipVersions), skipVersions)
270292
}
271293

272-
private static void limitLargeRanges(VersionRangeResult result, Set<Version> versions, Set<String> skipVersions) {
273-
List<Version> copy = new ArrayList<>(versions)
274-
Collections.shuffle(copy)
275-
copy.removeAll(skipVersions)
276-
while (RANGE_COUNT_LIMIT <= copy.size()) {
277-
Version version = copy.pop()
294+
private static Set<Version> limitLargeRanges(VersionRangeResult result, Set<Version> versions, Set<String> skipVersions) {
295+
List<Version> versionsCopy = new ArrayList<>(versions)
296+
Set<String> skipCopy = new HashSet<>(skipVersions)
297+
versionsCopy.removeAll(skipVersions)
298+
Collections.shuffle(versionsCopy)
299+
while (RANGE_COUNT_LIMIT <= versionsCopy.size()) {
300+
Version version = versionsCopy.pop()
278301
if (!(version.equals(result.lowestVersion) || version.equals(result.highestVersion))) {
279-
skipVersions.add(version.toString())
302+
skipCopy.add(version.toString())
280303
}
281304
}
282-
if (skipVersions.size() > 0) {
283-
println "Muzzle skipping " + skipVersions.size() + " versions"
305+
if (skipCopy.size() > 0) {
306+
println "Muzzle skipping " + skipCopy.size() + " versions"
284307
}
308+
309+
return versionsCopy.toSet()
285310
}
286311

287312
/**
@@ -380,6 +405,7 @@ class MuzzlePlugin implements Plugin<Project> {
380405
}
381406

382407
private static final Pattern GIT_SHA_PATTERN = Pattern.compile('^.*-[0-9a-f]{7,}$')
408+
private static final Pattern END_NMN_PATTERN = Pattern.compile('^.*\\.[0-9]+[mM][0-9]+$')
383409

384410
/**
385411
* Filter out snapshot-type builds from versions list.
@@ -398,7 +424,9 @@ class MuzzlePlugin implements Plugin<Project> {
398424
version.contains("-ea") ||
399425
version.contains("-atlassian-") ||
400426
version.contains("public_draft") ||
427+
version.contains("-cr") ||
401428
skipVersions.contains(version) ||
429+
version.matches(END_NMN_PATTERN) ||
402430
version.matches(GIT_SHA_PATTERN)
403431
}
404432
return list

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/Agent.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.bootstrap;
22

3+
import static datadog.trace.api.Platform.isJavaVersionAtLeast;
34
import static datadog.trace.util.AgentThreadFactory.AgentThread.JMX_STARTUP;
45
import static datadog.trace.util.AgentThreadFactory.AgentThread.PROFILER_STARTUP;
56
import static datadog.trace.util.AgentThreadFactory.AgentThread.TRACE_STARTUP;
@@ -111,7 +112,7 @@ public static void start(final Instrumentation inst, final URL bootstrapURL) {
111112
* events which in turn loads LogManager. This is not a problem on newer JDKs because there JFR uses different
112113
* logging facility.
113114
*/
114-
if (isJavaBefore9() && appUsingCustomLogManager) {
115+
if (!isJavaVersionAtLeast(9) && appUsingCustomLogManager) {
115116
log.debug("Custom logger detected. Delaying Profiling Agent startup.");
116117
registerLogManagerCallback(new StartProfilingAgentCallback(inst, bootstrapURL));
117118
} else {
@@ -237,7 +238,7 @@ private static synchronized void createParentClassloader(final URL bootstrapURL)
237238
BOOTSTRAP_PROXY = (ClassLoader) constructor.newInstance(bootstrapURL);
238239

239240
final ClassLoader grandParent;
240-
if (isJavaBefore9()) {
241+
if (!isJavaVersionAtLeast(9)) {
241242
grandParent = null; // bootstrap
242243
} else {
243244
// platform classloader is parent of system in java 9+
@@ -298,11 +299,6 @@ private static synchronized void startJmx(final URL bootstrapURL) {
298299
}
299300
initializeJmxSystemAccessProvider(AGENT_CLASSLOADER);
300301

301-
if (PROFILING_CLASSLOADER == null) {
302-
throw new IllegalStateException("Datadog profiling agent should have been started already");
303-
}
304-
initializeJmxSystemAccessProvider(PROFILING_CLASSLOADER);
305-
306302
registerDeadlockDetectionEvent(bootstrapURL);
307303
}
308304

@@ -606,12 +602,8 @@ private static boolean isAppUsingCustomJMXBuilder() {
606602
return false;
607603
}
608604

609-
private static boolean isJavaBefore9() {
610-
return System.getProperty("java.version").startsWith("1.");
611-
}
612-
613605
private static boolean isJavaBefore9WithJFR() {
614-
if (!isJavaBefore9()) {
606+
if (isJavaVersionAtLeast(9)) {
615607
return false;
616608
}
617609
// FIXME: this is quite a hack because there maybe jfr classes on classpath somehow that have

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/InternalJarURLHandler.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ public class InternalJarURLHandler extends URLStreamHandler {
2828
private final Map<String, Lock> packages = new HashMap<>();
2929
private final JarFile bootstrapJarFile;
3030

31-
private static final ThreadLocal<StringBuilder> JAR_ENTRY_QUERY =
32-
new ThreadLocal<StringBuilder>() {
33-
@Override
34-
protected StringBuilder initialValue() {
35-
return new StringBuilder(128);
36-
}
37-
};
38-
3931
private WeakReference<Pair<String, JarEntry>> cache = NULL;
4032

4133
InternalJarURLHandler(final String internalJarFileName, final URL bootstrapJarLocation) {
@@ -94,13 +86,7 @@ protected URLConnection openConnection(final URL url) throws IOException {
9486
// and the key will be a new object each time.
9587
Pair<String, JarEntry> pair = cache.get();
9688
if (null == pair || !filename.equals(pair.getLeft())) {
97-
StringBuilder sb = JAR_ENTRY_QUERY.get();
98-
sb.append(this.name).append(filename);
99-
if (filename.endsWith(".class")) {
100-
sb.append("data");
101-
}
102-
String classFileName = sb.toString();
103-
sb.setLength(0);
89+
String classFileName = this.name + filename + (filename.endsWith(".class") ? "data" : "");
10490
JarEntry entry = bootstrapJarFile.getJarEntry(classFileName);
10591
if (null != entry) {
10692
pair = Pair.of(filename, entry);
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
package datadog.trace.bootstrap;
22

3-
import java.util.concurrent.Callable;
3+
import datadog.trace.api.Function;
44

55
public interface WeakCache<K, V> {
6-
interface Provider<K, V> {
7-
WeakCache<K, V> newWeakCache();
8-
9-
WeakCache<K, V> newWeakCache(final long maxSize);
6+
interface Provider {
7+
<K, V> WeakCache<K, V> newWeakCache(long maxSize);
108
}
119

1210
V getIfPresent(final K key);
1311

14-
V getIfPresentOrCompute(final K key, final Callable<? extends V> loader);
15-
16-
V get(final K key, final Callable<? extends V> loader);
12+
V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction);
1713

1814
void put(final K key, final V value);
1915
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/WeakMap.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package datadog.trace.bootstrap;
22

3+
import datadog.trace.api.Function;
34
import java.util.Collections;
45
import java.util.Map;
56
import java.util.WeakHashMap;
@@ -18,7 +19,7 @@ public interface WeakMap<K, V> {
1819

1920
void putIfAbsent(K key, V value);
2021

21-
V computeIfAbsent(K key, ValueSupplier<? super K, ? extends V> supplier);
22+
V computeIfAbsent(K key, Function<? super K, ? extends V> supplier);
2223

2324
@Slf4j
2425
class Provider {
@@ -58,14 +59,6 @@ public <K, V> WeakMap<K, V> get() {
5859
}
5960
}
6061

61-
/**
62-
* Supplies the value to be stored and it is called only when a value does not exists yet in the
63-
* registry.
64-
*/
65-
interface ValueSupplier<K, V> {
66-
V get(K key);
67-
}
68-
6962
class MapAdapter<K, V> implements WeakMap<K, V> {
7063
private final Object[] locks = new Object[16];
7164
private final Map<K, V> map;
@@ -111,14 +104,14 @@ public void putIfAbsent(final K key, final V value) {
111104
}
112105

113106
@Override
114-
public V computeIfAbsent(final K key, final ValueSupplier<? super K, ? extends V> supplier) {
107+
public V computeIfAbsent(final K key, final Function<? super K, ? extends V> supplier) {
115108
// We can't use computeIfAbsent since it was added in 1.8.
116109
V value = map.get(key);
117110
if (null == value) {
118111
synchronized (locks[key.hashCode() & (locks.length - 1)]) {
119112
value = map.get(key);
120113
if (null == value) {
121-
value = supplier.get(key);
114+
value = supplier.apply(key);
122115
map.put(key, value);
123116
}
124117
}

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/BaseDecorator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public String apply(Class<?> clazz) {
3737

3838
protected final boolean endToEndDurationsEnabled;
3939
protected final boolean traceAnalyticsEnabled;
40-
protected final float traceAnalyticsSampleRate;
40+
protected final Double traceAnalyticsSampleRate;
4141

4242
protected BaseDecorator() {
4343
final Config config = Config.get();
@@ -47,7 +47,7 @@ protected BaseDecorator() {
4747
&& config.isTraceAnalyticsIntegrationEnabled(
4848
traceAnalyticsDefault(), instrumentationNames);
4949
this.traceAnalyticsSampleRate =
50-
config.getInstrumentationAnalyticsSampleRate(instrumentationNames);
50+
(double) config.getInstrumentationAnalyticsSampleRate(instrumentationNames);
5151
this.endToEndDurationsEnabled =
5252
instrumentationNames.length > 0
5353
&& config.isEndToEndDurationEnabled(endToEndDurationsDefault(), instrumentationNames);
@@ -74,7 +74,7 @@ public AgentSpan afterStart(final AgentSpan span) {
7474
}
7575
span.setTag(Tags.COMPONENT, component());
7676
if (traceAnalyticsEnabled) {
77-
span.setTag(DDTags.ANALYTICS_SAMPLE_RATE, traceAnalyticsSampleRate);
77+
span.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, traceAnalyticsSampleRate);
7878
}
7979
if (endToEndDurationsEnabled) {
8080
if (null == span.getBaggageItem(DDTags.TRACE_START_TIME)) {

0 commit comments

Comments
 (0)