diff --git a/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java b/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java index 494adbe..ee609e8 100644 --- a/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java +++ b/jaeger-core/src/main/java/com/uber/jaeger/Configuration.java @@ -35,8 +35,14 @@ import com.uber.jaeger.senders.HttpSender; import com.uber.jaeger.senders.Sender; import com.uber.jaeger.senders.UdpSender; +import com.uber.jaeger.throttler.HttpThrottlerProxy; +import com.uber.jaeger.throttler.RemoteThrottler; +import com.uber.jaeger.throttler.Throttler; import io.opentracing.propagation.Format; import io.opentracing.propagation.TextMap; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + import java.text.NumberFormat; import java.text.ParseException; import java.util.Arrays; @@ -45,36 +51,32 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import lombok.Getter; -import lombok.extern.slf4j.Slf4j; /** * This class is designed to provide {@link Tracer} or {@link Tracer.Builder} when Jaeger client - * configuration is provided in environmental or property variables. It also simplifies creation - * of the client from configuration files. - * @deprecated use package {@code io.jaegertracing} instead. See https://github.com/jaegertracing/legacy-client-java/issues/13 + * configuration is provided in environmental or property variables. It also simplifies creation of + * the client from configuration files. + * + * @deprecated use package {@code io.jaegertracing} instead. See + * https://github.com/jaegertracing/legacy-client-java/issues/13 */ @Deprecated @Slf4j public class Configuration { - /** - * @deprecated use {@link ProbabilisticSampler#DEFAULT_SAMPLING_PROBABILITY} instead - */ + /** @deprecated use {@link ProbabilisticSampler#DEFAULT_SAMPLING_PROBABILITY} instead */ @Deprecated - public static final double DEFAULT_SAMPLING_PROBABILITY = ProbabilisticSampler.DEFAULT_SAMPLING_PROBABILITY; + public static final double DEFAULT_SAMPLING_PROBABILITY = + ProbabilisticSampler.DEFAULT_SAMPLING_PROBABILITY; - /** - * Prefix for all properties used to configure the Jaeger tracer. - */ + /** Prefix for all properties used to configure the Jaeger tracer. */ public static final String JAEGER_PREFIX = "JAEGER_"; - /** - * The full URL to the {@code traces} endpoint, like https://jaeger-collector:14268/api/traces - */ + /** The full URL to the {@code traces} endpoint, like https://jaeger-collector:14268/api/traces */ public static final String JAEGER_ENDPOINT = JAEGER_PREFIX + "ENDPOINT"; /** - * The Auth Token to be added as "Bearer" on Authorization headers for requests sent to the endpoint + * The Auth Token to be added as "Bearer" on Authorization headers for requests sent to the + * endpoint */ public static final String JAEGER_AUTH_TOKEN = JAEGER_PREFIX + "AUTH_TOKEN"; @@ -88,54 +90,37 @@ public class Configuration { */ public static final String JAEGER_PASSWORD = JAEGER_PREFIX + "PASSWORD"; - /** - * The host name used to locate the agent. - */ + /** The host name used to locate the agent. */ public static final String JAEGER_AGENT_HOST = JAEGER_PREFIX + "AGENT_HOST"; - /** - * The port used to locate the agent. - */ + /** The port used to locate the agent. */ public static final String JAEGER_AGENT_PORT = JAEGER_PREFIX + "AGENT_PORT"; - /** - * Whether the reporter should log the spans. - */ + /** Whether the reporter should log the spans. */ public static final String JAEGER_REPORTER_LOG_SPANS = JAEGER_PREFIX + "REPORTER_LOG_SPANS"; - /** - * The maximum queue size for use when reporting spans remotely. - */ - public static final String JAEGER_REPORTER_MAX_QUEUE_SIZE = JAEGER_PREFIX + "REPORTER_MAX_QUEUE_SIZE"; + /** The maximum queue size for use when reporting spans remotely. */ + public static final String JAEGER_REPORTER_MAX_QUEUE_SIZE = + JAEGER_PREFIX + "REPORTER_MAX_QUEUE_SIZE"; - /** - * The flush interval when reporting spans remotely. - */ - public static final String JAEGER_REPORTER_FLUSH_INTERVAL = JAEGER_PREFIX + "REPORTER_FLUSH_INTERVAL"; + /** The flush interval when reporting spans remotely. */ + public static final String JAEGER_REPORTER_FLUSH_INTERVAL = + JAEGER_PREFIX + "REPORTER_FLUSH_INTERVAL"; - /** - * The sampler type. - */ + /** The sampler type. */ public static final String JAEGER_SAMPLER_TYPE = JAEGER_PREFIX + "SAMPLER_TYPE"; - /** - * The sampler parameter (number). - */ + /** The sampler parameter (number). */ public static final String JAEGER_SAMPLER_PARAM = "JAEGER_SAMPLER_PARAM"; - /** - * The sampler manager host:port. - */ - public static final String JAEGER_SAMPLER_MANAGER_HOST_PORT = JAEGER_PREFIX + "SAMPLER_MANAGER_HOST_PORT"; + /** The sampler manager host:port. */ + public static final String JAEGER_SAMPLER_MANAGER_HOST_PORT = + JAEGER_PREFIX + "SAMPLER_MANAGER_HOST_PORT"; - /** - * The service name. - */ + /** The service name. */ public static final String JAEGER_SERVICE_NAME = JAEGER_PREFIX + "SERVICE_NAME"; - /** - * The tracer level tags. - */ + /** The tracer level tags. */ public static final String JAEGER_TAGS = JAEGER_PREFIX + "TAGS"; /** @@ -144,44 +129,34 @@ public class Configuration { */ public static final String JAEGER_PROPAGATION = JAEGER_PREFIX + "PROPAGATION"; - /** - * The supported trace context propagation formats. - */ + /** The supported trace context propagation formats. */ public enum Propagation { - /** - * The default Jaeger trace context propagation format. - */ + /** The default Jaeger trace context propagation format. */ JAEGER, - /** - * The Zipkin B3 trace context propagation format. - */ + /** The Zipkin B3 trace context propagation format. */ B3 } - /** - * The serviceName that the tracer will use - */ + /** The serviceName that the tracer will use */ private final String serviceName; + private SamplerConfiguration samplerConfig; private ReporterConfiguration reporterConfig; + private ThrottlerConfiguration throttlerConfig; private CodecConfiguration codecConfig; private MetricsFactory metricsFactory; private Map tracerTags; - /** - * lazy singleton Tracer initialized in getTracer() method. - */ + /** lazy singleton Tracer initialized in getTracer() method. */ private Tracer tracer; public Configuration(String serviceName) { this.serviceName = Tracer.Builder.checkValidServiceName(serviceName); } - /** - * @deprecated use {@link #Configuration(String)} and fluent API - */ + /** @deprecated use {@link #Configuration(String)} and fluent API */ @Deprecated public Configuration( String serviceName, @@ -190,9 +165,7 @@ public Configuration( this(serviceName, samplerConfig, reporterConfig, null); } - /** - * @deprecated use {@link #Configuration(String)} and fluent API - */ + /** @deprecated use {@link #Configuration(String)} and fluent API */ @Deprecated private Configuration( String serviceName, @@ -206,9 +179,7 @@ private Configuration( this.metricsFactory = new NoopMetricsFactory(); } - /** - * @return Configuration object from environmental variables - */ + /** @return Configuration object from environmental variables */ public static Configuration fromEnv() { return new Configuration(getProperty(JAEGER_SERVICE_NAME)) .withTracerTags(tracerTagsFromEnv()) @@ -233,11 +204,12 @@ public Tracer.Builder getTracerBuilder() { Metrics metrics = new Metrics(metricsFactory); Reporter reporter = reporterConfig.getReporter(metrics); Sampler sampler = samplerConfig.createSampler(serviceName, metrics); - Tracer.Builder builder = new Tracer.Builder(serviceName) - .withSampler(sampler) - .withReporter(reporter) - .withMetrics(metrics) - .withTags(tracerTags); + Tracer.Builder builder = + new Tracer.Builder(serviceName) + .withSampler(sampler) + .withReporter(reporter) + .withMetrics(metrics) + .withTags(tracerTags); codecConfig.apply(builder); return builder; } @@ -279,9 +251,7 @@ public void setStatsFactory(StatsFactory statsFactory) { this.metricsFactory = statsFactory; } - /** - * @param metricsFactory the MetricsFactory to use on the Tracer to be built - */ + /** @param metricsFactory the MetricsFactory to use on the Tracer to be built */ public void withMetricsFactory(MetricsFactory metricsFactory) { this.metricsFactory = metricsFactory; } @@ -301,6 +271,11 @@ public Configuration withCodec(CodecConfiguration codecConfig) { return this; } + public Configuration withThrottler(ThrottlerConfiguration throttlerConfig) { + this.throttlerConfig = throttlerConfig; + return this; + } + public Configuration withTracerTags(Map tracerTags) { if (tracerTags != null) { this.tracerTags = new HashMap(tracerTags); @@ -316,13 +291,15 @@ public SamplerConfiguration getSampler() { return samplerConfig; } + public ThrottlerConfiguration getThrottler() { + return throttlerConfig; + } + public Map getTracerTags() { return tracerTags == null ? null : Collections.unmodifiableMap(tracerTags); } - /** - * SamplerConfiguration allows to configure which sampler the tracer will use. - */ + /** SamplerConfiguration allows to configure which sampler the tracer will use. */ public static class SamplerConfiguration { /** * The type of sampler to use in the tracer. Optional. Valid values: remote (default), @@ -341,20 +318,15 @@ public static class SamplerConfiguration { */ private String managerHostPort; - public SamplerConfiguration() { - } + public SamplerConfiguration() {} - /** - * @deprecated use {@link #SamplerConfiguration()} and fluent API - */ + /** @deprecated use {@link #SamplerConfiguration()} and fluent API */ @Deprecated public SamplerConfiguration(String type, Number param) { this(type, param, null); } - /** - * @deprecated use {@link #SamplerConfiguration()} and fluent API - */ + /** @deprecated use {@link #SamplerConfiguration()} and fluent API */ @Deprecated public SamplerConfiguration(String type, Number param, String managerHostPort) { this.type = type; @@ -372,8 +344,10 @@ public static SamplerConfiguration fromEnv() { // for tests Sampler createSampler(String serviceName, Metrics metrics) { String samplerType = stringOrDefault(this.getType(), RemoteControlledSampler.TYPE); - Number samplerParam = numberOrDefault(this.getParam(), ProbabilisticSampler.DEFAULT_SAMPLING_PROBABILITY); - String hostPort = stringOrDefault(this.getManagerHostPort(), HttpSamplingManager.DEFAULT_HOST_PORT); + Number samplerParam = + numberOrDefault(this.getParam(), ProbabilisticSampler.DEFAULT_SAMPLING_PROBABILITY); + String hostPort = + stringOrDefault(this.getManagerHostPort(), HttpSamplingManager.DEFAULT_HOST_PORT); if (samplerType.equals(ConstSampler.TYPE)) { return new ConstSampler(samplerParam.intValue() != 0); @@ -426,9 +400,7 @@ public SamplerConfiguration withManagerHostPort(String managerHostPort) { } } - /** - * CodecConfiguration can be used to support additional trace context propagation codec. - */ + /** CodecConfiguration can be used to support additional trace context propagation codec. */ public static class CodecConfiguration { private Map, List>> codecs; @@ -463,7 +435,8 @@ public static CodecConfiguration fromEnv() { return new CodecConfiguration(codecs); } - private static void addCodec(Map, List>> codecs, Format format, Codec codec) { + private static void addCodec( + Map, List>> codecs, Format format, Codec codec) { List> codecList = codecs.get(format); if (codecList == null) { codecList = new LinkedList>(); @@ -482,8 +455,10 @@ public void apply(Tracer.Builder builder) { protected void registerCodec(Tracer.Builder builder, Format format) { if (codecs.containsKey(format)) { List> codecsForFormat = codecs.get(format); - Codec codec = codecsForFormat.size() == 1 - ? codecsForFormat.get(0) : new CompositeCodec(codecsForFormat); + Codec codec = + codecsForFormat.size() == 1 + ? codecsForFormat.get(0) + : new CompositeCodec(codecsForFormat); builder.registerInjector(format, codec); builder.registerExtractor(format, codec); } @@ -496,22 +471,18 @@ public static class ReporterConfiguration { private Integer maxQueueSize; private SenderConfiguration senderConfiguration = new SenderConfiguration(); - public ReporterConfiguration() { - } + public ReporterConfiguration() {} /** * @deprecated use {@link Tracer.Builder} instead or {@link Configuration#getTracerBuilder()} */ @Deprecated public ReporterConfiguration(Sender sender) { - this.senderConfiguration = new Configuration.SenderConfiguration.Builder() - .sender(sender) - .build(); + this.senderConfiguration = + new Configuration.SenderConfiguration.Builder().sender(sender).build(); } - /** - * @deprecated use {@link #ReporterConfiguration()} and fluent API - */ + /** @deprecated use {@link #ReporterConfiguration()} and fluent API */ @Deprecated public ReporterConfiguration( Boolean logSpans, @@ -522,13 +493,10 @@ public ReporterConfiguration( this.logSpans = logSpans; this.flushIntervalMs = flushIntervalMs; this.maxQueueSize = maxQueueSize; - this.senderConfiguration.withAgentHost(agentHost) - .withAgentPort(agentPort); + this.senderConfiguration.withAgentHost(agentHost).withAgentPort(agentPort); } - /** - * @deprecated use {@link #ReporterConfiguration()} and fluent API - */ + /** @deprecated use {@link #ReporterConfiguration()} and fluent API */ @Deprecated public ReporterConfiguration( Boolean logSpans, @@ -570,12 +538,17 @@ public ReporterConfiguration withSender(SenderConfiguration senderConfiguration) } private Reporter getReporter(Metrics metrics) { - Reporter reporter = new RemoteReporter.Builder() - .withMetrics(metrics) - .withSender(senderConfiguration.getSender()) - .withFlushInterval(numberOrDefault(this.flushIntervalMs, RemoteReporter.DEFAULT_FLUSH_INTERVAL_MS).intValue()) - .withMaxQueueSize(numberOrDefault(this.maxQueueSize, RemoteReporter.DEFAULT_MAX_QUEUE_SIZE).intValue()) - .build(); + Reporter reporter = + new RemoteReporter.Builder() + .withMetrics(metrics) + .withSender(senderConfiguration.getSender()) + .withFlushInterval( + numberOrDefault(this.flushIntervalMs, RemoteReporter.DEFAULT_FLUSH_INTERVAL_MS) + .intValue()) + .withMaxQueueSize( + numberOrDefault(this.maxQueueSize, RemoteReporter.DEFAULT_MAX_QUEUE_SIZE) + .intValue()) + .build(); if (Boolean.TRUE.equals(this.logSpans)) { Reporter loggingReporter = new LoggingReporter(); @@ -588,9 +561,7 @@ public Boolean getLogSpans() { return logSpans; } - /** - * @deprecated use {@link #getSenderConfiguration()} - */ + /** @deprecated use {@link #getSenderConfiguration()} */ @Deprecated public String getAgentHost() { if (null == this.senderConfiguration) { @@ -600,9 +571,7 @@ public String getAgentHost() { return this.senderConfiguration.agentHost; } - /** - * @deprecated use {@link #getSenderConfiguration()} - */ + /** @deprecated use {@link #getSenderConfiguration()} */ @Deprecated public Integer getAgentPort() { if (null == this.senderConfiguration) { @@ -626,48 +595,42 @@ public SenderConfiguration getSenderConfiguration() { } /** - * Holds the configuration related to the sender. A sender can be a {@link HttpSender} or {@link UdpSender} - * + * Holds the configuration related to the sender. A sender can be a {@link HttpSender} or {@link + * UdpSender} */ @Getter public static class SenderConfiguration { - /** - * A custom sender set by our consumers. If set, nothing else has effect. Optional. - */ + /** A custom sender set by our consumers. If set, nothing else has effect. Optional. */ private Sender sender; - /** - * The Agent Host. Has no effect if the sender is set. Optional. - */ + /** The Agent Host. Has no effect if the sender is set. Optional. */ private String agentHost; - /** - * The Agent Port. Has no effect if the sender is set. Optional. - */ + /** The Agent Port. Has no effect if the sender is set. Optional. */ private Integer agentPort; - /** - * The endpoint, like https://jaeger-collector:14268/api/traces - */ + /** The endpoint, like https://jaeger-collector:14268/api/traces */ private String endpoint; /** - * The Auth Token to be added as "Bearer" on Authorization headers for requests sent to the endpoint + * The Auth Token to be added as "Bearer" on Authorization headers for requests sent to the + * endpoint */ private String authToken; /** - * The Basic Auth username to be added on Authorization headers for requests sent to the endpoint + * The Basic Auth username to be added on Authorization headers for requests sent to the + * endpoint */ private String authUsername; /** - * The Basic Auth password to be added on Authorization headers for requests sent to the endpoint + * The Basic Auth password to be added on Authorization headers for requests sent to the + * endpoint */ private String authPassword; - public SenderConfiguration() { - } + public SenderConfiguration() {} private SenderConfiguration(SenderConfiguration.Builder builder) { this.sender = builder.sender; @@ -710,8 +673,9 @@ public SenderConfiguration withAuthPassword(String password) { } /** - * Returns a sender if one was given when creating the configuration, or attempts to create a sender based on the - * configuration's state. + * Returns a sender if one was given when creating the configuration, or attempts to create a + * sender based on the configuration's state. + * * @return the sender passed via the constructor or a properly configured sender */ public Sender getSender() { @@ -722,8 +686,10 @@ public Sender getSender() { if (null != endpoint && !endpoint.isEmpty()) { HttpSender.Builder httpSenderBuilder = new HttpSender.Builder(endpoint); - if (null != authUsername && !authUsername.isEmpty() - && null != authPassword && !authPassword.isEmpty()) { + if (null != authUsername + && !authUsername.isEmpty() + && null != authPassword + && !authPassword.isEmpty()) { log.debug("Using HTTP Basic authentication with data from the environment variables."); httpSenderBuilder.withAuth(authUsername, authPassword); } else if (null != authToken && !authToken.isEmpty()) { @@ -737,13 +703,14 @@ public Sender getSender() { log.debug("Using the UDP Sender to send spans to the agent."); return new UdpSender( - stringOrDefault(this.agentHost, UdpSender.DEFAULT_AGENT_UDP_HOST), - numberOrDefault(this.agentPort, UdpSender.DEFAULT_AGENT_UDP_COMPACT_PORT).intValue(), - 0 /* max packet size */); + stringOrDefault(this.agentHost, UdpSender.DEFAULT_AGENT_UDP_HOST), + numberOrDefault(this.agentPort, UdpSender.DEFAULT_AGENT_UDP_COMPACT_PORT).intValue(), + 0 /* max packet size */); } /** * Attempts to create a new {@link SenderConfiguration} based on the environment variables + * * @return a new sender configuration based on environment variables */ public static SenderConfiguration fromEnv() { @@ -756,17 +723,15 @@ public static SenderConfiguration fromEnv() { String authPassword = getProperty(JAEGER_PASSWORD); return new SenderConfiguration() - .withAgentHost(agentHost) - .withAgentPort(agentPort) - .withEndpoint(collectorEndpoint) - .withAuthToken(authToken) - .withAuthUsername(authUsername) - .withAuthPassword(authPassword); + .withAgentHost(agentHost) + .withAgentPort(agentPort) + .withEndpoint(collectorEndpoint) + .withAuthToken(authToken) + .withAuthUsername(authUsername) + .withAuthPassword(authPassword); } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public static class Builder { private Sender sender; @@ -786,63 +751,49 @@ public Builder sender(Sender sender) { return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Builder agentHost(String agentHost) { this.agentHost = agentHost; return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Builder agentPort(Integer agentPort) { this.agentPort = agentPort; return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Builder endpoint(String endpoint) { this.endpoint = endpoint; return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Builder authToken(String authToken) { this.authToken = authToken; return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Builder authUsername(String authUsername) { this.authUsername = authUsername; return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Builder authPassword(String authPassword) { this.authPassword = authPassword; return this; } - /** - * @deprecated use {@link SenderConfiguration} directly - */ + /** @deprecated use {@link SenderConfiguration} directly */ @Deprecated public Configuration.SenderConfiguration build() { return new Configuration.SenderConfiguration(this); @@ -850,6 +801,52 @@ public Configuration.SenderConfiguration build() { } } + /** Holds the configuration related to the throttler. */ + public static class ThrottlerConfiguration { + private String proxyHostPort; + private int refreshIntervalMs; + private boolean synchronousInitialization; + + /** Default constructor. */ + public ThrottlerConfiguration() {} + + public String getProxyHostPort() { + return proxyHostPort; + } + + public int getRefreshIntervalMs() { + return refreshIntervalMs; + } + + public boolean getSynchronousInitialization() { + return synchronousInitialization; + } + + public ThrottlerConfiguration withProxyHostPort(String proxyHostPort) { + this.proxyHostPort = proxyHostPort; + return this; + } + + public ThrottlerConfiguration withRefreshIntervalMs(int refreshIntervalMs) { + this.refreshIntervalMs = refreshIntervalMs; + return this; + } + + public ThrottlerConfiguration withSynchronousInitialization(boolean synchronousInitialization) { + this.synchronousInitialization = synchronousInitialization; + return this; + } + + // For tests. + Throttler createThrottler(Metrics metrics) { + return new RemoteThrottler( + new HttpThrottlerProxy(proxyHostPort), + metrics, + refreshIntervalMs, + synchronousInitialization); + } + } + private static String stringOrDefault(String value, String defaultValue) { return value != null && value.length() > 0 ? value : defaultValue; } @@ -868,7 +865,8 @@ private static Integer getPropertyAsInt(String name) { try { return Integer.parseInt(value); } catch (NumberFormatException e) { - log.error("Failed to parse integer for property '" + name + "' with value '" + value + "'", e); + log.error( + "Failed to parse integer for property '" + name + "' with value '" + value + "'", e); } } return null; @@ -880,15 +878,17 @@ private static Number getPropertyAsNum(String name) { try { return NumberFormat.getInstance().parse(value); } catch (ParseException e) { - log.error("Failed to parse number for property '" + name + "' with value '" + value + "'", e); + log.error( + "Failed to parse number for property '" + name + "' with value '" + value + "'", e); } } return null; } /** - * Gets the system property defined by the name , and returns a boolean value represented by - * the name. This method defaults to returning false for a name that doesn't exist. + * Gets the system property defined by the name , and returns a boolean value represented by the + * name. This method defaults to returning false for a name that doesn't exist. + * * @param name The name of the system property */ private static boolean getPropertyAsBool(String name) { diff --git a/jaeger-core/src/test/java/com/uber/jaeger/ConfigurationTest.java b/jaeger-core/src/test/java/com/uber/jaeger/ConfigurationTest.java index a7f645b..25ac885 100644 --- a/jaeger-core/src/test/java/com/uber/jaeger/ConfigurationTest.java +++ b/jaeger-core/src/test/java/com/uber/jaeger/ConfigurationTest.java @@ -14,29 +14,28 @@ package com.uber.jaeger; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import com.uber.jaeger.Configuration.ReporterConfiguration; import com.uber.jaeger.Configuration.SamplerConfiguration; +import com.uber.jaeger.metrics.InMemoryMetricsFactory; import com.uber.jaeger.metrics.Metrics; +import com.uber.jaeger.metrics.MetricsFactory; import com.uber.jaeger.metrics.NullStatsReporter; import com.uber.jaeger.metrics.StatsFactoryImpl; import com.uber.jaeger.samplers.ConstSampler; - import com.uber.jaeger.samplers.ProbabilisticSampler; import com.uber.jaeger.samplers.RateLimitingSampler; import com.uber.jaeger.samplers.Sampler; import com.uber.jaeger.senders.HttpSender; import com.uber.jaeger.senders.Sender; +import com.uber.jaeger.throttler.Throttler; import io.opentracing.noop.NoopTracerFactory; import io.opentracing.propagation.Format; import io.opentracing.propagation.TextMap; import io.opentracing.util.GlobalTracer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import java.lang.reflect.Field; import java.net.SocketException; import java.util.HashMap; @@ -44,9 +43,7 @@ import java.util.Map; import java.util.Map.Entry; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.Assert.*; public class ConfigurationTest { @@ -183,7 +180,8 @@ public void testSenderWithAgentDataFromEnv() { Configuration.SenderConfiguration.fromEnv().getSender(); fail("expecting exception"); } catch (RuntimeException re) { - // we need to catch it here instead of using @Test(expected = ...) because the SocketException is + // we need to catch it here instead of using @Test(expected = ...) because the SocketException + // is // wrapped into a runtime exception assertTrue(re.getCause() instanceof SocketException); } @@ -194,13 +192,14 @@ public void testSenderBackwardsCompatibilityGettingAgentHostAndPort() { System.setProperty(Configuration.JAEGER_AGENT_HOST, "jaeger-agent"); System.setProperty(Configuration.JAEGER_AGENT_PORT, "6832"); assertEquals("jaeger-agent", Configuration.ReporterConfiguration.fromEnv().getAgentHost()); - assertEquals(Integer.valueOf(6832), Configuration.ReporterConfiguration.fromEnv().getAgentPort()); + assertEquals( + Integer.valueOf(6832), Configuration.ReporterConfiguration.fromEnv().getAgentPort()); } @Test public void testNoNullPointerOnNullSender() { Configuration.ReporterConfiguration reporterConfiguration = - new Configuration.ReporterConfiguration(null, null, null, null); + new Configuration.ReporterConfiguration(null, null, null, null); assertNull(reporterConfiguration.getAgentHost()); assertNull(reporterConfiguration.getAgentPort()); @@ -218,15 +217,15 @@ public void testCustomSender() { String endpoint = "https://custom-sender-endpoint:14268/api/traces"; System.setProperty(Configuration.JAEGER_ENDPOINT, "https://jaeger-collector:14268/api/traces"); CustomSender customSender = new CustomSender(endpoint); - Configuration.SenderConfiguration senderConfiguration = new Configuration.SenderConfiguration.Builder() - .sender(customSender) - .build(); - assertEquals(endpoint, ((CustomSender)senderConfiguration.getSender()).getEndpoint()); + Configuration.SenderConfiguration senderConfiguration = + new Configuration.SenderConfiguration.Builder().sender(customSender).build(); + assertEquals(endpoint, ((CustomSender) senderConfiguration.getSender()).getEndpoint()); } @Test public void testSenderWithBasicAuthUsesHttpSender() { - Configuration.SenderConfiguration senderConfiguration = new Configuration.SenderConfiguration.Builder() + Configuration.SenderConfiguration senderConfiguration = + new Configuration.SenderConfiguration.Builder() .endpoint("https://jaeger-collector:14268/api/traces") .authUsername("username") .authPassword("password") @@ -236,7 +235,8 @@ public void testSenderWithBasicAuthUsesHttpSender() { @Test public void testSenderWithAuthTokenUsesHttpSender() { - Configuration.SenderConfiguration senderConfiguration = new Configuration.SenderConfiguration.Builder() + Configuration.SenderConfiguration senderConfiguration = + new Configuration.SenderConfiguration.Builder() .endpoint("https://jaeger-collector:14268/api/traces") .authToken("authToken") .build(); @@ -261,7 +261,7 @@ public void testPropagationB3Only() { long spanId = 5678; TestTextMap textMap = new TestTextMap(); - SpanContext spanContext = new SpanContext(traceId, spanId, 0, (byte)0); + SpanContext spanContext = new SpanContext(traceId, spanId, 0, (byte) 0); io.opentracing.Tracer tracer = Configuration.fromEnv().getTracer(); tracer.inject(spanContext, Format.Builtin.TEXT_MAP, textMap); @@ -270,7 +270,7 @@ public void testPropagationB3Only() { assertNotNull(textMap.get("X-B3-SpanId")); assertNull(textMap.get("uber-trace-id")); - SpanContext extractedContext = (SpanContext)tracer.extract(Format.Builtin.TEXT_MAP, textMap); + SpanContext extractedContext = (SpanContext) tracer.extract(Format.Builtin.TEXT_MAP, textMap); assertEquals(traceId, extractedContext.getTraceId()); assertEquals(spanId, extractedContext.getSpanId()); } @@ -284,7 +284,7 @@ public void testPropagationJaegerAndB3() { long spanId = 5678; TestTextMap textMap = new TestTextMap(); - SpanContext spanContext = new SpanContext(traceId, spanId, 0, (byte)0); + SpanContext spanContext = new SpanContext(traceId, spanId, 0, (byte) 0); io.opentracing.Tracer tracer = Configuration.fromEnv().getTracer(); tracer.inject(spanContext, Format.Builtin.TEXT_MAP, textMap); @@ -293,7 +293,7 @@ public void testPropagationJaegerAndB3() { assertNotNull(textMap.get("X-B3-TraceId")); assertNotNull(textMap.get("X-B3-SpanId")); - SpanContext extractedContext = (SpanContext)tracer.extract(Format.Builtin.TEXT_MAP, textMap); + SpanContext extractedContext = (SpanContext) tracer.extract(Format.Builtin.TEXT_MAP, textMap); assertEquals(traceId, extractedContext.getTraceId()); assertEquals(spanId, extractedContext.getSpanId()); } @@ -303,7 +303,7 @@ public void testPropagationDefault() { System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test"); TestTextMap textMap = new TestTextMap(); - SpanContext spanContext = new SpanContext(1234, 5678, 0, (byte)0); + SpanContext spanContext = new SpanContext(1234, 5678, 0, (byte) 0); Configuration.fromEnv().getTracer().inject(spanContext, Format.Builtin.TEXT_MAP, textMap); @@ -318,7 +318,7 @@ public void testPropagationValidFormat() { System.setProperty(Configuration.JAEGER_SERVICE_NAME, "Test"); TestTextMap textMap = new TestTextMap(); - SpanContext spanContext = new SpanContext(1234, 5678, 0, (byte)0); + SpanContext spanContext = new SpanContext(1234, 5678, 0, (byte) 0); Configuration.fromEnv().getTracer().inject(spanContext, Format.Builtin.TEXT_MAP, textMap); @@ -343,41 +343,42 @@ public void testDefaultTracer() { public void testUnknownSampler() { SamplerConfiguration samplerConfiguration = new SamplerConfiguration(); samplerConfiguration.withType("unknown"); - new Configuration("name") - .withSampler(samplerConfiguration) - .getTracer(); + new Configuration("name").withSampler(samplerConfiguration).getTracer(); } @Test public void testConstSampler() { - SamplerConfiguration samplerConfiguration = new SamplerConfiguration() - .withType(ConstSampler.TYPE); - Sampler sampler = samplerConfiguration.createSampler("name", - new Metrics(new StatsFactoryImpl(new NullStatsReporter()))); + SamplerConfiguration samplerConfiguration = + new SamplerConfiguration().withType(ConstSampler.TYPE); + Sampler sampler = + samplerConfiguration.createSampler( + "name", new Metrics(new StatsFactoryImpl(new NullStatsReporter()))); assertTrue(sampler instanceof ConstSampler); } @Test public void testProbabilisticSampler() { - SamplerConfiguration samplerConfiguration = new SamplerConfiguration() - .withType(ProbabilisticSampler.TYPE); - Sampler sampler = samplerConfiguration.createSampler("name", - new Metrics(new StatsFactoryImpl(new NullStatsReporter()))); + SamplerConfiguration samplerConfiguration = + new SamplerConfiguration().withType(ProbabilisticSampler.TYPE); + Sampler sampler = + samplerConfiguration.createSampler( + "name", new Metrics(new StatsFactoryImpl(new NullStatsReporter()))); assertTrue(sampler instanceof ProbabilisticSampler); } @Test public void testRateLimitingSampler() { - SamplerConfiguration samplerConfiguration = new SamplerConfiguration() - .withType(RateLimitingSampler.TYPE); - Sampler sampler = samplerConfiguration.createSampler("name", - new Metrics(new StatsFactoryImpl(new NullStatsReporter()))); + SamplerConfiguration samplerConfiguration = + new SamplerConfiguration().withType(RateLimitingSampler.TYPE); + Sampler sampler = + samplerConfiguration.createSampler( + "name", new Metrics(new StatsFactoryImpl(new NullStatsReporter()))); assertTrue(sampler instanceof RateLimitingSampler); } static class TestTextMap implements TextMap { - private Map values = new HashMap<>(); + private Map values = new HashMap<>(); @Override public Iterator> iterator() { @@ -406,4 +407,24 @@ public String getEndpoint() { return endpoint; } } + + @Test + public void testThrottler() { + Configuration.ThrottlerConfiguration throttlerConfig = + new Configuration.ThrottlerConfiguration(); + throttlerConfig + .withProxyHostPort("abcd:1234") + .withRefreshIntervalMs(1) + .withSynchronousInitialization(false); + assertEquals("abcd:1234", throttlerConfig.getProxyHostPort()); + assertEquals(1, throttlerConfig.getRefreshIntervalMs()); + assertFalse(throttlerConfig.getSynchronousInitialization()); + MetricsFactory factory = new InMemoryMetricsFactory(); + Metrics metrics = new Metrics(factory); + Configuration config = new Configuration("test-service"); + config.withThrottler(throttlerConfig); + assertEquals(throttlerConfig, config.getThrottler()); + Throttler throttler = config.getThrottler().createThrottler(metrics); + assertNotNull(throttler); + } }