Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ ResponseEntity<String> bigResponse() throws IOException {

generator.writeStartObject();
generator.writeStringField("start", "here");
generator.writeStringField("longValue", generateLongString(64*1024));
for(int i = 0; i < 10; i++) {
generator.writeStringField("longValue" + i, generateLongString(25*1024));
}
generator.writeStringField("end", "here");
generator.writeEndObject();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.entur.example.web.rest;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.io.CharArrayWriter;
import java.io.IOException;

@RestController
@RequestMapping("/api/document")
Expand Down Expand Up @@ -46,5 +48,38 @@ public ResponseEntity errorMethod(@RequestBody MyEntity entity) throws Interrupt
}


@GetMapping(value = "/some/bigResponse", produces = "application/json")
ResponseEntity<String> bigResponse() throws IOException {
JsonFactory factory = new JsonFactory();

CharArrayWriter writer = new CharArrayWriter();

JsonGenerator generator = factory.createGenerator(writer);

generator.writeStartObject();
generator.writeStringField("start", "here");
for(int i = 0; i < 10; i++) {
generator.writeStringField("longValue" + i, generateLongString(25*1024));
}
generator.writeStringField("end", "here");
generator.writeEndObject();

generator.flush();

return new ResponseEntity<>(writer.toString(), HttpStatus.OK);
}

