Skip to content

Commit e7e941d

Browse files
committed
address code review feedback. Cache the return from getMethod("getFunctionArn").
1 parent c15aba4 commit e7e941d

File tree

1 file changed

+16
-7
lines changed
  • instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11

1 file changed

+16
-7
lines changed

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/RequestAccess.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
import java.lang.invoke.MethodHandle;
99
import java.lang.invoke.MethodHandles;
1010
import java.lang.invoke.MethodType;
11-
import java.lang.reflect.Method;
1211
import javax.annotation.Nullable;
1312

1413
final class RequestAccess {
14+
private static final String LAMBDA_REQUEST_CLASS_PREFIX = "com.amazonaws.services.lambda.model.";
1515
private static final String SECRETS_MANAGER_REQUEST_CLASS_PREFIX =
1616
"com.amazonaws.services.secretsmanager.model.";
17-
private static final String LAMBDA_REQUEST_CLASS_PREFIX = "com.amazonaws.services.lambda.model.";
1817
private static final String STEP_FUNCTIONS_REQUEST_CLASS_PREFIX =
1918
"com.amazonaws.services.stepfunctions.model.";
2019

@@ -34,11 +33,7 @@ static String getLambdaArn(Object request) {
3433
}
3534
try {
3635
Object config = access.getLambdaConfiguration.invoke(request);
37-
if (config == null) {
38-
return null;
39-
}
40-
Method method = config.getClass().getMethod("getFunctionArn");
41-
return (String) method.invoke(config);
36+
return invokeOrNull(access.getLambdaArnFromConfiguration, config);
4237
} catch (Throwable t) {
4338
return null;
4439
}
@@ -130,6 +125,7 @@ private static String invokeOrNull(@Nullable MethodHandle method, Object obj) {
130125

131126
@Nullable private final MethodHandle getBucketName;
132127
@Nullable private final MethodHandle getLambdaConfiguration;
128+
@Nullable private final MethodHandle getLambdaArnFromConfiguration;
133129
@Nullable private final MethodHandle getLambdaName;
134130
@Nullable private final MethodHandle getLambdaResourceMappingId;
135131
@Nullable private final MethodHandle getQueueUrl;
@@ -153,6 +149,8 @@ private RequestAccess(Class<?> clz) {
153149

154150
boolean isLambda = clz.getName().startsWith(LAMBDA_REQUEST_CLASS_PREFIX);
155151
getLambdaConfiguration = isLambda ? findLambdaGetConfigurationMethod(clz) : null;
152+
getLambdaArnFromConfiguration =
153+
getLambdaConfiguration != null ? findGetLambdaArnMethod() : null;
156154
getLambdaName = isLambda ? findAccessorOrNull(clz, "getFunctionName") : null;
157155
getLambdaResourceMappingId = isLambda ? findAccessorOrNull(clz, "getUUID") : null;
158156
boolean isSecretsManager = clz.getName().startsWith(SECRETS_MANAGER_REQUEST_CLASS_PREFIX);
@@ -188,4 +186,15 @@ private static MethodHandle findLambdaGetConfigurationMethod(Class<?> clz) {
188186
return null;
189187
}
190188
}
189+
190+
@Nullable
191+
private static MethodHandle findGetLambdaArnMethod() {
192+
try {
193+
Class<?> lambdaConfigurationClass =
194+
Class.forName("com.amazonaws.services.lambda.model.FunctionConfiguration");
195+
return findAccessorOrNull(lambdaConfigurationClass, "getFunctionArn");
196+
} catch (Throwable t) {
197+
return null;
198+
}
199+
}
191200
}

0 commit comments

Comments
 (0)