Search in sources :

Example 1 with HttpTrace

use of org.springframework.boot.actuate.trace.http.HttpTrace in project spring-cloud-netflix by spring-cloud.

the class ProxyRequestHelperTests method debug.

@Test
public void debug() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
    request.setContent("{}".getBytes());
    request.addHeader("singleName", "singleValue");
    request.addHeader("multiName", "multiValue1");
    request.addHeader("multiName", "multiValue2");
    RequestContext.getCurrentContext().setRequest(request);
    TraceProxyRequestHelper helper = new TraceProxyRequestHelper();
    this.traceRepository = new InMemoryHttpTraceRepository();
    helper.setTraces(this.traceRepository);
    MultiValueMap<String, String> headers = helper.buildZuulRequestHeaders(request);
    helper.debug("POST", "http://example.com", headers, new LinkedMultiValueMap<>(), request.getInputStream());
    HttpTrace actual = this.traceRepository.findAll().get(0);
    Assertions.assertThat(actual.getRequest().getHeaders()).containsKeys("singleName", "multiName");
}
Also used : HttpTrace(org.springframework.boot.actuate.trace.http.HttpTrace) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) InMemoryHttpTraceRepository(org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository) Test(org.junit.Test)

Example 2 with HttpTrace

use of org.springframework.boot.actuate.trace.http.HttpTrace in project spring-boot by spring-projects.

the class HttpTraceEndpointDocumentationTests method traces.

@Test
void traces() throws Exception {
    TraceableRequest request = mock(TraceableRequest.class);
    given(request.getUri()).willReturn(URI.create("https://api.example.com"));
    given(request.getMethod()).willReturn("GET");
    given(request.getHeaders()).willReturn(Collections.singletonMap(HttpHeaders.ACCEPT, Arrays.asList("application/json")));
    TraceableResponse response = mock(TraceableResponse.class);
    given(response.getStatus()).willReturn(200);
    given(response.getHeaders()).willReturn(Collections.singletonMap(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json")));
    Principal principal = mock(Principal.class);
    given(principal.getName()).willReturn("alice");
    HttpExchangeTracer tracer = new HttpExchangeTracer(EnumSet.allOf(Include.class));
    HttpTrace trace = tracer.receivedRequest(request);
    tracer.sendingResponse(trace, response, () -> principal, () -> UUID.randomUUID().toString());
    given(this.repository.findAll()).willReturn(Arrays.asList(trace));
    this.mockMvc.perform(get("/actuator/httptrace")).andExpect(status().isOk()).andDo(document("httptrace", responseFields(fieldWithPath("traces").description("An array of traced HTTP request-response exchanges."), fieldWithPath("traces.[].timestamp").description("Timestamp of when the traced exchange occurred."), fieldWithPath("traces.[].principal").description("Principal of the exchange, if any.").optional(), fieldWithPath("traces.[].principal.name").description("Name of the principal.").optional(), fieldWithPath("traces.[].request.method").description("HTTP method of the request."), fieldWithPath("traces.[].request.remoteAddress").description("Remote address from which the request was received, if known.").optional().type(JsonFieldType.STRING), fieldWithPath("traces.[].request.uri").description("URI of the request."), fieldWithPath("traces.[].request.headers").description("Headers of the request, keyed by header name."), fieldWithPath("traces.[].request.headers.*.[]").description("Values of the header"), fieldWithPath("traces.[].response.status").description("Status of the response"), fieldWithPath("traces.[].response.headers").description("Headers of the response, keyed by header name."), fieldWithPath("traces.[].response.headers.*.[]").description("Values of the header"), fieldWithPath("traces.[].session").description("Session associated with the exchange, if any.").optional(), fieldWithPath("traces.[].session.id").description("ID of the session."), fieldWithPath("traces.[].timeTaken").description("Time, in milliseconds, taken to handle the exchange."))));
}
Also used : HttpExchangeTracer(org.springframework.boot.actuate.trace.http.HttpExchangeTracer) HttpTrace(org.springframework.boot.actuate.trace.http.HttpTrace) TraceableResponse(org.springframework.boot.actuate.trace.http.TraceableResponse) Include(org.springframework.boot.actuate.trace.http.Include) Principal(java.security.Principal) TraceableRequest(org.springframework.boot.actuate.trace.http.TraceableRequest) Test(org.junit.jupiter.api.Test)

Example 3 with HttpTrace

use of org.springframework.boot.actuate.trace.http.HttpTrace in project spring-boot by spring-projects.

the class HttpTraceFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (!isRequestValid(request)) {
        filterChain.doFilter(request, response);
        return;
    }
    TraceableHttpServletRequest traceableRequest = new TraceableHttpServletRequest(request);
    HttpTrace trace = this.tracer.receivedRequest(traceableRequest);
    int status = HttpStatus.INTERNAL_SERVER_ERROR.value();
    try {
        filterChain.doFilter(request, response);
        status = response.getStatus();
    } finally {
        TraceableHttpServletResponse traceableResponse = new TraceableHttpServletResponse((status != response.getStatus()) ? new CustomStatusResponseWrapper(response, status) : response);
        this.tracer.sendingResponse(trace, traceableResponse, request::getUserPrincipal, () -> getSessionId(request));
        this.repository.add(trace);
    }
}
Also used : HttpTrace(org.springframework.boot.actuate.trace.http.HttpTrace)

Example 4 with HttpTrace

use of org.springframework.boot.actuate.trace.http.HttpTrace in project spring-boot by spring-projects.

the class HttpTraceWebFilter method filter.

private Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain, Principal principal, WebSession session) {
    ServerWebExchangeTraceableRequest request = new ServerWebExchangeTraceableRequest(exchange);
    HttpTrace trace = this.tracer.receivedRequest(request);
    exchange.getResponse().beforeCommit(() -> {
        TraceableServerHttpResponse response = new TraceableServerHttpResponse(exchange.getResponse());
        this.tracer.sendingResponse(trace, response, () -> principal, () -> getStartedSessionId(session));
        this.repository.add(trace);
        return Mono.empty();
    });
    return chain.filter(exchange);
}
Also used : HttpTrace(org.springframework.boot.actuate.trace.http.HttpTrace)