private String generateLongString(int length) {
StringBuilder builder = new StringBuilder(length);

int mod = 'z' - 'a';

for(int i = 0; i < length; i++) {
char c = (char) ('a' + i % mod);
builder.append(c);
}
return builder.toString();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.entur.example.web;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class WebLoggingFormatWithBigResponsesTest {

@LocalServerPort
private int randomServerPort;

@Autowired
private TestRestTemplate restTemplate;

@Test
public void useMachineReadableJsonEncoder() {
ResponseEntity<String> response = restTemplate.getForEntity("/api/document/some/bigResponse", String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package no.entur.logging.cloud.logbook.logbook.test;

import com.fasterxml.jackson.core.JsonFactory;
import no.entur.logging.cloud.logbook.AbstractLogLevelLogstashLogbackSink;
import no.entur.logging.cloud.logbook.AbstractSinkBuilder;
import no.entur.logging.cloud.logbook.DefaultRemoteHttpMessageContextSupplier;
import no.entur.logging.cloud.logbook.MessageComposer;
import no.entur.logging.cloud.logbook.RemoteHttpMessageContextSupplier;
import no.entur.logging.cloud.logbook.*;
import org.slf4j.Marker;
import org.zalando.logbook.HttpRequest;
import org.zalando.logbook.HttpResponse;
Expand Down Expand Up @@ -60,14 +56,14 @@ public PrettyPrintingLogLevelLogstashLogbackSink(BiConsumer<Marker, String> logC
}

@Override
protected Marker newRequestSingleFieldAppendingMarker(HttpRequest request, String body, boolean wellformed) {
return new PrettyPrintingRequestSingleFieldAppendingMarker(request, body, wellformed);
protected RequestResponseSingleFieldAppendingMarker newRequestSingleFieldAppendingMarker(HttpRequest request, String body, boolean wellformed, int truncated) {
return new PrettyPrintingRequestSingleFieldAppendingMarker(request, body, wellformed, truncated);
}

@Override
protected Marker newResponseSingleFieldAppendingMarker(HttpResponse response, Duration duration, String body,
boolean wellformed) {
return new PrettyPrintingResponseSingleFieldAppendingMarker(response, duration, body, wellformed);
protected RequestResponseSingleFieldAppendingMarker newResponseSingleFieldAppendingMarker(HttpResponse response, Duration duration, String body,
boolean wellformed, int truncated) {
return new PrettyPrintingResponseSingleFieldAppendingMarker(response, duration, body, wellformed, truncated);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

public class PrettyPrintingRequestSingleFieldAppendingMarker extends RequestSingleFieldAppendingMarker {

public PrettyPrintingRequestSingleFieldAppendingMarker(HttpRequest request, String body, boolean wellformed) {
super(request, body, wellformed);
public PrettyPrintingRequestSingleFieldAppendingMarker(HttpRequest request, String body, boolean wellformed, int truncated) {
super(request, body, wellformed, truncated);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public class PrettyPrintingResponseSingleFieldAppendingMarker extends ResponseSingleFieldAppendingMarker {


public PrettyPrintingResponseSingleFieldAppendingMarker(HttpResponse response, Duration duration, String body, boolean wellformed) {
super(response, duration, body, wellformed);
public PrettyPrintingResponseSingleFieldAppendingMarker(HttpResponse response, Duration duration, String body, boolean wellformed, int truncated) {
super(response, duration, body, wellformed, truncated);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public PrettyPrintingSink(BooleanSupplier logLevelEnabled, BiConsumer<Marker, St
}

@Override
protected void requestMessage(HttpRequest request, StringBuilder messageBuilder) throws IOException {
super.requestMessage(request, messageBuilder);
protected void requestMessage(HttpRequest request, StringBuilder messageBuilder, int truncated) throws IOException {
super.requestMessage(request, messageBuilder, truncated);

messageBuilder.append('\n');
writeHeaders(request.getHeaders(), messageBuilder);
Expand All @@ -98,8 +98,8 @@ protected void requestMessage(HttpRequest request, StringBuilder messageBuilder)

@Override
protected void responseMessage(Correlation correlation, HttpRequest request, HttpResponse response,
StringBuilder messageBuilder) throws IOException {
super.responseMessage(correlation, request, response, messageBuilder);
StringBuilder messageBuilder, int truncated) throws IOException {
super.responseMessage(correlation, request, response, messageBuilder, truncated);

messageBuilder.append('\n');
writeHeaders(response.getHeaders(), messageBuilder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package no.entur.logging.cloud.logbook.logbook.test.ondemand;

import com.fasterxml.jackson.core.JsonFactory;
import no.entur.logging.cloud.logbook.*;
import no.entur.logging.cloud.logbook.AbstractLogLevelLogstashLogbackSink;
import no.entur.logging.cloud.logbook.DefaultRemoteHttpMessageContextSupplier;
import no.entur.logging.cloud.logbook.MessageComposer;
import no.entur.logging.cloud.logbook.RemoteHttpMessageContextSupplier;
import no.entur.logging.cloud.logbook.ondemand.*;
import no.entur.logging.cloud.logbook.ondemand.state.HttpMessageStateSupplier;
import no.entur.logging.cloud.logbook.ondemand.state.RequestHttpMessageStateSupplierSource;
Expand Down Expand Up @@ -79,28 +77,32 @@ public PrettyPrintingOndemandLogLevelLogstashLogbackSink(BiConsumer<Marker, Stri
client);
}

public Marker createRequestMarker(HttpRequest request) {
public RequestResponseSingleFieldAppendingMarker createRequestMarker(HttpRequest request) {
HttpMessageBodyWriter writer = EmptyHttpMessageBodyWriter.INSTANCE;

int truncated = -1;
if (ContentType.isJsonMediaType(request.getContentType())) {
try {
byte[] body = request.getBody();
if (body != null && body.length > 0) {
if (request.getOrigin() == Origin.LOCAL) {
// trust our own data to be wellformed
if (body.length > maxBodySize) {
if (body.length <= maxBodySize) {
writer = new PrettyPrintingLocalHttpMessageBodyWriter(body);
} else {
truncated = body.length - maxBodySize;

writer = new PrettyPrintingLocalMaxSizeHttpMessageBodyWriter(jsonFactory, body,
maxBodySize);
}
} else {
HttpMessageStateSupplier httpMessageStateSupplier = requestHttpMessageStateSupplierSource.get();
if (body.length > maxBodySize) {
writer = new PrettyPrintingRemoteHttpMessageBodyWriter(jsonFactory, body,
httpMessageStateSupplier);
if (body.length <= maxBodySize) {
writer = new PrettyPrintingRemoteHttpMessageBodyWriter(jsonFactory, body, httpMessageStateSupplier);
} else {
writer = new PrettyPrintingRemoteMaxSizeHttpMessageBodyWriter(jsonFactory, body, maxSize,
truncated = body.length - maxBodySize;

writer = new PrettyPrintingRemoteMaxSizeHttpMessageBodyWriter(jsonFactory, body, maxBodySize,
httpMessageStateSupplier);
}
}
Expand All @@ -114,8 +116,10 @@ public Marker createRequestMarker(HttpRequest request) {
String bodyAsString = request.getBodyAsString();
if (bodyAsString != null && bodyAsString.length() > 0) {
if (bodyAsString.length() > maxBodySize) {
String truncated = bodyAsString.substring(0, maxBodySize);
writer = new StringHttpMessageBodyWriter(truncated);
truncated = bodyAsString.length() - maxBodySize;

String truncatedString = bodyAsString.substring(0, maxBodySize);
writer = new StringHttpMessageBodyWriter(truncatedString);
} else {
writer = new StringHttpMessageBodyWriter(bodyAsString);
}
Expand All @@ -125,32 +129,37 @@ public Marker createRequestMarker(HttpRequest request) {
}
}

return new RequestOndemandSingleFieldAppendingMarker(request, writer);
return new RequestOndemandSingleFieldAppendingMarker(request, writer, truncated);
}

public Marker createResponseMarker(Correlation correlation, HttpResponse response) {
public RequestResponseSingleFieldAppendingMarker createResponseMarker(Correlation correlation, HttpResponse response) {

HttpMessageBodyWriter writer = EmptyHttpMessageBodyWriter.INSTANCE;

int truncated = -1;
if (ContentType.isJsonMediaType(response.getContentType())) {
try {
byte[] body = response.getBody();
if (body != null && body.length > 0) {
if (response.getOrigin() == Origin.LOCAL) {
// trust our own data to be wellformed
if (body.length > maxBodySize) {
if (body.length <= maxBodySize) {
writer = new PrettyPrintingLocalHttpMessageBodyWriter(body);
} else {
truncated = body.length - maxBodySize;

writer = new PrettyPrintingLocalMaxSizeHttpMessageBodyWriter(jsonFactory, body,
maxBodySize);
}
} else {
HttpMessageStateSupplier httpMessageStateSupplier = responseHttpMessageStateSupplierSource
.get();
if (body.length > maxBodySize) {
if (body.length <= maxBodySize) {
writer = new PrettyPrintingRemoteHttpMessageBodyWriter(jsonFactory, body,
httpMessageStateSupplier);
} else {
truncated = body.length - maxBodySize;

writer = new PrettyPrintingRemoteMaxSizeHttpMessageBodyWriter(jsonFactory, body, maxSize,
httpMessageStateSupplier);
}
Expand All @@ -165,8 +174,10 @@ public Marker createResponseMarker(Correlation correlation, HttpResponse respons
String bodyAsString = response.getBodyAsString();
if (bodyAsString != null && bodyAsString.length() > 0) {
if (bodyAsString.length() > maxBodySize) {
String truncated = bodyAsString.substring(0, maxBodySize);
writer = new StringHttpMessageBodyWriter(truncated);
truncated = bodyAsString.length() - maxBodySize;

String truncatedString = bodyAsString.substring(0, maxBodySize);
writer = new StringHttpMessageBodyWriter(truncatedString);
} else {
writer = new StringHttpMessageBodyWriter(bodyAsString);
}
Expand All @@ -176,7 +187,7 @@ public Marker createResponseMarker(Correlation correlation, HttpResponse respons
}
}

return new ResponseOndemandSingleFieldAppendingMarker(response, correlation.getDuration(), writer);
return new ResponseOndemandSingleFieldAppendingMarker(response, correlation.getDuration(), writer, truncated);
}

}
Loading
Loading