Skip to content

Commit

Permalink
PoC: Use Throwable.getStackTrace() instead of currentThread.getStackT…
Browse files Browse the repository at this point in the history
…race() for optimise
  • Loading branch information
IshikaDawda committed Dec 18, 2024
1 parent fe0ce16 commit e1810c5
Show file tree
Hide file tree
Showing 30 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void preProcessHttpRequest (Boolean isServletLockAcquired, HttpReq

securityRequest.setContentType(httpRequest.entity().getContentType().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setBody(requestBody);
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void preProcessHttpRequest (Boolean isServletLockAcquired, HttpReq

securityRequest.setContentType(httpRequest.entity().getContentType().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setBody(requestBody);
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static void preProcessHttpRequest (Boolean isServletLockAcquired, HttpReq

securityRequest.setContentType(httpRequest.entity().getContentType().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setBody(requestBody);
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public static void preProcessHttpRequest (Boolean isServletLockAcquired, HttpReq

securityRequest.setContentType(httpRequest.entity().getContentType().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setBody(requestBody);
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static void preProcessHttpRequest(Boolean isServletLockAcquired, HttpRequ
securityRequest.setContentType(request.entity().getContentType().toString());
}

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setBody(requestBody);
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onMessage(ReqT message) {
public void onHalfClose() {
if (NewRelicSecurity.isHookProcessingActive()) {
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData();
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
}
if (tokenForCsec != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class GrpcServerUtils {
private static final String EMPTY = "";
public static final String METHOD_NAME_START_CALL = "startCall";
public static final String NR_SEC_CUSTOM_ATTRIB_NAME = "NR_CSEC_GRPC_SERVER_OPERATIONAL_LOCK_";
private static Set<Descriptors.Descriptor> typeRegistries = new HashSet<>();
private static final Set<Descriptors.Descriptor> typeRegistries = new HashSet<>();

public static <ReqT, ResT> void preprocessSecurityHook(ServerStream_Instrumentation call, ServerMethodDefinition<ReqT, ResT> methodDef, Metadata meta, String klass) {
try {
Expand Down Expand Up @@ -79,7 +79,7 @@ public static <ReqT, ResT> void preprocessSecurityHook(ServerStream_Instrumentat
securityRequest.setContentType(meta.get(Metadata.Key.of("content-type", Metadata.ASCII_STRING_MARSHALLER)));

// TODO: moving this code to its proper place
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));

securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onMessage(ReqT message) {
public void onHalfClose() {
if (NewRelicSecurity.isHookProcessingActive()) {
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData();
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
}
if (tokenForCsec != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void onMessage(ReqT message) {
public void onHalfClose() {
if (NewRelicSecurity.isHookProcessingActive()) {
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData();
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
}
if (tokenForCsec != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void preprocessSecurityHook(ContainerRequest requestContext) {
securityMetaData.setTracingHeaderValue(HttpRequestHelper.getTraceHeader(securityRequest.getHeaders()));
securityRequest.setUrl(requestContext.getRequestUri().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e) {
Expand Down Expand Up @@ -269,7 +269,7 @@ public static void registerUserLevelCode(String frameworkName) {
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData();
if (!securityMetaData.getMetaData().isUserLevelServiceMethodEncountered(frameworkName)) {
securityMetaData.getMetaData().setUserLevelServiceMethodEncountered(true);
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
}
} catch (Throwable ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void preprocessSecurityHook(ContainerRequest requestContext) {
securityMetaData.setTracingHeaderValue(HttpRequestHelper.getTraceHeader(securityRequest.getHeaders()));
securityRequest.setUrl(requestContext.getRequestUri().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e) {
Expand Down Expand Up @@ -255,7 +255,7 @@ public static void registerUserLevelCode(String frameworkName) {
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData();
if (!securityMetaData.getMetaData().isUserLevelServiceMethodEncountered(frameworkName)) {
securityMetaData.getMetaData().setUserLevelServiceMethodEncountered(true);
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
}
} catch (Throwable ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static void preprocessSecurityHook(ContainerRequest requestContext) {
securityMetaData.setTracingHeaderValue(HttpRequestHelper.getTraceHeader(securityRequest.getHeaders()));
securityRequest.setUrl(requestContext.getRequestUri().toString());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e) {
Expand Down Expand Up @@ -281,7 +281,7 @@ public static void registerUserLevelCode(String frameworkName) {
SecurityMetaData securityMetaData = NewRelicSecurity.getAgent().getSecurityMetaData();
if (!securityMetaData.getMetaData().isUserLevelServiceMethodEncountered(frameworkName)) {
securityMetaData.getMetaData().setUserLevelServiceMethodEncountered(true);
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
}
} catch (Throwable ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static void preprocessSecurityHook(HttpServletRequest httpServletRequest)
}
securityRequest.setContentType(httpServletRequest.getContentType());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static void preprocessSecurityHook(Request request) {
}
securityRequest.setContentType(request.getHeaders().get(HttpHeader.CONTENT_TYPE));

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static void preprocessSecurityHook(HttpServletRequest httpServletRequest)
}
securityRequest.setContentType(httpServletRequest.getContentType());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void processSecurityRequest(ChannelHandlerContext ctx, Object msg,
securityRequest.setProtocol(((HttpRequest) msg).getProtocolVersion().protocolName().toLowerCase());
securityRequest.setContentType(securityRequest.getHeaders().get("content-type"));
if (!securityMetaData.getMetaData().isUserLevelServiceMethodEncountered(IO_NETTY)){
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
StackTraceElement[] stack = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(stack, 2, stack.length));
}
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void processSecurityRequest(ChannelHandlerContext ctx, Object msg,
securityRequest.setProtocol(((HttpRequest) msg).getProtocolVersion().protocolName().toLowerCase());
securityRequest.setContentType(securityRequest.getHeaders().get("content-type"));
if (!securityMetaData.getMetaData().isUserLevelServiceMethodEncountered(IO_NETTY)){
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
StackTraceElement[] stack = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(stack, 2, stack.length));
}
securityRequest.setRequestParsed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void preprocessSecurityHook(ServletRequest request, ServletResponse resp
securityRequest.setContentType(httpServletRequest.getContentType());


StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void preprocessSecurityHook(ServletRequest request, ServletResponse resp
}
securityRequest.setContentType(httpServletRequest.getContentType());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void preprocessSecurityHook(ServletRequest_Instrumentation request, Serv
securityRequest.setContentType(httpServletRequest.getContentType());


StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void preprocessSecurityHook(ServletRequest request, ServletResponse resp
}
securityRequest.setContentType(httpServletRequest.getContentType());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void preprocessSecurityHook(ServletRequest request, ServletResponse resp
securityRequest.setContentType(httpServletRequest.getContentType());


StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void preprocessSecurityHook(ServletRequest_Instrumentation request, Serv
}
securityRequest.setContentType(httpServletRequest.getContentType());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void preprocessSecurityHook(ServletRequest request, ServletResponse resp
securityRequest.setContentType(httpServletRequest.getContentType());


StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void preprocessSecurityHook(ServletRequest request, ServletResponse resp
securityRequest.setContentType(httpServletRequest.getContentType());


StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void preprocessSecurityHook(ServletRequest_Instrumentation request, Serv
}
securityRequest.setContentType(httpServletRequest.getContentType());

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 1, trace.length));
securityRequest.setRequestParsed(true);
} catch (Throwable e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void preProcessRequestHook(HttpRequest request) {
securityRequest.setBody(new StringBuilder(request.entity().data().asString(StandardCharsets.UTF_8)));
}

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Exception e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static void preProcessRequestHook(HttpRequest request) {
securityRequest.setBody(new StringBuilder(request.entity().data().asString(StandardCharsets.UTF_8)));
}

StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
securityMetaData.getMetaData().setServiceTrace(Arrays.copyOfRange(trace, 2, trace.length));
securityRequest.setRequestParsed(true);
} catch (Exception e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public void registerOperation(AbstractOperation operation) {
} else if (operation instanceof SecureCookieOperationSet) {
operation.setStackTrace(securityMetaData.getMetaData().getServiceTrace());
} else {
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
StackTraceElement[] trace = (new Exception()).getStackTrace();
operation.setStackTrace(Arrays.copyOfRange(trace, securityMetaData.getMetaData().getFromJumpRequiredInStackTrace(), trace.length));
}

Expand Down
Loading

0 comments on commit e1810c5

Please sign in to comment.