brave.sampler.SamplerFunction Java Examples

The following examples show how to use brave.sampler.SamplerFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ITHttpClient.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void customSampler() throws IOException {
  String path = "/foo";

  closeClient(client);

  SamplerFunction<HttpRequest> sampler = HttpRuleSampler.newBuilder()
    .putRule(pathStartsWith(path), Sampler.NEVER_SAMPLE)
    .build();

  httpTracing = httpTracing.toBuilder().clientSampler(sampler).build();
  client = newClient(server.getPort());

  server.enqueue(new MockResponse());
  get(client, path);

  assertThat(extract(takeRequest()).sampled()).isFalse();
}
 
Example #2
Source File: TraceHttpAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
private SamplerFunction<HttpRequest> combineUserProvidedSamplerWithSkipPatternSampler(
		@Nullable SamplerFunction<HttpRequest> serverSampler,
		@Nullable SkipPatternProvider provider) {
	SamplerFunction<HttpRequest> skipPatternSampler = provider != null
			? new SkipPatternHttpServerSampler(provider) : null;
	if (serverSampler == null && skipPatternSampler == null) {
		return SamplerFunctions.deferDecision();
	}
	else if (serverSampler == null) {
		return skipPatternSampler;
	}
	else if (skipPatternSampler == null) {
		return serverSampler;
	}
	return new CompositeHttpSampler(skipPatternSampler, serverSampler);
}
 
Example #3
Source File: ITHttpServer.java    From brave with Apache License 2.0 6 votes vote down vote up
@Test public void customSampler() throws IOException {
  String path = "/foo";

  SamplerFunction<HttpRequest> sampler = HttpRuleSampler.newBuilder()
    .putRule(pathStartsWith(path), Sampler.NEVER_SAMPLE)
    .build();

  httpTracing = httpTracing.toBuilder().serverSampler(sampler).build();
  init();

  Request request = new Request.Builder().url(url(path)).build();
  try (Response response = client.newCall(request).execute()) {
    assertThat(response.isSuccessful()).isTrue();
  }

  // @After will check that nothing is reported
}
 
Example #4
Source File: TraceRpcAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
// NOTE: stable bean name as might be used outside sleuth
RpcTracing rpcTracing(Tracing tracing,
		@Nullable @RpcClientSampler SamplerFunction<RpcRequest> clientSampler,
		@Nullable @RpcServerSampler SamplerFunction<RpcRequest> serverSampler,
		@Nullable List<RpcTracingCustomizer> rpcTracingCustomizers) {

	RpcTracing.Builder builder = RpcTracing.newBuilder(tracing);
	if (clientSampler != null) {
		builder.clientSampler(clientSampler);
	}
	if (serverSampler != null) {
		builder.serverSampler(serverSampler);
	}
	if (rpcTracingCustomizers != null) {
		for (RpcTracingCustomizer customizer : rpcTracingCustomizers) {
			customizer.customize(builder);
		}
	}
	return builder.build();
}
 
Example #5
Source File: TraceMessagingAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean
// NOTE: stable bean name as might be used outside sleuth
MessagingTracing messagingTracing(Tracing tracing,
		@Nullable @ProducerSampler SamplerFunction<MessagingRequest> producerSampler,
		@Nullable @ConsumerSampler SamplerFunction<MessagingRequest> consumerSampler,
		@Nullable List<MessagingTracingCustomizer> messagingTracingCustomizers) {

	MessagingTracing.Builder builder = MessagingTracing.newBuilder(tracing);
	if (producerSampler != null) {
		builder.producerSampler(producerSampler);
	}
	if (consumerSampler != null) {
		builder.consumerSampler(consumerSampler);
	}
	if (messagingTracingCustomizers != null) {
		for (MessagingTracingCustomizer customizer : messagingTracingCustomizers) {
			customizer.customize(builder);
		}
	}
	return builder.build();
}
 
Example #6
Source File: TraceMessagingAutoConfigurationIntegrationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean(name = ConsumerSampler.NAME)
SamplerFunction<MessagingRequest> myMessagingSampler() {
	return MessagingRuleSampler.newBuilder()
			.putRule(channelNameEquals("alerts"), Sampler.NEVER_SAMPLE)
			.putRule(Matchers.alwaysMatch(), RateLimitingSampler.create(100))
			.build();
}
 
Example #7
Source File: TraceMessagingAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultsToBraveConsumerSampler() {
	contextRunner().run((context) -> {
		SamplerFunction<MessagingRequest> consumerSampler = context
				.getBean(MessagingTracing.class).consumerSampler();

		then(consumerSampler).isSameAs(SamplerFunctions.deferDecision());
	});
}
 
Example #8
Source File: HttpClientHandlerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void deprecatedNextSpan_samplerSeesHttpClientRequest() {
  SamplerFunction<HttpRequest> clientSampler = mock(SamplerFunction.class);
  init(httpTracingBuilder(tracingBuilder()).clientSampler(clientSampler));

  handler.nextSpan(request);

  verify(clientSampler).trySample(request);
}
 
Example #9
Source File: JmsTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
/** Creates a potentially noop remote span representing this request */
Span nextMessagingSpan(
  SamplerFunction<MessagingRequest> sampler,
  MessagingRequest request,
  TraceContextOrSamplingFlags extracted
) {
  Boolean sampled = extracted.sampled();
  // only recreate the context if the messaging sampler made a decision
  if (sampled == null && (sampled = sampler.trySample(request)) != null) {
    extracted = extracted.sampled(sampled.booleanValue());
  }
  return tracer.nextSpan(extracted);
}
 
Example #10
Source File: TraceRpcAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void configuresUserProvidedRpcServerSampler() {
	contextRunner().withUserConfiguration(RpcServerSamplerConfig.class)
			.run((context) -> {
				SamplerFunction<RpcRequest> serverSampler = context
						.getBean(RpcTracing.class).serverSampler();

				then(serverSampler).isSameAs(RpcServerSamplerConfig.INSTANCE);
			});
}
 
Example #11
Source File: TraceHttpAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultsServerSamplerToDeferWhenSkipPatternCleared() {
	contextRunner().withPropertyValues("spring.sleuth.web.skip-pattern")
			.run((context) -> {
				SamplerFunction<HttpRequest> clientSampler = context
						.getBean(HttpTracing.class).serverRequestSampler();

				then(clientSampler).isSameAs(SamplerFunctions.deferDecision());
			});
}
 
Example #12
Source File: TraceHttpAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = HttpClientSampler.NAME)
SamplerFunction<HttpRequest> sleuthHttpClientSampler(
		SleuthWebProperties sleuthWebProperties) {
	String skipPattern = sleuthWebProperties.getClient().getSkipPattern();
	if (skipPattern == null) {
		return SamplerFunctions.deferDecision();
	}

	return new SkipPatternHttpClientSampler(Pattern.compile(skipPattern));
}
 
Example #13
Source File: TraceHttpAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultsClientSamplerToDefer() {
	contextRunner().run((context) -> {
		SamplerFunction<HttpRequest> clientSampler = context
				.getBean(HttpTracing.class).clientRequestSampler();

		then(clientSampler).isSameAs(SamplerFunctions.deferDecision());
	});
}
 
Example #14
Source File: TraceHttpAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void configuresClientSkipPattern() throws Exception {
	contextRunner()
			.withPropertyValues("spring.sleuth.web.client.skip-pattern=foo.*|bar.*")
			.run((context) -> {
				SamplerFunction<HttpRequest> clientSampler = context
						.getBean(HttpTracing.class).clientRequestSampler();

				then(clientSampler).isInstanceOf(SkipPatternHttpClientSampler.class);
			});
}
 
Example #15
Source File: TraceHttpAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void configuresUserProvidedHttpClientSampler() {
	contextRunner().withUserConfiguration(HttpClientSamplerConfig.class)
			.run((context) -> {
				SamplerFunction<HttpRequest> clientSampler = context
						.getBean(HttpTracing.class).clientRequestSampler();

				then(clientSampler).isSameAs(HttpClientSamplerConfig.INSTANCE);
			});
}
 
Example #16
Source File: TraceHttpAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultsServerSamplerToSkipPattern() {
	contextRunner().run((context) -> {
		SamplerFunction<HttpRequest> serverSampler = context
				.getBean(HttpTracing.class).serverRequestSampler();

		then(serverSampler).isInstanceOf(SkipPatternHttpServerSampler.class);
	});
}
 
Example #17
Source File: TraceHttpAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
private ContextConsumer<AssertableApplicationContext> thenCompositeHttpServerSamplerOf(
		SamplerFunction<HttpRequest> instance) {
	return (context) -> {

		SamplerFunction<HttpRequest> serverSampler = context
				.getBean(HttpTracing.class).serverRequestSampler();

		then(serverSampler).isInstanceOf(CompositeHttpSampler.class);

		then(((CompositeHttpSampler) serverSampler).left)
				.isInstanceOf(SkipPatternHttpServerSampler.class);
		then(((CompositeHttpSampler) serverSampler).right).isSameAs(instance);
	};
}
 
Example #18
Source File: KafkaTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
/** Creates a potentially noop remote span representing this request */
Span nextMessagingSpan(
  SamplerFunction<MessagingRequest> sampler,
  MessagingRequest request,
  TraceContextOrSamplingFlags extracted
) {
  Boolean sampled = extracted.sampled();
  // only recreate the context if the messaging sampler made a decision
  if (sampled == null && (sampled = sampler.trySample(request)) != null) {
    extracted = extracted.sampled(sampled.booleanValue());
  }
  return tracer.nextSpan(extracted);
}
 
Example #19
Source File: TraceMessagingAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void defaultsToBraveProducerSampler() {
	contextRunner().run((context) -> {
		SamplerFunction<MessagingRequest> producerSampler = context
				.getBean(MessagingTracing.class).producerSampler();

		then(producerSampler).isSameAs(SamplerFunctions.deferDecision());
	});
}
 
Example #20
Source File: TraceMessagingAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void configuresUserProvidedProducerSampler() {
	contextRunner().withUserConfiguration(ProducerSamplerConfig.class)
			.run((context) -> {
				SamplerFunction<MessagingRequest> producerSampler = context
						.getBean(MessagingTracing.class).producerSampler();

				then(producerSampler).isSameAs(ProducerSamplerConfig.INSTANCE);
			});
}
 
Example #21
Source File: TraceRpcAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void configuresUserProvidedRpcClientSampler() {
	contextRunner().withUserConfiguration(RpcClientSamplerConfig.class)
			.run((context) -> {
				SamplerFunction<RpcRequest> clientSampler = context
						.getBean(RpcTracing.class).clientSampler();

				then(clientSampler).isSameAs(RpcClientSamplerConfig.INSTANCE);
			});
}
 
Example #22
Source File: TraceMessagingAutoConfigurationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Test
public void configuresUserProvidedConsumerSampler() {
	contextRunner().withUserConfiguration(ConsumerSamplerConfig.class)
			.run((context) -> {
				SamplerFunction<MessagingRequest> consumerSampler = context
						.getBean(MessagingTracing.class).consumerSampler();

				then(consumerSampler).isSameAs(ConsumerSamplerConfig.INSTANCE);
			});
}
 
Example #23
Source File: HttpClientHandlerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void handleSend_samplerSeesHttpClientRequest() {
  SamplerFunction<HttpRequest> clientSampler = mock(SamplerFunction.class);
  init(httpTracingBuilder(tracingBuilder()).clientSampler(clientSampler));

  handler.handleSend(request);

  verify(clientSampler).trySample(request);
}
 
Example #24
Source File: HttpServerHandlerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void handleReceive_samplerSeesHttpServerRequest() {
  SamplerFunction<HttpRequest> serverSampler = mock(SamplerFunction.class);
  init(httpTracingBuilder(tracingBuilder()).serverSampler(serverSampler));

  handler.handleReceive(request);

  verify(serverSampler).trySample(request);
}
 
Example #25
Source File: HttpSampler.java    From brave with Apache License 2.0 5 votes vote down vote up
static HttpSampler fromHttpRequestSampler(SamplerFunction<HttpRequest> sampler) {
  if (sampler == null) throw new NullPointerException("sampler == null");
  if (sampler.equals(SamplerFunctions.deferDecision())) return HttpSampler.TRACE_ID;
  if (sampler.equals(SamplerFunctions.neverSample())) return HttpSampler.NEVER_SAMPLE;
  return sampler instanceof HttpSampler ? (HttpSampler) sampler
    : new HttpRequestSamplerAdapter(sampler);
}
 
Example #26
Source File: TraceRpcAutoConfigurationIntegrationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean(name = RpcServerSampler.NAME)
SamplerFunction<RpcRequest> myRpcSampler() {
	Matcher<RpcRequest> userAuth = and(serviceEquals("users.UserService"),
			methodEquals("GetUserToken"));
	return RpcRuleSampler.newBuilder()
			.putRule(serviceEquals("grpc.health.v1.Health"), Sampler.NEVER_SAMPLE)
			.putRule(userAuth, RateLimitingSampler.create(100)).build();
}
 
Example #27
Source File: TraceFilterWebIntegrationTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@Bean(name = HttpServerSampler.NAME)
SamplerFunction<HttpRequest> myHttpSampler(SkipPatternProvider provider) {
	Pattern pattern = provider.skipPattern();
	return request -> {
		String url = request.path();
		boolean shouldSkip = pattern.matcher(url).matches();
		if (shouldSkip) {
			return false;
		}
		return null;
	};
}
 
Example #28
Source File: RpcServerHandlerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void handleReceive_samplerSeesRpcServerRequest() {
  SamplerFunction<RpcRequest> serverSampler = mock(SamplerFunction.class);
  init(httpTracingBuilder(tracingBuilder()).serverSampler(serverSampler));

  handler.handleReceive(request);

  verify(serverSampler).trySample(request);
}
 
Example #29
Source File: Tracer.java    From brave with Apache License 2.0 5 votes vote down vote up
<T> TraceContext nextContext(SamplerFunction<T> samplerFunction, T arg, TraceContext parent) {
  if (samplerFunction == null) throw new NullPointerException("samplerFunction == null");
  if (arg == null) throw new NullPointerException("arg == null");
  if (parent != null) return decorateContext(parent, parent.spanId());

  Boolean sampled = samplerFunction.trySample(arg);
  SamplingFlags flags = sampled != null ? (sampled ? SAMPLED : NOT_SAMPLED) : EMPTY;
  return newRootContext(InternalPropagation.instance.flags(flags));
}
 
Example #30
Source File: RpcClientHandlerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void handleSend_samplerSeesRpcClientRequest() {
  SamplerFunction<RpcRequest> clientSampler = mock(SamplerFunction.class);
  init(httpTracingBuilder(tracingBuilder()).clientSampler(clientSampler));

  handler.handleSend(request);

  verify(clientSampler).trySample(request);
}