3030
3131package com .google .api .gax .tracing ;
3232
33+ import static org .junit .jupiter .api .Assertions .assertEquals ;
34+ import static org .junit .jupiter .api .Assertions .assertTrue ;
35+
3336import com .google .api .gax .logging .TestLogger ;
37+ import com .google .api .gax .rpc .ApiExceptionFactory ;
38+ import com .google .api .gax .rpc .ErrorDetails ;
39+ import com .google .api .gax .rpc .StatusCode ;
40+ import com .google .api .gax .rpc .testing .FakeStatusCode ;
41+ import com .google .protobuf .Any ;
42+ import com .google .rpc .ErrorInfo ;
43+ import java .util .Collections ;
44+ import java .util .Map ;
3445import org .junit .jupiter .api .BeforeEach ;
3546import org .junit .jupiter .api .Test ;
3647import org .slf4j .LoggerFactory ;
@@ -42,6 +53,9 @@ class LoggingTracerTest {
4253 @ BeforeEach
4354 void setUp () {
4455 testLogger = (TestLogger ) LoggerFactory .getLogger (LoggingTracer .class );
56+ testLogger .getMessageList ().clear ();
57+ testLogger .getMDCMap ().clear ();
58+ testLogger .getKeyValuePairsMap ().clear ();
4559 }
4660
4761 @ Test
@@ -51,10 +65,79 @@ void testAttemptFailed_LogsError() {
5165
5266 // Call attemptFailed with a generic exception
5367 Exception error = new RuntimeException ("generic failure" );
54- tracer .attemptFailed (error , org .threeten .bp .Duration .ZERO );
68+ tracer .attemptFailedDuration (error , java .time .Duration .ZERO );
69+
70+ assertEquals (1 , testLogger .getMessageList ().size ());
71+ assertEquals ("generic failure" , testLogger .getMessageList ().get (0 ));
72+ }
73+
74+ @ Test
75+ void testAttemptFailed_LogsErrorAndAttributes () {
76+ ApiTracerContext context =
77+ ApiTracerContext .empty ().toBuilder ()
78+ .setServiceName ("test-service" )
79+ .setServerAddress ("test.example.com" )
80+ .setServerPort (443 )
81+ .setFullMethodName ("test.service.v1.Method" )
82+ .setTransport (ApiTracerContext .Transport .GRPC )
83+ .build ();
84+ LoggingTracer tracer = new LoggingTracer (context );
85+
86+ ErrorInfo errorInfo =
87+ ErrorInfo .newBuilder ()
88+ .setReason ("TEST_REASON" )
89+ .setDomain ("test.domain.com" )
90+ .putMetadata ("test_key" , "test_value" )
91+ .build ();
92+
93+ ErrorDetails errorDetails =
94+ ErrorDetails .builder ()
95+ .setRawErrorMessages (Collections .singletonList (Any .pack (errorInfo )))
96+ .build ();
97+
98+ Exception error =
99+ ApiExceptionFactory .createException (
100+ "test error message" ,
101+ new RuntimeException ("cause" ),
102+ FakeStatusCode .of (StatusCode .Code .INVALID_ARGUMENT ),
103+ false ,
104+ errorDetails );
105+
106+ tracer .attemptFailedDuration (error , java .time .Duration .ZERO );
107+
108+ assertEquals (1 , testLogger .getMessageList ().size ());
109+ assertEquals ("test error message" , testLogger .getMessageList ().get (0 ));
110+
111+ Map <String , ?> attributesMap ;
112+ // SLF4J 1.x logs using MDC, SLF4J 2.x logs using KeyValuePairs
113+ // Depending on the classpath binding active, one will be populated by TestLogger
114+ if (!testLogger .getMDCMap ().isEmpty ()) {
115+ attributesMap = testLogger .getMDCMap ();
116+ } else {
117+ attributesMap = testLogger .getKeyValuePairsMap ();
118+ }
119+
120+ assertTrue (attributesMap != null && !attributesMap .isEmpty ());
121+ assertEquals (
122+ "test-service" , attributesMap .get (ObservabilityAttributes .GCP_CLIENT_SERVICE_ATTRIBUTE ));
123+ assertEquals (
124+ "test.example.com" , attributesMap .get (ObservabilityAttributes .SERVER_ADDRESS_ATTRIBUTE ));
125+
126+ Object portValue = attributesMap .get (ObservabilityAttributes .SERVER_PORT_ATTRIBUTE );
127+ assertTrue ("443" .equals (String .valueOf (portValue )));
55128
56- // To prevent failing due to disabled logging or other missing context,
57- // we don't strictly assert the contents of the log here if the logger isn't enabled.
58- // The main verification is that calling attemptFailed doesn't throw.
129+ assertEquals (
130+ "test.service.v1.Method" ,
131+ attributesMap .get (ObservabilityAttributes .GRPC_RPC_METHOD_ATTRIBUTE ));
132+ assertEquals ("grpc" , attributesMap .get (ObservabilityAttributes .RPC_SYSTEM_NAME_ATTRIBUTE ));
133+ assertEquals (
134+ "INVALID_ARGUMENT" ,
135+ attributesMap .get (ObservabilityAttributes .RPC_RESPONSE_STATUS_ATTRIBUTE ));
136+ assertEquals ("TEST_REASON" , attributesMap .get (ObservabilityAttributes .ERROR_TYPE_ATTRIBUTE ));
137+ assertEquals (
138+ "test.domain.com" , attributesMap .get (ObservabilityAttributes .ERROR_DOMAIN_ATTRIBUTE ));
139+ assertEquals (
140+ "test_value" ,
141+ attributesMap .get (ObservabilityAttributes .ERROR_METADATA_ATTRIBUTE_PREFIX + "test_key" ));
59142 }
60143}
0 commit comments