22
22
import static org .easymock .EasyMock .expect ;
23
23
import static org .easymock .EasyMock .newCapture ;
24
24
import static org .easymock .EasyMock .replay ;
25
- import static org .junit .Assert .assertEquals ;
26
- import static org .junit .Assert .assertNull ;
25
+ import static org .junit .Assert .*;
27
26
28
27
import com .google .api .core .ApiFutures ;
29
28
import com .google .cloud .MonitoredResource ;
@@ -74,6 +73,7 @@ public class AutoPopulateMetadataTests {
74
73
private static final String FORMATTED_TRACE_ID =
75
74
String .format (LoggingImpl .RESOURCE_NAME_FORMAT , RESOURCE_PROJECT_ID , TRACE_ID );
76
75
private static final String SPAN_ID = "1" ;
76
+ private static final boolean TRACE_SAMPLED = true ;
77
77
78
78
private LoggingRpcFactory mockedRpcFactory ;
79
79
private LoggingRpc mockedRpc ;
@@ -111,22 +111,23 @@ public void teardown() {
111
111
new ContextHandler ().removeCurrentContext ();
112
112
}
113
113
114
- private void mockCurrentContext (HttpRequest request , String traceId , String spanId ) {
114
+ private void mockCurrentContext (HttpRequest request , String traceId , String spanId , boolean traceSampled ) {
115
115
Context mockedContext =
116
- Context .newBuilder ().setRequest (request ).setTraceId (traceId ).setSpanId (spanId ).build ();
116
+ Context .newBuilder ().setRequest (request ).setTraceId (traceId ).setSpanId (spanId ).setTraceSampled ( traceSampled ). build ();
117
117
new ContextHandler ().setCurrentContext (mockedContext );
118
118
}
119
119
120
120
@ Test
121
121
public void testAutoPopulationEnabledInLoggingOptions () {
122
- mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID );
122
+ mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID , TRACE_SAMPLED );
123
123
124
124
logging .write (ImmutableList .of (SIMPLE_LOG_ENTRY ));
125
125
126
126
LogEntry actual = LogEntry .fromPb (rpcWriteArgument .getValue ().getEntries (0 ));
127
127
assertEquals (HTTP_REQUEST , actual .getHttpRequest ());
128
128
assertEquals (FORMATTED_TRACE_ID , actual .getTrace ());
129
129
assertEquals (SPAN_ID , actual .getSpanId ());
130
+ assertEquals (TRACE_SAMPLED , actual .getTraceSampled ());
130
131
assertEquals (RESOURCE , actual .getResource ());
131
132
}
132
133
@@ -136,27 +137,29 @@ public void testAutoPopulationEnabledInWriteOptionsAndDisabledInLoggingOptions()
136
137
LoggingOptions options =
137
138
logging .getOptions ().toBuilder ().setAutoPopulateMetadata (false ).build ();
138
139
logging = options .getService ();
139
- mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID );
140
+ mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID , TRACE_SAMPLED );
140
141
141
142
logging .write (ImmutableList .of (SIMPLE_LOG_ENTRY ), WriteOption .autoPopulateMetadata (true ));
142
143
143
144
LogEntry actual = LogEntry .fromPb (rpcWriteArgument .getValue ().getEntries (0 ));
144
145
assertEquals (HTTP_REQUEST , actual .getHttpRequest ());
145
146
assertEquals (FORMATTED_TRACE_ID , actual .getTrace ());
146
147
assertEquals (SPAN_ID , actual .getSpanId ());
148
+ assertEquals (TRACE_SAMPLED , actual .getTraceSampled ());
147
149
assertEquals (RESOURCE , actual .getResource ());
148
150
}
149
151
150
152
@ Test
151
153
public void testAutoPopulationDisabledInWriteOptions () {
152
- mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID );
154
+ mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID , TRACE_SAMPLED );
153
155
154
156
logging .write (ImmutableList .of (SIMPLE_LOG_ENTRY ), WriteOption .autoPopulateMetadata (false ));
155
157
156
158
LogEntry actual = LogEntry .fromPb (rpcWriteArgument .getValue ().getEntries (0 ));
157
159
assertNull (actual .getHttpRequest ());
158
160
assertNull (actual .getTrace ());
159
161
assertNull (actual .getSpanId ());
162
+ assertFalse (actual .getTraceSampled ());
160
163
assertNull (actual .getResource ());
161
164
}
162
165
@@ -174,7 +177,7 @@ public void testSourceLocationPopulation() {
174
177
175
178
@ Test
176
179
public void testNotFormattedTraceId () {
177
- mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID );
180
+ mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID , TRACE_SAMPLED );
178
181
179
182
final MonitoredResource expectedResource = MonitoredResource .newBuilder ("custom" ).build ();
180
183
@@ -186,7 +189,7 @@ public void testNotFormattedTraceId() {
186
189
187
190
@ Test
188
191
public void testMonitoredResourcePopulationInWriteOptions () {
189
- mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID );
192
+ mockCurrentContext (HTTP_REQUEST , TRACE_ID , SPAN_ID , TRACE_SAMPLED );
190
193
191
194
final MonitoredResource expectedResource = MonitoredResource .newBuilder ("custom" ).build ();
192
195
0 commit comments