Skip to content

Commit

Permalink
fixing correlation id set if not available
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenag committed Sep 4, 2017
1 parent 4772292 commit a695b0e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;

import org.neo4j.kernel.api.exceptions.Status;
Expand All @@ -46,7 +46,7 @@ class BoltMessageLoggerImpl implements BoltMessageLogger
private final String remoteAddress;
private Channel channel;

public static final String BOLT_X_CORRELATION_ID_HEADER = "Bolt-X-CorrelationId";
private static final String BOLT_X_CORRELATION_ID_HEADER = "Bolt-X-CorrelationId";
public static final AttributeKey<String> CORRELATION_ATTRIBUTE_KEY = AttributeKey.valueOf(
BOLT_X_CORRELATION_ID_HEADER );

Expand Down Expand Up @@ -156,18 +156,19 @@ public void logIgnored()

private Consumer<String> infoLogger()
{
String correlationId;
if ( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) )
if ( !channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) )
{
String boltXCorrelationIdValue = channel.attr( CORRELATION_ATTRIBUTE_KEY ).get();
correlationId = format( BOLT_X_CORRELATION_ID_HEADER + ": %s", boltXCorrelationIdValue );
if ( StringUtils.isNotBlank( correlationId ) )
{
return formatMessageWithEventName ->
messageLog.info( remoteAddress, formatMessageWithEventName, correlationId );
}
channel.attr( CORRELATION_ATTRIBUTE_KEY ).set( randomCorrelationIdGenerator() );
}
return formatMessageWithEventName -> messageLog.info( remoteAddress, formatMessageWithEventName );

String boltXCorrelationId = channel.attr( CORRELATION_ATTRIBUTE_KEY ).get();
return formatMessageWithEventName ->
messageLog.info( remoteAddress, formatMessageWithEventName, boltXCorrelationId );
}

private String randomCorrelationIdGenerator()
{
return UUID.randomUUID().toString();
}

private BiConsumer<String, String> infoLoggerWithArgs()
Expand All @@ -178,20 +179,14 @@ private BiConsumer<String, String> infoLoggerWithArgs()

private Consumer<String> errorLogger( String errorMessage )
{

String correlationId;
if ( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) )
if ( !channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) )
{
String boltXCorrelationIdValue = channel.attr( CORRELATION_ATTRIBUTE_KEY ).get();
correlationId = format( BOLT_X_CORRELATION_ID_HEADER + ": %s", boltXCorrelationIdValue );
if ( StringUtils.isNotBlank( correlationId ) )
{
return formatMessageWithEventName ->
messageLog.error( remoteAddress, errorMessage, formatMessageWithEventName, correlationId );
}
channel.attr( CORRELATION_ATTRIBUTE_KEY ).set( randomCorrelationIdGenerator() );
}

String boltXCorrelationId = channel.attr( CORRELATION_ATTRIBUTE_KEY ).get();
return formatMessageWithEventName ->
messageLog.error( remoteAddress, errorMessage, formatMessageWithEventName );
messageLog.error( remoteAddress, errorMessage, formatMessageWithEventName, boltXCorrelationId );
}