Example 5 with HttpTrace

use of org.springframework.boot.actuate.trace.http.HttpTrace in project spring-cloud-netflix by spring-cloud.

the class TraceProxyRequestHelper method debug.

@Override
public Map<String, Object> debug(String verb, String uri, MultiValueMap<String, String> headers, MultiValueMap<String, String> params, InputStream requestEntity) throws IOException {
    Map<String, Object> info = new LinkedHashMap<>();
    if (this.traces != null) {
        RequestContext context = RequestContext.getCurrentContext();
        info.put("method", verb);
        info.put("path", uri);
        info.put("query", getQueryString(params));
        info.put("remote", true);
        info.put("proxy", context.get("proxy"));
        Map<String, Object> trace = new LinkedHashMap<>();
        Map<String, Object> input = new LinkedHashMap<>();
        trace.put("request", input);
        info.put("headers", trace);
        debugHeaders(headers, input);
        HttpServletRequest request = context.getRequest();
        if (shouldDebugBody(context)) {
            // Prevent input stream from being read if it needs to go downstream
            if (requestEntity != null) {
                debugRequestEntity(info, request.getInputStream());
            }
        }
        HttpTrace httpTrace = tracer.receivedRequest(new ServletTraceableRequest(request));
        this.traces.add(httpTrace);
        return info;
    }
    return info;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpTrace(org.springframework.boot.actuate.trace.http.HttpTrace) RequestContext(com.netflix.zuul.context.RequestContext) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

HttpTrace (org.springframework.boot.actuate.trace.http.HttpTrace)5 RequestContext (com.netflix.zuul.context.RequestContext)1 Principal (java.security.Principal)1 LinkedHashMap (java.util.LinkedHashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 HttpExchangeTracer (org.springframework.boot.actuate.trace.http.HttpExchangeTracer)1 InMemoryHttpTraceRepository (org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository)1 Include (org.springframework.boot.actuate.trace.http.Include)1 TraceableRequest (org.springframework.boot.actuate.trace.http.TraceableRequest)1 TraceableResponse (org.springframework.boot.actuate.trace.http.TraceableResponse)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1