-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2077 from newrelic/actuator-module
Actuator endpoint naming for Spring Boot 3
- Loading branch information
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# spring-actuator-3.0.0 Instrumentation Module | ||
|
||
By default, built-in actuator endpoints and custom actuator endpoints (using the @Endpoint annotation | ||
and it's subclasses) will all be named as "OperationHandler/handle" in New Relic. Activating this | ||
module will result in the transaction name reflecting the actual base actuator endpoint URI. | ||
For example, invoking "/actuator/loggers" or "actuator/loggers/com.newrelic" will result in the | ||
transaction name "actuator/loggers (GET)". This is to prevent MGI. | ||
|
||
To activate actuator naming, set the following configuration to true: | ||
- `class_transformer.name_actuator_endpoints` | ||
|
||
The default value is false. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
plugins { | ||
id "org.jetbrains.kotlin.jvm" | ||
} | ||
|
||
dependencies { | ||
implementation(project(":agent-bridge")) | ||
implementation("org.springframework.boot:spring-boot-actuator:3.0.0") | ||
implementation('jakarta.servlet:jakarta.servlet-api:5.0.0') | ||
} | ||
|
||
jar { | ||
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.spring-boot-actuator-3.0.0' } | ||
} | ||
|
||
verifyInstrumentation { | ||
passesOnly('org.springframework.boot:spring-boot-actuator:[3.0.0,)') { | ||
implementation('jakarta.servlet:jakarta.servlet-api:5.0.0') | ||
} | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
} | ||
|
||
test { | ||
// These instrumentation tests only run on Java 17+ regardless of the -PtestN gradle property that is set. | ||
onlyIf { | ||
!project.hasProperty('test8') && !project.hasProperty('test11') | ||
} | ||
} | ||
|
||
site { | ||
title 'Spring' | ||
type 'Framework' | ||
} |
32 changes: 32 additions & 0 deletions
32
...tuator-3.0.0/src/main/java/com/nr/agent/instrumentation/actuator/SpringActuatorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package com.nr.agent.instrumentation.actuator; | ||
|
||
import com.newrelic.api.agent.NewRelic; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.List; | ||
|
||
public class SpringActuatorUtils { | ||
public static final boolean isActuatorEndpointNamingEnabled = NewRelic.getAgent().getConfig().getValue("class_transformer.name_actuator_endpoints", false); | ||
|
||
public static String normalizeActuatorUri(String uri) { | ||
String modifiedUri = null; | ||
if (uri != null && uri.length() > 1) { | ||
// Normalize the uri by removing the leading "/" and stripping and path components | ||
// other than the first two, to prevent MGI for certain actuator endpoints. | ||
// For example, "/actuator/loggers/com.newrelic" will be converted into | ||
// "actuator/loggers" | ||
String [] parts = uri.substring(uri.charAt(0) == '/' ? 1 : 0).split("/"); | ||
if (parts.length >= 2) { | ||
modifiedUri = parts[0] + "/" + parts[1]; | ||
} | ||
} | ||
|
||
return modifiedUri; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...ot/actuate/endpoint/web/servlet/AbstractWebMvcEndpointHandlerMapping_Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.springframework.boot.actuate.endpoint.web.servlet; | ||
|
||
import com.newrelic.agent.bridge.AgentBridge; | ||
import com.newrelic.api.agent.NewRelic; | ||
import com.newrelic.api.agent.Trace; | ||
import com.newrelic.api.agent.Transaction; | ||
import com.newrelic.api.agent.TransactionNamePriority; | ||
import com.newrelic.api.agent.weaver.MatchType; | ||
import com.newrelic.api.agent.weaver.Weave; | ||
import com.newrelic.api.agent.weaver.Weaver; | ||
import com.nr.agent.instrumentation.actuator.SpringActuatorUtils; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
|
||
import java.util.Map; | ||
|
||
@Weave(type = MatchType.BaseClass, originalName = "org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping") | ||
public class AbstractWebMvcEndpointHandlerMapping_Instrumentation { | ||
@Weave(type = MatchType.ExactClass, originalName = "org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler") | ||
private static final class OperationHandler_Instrumentation { | ||
@Trace | ||
Object handle(HttpServletRequest request, Map<String, String> body) { | ||
if (SpringActuatorUtils.isActuatorEndpointNamingEnabled) { | ||
Transaction transaction = AgentBridge.getAgent().getTransaction(false); | ||
|
||
if (transaction != null) { | ||
String reportablePrefix = SpringActuatorUtils.normalizeActuatorUri(request.getRequestURI()); | ||
|
||
if (reportablePrefix != null) { | ||
transaction.setTransactionName(TransactionNamePriority.FRAMEWORK_HIGH, true, "Spring", | ||
reportablePrefix + " (" + request.getMethod() + ")"); | ||
} | ||
} | ||
} | ||
|
||
return Weaver.callOriginal(); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...t/java/org/springframework/boot/actuate/endpoint/web/servlet/SpringActuatorUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
package org.springframework.boot.actuate.endpoint.web.servlet; | ||
|
||
import com.nr.agent.instrumentation.actuator.SpringActuatorUtils; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class SpringActuatorUtilsTest { | ||
@Test | ||
public void normalizeActuatorUri_withNullValue_returnsNull() { | ||
assertNull(SpringActuatorUtils.normalizeActuatorUri(null)); | ||
} | ||
|
||
@Test | ||
public void normalizeActuatorUri_withEmptyString_returnsNull() { | ||
assertNull(SpringActuatorUtils.normalizeActuatorUri("")); | ||
} | ||
|
||
@Test | ||
public void normalizeActuatorUri_withValidUri_returnsModifiedUri() { | ||
assertEquals("actuator/health", SpringActuatorUtils.normalizeActuatorUri("/actuator/health")); | ||
assertEquals("actuator/health", SpringActuatorUtils.normalizeActuatorUri("actuator/health")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters