Skip to content

Instantly share code, notes, and snippets.

@ScienJus
Created January 18, 2018 11:48
Show Gist options
  • Save ScienJus/9b7804ed56c58f7bb0c90fbb9913a1c1 to your computer and use it in GitHub Desktop.
Save ScienJus/9b7804ed56c58f7bb0c90fbb9913a1c1 to your computer and use it in GitHub Desktop.
Custom sampler for special api
package com.scienjus.spring.cloud.sleuth;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@Data
@ConfigurationProperties("spring.sleuth.sampler")
public class SleuthSamplerProperties {
private List<String> whitelist;
}
package com.scienjus.spring.cloud.sleuth;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.sampler.PercentageBasedSampler;
import org.springframework.cloud.sleuth.sampler.SamplerProperties;
import java.util.List;
public class SpanNameWhitelistSampler extends PercentageBasedSampler {
private final List<String> spanNameWhitelist;
public SpanNameWhitelistSampler(SamplerProperties config, SleuthSamplerProperties extension) {
super(config);
this.spanNameWhitelist = extension.getWhitelist();
}
@Override
public boolean isSampled(Span currentSpan) {
String spanName = currentSpan.getName();
if (CollectionUtils.isNotEmpty(spanNameWhitelist) && spanNameWhitelist.contains(spanName)) {
return true;
}
return super.isSampled(currentSpan);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment