Skip to content

Commit

Permalink
More move from core (#15)
Browse files Browse the repository at this point in the history
* moved OpenTracingInvocationInstrumenterSpec from core

* add bom
  • Loading branch information
burtbeckwith authored Jan 25, 2022
1 parent 2ce19b5 commit eea16ca
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 31 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {

rootProject.name = 'micronaut-tracing'

include 'tracing-bom'
include 'tracing-core'
include 'tracing-jaeger'
include 'tracing-zipkin'
Expand Down
6 changes: 3 additions & 3 deletions src/main/docs/guide/jaeger.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ tracing:

You can also optionally dependency-inject common configuration classes into api:tracing.jaeger.JaegerConfiguration[] such as `io.jaegertracing.Configuration.SamplerConfiguration` just by defining them as beans. See the API for api:tracing.jaeger.JaegerConfiguration[] for available injection points.

== Filtering http spans
== Filtering HTTP spans

It may be useful to exclude health-checks and other http requests to your service.
It may be useful to exclude health-checks and other HTTP requests to your service.
This can be achieved by adding a list of regular expression patterns to your configuration:

.Filtering http request spans
.Filtering HTTP request spans
[source,yaml]
----
tracing:
Expand Down
6 changes: 3 additions & 3 deletions src/main/docs/guide/zipkin.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ tracing:

You can also optionally dependency-inject common configuration classes into api:tracing.brave.BraveTracerConfiguration[] such as `brave.sampler.Sampler` just by defining them as beans. See the API for api:tracing.brave.BraveTracerConfiguration[] for available injection points.

== Filtering http spans
== Filtering HTTP spans

It may be useful to exclude health-checks and other http requests to your service.
It may be useful to exclude health-checks and other HTTP requests to your service.
This can be achieved by adding a list of regular expression patterns to your configuration:

.Filtering http request spans
.Filtering HTTP request spans
[source,yaml]
----
tracing:
Expand Down
3 changes: 3 additions & 0 deletions tracing-bom/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id 'io.micronaut.build.internal.bom'
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ public AbstractOpenTracingFilter(Tracer tracer) {
}

/**
* Configure tracer in the filter for span creation and propagation across arbitrary transports.
* Configure tracer in the filter for span creation and propagation across
* arbitrary transports.
*
* @param tracer the tracer
* @param pathExclusionTest the predicate for excluding URI paths from tracing.
* @param tracer the tracer
* @param pathExclusionTest the predicate for excluding URI paths from tracing
*/
public AbstractOpenTracingFilter(Tracer tracer, @Nullable Predicate<String> pathExclusionTest) {
public AbstractOpenTracingFilter(Tracer tracer,
@Nullable Predicate<String> pathExclusionTest) {
this.tracer = tracer;
this.pathExclusionTest = pathExclusionTest;
}
Expand Down Expand Up @@ -147,8 +149,8 @@ protected SpanBuilder newSpan(HttpRequest<?> request, SpanContext spanContext) {
/**
* Tests if the defined path should be excluded from tracing.
*
* @param path the path to test.
* @return {@code true} if the path should be excluded, {@code false} otherwise.
* @param path the path to test
* @return {@code true} if the path should be excluded
*/
protected boolean shouldExclude(@Nullable String path) {
return pathExclusionTest != null && path != null && pathExclusionTest.test(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ public OpenTracingClientFilter(Tracer tracer) {
/**
* Initialize the open tracing client filter with tracer and exclusion configuration.
*
* @param tracer the tracer for span creation and configuring across arbitrary transports
* @param exclusionsConfiguration The {@link TracingExclusionsConfiguration}
* @param tracer the tracer for span creation and configuring across arbitrary transports
* @param exclusionsConfig The {@link TracingExclusionsConfiguration}
*/
@Inject
public OpenTracingClientFilter(Tracer tracer, @Nullable TracingExclusionsConfiguration exclusionsConfiguration) {
super(tracer, exclusionsConfiguration == null ? null : exclusionsConfiguration.exclusionTest());
public OpenTracingClientFilter(Tracer tracer,
@Nullable TracingExclusionsConfiguration exclusionsConfig) {
super(tracer, exclusionsConfig == null ? null : exclusionsConfig.exclusionTest());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ public OpenTracingServerFilter(Tracer tracer) {
/**
* Creates an HTTP server instrumentation filter.
*
* @param tracer for span creation and propagation across transport
* @param exclusionsConfiguration The {@link TracingExclusionsConfiguration}
* @param tracer for span creation and propagation across transport
* @param exclusionsConfig The {@link TracingExclusionsConfiguration}
*/
@Inject
public OpenTracingServerFilter(Tracer tracer, @Nullable TracingExclusionsConfiguration exclusionsConfiguration) {
super(tracer, exclusionsConfiguration == null ? null : exclusionsConfiguration.exclusionTest());
public OpenTracingServerFilter(Tracer tracer,
@Nullable TracingExclusionsConfiguration exclusionsConfig) {
super(tracer, exclusionsConfig == null ? null : exclusionsConfig.exclusionTest());
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@
import java.util.stream.Collectors;

/**
* @since 3.3.0
* @since 4.0
*/
@ConfigurationProperties(TracingExclusionsConfiguration.PREFIX)
public class TracingExclusionsConfiguration {

public static final String PREFIX = "tracing";

private List<String> exclusions;

/**
* @return The URI patterns to exclude from the tracing.
* @return the URI patterns to exclude from the tracing
*/
@Nullable public List<String> getExclusions() {
@Nullable
public List<String> getExclusions() {
return exclusions;
}

/**
* Sets the URI patterns to be excluded from tracing.
*
* @param exclusions A list of regular expression patterns to be excluded from tracing if the request URI matches.
* @param exclusions regular expression patterns to be excluded from
* tracing if the request URI matches
*
* @see Pattern#compile(String)
*/
Expand All @@ -52,15 +55,19 @@ public void setExclusions(@Nullable List<String> exclusions) {
}

/**
* @return Either null (implying everything should be included), or a Predicate which when given a URL path returns
* whether that path should be excluded from tracing.
* @return null (implying everything should be included), or a Predicate
* which, when given a URL path, returns whether that path should
* be excluded from tracing.
*/
@Nullable public Predicate<String> exclusionTest() {
@Nullable
public Predicate<String> exclusionTest() {
if (CollectionUtils.isEmpty(exclusions)) {
return null;
} else {
List<Pattern> patterns = exclusions.stream().map(Pattern::compile).collect(Collectors.toList());
return uri -> patterns.stream().anyMatch(pattern -> pattern.matcher(uri).matches());
}

List<Pattern> patterns = exclusions.stream()
.map(Pattern::compile)
.collect(Collectors.toList());
return uri -> patterns.stream().anyMatch(pattern -> pattern.matcher(uri).matches());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TracingExclusionsConfigurationSpec extends Specification {
Predicate<String> pathPredicate = configuration.exclusionTest()

@Unroll
def "path #path is #desc by predicate"() {
void 'path #path is #desc by predicate'() {
expect:
pathPredicate.test(path) == excluded

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.micronaut.tracing.jaeger

import io.micronaut.context.ApplicationContext
import io.opentracing.Scope
import io.opentracing.Span
import io.opentracing.Tracer
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import spock.lang.Specification

/**
* Regression test: when executing certain reactive operations the ScopeManager's internal state can become permanently
* corrupted resulting in incorrect tracing.
*
* @author lgathy
*/
class OpenTracingInvocationInstrumenterSpec extends Specification {

void "test regression of corrupted ScopeManager state"() {
given: 'Jaeger tracer is enabled'
ApplicationContext context = ApplicationContext.run('tracing.jaeger.enabled': true)
Tracer tracer = context.getBean(Tracer)
String[] words = ['one', 'two', 'three']

expect: 'no active span'
tracer.activeSpan() == null

when: 'reactive operations are executed inside a span'
Span rootSpan = tracer.buildSpan('root').start()
Scope scope = tracer.activateSpan(rootSpan)
String combined = Flux
.merge(words.collect { Mono.just(it).flux() })
.reduce { a, b -> "$a, $b" }
.block()
scope.close()
rootSpan.finish()

then: 'there should be no active span after it was finished'
combined.split(", ").sort() == words.sort()
tracer.activeSpan() == null
}
}

0 comments on commit eea16ca

Please sign in to comment.