-
Notifications
You must be signed in to change notification settings - Fork 784
Added rate limit sampler via a property #1175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||
| */ | ||
| public class RateLimitingSampler extends Sampler { | ||
marcingrzejszczak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private final Sampler sampler; | ||
|
|
||
| public RateLimitingSampler(SamplerProperties configuration) { | ||
| this.sampler = brave.sampler.RateLimitingSampler.create(rateLimit(configuration)); | ||
| } | ||
|
|
||
| private Integer rateLimit(SamplerProperties configuration) { | ||
| return configuration.getRatelimit() != null ? configuration.getRatelimit() : 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 hidden or 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.getRatelimit() != null) { | ||
| return new RateLimitingSampler(config); | ||
| } | ||
| return new ProbabilityBasedSampler(config); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
...src/test/java/org/springframework/cloud/sleuth/sampler/SamplerAutoConfigurationTests.java
This file contains hidden or 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,34 @@ | ||
| package org.springframework.cloud.sleuth.sampler; | ||
|
|
||
| import brave.sampler.Sampler; | ||
| import org.assertj.core.api.BDDAssertions; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| /** | ||
| * @author Marcin Grzejszczak | ||
| * @since | ||
| */ | ||
| public class SamplerAutoConfigurationTests { | ||
|
|
||
| @Test | ||
| public void should_use_rate_limit_sampler_when_property_set() { | ||
| SamplerProperties properties = new SamplerProperties(); | ||
| properties.setRatelimit(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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.