Skip to content

Commit 9da6ddb

Browse files
committed
Trace IDs of sessions created downstream of trace filter
Closes gh-11717
1 parent 7ae39ea commit 9da6ddb

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -112,14 +112,21 @@ protected void doFilterInternal(HttpServletRequest request,
112112
}
113113
finally {
114114
addTimeTaken(trace, startTime);
115+
addSessionIdIfNecessary(request, trace);
115116
enhanceTrace(trace, status == response.getStatus() ? response
116117
: new CustomStatusResponseWrapper(response, status));
117118
this.repository.add(trace);
118119
}
119120
}
120121

121-
protected Map<String, Object> getTrace(HttpServletRequest request) {
122+
private void addSessionIdIfNecessary(HttpServletRequest request,
123+
Map<String, Object> trace) {
122124
HttpSession session = request.getSession(false);
125+
add(trace, Include.SESSION_ID, "sessionId",
126+
(session == null ? null : session.getId()));
127+
}
128+
129+
protected Map<String, Object> getTrace(HttpServletRequest request) {
123130
Throwable exception = (Throwable) request
124131
.getAttribute("javax.servlet.error.exception");
125132
Principal userPrincipal = request.getUserPrincipal();
@@ -143,8 +150,6 @@ protected Map<String, Object> getTrace(HttpServletRequest request) {
143150
add(trace, Include.QUERY_STRING, "query", request.getQueryString());
144151
add(trace, Include.AUTH_TYPE, "authType", request.getAuthType());
145152
add(trace, Include.REMOTE_ADDRESS, "remoteAddress", request.getRemoteAddr());
146-
add(trace, Include.SESSION_ID, "sessionId",
147-
(session == null ? null : session.getId()));
148153
add(trace, Include.REMOTE_USER, "remoteUser", request.getRemoteUser());
149154
if (isIncluded(Include.ERRORS) && exception != null
150155
&& this.errorAttributes != null) {

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.actuate.trace;
1818

19-
import java.io.BufferedReader;
2019
import java.io.File;
2120
import java.io.IOException;
2221
import java.security.Principal;
@@ -28,6 +27,9 @@
2827
import javax.servlet.ServletException;
2928
import javax.servlet.ServletRequest;
3029
import javax.servlet.ServletResponse;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
3133

3234
import org.junit.Test;
3335

@@ -105,19 +107,16 @@ public String getName() {
105107
MockHttpServletResponse response = new MockHttpServletResponse();
106108
response.addHeader("Content-Type", "application/json");
107109
response.addHeader("Set-Cookie", "a=b");
108-
this.filter.doFilterInternal(request, response, new FilterChain() {
110+
this.filter.doFilterInternal(request, response,
111+
new MockFilterChain(new HttpServlet() {
109112

110-
@Override
111-
public void doFilter(ServletRequest request, ServletResponse response)
112-
throws IOException, ServletException {
113-
BufferedReader bufferedReader = request.getReader();
114-
while (bufferedReader.readLine() != null) {
115-
// read the contents as normal (forces cache to fill up)
116-
}
117-
response.getWriter().println("Goodbye, World!");
118-
}
113+
@Override
114+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
115+
throws ServletException, IOException {
116+
req.getSession(true);
117+
}
119118

120-
});
119+
}));
121120
assertThat(this.repository.findAll()).hasSize(1);
122121
Map<String, Object> trace = this.repository.findAll().iterator().next().getInfo();
123122
Map<String, Object> map = (Map<String, Object>) trace.get("headers");
@@ -136,6 +135,7 @@ public void doFilter(ServletRequest request, ServletResponse response)
136135
assertThat(trace.get("authType")).isEqualTo("authType");
137136
assertThat(map.get("request").toString())
138137
.isEqualTo("{Accept=application/json, Cookie=testCookie=testValue;}");
138+
assertThat(trace).containsKey("sessionId");
139139
}
140140

141141
@Test

0 commit comments

Comments
 (0)