From e0a8067476fb88fd9d6e9dabca4575d7078d7579 Mon Sep 17 00:00:00 2001 From: Won Jun Jang Date: Thu, 10 Aug 2017 17:19:51 -0400 Subject: [PATCH 1/5] Add RemoteBaggageRestrictionManager --- idl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idl b/idl index 23f33f2d1..c5adbc983 160000 --- a/idl +++ b/idl @@ -1 +1 @@ -Subproject commit 23f33f2d162607dd4b2177e7ebcfe78d4f9717bc +Subproject commit c5adbc98341c5228472af96ea612c54173a3ec2e From c1759b40153a32751f0048f6dd6d8db7e08aaf1b Mon Sep 17 00:00:00 2001 From: Won Jun Jang Date: Tue, 15 Aug 2017 15:23:12 -0400 Subject: [PATCH 2/5] yay? --- .../BaggageRestrictionManagerProxy.java | 37 +++++ .../HttpBaggageRestrictionManagerProxy.java | 69 +++++++++ .../RemoteBaggageRestrictionManager.java | 141 ++++++++++++++++++ .../http/BaggageRestrictionResponse.java | 31 ++++ .../BaggageRestrictionManagerException.java | 35 +++++ .../java/com/uber/jaeger/metrics/Metrics.java | 14 ++ .../jaeger/samplers/HttpSamplingManager.java | 49 ++---- .../java/com/uber/jaeger/utils/Utils.java | 34 +++++ ...ttpBaggageRestrictionManagerProxyTest.java | 100 +++++++++++++ .../RemoteBaggageRestrictionManagerTest.java | 107 +++++++++++++ .../MockAgentResource.java | 12 +- .../samplers/HttpSamplingManagerTest.java | 1 + 12 files changed, 591 insertions(+), 39 deletions(-) create mode 100644 jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageRestrictionManagerProxy.java create mode 100644 jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java create mode 100644 jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java create mode 100644 jaeger-core/src/main/java/com/uber/jaeger/baggage/http/BaggageRestrictionResponse.java create mode 100644 jaeger-core/src/main/java/com/uber/jaeger/exceptions/BaggageRestrictionManagerException.java create mode 100644 jaeger-core/src/test/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxyTest.java create mode 100644 jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java rename jaeger-core/src/test/java/com/uber/jaeger/{samplers => mocks}/MockAgentResource.java (82%) diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageRestrictionManagerProxy.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageRestrictionManagerProxy.java new file mode 100644 index 000000000..c61895567 --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageRestrictionManagerProxy.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.baggage; + +import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; +import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; + +import java.util.List; + +/** + * BaggageRestrictionManagerProxy is an interface for a class that fetches baggage + * restrictions for specific service from a remote source i.e. jaeger-agent. + */ +public interface BaggageRestrictionManagerProxy { + List getBaggageRestrictions(String serviceName) + throws BaggageRestrictionManagerException; +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java new file mode 100644 index 000000000..2701d4d96 --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.baggage; + +import static com.uber.jaeger.utils.Utils.makeGetRequest; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.google.gson.reflect.TypeToken; +import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; +import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; + +import java.lang.reflect.Type; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.List; + +public class HttpBaggageRestrictionManagerProxy implements BaggageRestrictionManagerProxy { + private static final String DEFAULT_HOST_PORT = "localhost:5778"; + private final Gson gson = new Gson(); + private final String hostPort; + + public HttpBaggageRestrictionManagerProxy(String hostPort) { + this.hostPort = hostPort != null ? hostPort : DEFAULT_HOST_PORT; + } + + List parseJson(String json) throws BaggageRestrictionManagerException { + try { + Type listType = new TypeToken>(){}.getType(); + return gson.fromJson(json, listType); + } catch (JsonSyntaxException e) { + throw new BaggageRestrictionManagerException("Cannot deserialize json", e); + } + } + + @Override + public List getBaggageRestrictions(String serviceName) + throws BaggageRestrictionManagerException { + String jsonString; + try { + jsonString = + makeGetRequest("http://" + hostPort + "/baggageRestrictions?service=" + URLEncoder.encode(serviceName, "UTF-8")); + } catch (Exception e) { + throw new BaggageRestrictionManagerException( + "http call to get baggage restriction from local agent failed.", e); + } + return parseJson(jsonString); + } +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java new file mode 100644 index 000000000..59ca9178d --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.baggage; + +import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; +import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; +import com.uber.jaeger.metrics.Metrics; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; + +/** + * NewRestrictionManager returns a BaggageRestrictionManager that polls the agent for the latest + * baggage restrictions. + */ +public class RemoteBaggageRestrictionManager implements BaggageRestrictionManager { + private static final int DEFAULT_REFRESH_INTERVAL_MS = 60000; + private static final int DEFAULT_INITIAL_DELAY_MS = 0; + + private final String serviceName; + private final BaggageRestrictionManagerProxy proxy; + private final Timer pollTimer; + private final Metrics metrics; + private final boolean denyBaggageOnInitializationFailure; + private volatile boolean initialized; + private volatile Map restrictions = new HashMap(); + private final Restriction invalidRestriction; + private final Restriction validRestriction; + + public RemoteBaggageRestrictionManager( + String serviceName, BaggageRestrictionManagerProxy proxy, Metrics metrics, boolean denyBaggageOnInitializationFailure) { + this(serviceName, proxy, metrics, denyBaggageOnInitializationFailure, DEFAULT_REFRESH_INTERVAL_MS); + } + + public RemoteBaggageRestrictionManager( + String serviceName, + BaggageRestrictionManagerProxy proxy, + Metrics metrics, + boolean denyBaggageOnInitializationFailure, + int refreshIntervalMs + ) { + this(serviceName, proxy, metrics, denyBaggageOnInitializationFailure, refreshIntervalMs, DEFAULT_INITIAL_DELAY_MS); + } + + RemoteBaggageRestrictionManager( + String serviceName, + BaggageRestrictionManagerProxy proxy, + Metrics metrics, + boolean denyBaggageOnInitializationFailure, + int refreshIntervalMs, + int initialDelayMs + ) { + this.serviceName = serviceName; + this.proxy = proxy; + this.metrics = metrics; + this.denyBaggageOnInitializationFailure = denyBaggageOnInitializationFailure; + this.initialized = false; + this.invalidRestriction = Restriction.of(false, 0); + this.validRestriction = Restriction.of(true, DEFAULT_MAX_VALUE_LENGTH); + + pollTimer = new Timer(true); // true makes this a daemon thread + pollTimer.schedule( + new TimerTask() { + @Override + public void run() { + updateBaggageRestrictions(); + } + }, + initialDelayMs, + refreshIntervalMs); + } + + public boolean isReady() { + return initialized; + } + + void updateBaggageRestrictions() { + List response; + try { + response = proxy.getBaggageRestrictions(serviceName); + } catch (BaggageRestrictionManagerException e) { + metrics.baggageRestrictionsUpdateFailure.inc(1); + return; + } + + updateBaggageRestrictions(response); + metrics.baggageRestrictionsUpdateSuccess.inc(1); + } + + private void updateBaggageRestrictions(List restrictions) { + Map baggageRestrictions = new HashMap(); + for (BaggageRestrictionResponse restriction : restrictions) { + baggageRestrictions.put(restriction.getBaggageKey(), Restriction.of(true, restriction.getMaxValueLength())); + } + this.restrictions = baggageRestrictions; + initialized = true; + } + + public void close() { + pollTimer.cancel(); + } + + @Override + public Restriction getRestriction(String service, String key) { + if (!initialized) { + if (denyBaggageOnInitializationFailure) { + return invalidRestriction; + } else { + return validRestriction; + } + } + Restriction restriction = this.restrictions.get(key); + if (restriction != null) { + return restriction; + } + return invalidRestriction; + } +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/http/BaggageRestrictionResponse.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/http/BaggageRestrictionResponse.java new file mode 100644 index 000000000..d00626d1f --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/http/BaggageRestrictionResponse.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.baggage.http; + +import lombok.Value; + +@Value +public class BaggageRestrictionResponse { + String baggageKey; + int maxValueLength; +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/exceptions/BaggageRestrictionManagerException.java b/jaeger-core/src/main/java/com/uber/jaeger/exceptions/BaggageRestrictionManagerException.java new file mode 100644 index 000000000..c58dd30e6 --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/exceptions/BaggageRestrictionManagerException.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.exceptions; + +import java.io.IOException; + +public class BaggageRestrictionManagerException extends IOException { + public BaggageRestrictionManagerException(String msg) { + super(msg); + } + + public BaggageRestrictionManagerException(String msg, Throwable cause) { + super(msg, cause); + } +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java b/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java index 44542656d..e02da6d47 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/metrics/Metrics.java @@ -229,4 +229,18 @@ public static Metrics fromStatsReporter(StatsReporter reporter) { @Metric(name = "baggage-truncate") // Number of times baggage was truncated as per baggage restrictions public Counter baggageTruncate; + + @Metric( + name = "baggage-restrictions-update", + tags = {@Tag(key = "result", value = "ok")} + ) + // Number of times baggage restrictions were successfully updated + public Counter baggageRestrictionsUpdateSuccess; + + @Metric( + name = "baggage-restrictions-update", + tags = {@Tag(key = "result", value = "err")} + ) + // Number of times baggage restrictions failed to update + public Counter baggageRestrictionsUpdateFailure; } diff --git a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java index 9ece37a8c..99349e846 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java @@ -26,48 +26,21 @@ import com.google.gson.JsonSyntaxException; import com.uber.jaeger.exceptions.SamplingStrategyErrorException; import com.uber.jaeger.samplers.http.SamplingStrategyResponse; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; +import java.net.URISyntaxException; import java.net.URLEncoder; -import java.nio.charset.Charset; + import lombok.ToString; +import static com.uber.jaeger.utils.Utils.makeGetRequest; + @ToString public class HttpSamplingManager implements SamplingManager { - private static final String defaultSamplingServerHostPort = "localhost:5778"; - private String hostPort = defaultSamplingServerHostPort; - private Gson gson = new Gson(); + private static final String DEFAULT_HOST_PORT = "localhost:5778"; + private final Gson gson = new Gson(); + private final String hostPort; public HttpSamplingManager(String hostPort) { - if (hostPort != null) { - this.hostPort = hostPort; - } - } - - private String makeGetRequest(String urlToRead) throws IOException { - URL url = new URL(urlToRead); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - StringBuilder result = new StringBuilder(); - try { - conn.setRequestMethod("GET"); - BufferedReader rd = - new BufferedReader( - new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"))); - try { - String line; - while ((line = rd.readLine()) != null) { - result.append(line); - } - } finally { - rd.close(); - } - } finally { - conn.disconnect(); - } - return result.toString(); + this.hostPort = hostPort != null ? hostPort : DEFAULT_HOST_PORT; } SamplingStrategyResponse parseJson(String json) { @@ -83,10 +56,10 @@ public SamplingStrategyResponse getSamplingStrategy(String serviceName) throws SamplingStrategyErrorException { String jsonString; try { + // NB. URIBuilder is not thread safe however given the frequency of use jsonString = - makeGetRequest( - "http://" + hostPort + "/?service=" + URLEncoder.encode(serviceName, "UTF-8")); - } catch (IOException e) { + makeGetRequest("http://" + hostPort + "/?service=" + URLEncoder.encode(serviceName, "UTF-8")); + } catch (Exception e) { throw new SamplingStrategyErrorException( "http call to get sampling strategy from local agent failed.", e); } diff --git a/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java b/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java index 1643cb72c..3843d2c39 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java @@ -24,8 +24,16 @@ import com.uber.jaeger.exceptions.EmptyIpException; import com.uber.jaeger.exceptions.NotFourOctetsException; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; import java.net.InetAddress; +import java.net.URI; +import java.net.URL; import java.net.UnknownHostException; +import java.nio.charset.Charset; public class Utils { public static String normalizeBaggageKey(String key) { @@ -66,4 +74,30 @@ public static long uniqueId() { public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); } + + private static final int TIMEOUT_MS = 5000; + + public static String makeGetRequest(String urlToRead) throws IOException { + URL url = new URL(urlToRead); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(TIMEOUT_MS); + StringBuilder result = new StringBuilder(); + try { + conn.setRequestMethod("GET"); + BufferedReader rd = + new BufferedReader( + new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"))); + try { + String line; + while ((line = rd.readLine()) != null) { + result.append(line); + } + } finally { + rd.close(); + } + } finally { + conn.disconnect(); + } + return result.toString(); + } } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxyTest.java b/jaeger-core/src/test/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxyTest.java new file mode 100644 index 000000000..200d84af3 --- /dev/null +++ b/jaeger-core/src/test/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxyTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.baggage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; +import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; +import com.uber.jaeger.mocks.MockAgentResource; + +import java.net.URI; +import java.util.List; +import java.util.Properties; + +import javax.ws.rs.core.Application; + +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.glassfish.jersey.test.TestProperties; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + + +public class HttpBaggageRestrictionManagerProxyTest extends JerseyTest { + + private HttpBaggageRestrictionManagerProxy undertest; + private BaggageRestrictionResponse expectedRestriction = new BaggageRestrictionResponse("key", 10); + + private static Properties originalProps; + + @BeforeClass + public static void beforeClass() { + Properties originalProps = new Properties(System.getProperties()); + if (System.getProperty(TestProperties.CONTAINER_PORT) == null) { + System.setProperty(TestProperties.CONTAINER_PORT, "0"); + } + } + + @AfterClass + public static void afterClass() { + System.setProperties(originalProps); + } + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + undertest = new HttpBaggageRestrictionManagerProxy(null); + } + + @Override + protected Application configure() { + return new ResourceConfig(MockAgentResource.class); + } + + @Test + public void testGetBaggageRestrictions() throws Exception { + URI uri = target().getUri(); + undertest = new HttpBaggageRestrictionManagerProxy(uri.getHost() + ":" + uri.getPort()); + List response = undertest.getBaggageRestrictions("clairvoyant"); + assertNotNull(response); + assertEquals(1, response.size()); + assertEquals(expectedRestriction, response.get(0)); + } + + @Test(expected = BaggageRestrictionManagerException.class) + public void testGetSamplingStrategyError() throws Exception { + URI uri = target().getUri(); + undertest = new HttpBaggageRestrictionManagerProxy(uri.getHost() + ":" + uri.getPort()); + undertest.getBaggageRestrictions(""); + } + + @Test(expected = BaggageRestrictionManagerException.class) + public void testParseInvalidJson() throws Exception { + undertest.parseJson("invalid json"); + } +} diff --git a/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java b/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java new file mode 100644 index 000000000..d2b67bc0d --- /dev/null +++ b/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2017, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.baggage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; +import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; +import com.uber.jaeger.metrics.InMemoryStatsReporter; +import com.uber.jaeger.metrics.Metrics; + +import java.util.ArrayList; +import java.util.Arrays; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class RemoteBaggageRestrictionManagerTest { + @Mock private BaggageRestrictionManagerProxy baggageRestrictionProxy; + private Metrics metrics; + private InMemoryStatsReporter metricsReporter; + private static final String SERVICE_NAME = "service"; + private static final String BAGGAGE_KEY = "key"; + private static final int MAX_VALUE_LENGTH = 10; + private static final BaggageRestrictionResponse RESTRICTION = new BaggageRestrictionResponse(BAGGAGE_KEY, MAX_VALUE_LENGTH); + + private RemoteBaggageRestrictionManager undertest; + + @Before + public void setUp() throws Exception { + metricsReporter = new InMemoryStatsReporter(); + metrics = Metrics.fromStatsReporter(metricsReporter); + undertest = new RemoteBaggageRestrictionManager(SERVICE_NAME, baggageRestrictionProxy, metrics, + false); + } + + @After + public void tearDown() { + undertest.close(); + } + + @Test + public void testUpdateBaggageRestrictions() throws Exception { + when(baggageRestrictionProxy.getBaggageRestrictions(SERVICE_NAME)) + .thenReturn(new ArrayList(Arrays.asList(RESTRICTION))); + undertest.updateBaggageRestrictions(); + + assertEquals(Restriction.of(true, MAX_VALUE_LENGTH), undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY)); + assertFalse(undertest.getRestriction(SERVICE_NAME, "bad-key").isKeyAllowed()); + assertTrue( + metricsReporter.counters.get("jaeger.baggage-restrictions-update.result=ok") > 0L); + } + + @Test + public void testAllowBaggageOnInitializationFailure() throws Exception { + when(baggageRestrictionProxy.getBaggageRestrictions(SERVICE_NAME)) + .thenThrow(new BaggageRestrictionManagerException("error")); + undertest = new RemoteBaggageRestrictionManager(SERVICE_NAME, baggageRestrictionProxy, metrics, + false, 60000, 60000); + + assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); + undertest.updateBaggageRestrictions(); + // If baggage restriction update fails, all baggage should still be allowed. + assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); + assertTrue(metricsReporter.counters.get("jaeger.baggage-restrictions-update.result=err") > 0L); + } + + @Test + public void testDenyBaggageOnInitializationFailure() throws Exception { + when(baggageRestrictionProxy.getBaggageRestrictions(SERVICE_NAME)) + .thenReturn(new ArrayList(Arrays.asList(RESTRICTION))); + undertest = new RemoteBaggageRestrictionManager(SERVICE_NAME, baggageRestrictionProxy, metrics, + true, 60000, 60000); + + assertFalse(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); + undertest.updateBaggageRestrictions(); + assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); + } +} diff --git a/jaeger-core/src/test/java/com/uber/jaeger/samplers/MockAgentResource.java b/jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java similarity index 82% rename from jaeger-core/src/test/java/com/uber/jaeger/samplers/MockAgentResource.java rename to jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java index 8bd0c7b1c..78b295531 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/samplers/MockAgentResource.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/mocks/MockAgentResource.java @@ -20,7 +20,7 @@ * THE SOFTWARE. */ -package com.uber.jaeger.samplers; +package com.uber.jaeger.mocks; import javax.ws.rs.GET; import javax.ws.rs.Path; @@ -42,4 +42,14 @@ public String getServiceSamplingStrategy(@QueryParam("service") String serviceNa throw new WebApplicationException(); } + + @GET + @Path("baggageRestrictions") + @Produces(MediaType.APPLICATION_JSON) + public String getBaggageRestrictions(@QueryParam("service") String serviceName) { + if (serviceName.equals("clairvoyant")) { + return "[{\"baggageKey\":\"key\",\"maxValueLength\":\"10\"}]"; + } + throw new WebApplicationException(); + } } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/samplers/HttpSamplingManagerTest.java b/jaeger-core/src/test/java/com/uber/jaeger/samplers/HttpSamplingManagerTest.java index 190cd0b9e..e33307a04 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/samplers/HttpSamplingManagerTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/samplers/HttpSamplingManagerTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertNull; import com.uber.jaeger.exceptions.SamplingStrategyErrorException; +import com.uber.jaeger.mocks.MockAgentResource; import com.uber.jaeger.samplers.http.OperationSamplingParameters; import com.uber.jaeger.samplers.http.PerOperationSamplingParameters; import com.uber.jaeger.samplers.http.ProbabilisticSamplingStrategy; From d428ecc5df0ec55a24b9bdd3b3b59edfb9e0c908 Mon Sep 17 00:00:00 2001 From: Won Jun Jang Date: Tue, 15 Aug 2017 15:33:18 -0400 Subject: [PATCH 3/5] fixes --- .../baggage/HttpBaggageRestrictionManagerProxy.java | 6 ++++-- .../com/uber/jaeger/samplers/HttpSamplingManager.java | 9 +++++---- .../baggage/RemoteBaggageRestrictionManagerTest.java | 2 ++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java index 2701d4d96..a5cdff6c8 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java @@ -30,6 +30,7 @@ import com.uber.jaeger.baggage.http.BaggageRestrictionResponse; import com.uber.jaeger.exceptions.BaggageRestrictionManagerException; +import java.io.IOException; import java.lang.reflect.Type; import java.net.URLEncoder; import java.util.ArrayList; @@ -59,8 +60,9 @@ public List getBaggageRestrictions(String serviceNam String jsonString; try { jsonString = - makeGetRequest("http://" + hostPort + "/baggageRestrictions?service=" + URLEncoder.encode(serviceName, "UTF-8")); - } catch (Exception e) { + makeGetRequest( + "http://" + hostPort + "/baggageRestrictions?service=" + URLEncoder.encode(serviceName, "UTF-8")); + } catch (IOException e) { throw new BaggageRestrictionManagerException( "http call to get baggage restriction from local agent failed.", e); } diff --git a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java index 99349e846..2b061b1fe 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java @@ -26,7 +26,8 @@ import com.google.gson.JsonSyntaxException; import com.uber.jaeger.exceptions.SamplingStrategyErrorException; import com.uber.jaeger.samplers.http.SamplingStrategyResponse; -import java.net.URISyntaxException; + +import java.io.IOException; import java.net.URLEncoder; import lombok.ToString; @@ -56,10 +57,10 @@ public SamplingStrategyResponse getSamplingStrategy(String serviceName) throws SamplingStrategyErrorException { String jsonString; try { - // NB. URIBuilder is not thread safe however given the frequency of use jsonString = - makeGetRequest("http://" + hostPort + "/?service=" + URLEncoder.encode(serviceName, "UTF-8")); - } catch (Exception e) { + makeGetRequest( + "http://" + hostPort + "/?service=" + URLEncoder.encode(serviceName, "UTF-8")); + } catch (IOException e) { throw new SamplingStrategyErrorException( "http call to get sampling strategy from local agent failed.", e); } diff --git a/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java b/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java index d2b67bc0d..515246ceb 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java @@ -88,6 +88,7 @@ public void testAllowBaggageOnInitializationFailure() throws Exception { assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); undertest.updateBaggageRestrictions(); + assertFalse(undertest.isReady()); // If baggage restriction update fails, all baggage should still be allowed. assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); assertTrue(metricsReporter.counters.get("jaeger.baggage-restrictions-update.result=err") > 0L); @@ -102,6 +103,7 @@ public void testDenyBaggageOnInitializationFailure() throws Exception { assertFalse(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); undertest.updateBaggageRestrictions(); + assertTrue(undertest.isReady()); assertTrue(undertest.getRestriction(SERVICE_NAME, BAGGAGE_KEY).isKeyAllowed()); } } From 1545df33fad028bc9da745d40d1d4930235ce602 Mon Sep 17 00:00:00 2001 From: Won Jun Jang Date: Tue, 15 Aug 2017 15:42:13 -0400 Subject: [PATCH 4/5] lint --- .../jaeger/baggage/RemoteBaggageRestrictionManager.java | 6 +++++- .../java/com/uber/jaeger/samplers/HttpSamplingManager.java | 3 ++- .../jaeger/baggage/RemoteBaggageRestrictionManagerTest.java | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java index 59ca9178d..590f1d2bb 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java @@ -51,7 +51,11 @@ public class RemoteBaggageRestrictionManager implements BaggageRestrictionManage private final Restriction validRestriction; public RemoteBaggageRestrictionManager( - String serviceName, BaggageRestrictionManagerProxy proxy, Metrics metrics, boolean denyBaggageOnInitializationFailure) { + String serviceName, + BaggageRestrictionManagerProxy proxy, + Metrics metrics, + boolean denyBaggageOnInitializationFailure + ) { this(serviceName, proxy, metrics, denyBaggageOnInitializationFailure, DEFAULT_REFRESH_INTERVAL_MS); } diff --git a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java index 2b061b1fe..db3528043 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java @@ -22,6 +22,8 @@ package com.uber.jaeger.samplers; +import static com.uber.jaeger.utils.Utils.makeGetRequest; + import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.uber.jaeger.exceptions.SamplingStrategyErrorException; @@ -32,7 +34,6 @@ import lombok.ToString; -import static com.uber.jaeger.utils.Utils.makeGetRequest; @ToString public class HttpSamplingManager implements SamplingManager { diff --git a/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java b/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java index 515246ceb..be954c13d 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManagerTest.java @@ -50,7 +50,8 @@ public class RemoteBaggageRestrictionManagerTest { private static final String SERVICE_NAME = "service"; private static final String BAGGAGE_KEY = "key"; private static final int MAX_VALUE_LENGTH = 10; - private static final BaggageRestrictionResponse RESTRICTION = new BaggageRestrictionResponse(BAGGAGE_KEY, MAX_VALUE_LENGTH); + private static final BaggageRestrictionResponse RESTRICTION = + new BaggageRestrictionResponse(BAGGAGE_KEY, MAX_VALUE_LENGTH); private RemoteBaggageRestrictionManager undertest; From db31b9c897a91cfd4232857eb3344dff48a51e0c Mon Sep 17 00:00:00 2001 From: Won Jun Jang Date: Mon, 21 Aug 2017 11:50:56 -0400 Subject: [PATCH 5/5] code review --- .../src/main/java/com/uber/jaeger/Span.java | 7 ++- .../uber/jaeger/baggage/BaggageSetter.java | 2 +- .../HttpBaggageRestrictionManagerProxy.java | 2 +- .../RemoteBaggageRestrictionManager.java | 22 +++++++ .../jaeger/samplers/HttpSamplingManager.java | 2 +- .../main/java/com/uber/jaeger/utils/Http.java | 59 +++++++++++++++++++ .../java/com/uber/jaeger/utils/Utils.java | 33 ----------- 7 files changed, 90 insertions(+), 37 deletions(-) create mode 100644 jaeger-core/src/main/java/com/uber/jaeger/utils/Http.java diff --git a/jaeger-core/src/main/java/com/uber/jaeger/Span.java b/jaeger-core/src/main/java/com/uber/jaeger/Span.java index 11aff3b4a..bc7f15670 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/Span.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/Span.java @@ -22,7 +22,6 @@ package com.uber.jaeger; -import com.uber.jaeger.baggage.BaggageSetter; import io.opentracing.tag.Tags; import java.util.ArrayList; import java.util.Collections; @@ -106,6 +105,12 @@ public String getOperationName() { } } + public String getServiceName() { + synchronized (this) { + return this.getTracer().getServiceName(); + } + } + public List getLogs() { synchronized (this) { if (logs == null) { diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageSetter.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageSetter.java index 2405f3654..3f145aa18 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageSetter.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/BaggageSetter.java @@ -58,7 +58,7 @@ public BaggageSetter(BaggageRestrictionManager restrictionManager, Metrics metri * @return the SpanContext with the baggage set */ public SpanContext setBaggage(Span span, String key, String value) { - Restriction restriction = restrictionManager.getRestriction(span.getTracer().getServiceName(), key); + Restriction restriction = restrictionManager.getRestriction(span.getServiceName(), key); boolean truncated = false; String prevItem = null; if (!restriction.isKeyAllowed()) { diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java index a5cdff6c8..89b6ede40 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/HttpBaggageRestrictionManagerProxy.java @@ -22,7 +22,7 @@ package com.uber.jaeger.baggage; -import static com.uber.jaeger.utils.Utils.makeGetRequest; +import static com.uber.jaeger.utils.Http.makeGetRequest; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; diff --git a/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java b/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java index 590f1d2bb..02c4d8eb8 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/baggage/RemoteBaggageRestrictionManager.java @@ -69,6 +69,28 @@ public RemoteBaggageRestrictionManager( this(serviceName, proxy, metrics, denyBaggageOnInitializationFailure, refreshIntervalMs, DEFAULT_INITIAL_DELAY_MS); } + /** + * Creates a RemoteBaggageRestrictionManager that fetches {@link BaggageRestrictionResponse} from a remote + * agent and keeps track of {@link Restriction} for a service. + * + * {@param initialDelayMs} is only exposed for testing purposes so users can determine when the first call to + * remote agent is made. Under normal operations, this RemoteBaggageRestrictionManager will start up and + * asynchronously fetch restrictions. If the user wants to know if restrictions are ready, they can check via + * isReady(). + * + * @param serviceName restrictions for this service are kept track of. + * @param proxy proxy to remote agent. + * @param metrics metrics for metrics emission. + * @param denyBaggageOnInitializationFailure determines the startup failure mode of RemoteBaggageRestrictionManager. + * If DenyBaggageOnInitializationFailure is true, + * RemoteBaggageRestrictionManager will not allow any baggage to be written + * until baggage restrictions have been retrieved from agent. If + * DenyBaggageOnInitializationFailure is false, + * RemoteBaggageRestrictionManager will allow any baggage to be written + * until baggage restrictions have been retrieved from agent. + * @param refreshIntervalMs how often restriction are fetched from remote agent. + * @param initialDelayMs delay before first fetch of restrictions. + */ RemoteBaggageRestrictionManager( String serviceName, BaggageRestrictionManagerProxy proxy, diff --git a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java index db3528043..278ff5c00 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/samplers/HttpSamplingManager.java @@ -22,7 +22,7 @@ package com.uber.jaeger.samplers; -import static com.uber.jaeger.utils.Utils.makeGetRequest; +import static com.uber.jaeger.utils.Http.makeGetRequest; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; diff --git a/jaeger-core/src/main/java/com/uber/jaeger/utils/Http.java b/jaeger-core/src/main/java/com/uber/jaeger/utils/Http.java new file mode 100644 index 000000000..b8140ce23 --- /dev/null +++ b/jaeger-core/src/main/java/com/uber/jaeger/utils/Http.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016, Uber Technologies, Inc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package com.uber.jaeger.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; + +public class Http { + + private static final int TIMEOUT_MS = 5000; + + public static String makeGetRequest(String urlToRead) throws IOException { + URL url = new URL(urlToRead); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(TIMEOUT_MS); + StringBuilder result = new StringBuilder(); + try { + conn.setRequestMethod("GET"); + BufferedReader rd = + new BufferedReader( + new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"))); + try { + String line; + while ((line = rd.readLine()) != null) { + result.append(line); + } + } finally { + rd.close(); + } + } finally { + conn.disconnect(); + } + return result.toString(); + } +} diff --git a/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java b/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java index 3843d2c39..33a057c96 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/utils/Utils.java @@ -25,15 +25,8 @@ import com.uber.jaeger.exceptions.EmptyIpException; import com.uber.jaeger.exceptions.NotFourOctetsException; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; import java.net.InetAddress; -import java.net.URI; -import java.net.URL; import java.net.UnknownHostException; -import java.nio.charset.Charset; public class Utils { public static String normalizeBaggageKey(String key) { @@ -74,30 +67,4 @@ public static long uniqueId() { public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); } - - private static final int TIMEOUT_MS = 5000; - - public static String makeGetRequest(String urlToRead) throws IOException { - URL url = new URL(urlToRead); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setConnectTimeout(TIMEOUT_MS); - StringBuilder result = new StringBuilder(); - try { - conn.setRequestMethod("GET"); - BufferedReader rd = - new BufferedReader( - new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8"))); - try { - String line; - while ((line = rd.readLine()) != null) { - result.append(line); - } - } finally { - rd.close(); - } - } finally { - conn.disconnect(); - } - return result.toString(); - } }