Skip to content

Commit

Permalink
feat: Multiple Methods in Scope
Browse files Browse the repository at this point in the history
  • Loading branch information
levinkerschberger committed Aug 22, 2024
1 parent 6ba4514 commit 971f534
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package rocks.inspectit.gepard.agent.internal.configuration.model.instrumentation;

import java.util.List;

/**
* Represents a scope in the instrumentation configuration. A scope defines a set of methods which
* should be instrumented.
Expand All @@ -8,7 +10,7 @@ public class Scope {

private String fqn;

private String method;
private List<String> methods;

private boolean enabled;

Expand All @@ -19,18 +21,18 @@ public Scope(String fqn, boolean enabled) {
this.enabled = enabled;
}

public Scope(String fqn, String method, boolean enabled) {
public Scope(String fqn, List<String> methods, boolean enabled) {
this.fqn = fqn;
this.method = method;
this.methods = methods;
this.enabled = enabled;
}

public String getFqn() {
return fqn;
}

public String getMethod() {
return method;
public List<String> getMethods() {
return methods;
}

public boolean isEnabled() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import rocks.inspectit.gepard.agent.internal.configuration.model.instrumentation.InstrumentationConfiguration;
import rocks.inspectit.gepard.agent.internal.configuration.model.instrumentation.Scope;

import java.util.*;

/**
* Utility class to resolve the {@link InstrumentationConfiguration} and determine whether class
* byte code needs updates.
Expand Down Expand Up @@ -85,10 +87,10 @@ private boolean shouldIgnore(String fullyQualifiedName) {
public ElementMatcher.Junction<MethodDescription> getElementMatcherForType(TypeDescription type) {
InstrumentationConfiguration configuration = getConfiguration();
Scope scope = configuration.getScopeByFqn(type.getName());
String methodName = scope.getMethod();
if (methodName == null) {
List<String> methodNames = scope.getMethods();
if (Objects.isNull(methodNames) ||methodNames.isEmpty()) {
return ElementMatchers.isMethod();
}
return ElementMatchers.named(methodName);
return ElementMatchers.anyOf(methodNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ public void advice_is_executed_only_in_greeting() throws IOException, Interrupte
waitFor("BYE GEPARD", 1);
stopTarget();
}

@Test
void adviceIsExecutedInMultipleMethods() throws IOException, InterruptedException {
configurationServerMock.configServerSetup(
"integrationtest/configurations/scope-with-multiple-methods.json");
startTarget("/opentelemetry-extensions.jar");
sendRequestToTarget();
// Now two methods should be instrumented, so we expect two log entries.
waitFor("HELLO GEPARD", 2);
waitFor("BYE GEPARD", 2);
stopTarget();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package rocks.inspectit.gepard.agent.resolver;

import static net.bytebuddy.matcher.ElementMatchers.anyOf;
import static net.bytebuddy.matcher.ElementMatchers.hasMethodName;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -78,11 +79,24 @@ void methodIsNullShouldReturnIsMethodMatcher() {
}

@Test
void methodIsSpecifiedShouldReturnMatcherForMethod() {
InspectitConfiguration configuration = createConfiguration(true, "create");
void methodIsSpecifiedShouldReturnMatcherForOneMethod() {
InspectitConfiguration configuration = createConfiguration(true, List.of("create"));
when(holder.getConfiguration()).thenReturn(configuration);

ElementMatcher expectedMatcher = hasMethodName("create");
ElementMatcher<MethodDescription> expectedMatcher = hasMethodName("create");

ElementMatcher.Junction<MethodDescription> elementMatcher =
resolver.getElementMatcherForType(TEST_TYPE);

assertEquals(expectedMatcher, elementMatcher);
}

@Test
void multipleMethodsAreSpecifiedReturnMatcherForMultipleMethods() {
InspectitConfiguration configuration = createConfiguration(true, List.of("create", "initialize"));
when(holder.getConfiguration()).thenReturn(configuration);

ElementMatcher<MethodDescription> expectedMatcher = anyOf("create","initialize");

ElementMatcher.Junction<MethodDescription> elementMatcher =
resolver.getElementMatcherForType(TEST_TYPE);
Expand All @@ -101,8 +115,8 @@ private InspectitConfiguration createConfiguration(boolean enabled) {
return new InspectitConfiguration(instrumentationConfiguration);
}

private InspectitConfiguration createConfiguration(boolean enabled, String methodName) {
Scope scope = new Scope(TEST_TYPE.getName(), methodName, enabled);
private InspectitConfiguration createConfiguration(boolean enabled, List<String> methodNames) {
Scope scope = new Scope(TEST_TYPE.getName(), methodNames, enabled);
InstrumentationConfiguration instrumentationConfiguration =
new InstrumentationConfiguration(List.of(scope));
return new InspectitConfiguration(instrumentationConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scopes": [
{
"fqn": "io.opentelemetry.smoketest.springboot.controller.WebController",
"method": "greeting",
"methods": ["greeting"],
"enabled": true
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"instrumentationConfiguration": {
"scopes": [
{
"fqn": "io.opentelemetry.smoketest.springboot.controller.WebController",
"methods": ["greeting", "withSpan"],
"enabled": true
}
]
}
}

0 comments on commit 971f534

Please sign in to comment.