Skip to content

Commit

Permalink
Merge pull request #17 from lsd-consulting/#16
Browse files Browse the repository at this point in the history
#16 Log messaging headers
  • Loading branch information
lukasz-gryzbon authored Mar 7, 2023
2 parents 155d6a8 + c4b2ad6 commit e5b4b5c
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ allprojects {
plugins.withId('java') {
dependencies {
implementation 'io.github.lsd-consulting:lsd-core:1.2.6'
implementation 'io.github.lsd-consulting:lsd-formatting-library:1.0.6'
implementation 'io.github.lsd-consulting:lsd-formatting-library:1.0.7'

implementation 'org.projectlombok:lombok:1.18.22'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.lsdconsulting.interceptors.http.common;
package io.lsdconsulting.interceptors.common;

import java.util.Collection;
import java.util.Map;
Expand All @@ -11,7 +11,7 @@ public enum HeaderKeys {
SOURCE_NAME("Source-Name"),
TARGET_NAME("Target-Name");

private String headerName;
private final String headerName;

HeaderKeys(String headerName) {
this.headerName = headerName;
Expand All @@ -24,7 +24,7 @@ public String key() {

public static Map<String, String> singleValueMap(Map<String, ? extends Collection<String>> headers) {
return headers.entrySet().stream()
.collect(toMap(entry -> entry.getKey(), entry -> entry.getValue().stream()
.collect(toMap(Map.Entry::getKey, entry -> entry.getValue().stream()
.findFirst()
.orElse("")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import feign.Request;
import feign.Response;
import feign.Util;
import io.lsdconsulting.interceptors.http.common.Headers;
import io.lsdconsulting.interceptors.common.Headers;
import io.lsdconsulting.interceptors.http.common.HttpInteractionHandler;
import org.springframework.http.HttpStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.lsdconsulting.interceptors.http;

import io.lsdconsulting.interceptors.http.common.Headers;
import io.lsdconsulting.interceptors.common.Headers;
import io.lsdconsulting.interceptors.http.common.HttpInteractionHandler;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.lsd.LsdContext;
import com.lsd.diagram.ValidComponentName;
import com.lsd.events.Markup;
import io.lsdconsulting.interceptors.common.Headers;
import io.lsdconsulting.interceptors.http.naming.DestinationNameMappings;
import io.lsdconsulting.interceptors.http.naming.SourceNameMappings;
import lombok.EqualsAndHashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptor;

import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME;
import static io.lsdconsulting.interceptors.messaging.HtmlRenderer.renderHtmlFor;
import static io.lsdconsulting.interceptors.messaging.TypeConverter.convertToString;
import static lsd.format.PrettyPrinter.prettyPrint;

Expand All @@ -23,7 +24,7 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
String source = convertToString(message.getHeaders().get(SOURCE_NAME.key()));
String target = convertToString(message.getHeaders().get(TARGET_NAME.key()));

lsdContext.capture("Consume event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), payload);
lsdContext.capture("Consume event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), renderHtmlFor(message.getHeaders(), payload));

return message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.ChannelInterceptor;

import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME;
import static io.lsdconsulting.interceptors.messaging.HtmlRenderer.renderHtmlFor;
import static io.lsdconsulting.interceptors.messaging.TypeConverter.convertToString;
import static lsd.format.PrettyPrinter.prettyPrint;

Expand All @@ -23,7 +24,7 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
String source = convertToString(message.getHeaders().get(SOURCE_NAME.key()));
String target = convertToString(message.getHeaders().get(TARGET_NAME.key()));

lsdContext.capture("Publish event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), payload);
lsdContext.capture("Publish event from " + ValidComponentName.of(source) + " to " + ValidComponentName.of(target), renderHtmlFor(message.getHeaders(), payload));

return message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.lsdconsulting.interceptors.messaging;

import org.springframework.messaging.MessageHeaders;

import static j2html.TagCreator.*;
import static java.lang.System.lineSeparator;
import static java.util.stream.Collectors.joining;
import static org.apache.commons.lang3.StringUtils.isEmpty;

public class HtmlRenderer {
public static String renderHtmlFor(MessageHeaders messageHeaders, String prettyBody) {
return p(
p(h4("Message Headers"), code(prettyPrintHeaders(messageHeaders)))
, isEmpty(prettyBody)
? p()
: p(h4("Body"), code(prettyBody)
)
).render();
}

private static String prettyPrintHeaders(MessageHeaders requestHeaders) {
return requestHeaders.entrySet().stream().map(entry ->
entry.getKey() + ": " + entry.getValue())
.collect(joining(lineSeparator()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.lsdconsulting.interceptors.http.common;

import com.lsd.LsdContext;
import io.lsdconsulting.interceptors.common.Headers;
import io.lsdconsulting.interceptors.http.naming.DestinationNameMappings;
import io.lsdconsulting.interceptors.http.naming.SourceNameMappings;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;

import java.util.Map;

import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
Expand All @@ -30,14 +31,17 @@ void logInteraction() {
given(message.getPayload()).willReturn("{\"key\":\"value\"}".getBytes(UTF_8));
given(message.getHeaders()).willReturn(new MessageHeaders(Map.of(SOURCE_NAME.key(), "Source", TARGET_NAME.key(), "Target")));

underTest.preSend(message, null);
underTest.preSend(message, mock(MessageChannel.class));

verify(lsdContext).capture(patternCaptor.capture(), payloadCaptor.capture());
assertThat(patternCaptor.getValue()).isEqualTo("Consume event from Source to Target");
assertThat(payloadCaptor.getValue()).isEqualTo(
"{\n" +
" \"key\": \"value\"\n" +
"}"
"<p><p><h4>Message Headers</h4><code>Target-Name: Target\n" +
"Source-Name: Source\n" +
"id: " + message.getHeaders().get("id") + "\n" +
"timestamp: " + message.getHeaders().get("timestamp") + "</code></p><p><h4>Body</h4><code>{\n" +
" &quot;key&quot;: &quot;value&quot;\n" +
"}</code></p></p>"
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;

import java.util.Map;

import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.http.common.Headers.HeaderKeys.TARGET_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.SOURCE_NAME;
import static io.lsdconsulting.interceptors.common.Headers.HeaderKeys.TARGET_NAME;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
Expand All @@ -30,14 +31,17 @@ void logInteraction() {
given(message.getPayload()).willReturn("{\"key\":\"value\"}".getBytes(UTF_8));
given(message.getHeaders()).willReturn(new MessageHeaders(Map.of(SOURCE_NAME.key(), "Source", TARGET_NAME.key(), "Target")));

underTest.preSend(message, null);
underTest.preSend(message, mock(MessageChannel.class));

verify(lsdContext).capture(patternCaptor.capture(), payloadCaptor.capture());
assertThat(patternCaptor.getValue()).isEqualTo("Publish event from Source to Target");
assertThat(payloadCaptor.getValue()).isEqualTo(
"{\n" +
" \"key\": \"value\"\n" +
"}"
"<p><p><h4>Message Headers</h4><code>Target-Name: Target\n" +
"Source-Name: Source\n" +
"id: " + message.getHeaders().get("id") + "\n" +
"timestamp: " + message.getHeaders().get("timestamp") + "</code></p><p><h4>Body</h4><code>{\n" +
" &quot;key&quot;: &quot;value&quot;\n" +
"}</code></p></p>"
);
}
}

0 comments on commit e5b4b5c

Please sign in to comment.