Skip to content

Commit

Permalink
Merge pull request #24 from agorapulse/fix/sc-71389-pop-scope-on-error
Browse files Browse the repository at this point in the history
[sc71389] Pop Sentry scope on error
  • Loading branch information
musketyr committed Apr 13, 2022
2 parents 9662e49 + c4b886b commit 391665b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions subprojects/micronaut-log4aws/micronaut-log4aws.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
testAnnotationProcessor "io.micronaut:micronaut-inject-java"

testImplementation "io.micronaut:micronaut-inject-groovy"
testImplementation "io.micronaut:micronaut-validation"
testImplementation "io.micronaut:micronaut-http-server-netty"

testImplementation "com.github.stefanbirkner:system-lambda:$systemLambdaVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.agorapulse.micronaut.log4aws.http;

import io.micronaut.core.order.Ordered;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.MutableHttpResponse;
import io.micronaut.http.annotation.Filter;
Expand All @@ -36,6 +37,11 @@ public SentryFilter(IHub hub) {
this.hub = hub;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE + 1000;
}

@Override
public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
return Flowable
Expand All @@ -46,6 +52,7 @@ public Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, Server
hub.configureScope(scope -> scope.addEventProcessor(new MicronautRequestEventProcessor(request)));
})
.switchMap(chain::proceed)
.doOnError(throwable -> hub.popScope())
.doOnNext(res -> hub.popScope());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import io.sentry.IHub
import spock.lang.AutoCleanup
import spock.lang.Specification

import java.util.concurrent.atomic.AtomicInteger

class SentryFilterSpec extends Specification {

@AutoCleanup Gru gru = Gru.create(Http.create(this))
Expand Down Expand Up @@ -57,11 +59,18 @@ class SentryFilterSpec extends Specification {
then:
gru.verify()

1 * hub.pushScope()
1 * hub.addBreadcrumb(_)
1 * hub.configureScope(_)
1 * hub.popScope()
}

void 'try error message'() {
given:
AtomicInteger pushCalls = new AtomicInteger()
AtomicInteger breadcrumbsCalls = new AtomicInteger()
AtomicInteger configureScopeCalls = new AtomicInteger()
AtomicInteger popCalls = new AtomicInteger()
when:
gru.test {
post('/test/parameter')
Expand All @@ -72,8 +81,62 @@ class SentryFilterSpec extends Specification {
then:
gru.verify()

_ * hub.addBreadcrumb(_)
_ * hub.configureScope(_)
_ * hub.pushScope() >> {
pushCalls.incrementAndGet()
}
_ * hub.addBreadcrumb(_) >> {
breadcrumbsCalls.incrementAndGet()
}
_ * hub.configureScope(_) >> {
configureScopeCalls.incrementAndGet()
}
_ * hub.popScope() >> {
popCalls.incrementAndGet()
}

expect:
// the filter is only called once for Micronaut 3.x but twice for Micronaut 1.x and 2.x
pushCalls.get() in 1..2
pushCalls.get() == breadcrumbsCalls.get()
pushCalls.get() == configureScopeCalls.get()
pushCalls.get() == popCalls.get()
}

void 'try validation'() {
given:
AtomicInteger pushCalls = new AtomicInteger()
AtomicInteger breadcrumbsCalls = new AtomicInteger()
AtomicInteger configureScopeCalls = new AtomicInteger()
AtomicInteger popCalls = new AtomicInteger()
when:
gru.test {
put('/test/validated')
expect {
status BAD_REQUEST
}
}
then:
gru.verify()

_ * hub.pushScope() >> {
pushCalls.incrementAndGet()
}
_ * hub.addBreadcrumb(_) >> {
breadcrumbsCalls.incrementAndGet()
}
_ * hub.configureScope(_) >> {
configureScopeCalls.incrementAndGet()
}
_ * hub.popScope() >> {
popCalls.incrementAndGet()
}

expect:
// the filter is only called once for Micronaut 3.x but twice for Micronaut 1.x and 2.x
pushCalls.get() in 1..2
pushCalls.get() == breadcrumbsCalls.get()
pushCalls.get() == configureScopeCalls.get()
pushCalls.get() == popCalls.get()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.PathVariable
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.Put
import io.micronaut.validation.Validated

import javax.validation.constraints.NotBlank

@Validated
@CompileStatic
@Controller('/test')
class TestController {
Expand All @@ -39,4 +44,9 @@ class TestController {
throw new RuntimeException(someerror)
}

@Put('/validated')
HttpResponse<String> validationIssue(@NotBlank String text) {
return HttpResponse.ok(text)
}

}

0 comments on commit 391665b

Please sign in to comment.