-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added rate limit sampler via a property
fixes gh-1162
- Loading branch information
1 parent
26dc5be
commit 2579e78
Showing
11 changed files
with
220 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...euth-core/src/main/java/org/springframework/cloud/sleuth/sampler/RateLimitingSampler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2013-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.sleuth.sampler; | ||
|
||
import brave.sampler.Sampler; | ||
|
||
/** | ||
* The rate-limited sampler allows you to choose an amount of traces to accept on a | ||
* per-second interval. The minimum number is 0 and the max is 2,147,483,647 (max int). | ||
* | ||
* You can read more about it in {@link brave.sampler.RateLimitingSampler} | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 2.1.0 | ||
*/ | ||
class RateLimitingSampler extends Sampler { | ||
|
||
private final Sampler sampler; | ||
|
||
public RateLimitingSampler(SamplerProperties configuration) { | ||
this.sampler = brave.sampler.RateLimitingSampler.create(rateLimit(configuration)); | ||
} | ||
|
||
private Integer rateLimit(SamplerProperties configuration) { | ||
return configuration.getRate() != null ? configuration.getRate() : 0; | ||
} | ||
|
||
@Override | ||
public boolean isSampled(long traceId) { | ||
return this.sampler.isSampled(traceId); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...core/src/main/java/org/springframework/cloud/sleuth/sampler/SamplerAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2013-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.cloud.sleuth.sampler; | ||
|
||
import brave.sampler.Sampler; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
* Auto-configuration} to setup sampling for Spring Cloud Sleuth. | ||
* | ||
* @author Marcin Grzejszczak | ||
* @since 2.1.0 | ||
*/ | ||
@Configuration | ||
@ConditionalOnProperty(value = "spring.sleuth.enabled", matchIfMissing = true) | ||
@EnableConfigurationProperties(SamplerProperties.class) | ||
public class SamplerAutoConfiguration { | ||
|
||
@Configuration | ||
@ConditionalOnBean(type = "org.springframework.cloud.context.scope.refresh.RefreshScope") | ||
protected static class RefreshScopedSamplerConfiguration { | ||
|
||
@Bean | ||
@RefreshScope | ||
@ConditionalOnMissingBean | ||
public Sampler defaultTraceSampler(SamplerProperties config) { | ||
return samplerFromProps(config); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@ConditionalOnMissingBean(type = "org.springframework.cloud.context.scope.refresh.RefreshScope") | ||
protected static class NonRefreshScopeSamplerConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public Sampler defaultTraceSampler(SamplerProperties config) { | ||
return samplerFromProps(config); | ||
} | ||
|
||
} | ||
|
||
static Sampler samplerFromProps(SamplerProperties config) { | ||
if (config.getRate() != null) { | ||
return new RateLimitingSampler(config); | ||
} | ||
return new ProbabilityBasedSampler(config); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
spring-cloud-sleuth-core/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...src/test/java/org/springframework/cloud/sleuth/sampler/SamplerAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.springframework.cloud.sleuth.sampler; | ||
|
||
import brave.sampler.Sampler; | ||
import org.assertj.core.api.BDDAssertions; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Marcin Grzejszczak | ||
* @since | ||
*/ | ||
public class SamplerAutoConfigurationTests { | ||
|
||
@Test | ||
public void should_use_rate_limit_sampler_when_property_set() { | ||
SamplerProperties properties = new SamplerProperties(); | ||
properties.setRate(10); | ||
|
||
Sampler sampler = SamplerAutoConfiguration.samplerFromProps(properties); | ||
|
||
BDDAssertions.then(sampler).isInstanceOf(RateLimitingSampler.class); | ||
} | ||
|
||
@Test | ||
public void should_use_probability_sampler_when_rate_limiting_not_set() { | ||
SamplerProperties properties = new SamplerProperties(); | ||
|
||
Sampler sampler = SamplerAutoConfiguration.samplerFromProps(properties); | ||
|
||
BDDAssertions.then(sampler).isInstanceOf(ProbabilityBasedSampler.class); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.