|
18 | 18 | import com.newrelic.agent.tracers.ClassMethodSignature;
|
19 | 19 | import com.newrelic.agent.tracers.DefaultTracer;
|
20 | 20 | import com.newrelic.agent.tracers.Tracer;
|
| 21 | +import com.newrelic.agent.transaction.TransactionTimer; |
21 | 22 | import com.newrelic.agent.util.MockDistributedTraceService;
|
22 | 23 | import com.newrelic.api.agent.HeaderType;
|
23 | 24 | import com.newrelic.api.agent.InboundHeaders;
|
24 | 25 | import com.newrelic.test.marker.RequiresFork;
|
25 | 26 | import org.junit.Test;
|
26 | 27 | import org.junit.experimental.categories.Category;
|
| 28 | +import org.mockito.Mockito; |
27 | 29 |
|
28 | 30 | import java.nio.charset.StandardCharsets;
|
29 | 31 | import java.util.Base64;
|
@@ -105,6 +107,97 @@ public void testGetSyntheticsInfoHeader() {
|
105 | 107 | (ImmutableMap.of("X-NewRelic-Synthetics-Info", synthInfoValue), HeaderType.HTTP)));
|
106 | 108 | }
|
107 | 109 |
|
| 110 | + @Test |
| 111 | + public void testInboundParentSampledTrueConfigAlwaysOn() { |
| 112 | + Transaction tx = setupAndCreateTx(DistributedTracingConfig.SAMPLE_ALWAYS_ON, DistributedTracingConfig.SAMPLE_DEFAULT); |
| 113 | + InboundHeaders inboundHeaders = createInboundHeaders(ImmutableMap.of( |
| 114 | + "traceparent", "01-0123456789abcdef0123456789abcdef-0123456789abcdef-01", // last entry is the sampled flag = true |
| 115 | + "tracestate", "trustyrusty@nr=0-0-709288-8599547-f85f42fd82a4cf1d-164d3b4b0d09cb05164d3b4b0d09cb05--0.5-1563574856827" |
| 116 | + ), HeaderType.HTTP); |
| 117 | + |
| 118 | + HeadersUtil.parseAndAcceptDistributedTraceHeaders(tx, inboundHeaders); |
| 119 | + assertTrue(tx.getSpanProxy().getInitiatingW3CTraceParent().sampled()); |
| 120 | + assertTrue(tx.getPriority() == 2.0f); |
| 121 | + |
| 122 | + Transaction.clearTransaction(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testInboundParentSampledTrueConfigAlwaysOff() { |
| 127 | + Transaction tx = setupAndCreateTx(DistributedTracingConfig.SAMPLE_ALWAYS_OFF, DistributedTracingConfig.SAMPLE_DEFAULT); |
| 128 | + InboundHeaders inboundHeaders = createInboundHeaders(ImmutableMap.of( |
| 129 | + "traceparent", "01-0123456789abcdef0123456789abcdef-0123456789abcdef-01", // last entry is the sampled flag = true |
| 130 | + "tracestate", "trustyrusty@nr=0-0-709288-8599547-f85f42fd82a4cf1d-164d3b4b0d09cb05164d3b4b0d09cb05--0.5-1563574856827" |
| 131 | + ), HeaderType.HTTP); |
| 132 | + |
| 133 | + HeadersUtil.parseAndAcceptDistributedTraceHeaders(tx, inboundHeaders); |
| 134 | + assertTrue(tx.getSpanProxy().getInitiatingW3CTraceParent().sampled()); |
| 135 | + assertTrue(tx.getPriority() == 0.0f); |
| 136 | + |
| 137 | + Transaction.clearTransaction(); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testInboundParentSampledFalseConfigAlwaysOn() { |
| 142 | + Transaction tx = setupAndCreateTx(DistributedTracingConfig.SAMPLE_DEFAULT, DistributedTracingConfig.SAMPLE_ALWAYS_ON); |
| 143 | + InboundHeaders inboundHeaders = createInboundHeaders(ImmutableMap.of( |
| 144 | + "traceparent", "01-0123456789abcdef0123456789abcdef-0123456789abcdef-00", // last entry is the sampled flag = true |
| 145 | + "tracestate", "trustyrusty@nr=0-0-709288-8599547-f85f42fd82a4cf1d-164d3b4b0d09cb05164d3b4b0d09cb05--0.5-1563574856827" |
| 146 | + ), HeaderType.HTTP); |
| 147 | + |
| 148 | + HeadersUtil.parseAndAcceptDistributedTraceHeaders(tx, inboundHeaders); |
| 149 | + assertFalse(tx.getSpanProxy().getInitiatingW3CTraceParent().sampled()); |
| 150 | + assertTrue(tx.getPriority() == 2.0f); |
| 151 | + |
| 152 | + Transaction.clearTransaction(); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testInboundParentSampledFalseConfigAlwaysOff() { |
| 157 | + Transaction tx = setupAndCreateTx(DistributedTracingConfig.SAMPLE_DEFAULT, DistributedTracingConfig.SAMPLE_ALWAYS_OFF); |
| 158 | + InboundHeaders inboundHeaders = createInboundHeaders(ImmutableMap.of( |
| 159 | + "traceparent", "01-0123456789abcdef0123456789abcdef-0123456789abcdef-00", // last entry is the sampled flag = true |
| 160 | + "tracestate", "trustyrusty@nr=0-0-709288-8599547-f85f42fd82a4cf1d-164d3b4b0d09cb05164d3b4b0d09cb05--0.5-1563574856827" |
| 161 | + ), HeaderType.HTTP); |
| 162 | + |
| 163 | + HeadersUtil.parseAndAcceptDistributedTraceHeaders(tx, inboundHeaders); |
| 164 | + assertFalse(tx.getSpanProxy().getInitiatingW3CTraceParent().sampled()); |
| 165 | + assertTrue(tx.getPriority() == 0.0f); |
| 166 | + |
| 167 | + Transaction.clearTransaction(); |
| 168 | + } |
| 169 | + |
| 170 | + private Transaction setupAndCreateTx(String remoteParentSampled, String remoteParentNotSampled) { |
| 171 | + System.out.println("Setting up config: "+remoteParentSampled+"; "+remoteParentNotSampled); |
| 172 | + ConfigService mockConfigService = new MockConfigService(AgentConfigImpl.createAgentConfig( |
| 173 | + ImmutableMap.of( |
| 174 | + AgentConfigImpl.APP_NAME, |
| 175 | + "Unit Test", |
| 176 | + AgentConfigImpl.DISTRIBUTED_TRACING, |
| 177 | + ImmutableMap.of( |
| 178 | + DistributedTracingConfig.ENABLED, true, |
| 179 | + DistributedTracingConfig.SAMPLER, |
| 180 | + ImmutableMap.of( |
| 181 | + DistributedTracingConfig.REMOTE_PARENT_SAMPLED, remoteParentSampled, |
| 182 | + DistributedTracingConfig.REMOTE_PARENT_NOT_SAMPLED, remoteParentNotSampled) |
| 183 | + ), |
| 184 | + AgentConfigImpl.SPAN_EVENTS, |
| 185 | + ImmutableMap.of( |
| 186 | + SpanEventsConfig.ENABLED, true, |
| 187 | + SpanEventsConfig.COLLECT_SPAN_EVENTS, true) |
| 188 | + ))); |
| 189 | + MockServiceManager mockServiceManager = new MockServiceManager(mockConfigService); |
| 190 | + mockServiceManager.setDistributedTraceService(new MockDistributedTraceService()); |
| 191 | + ServiceFactory.setServiceManager(mockServiceManager); |
| 192 | + Transaction tx = Mockito.spy(Transaction.getTransaction()); |
| 193 | + TransactionTimer timer = Mockito.mock(TransactionTimer.class); |
| 194 | + Mockito.when(timer.getStartTimeInNanos()).thenReturn(System.nanoTime()); |
| 195 | + Mockito.when(tx.getTransactionTimer()).thenReturn(timer); |
| 196 | + tx.setPriorityIfNotNull(0.5f); // something > 0 and < 1 |
| 197 | + |
| 198 | + return tx; |
| 199 | + } |
| 200 | + |
108 | 201 | private InboundHeaders createInboundHeaders(final Map<String, String> map, final HeaderType type) {
|
109 | 202 | return new InboundHeaders() {
|
110 | 203 | @Override
|
|
0 commit comments