private BiConsumer<String, String> errorLoggerWithArgs( String errorMessage )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void onRecord( QueryResult.Record item ) throws IOException
@Override
public void onSuccess( MapValue metadata ) throws IOException
{
//TODO: Use supplier
messageLogger.logSuccess( metadata );
packer.packStructHeader( 1, SUCCESS.signature() );
packer.packRawMap( metadata );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.mock;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -35,6 +35,8 @@ public class BoltMessageLoggerImplTest
public void setUp() throws Exception
{
when( channel.remoteAddress() ).thenReturn( new InetSocketAddress( "localhost", 60297 ) );
when( correlationIdAttribute.get() ).thenReturn( "Bolt-X-CorrelationId-1234" );
when( channel.attr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( correlationIdAttribute );
boltMessageLogger = new BoltMessageLoggerImpl( boltMessageLog, channel );
}

Expand All @@ -44,13 +46,12 @@ public void logCorrelationIdClientEvent() throws Exception
// given
when( correlationIdAttribute.get() ).thenReturn( "Bolt-X-CorrelationId-1234" );
when( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( true );
when( channel.attr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( correlationIdAttribute );

// when
boltMessageLogger.clientEvent( "TEST" );

// then
verify( boltMessageLog ).info( "localhost/127.0.0.1:60297", "C: <TEST>", "Bolt-X-CorrelationId: " +
verify( boltMessageLog ).info( "localhost/127.0.0.1:60297", "C: <TEST>",
"Bolt-X-CorrelationId-1234" );
}

Expand All @@ -60,14 +61,12 @@ public void logCorrelationIdClientErrorWithDetails() throws Exception
// given
when( correlationIdAttribute.get() ).thenReturn( "Bolt-X-CorrelationId-1234" );
when( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( true );
when( channel.attr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( correlationIdAttribute );

// when
boltMessageLogger.clientError( "TEST", "errorMessage", () -> "details" );

// then
verify( boltMessageLog ).error( "localhost/127.0.0.1:60297", "errorMessage", "C: <TEST> details",
"Bolt-X-CorrelationId: " +
"Bolt-X-CorrelationId-1234" );
}

Expand All @@ -76,16 +75,13 @@ public void logCorrelationIdClientErrorWithDetails() throws Exception
public void logCorrelationIdServerError() throws Exception
{
// given
when( correlationIdAttribute.get() ).thenReturn( "Bolt-X-CorrelationId-1234" );
when( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( true );
when( channel.attr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( correlationIdAttribute );

// when
boltMessageLogger.serverError( "TEST", "errorMessage" );

// then
verify( boltMessageLog ).error( "localhost/127.0.0.1:60297", "errorMessage", "S: <TEST>",
"Bolt-X-CorrelationId: " +
"Bolt-X-CorrelationId-1234" );
}

Expand All @@ -101,24 +97,38 @@ public void logCorrelationIdClientEventWithDetails() throws Exception
boltMessageLogger.clientEvent( "TEST", () -> "details" );

// then
verify( boltMessageLog ).info( "localhost/127.0.0.1:60297", "C: <TEST> details", "Bolt-X-CorrelationId: " +
verify( boltMessageLog ).info( "localhost/127.0.0.1:60297", "C: <TEST> details",
"Bolt-X-CorrelationId-1234" );
}

@Test
public void doNotLogCorrelationIdIfNotAvailable() throws Exception
public void createCorrelationIdIfNotAvailableInInfoLogger() throws Exception
{
// given

Channel channel = mock( Channel.class );
when( channel.remoteAddress() ).thenReturn( new InetSocketAddress( "localhost", 60297 ) );
BoltMessageLog boltMessageLog = mock( BoltMessageLog.class );
when( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( false );
BoltMessageLoggerImpl boltMessageLogger = new BoltMessageLoggerImpl( boltMessageLog, channel );

// when
boltMessageLogger.clientEvent( "TEST" );

// then
verify( boltMessageLog ).info( "localhost/127.0.0.1:60297", "C: <TEST>" );
verify( correlationIdAttribute ).set( anyString() );
verify( boltMessageLog ).info( "localhost/127.0.0.1:60297", "C: <TEST>", "Bolt-X-CorrelationId-1234" );
}

@Test
public void createCorrelationIdIfNotAvailableInErrorLogger() throws Exception
{
// given
when( channel.hasAttr( CORRELATION_ATTRIBUTE_KEY ) ).thenReturn( false );
BoltMessageLoggerImpl boltMessageLogger = new BoltMessageLoggerImpl( boltMessageLog, channel );

// when
boltMessageLogger.serverError( "TEST", "errorMessage" );

// then
verify( correlationIdAttribute ).set( anyString() );
verify( boltMessageLog ).error( "localhost/127.0.0.1:60297", "errorMessage", "S: <TEST>",
"Bolt-X-CorrelationId-1234" );
}
}

0 comments on commit a695b0e

Please sign in to comment.