From d026dcc4a2df0ab099dd9267af6576d84502ad4c Mon Sep 17 00:00:00 2001 From: Kate Anderson Date: Mon, 16 Dec 2024 10:06:22 -0800 Subject: [PATCH 1/2] Add scala3 source detection --- .../agent/language/SourceLibraryDetector.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java b/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java index e90a1bd640..bcda7d87bb 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java @@ -59,6 +59,7 @@ public boolean isDone() { void detectSourceLanguages() { ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); detectJava(); + detectScala3(systemClassLoader); detectScala(systemClassLoader); detectKotlin(systemClassLoader); detectClojure(systemClassLoader); @@ -89,6 +90,21 @@ private void detectJava() { } } + private void detectScala3(ClassLoader systemClassLoader) { + try { + //scala3-library removed all methods referencing the version number, so we just capture the major version + //by checking for a class introduced in the scala 3 API. + Class aClass = Class.forName("scala.deriving.Mirror", true, systemClassLoader); + if (aClass != null) { + recordSourceLanguageMetric("scala", "3.x"); + } + } catch (ClassNotFoundException cnfe){ + Agent.LOG.log(Level.FINEST, format("Class ''{0}'' was not found; scala3 is not present in the classpath", "scala.deriving.Mirror")); + } catch (Exception e) { + Agent.LOG.log(Level.FINEST, format("Exception occurred while trying to determine scala3 version", e.getMessage())); + } + } + private void detectScala(ClassLoader systemClassLoader) { try { // Scala has a static versionNumberString method that can be invoked to get the version number From bcf321e7d3c38fd5d9d6eaab10a280722fe3b068 Mon Sep 17 00:00:00 2001 From: Kate Anderson Date: Mon, 16 Dec 2024 12:20:42 -0800 Subject: [PATCH 2/2] Add additional comments for scala detection --- .../com/newrelic/agent/language/SourceLibraryDetector.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java b/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java index bcda7d87bb..248ebd1cf8 100644 --- a/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java +++ b/newrelic-agent/src/main/java/com/newrelic/agent/language/SourceLibraryDetector.java @@ -92,7 +92,7 @@ private void detectJava() { private void detectScala3(ClassLoader systemClassLoader) { try { - //scala3-library removed all methods referencing the version number, so we just capture the major version + //scala3-library removed all methods referencing the version 3 number, so we only capture the major version //by checking for a class introduced in the scala 3 API. Class aClass = Class.forName("scala.deriving.Mirror", true, systemClassLoader); if (aClass != null) { @@ -107,7 +107,8 @@ private void detectScala3(ClassLoader systemClassLoader) { private void detectScala(ClassLoader systemClassLoader) { try { - // Scala has a static versionNumberString method that can be invoked to get the version number + // Scala has a static versionNumberString method that can be invoked to get the version number. + // A scala 3 app will also capture this metric, because scala 3 includes the scala 2.13 library. Class aClass = Class.forName(SCALA_VERSION_CLASS, true, systemClassLoader); if (aClass != null) { String version = (String) aClass.getMethod(SCALA_VERSION_METHOD).invoke(null);