|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin.httpClient.v4; |
| 20 | + |
| 21 | +import static org.hamcrest.CoreMatchers.is; |
| 22 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 23 | +import static org.mockito.ArgumentMatchers.anyString; |
| 24 | +import static org.mockito.Mockito.never; |
| 25 | +import static org.mockito.Mockito.times; |
| 26 | +import static org.mockito.Mockito.verify; |
| 27 | +import static org.mockito.Mockito.when; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import org.apache.http.HttpHost; |
| 33 | +import org.apache.http.HttpResponse; |
| 34 | +import org.apache.http.ProtocolVersion; |
| 35 | +import org.apache.http.RequestLine; |
| 36 | +import org.apache.http.StatusLine; |
| 37 | +import org.apache.http.client.methods.HttpGet; |
| 38 | +import org.apache.skywalking.apm.agent.core.boot.ServiceManager; |
| 39 | +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; |
| 40 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; |
| 41 | +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; |
| 42 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; |
| 43 | +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; |
| 44 | +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; |
| 45 | +import org.apache.skywalking.apm.plugin.httpclient.HttpClientPluginConfig; |
| 46 | +import org.junit.Before; |
| 47 | +import org.junit.Rule; |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.runner.RunWith; |
| 50 | +import org.mockito.Mock; |
| 51 | +import org.mockito.junit.MockitoJUnit; |
| 52 | +import org.mockito.junit.MockitoRule; |
| 53 | + |
| 54 | +/** |
| 55 | + * Verifies that requests to ports listed in |
| 56 | + * {@link HttpClientPluginConfig.Plugin.HttpClient#PROPAGATION_EXCLUDE_PORTS} |
| 57 | + * are silently skipped (no span created, no sw8 header injected). |
| 58 | + */ |
| 59 | +@RunWith(TracingSegmentRunner.class) |
| 60 | +public class HttpClientPropagationExcludePortTest { |
| 61 | + |
| 62 | + @SegmentStoragePoint |
| 63 | + private SegmentStorage segmentStorage; |
| 64 | + |
| 65 | + @Rule |
| 66 | + public AgentServiceRule agentServiceRule = new AgentServiceRule(); |
| 67 | + @Rule |
| 68 | + public MockitoRule rule = MockitoJUnit.rule(); |
| 69 | + |
| 70 | + @Mock |
| 71 | + private HttpHost clickHouseHost; |
| 72 | + @Mock |
| 73 | + private HttpHost regularHost; |
| 74 | + @Mock |
| 75 | + private HttpGet request; |
| 76 | + @Mock |
| 77 | + private HttpResponse httpResponse; |
| 78 | + @Mock |
| 79 | + private StatusLine statusLine; |
| 80 | + @Mock |
| 81 | + private EnhancedInstance enhancedInstance; |
| 82 | + |
| 83 | + private HttpClientExecuteInterceptor interceptor; |
| 84 | + |
| 85 | + private Object[] clickHouseArgs; |
| 86 | + private Object[] regularArgs; |
| 87 | + private Class<?>[] argumentsType; |
| 88 | + |
| 89 | + @Before |
| 90 | + public void setUp() throws Exception { |
| 91 | + ServiceManager.INSTANCE.boot(); |
| 92 | + |
| 93 | + HttpClientPluginConfig.Plugin.HttpClient.PROPAGATION_EXCLUDE_PORTS = "8123"; |
| 94 | + interceptor = new HttpClientExecuteInterceptor(); |
| 95 | + |
| 96 | + when(statusLine.getStatusCode()).thenReturn(200); |
| 97 | + when(httpResponse.getStatusLine()).thenReturn(statusLine); |
| 98 | + |
| 99 | + RequestLine requestLine = new RequestLine() { |
| 100 | + @Override |
| 101 | + public String getMethod() { |
| 102 | + return "GET"; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public ProtocolVersion getProtocolVersion() { |
| 107 | + return new ProtocolVersion("http", 1, 1); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getUri() { |
| 112 | + return "http://my-service:8080/api/ping"; |
| 113 | + } |
| 114 | + }; |
| 115 | + when(request.getRequestLine()).thenReturn(requestLine); |
| 116 | + |
| 117 | + when(clickHouseHost.getHostName()).thenReturn("clickhouse-server"); |
| 118 | + when(clickHouseHost.getSchemeName()).thenReturn("http"); |
| 119 | + when(clickHouseHost.getPort()).thenReturn(8123); |
| 120 | + |
| 121 | + when(regularHost.getHostName()).thenReturn("my-service"); |
| 122 | + when(regularHost.getSchemeName()).thenReturn("http"); |
| 123 | + when(regularHost.getPort()).thenReturn(8080); |
| 124 | + |
| 125 | + clickHouseArgs = new Object[]{clickHouseHost, request}; |
| 126 | + regularArgs = new Object[]{regularHost, request}; |
| 127 | + argumentsType = new Class[]{HttpHost.class, HttpGet.class}; |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void requestToExcludedPort_noSpanAndNoHeaderInjected() throws Throwable { |
| 132 | + interceptor.beforeMethod(enhancedInstance, null, clickHouseArgs, argumentsType, null); |
| 133 | + interceptor.afterMethod(enhancedInstance, null, clickHouseArgs, argumentsType, httpResponse); |
| 134 | + |
| 135 | + List<TraceSegment> segments = segmentStorage.getTraceSegments(); |
| 136 | + assertThat(segments.size(), is(0)); |
| 137 | + verify(request, never()).setHeader(anyString(), anyString()); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void requestToRegularPort_spanCreatedAndHeadersInjected() throws Throwable { |
| 142 | + interceptor.beforeMethod(enhancedInstance, null, regularArgs, argumentsType, null); |
| 143 | + interceptor.afterMethod(enhancedInstance, null, regularArgs, argumentsType, httpResponse); |
| 144 | + |
| 145 | + List<TraceSegment> segments = segmentStorage.getTraceSegments(); |
| 146 | + assertThat(segments.size(), is(1)); |
| 147 | + verify(request, times(3)).setHeader(anyString(), anyString()); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void whenExcludePortsEmpty_allPortsAreTraced() throws Throwable { |
| 152 | + HttpClientPluginConfig.Plugin.HttpClient.PROPAGATION_EXCLUDE_PORTS = ""; |
| 153 | + |
| 154 | + HttpClientExecuteInterceptor freshInterceptor = new HttpClientExecuteInterceptor(); |
| 155 | + freshInterceptor.beforeMethod(enhancedInstance, null, clickHouseArgs, argumentsType, null); |
| 156 | + freshInterceptor.afterMethod(enhancedInstance, null, clickHouseArgs, argumentsType, httpResponse); |
| 157 | + |
| 158 | + List<TraceSegment> segments = segmentStorage.getTraceSegments(); |
| 159 | + assertThat(segments.size(), is(1)); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void multipleExcludedPorts_allSkippedAndNonExcludedStillTraced() throws Throwable { |
| 164 | + HttpClientPluginConfig.Plugin.HttpClient.PROPAGATION_EXCLUDE_PORTS = "8123,9200"; |
| 165 | + |
| 166 | + HttpClientExecuteInterceptor freshInterceptor = new HttpClientExecuteInterceptor(); |
| 167 | + |
| 168 | + // 8123 should be excluded |
| 169 | + freshInterceptor.beforeMethod(enhancedInstance, null, clickHouseArgs, argumentsType, null); |
| 170 | + freshInterceptor.afterMethod(enhancedInstance, null, clickHouseArgs, argumentsType, httpResponse); |
| 171 | + assertThat(segmentStorage.getTraceSegments().size(), is(0)); |
| 172 | + |
| 173 | + // 9200 should also be excluded |
| 174 | + HttpHost esHost = mock(HttpHost.class); |
| 175 | + when(esHost.getHostName()).thenReturn("es-server"); |
| 176 | + when(esHost.getSchemeName()).thenReturn("http"); |
| 177 | + when(esHost.getPort()).thenReturn(9200); |
| 178 | + Object[] esArgs = new Object[]{esHost, request}; |
| 179 | + |
| 180 | + freshInterceptor = new HttpClientExecuteInterceptor(); |
| 181 | + HttpClientPluginConfig.Plugin.HttpClient.PROPAGATION_EXCLUDE_PORTS = "8123,9200"; |
| 182 | + freshInterceptor.beforeMethod(enhancedInstance, null, esArgs, argumentsType, null); |
| 183 | + freshInterceptor.afterMethod(enhancedInstance, null, esArgs, argumentsType, httpResponse); |
| 184 | + assertThat(segmentStorage.getTraceSegments().size(), is(0)); |
| 185 | + |
| 186 | + // 8080 should still be traced |
| 187 | + freshInterceptor = new HttpClientExecuteInterceptor(); |
| 188 | + HttpClientPluginConfig.Plugin.HttpClient.PROPAGATION_EXCLUDE_PORTS = "8123,9200"; |
| 189 | + freshInterceptor.beforeMethod(enhancedInstance, null, regularArgs, argumentsType, null); |
| 190 | + freshInterceptor.afterMethod(enhancedInstance, null, regularArgs, argumentsType, httpResponse); |
| 191 | + assertThat(segmentStorage.getTraceSegments().size(), is(1)); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void excludedPort_handleMethodExceptionSkipped() throws Throwable { |
| 196 | + interceptor.beforeMethod(enhancedInstance, null, clickHouseArgs, argumentsType, null); |
| 197 | + interceptor.handleMethodException(enhancedInstance, null, clickHouseArgs, argumentsType, new RuntimeException("test")); |
| 198 | + interceptor.afterMethod(enhancedInstance, null, clickHouseArgs, argumentsType, httpResponse); |
| 199 | + |
| 200 | + List<TraceSegment> segments = segmentStorage.getTraceSegments(); |
| 201 | + assertThat(segments.size(), is(0)); |
| 202 | + } |
| 203 | +} |
0 commit comments