-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from raynigon/feature/okhttp-interceptor
Add Interceptor for OkHttp3
- Loading branch information
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,11 @@ | ||
dependencies { | ||
implementation(project(':ecs-logging-base')) | ||
implementation("org.slf4j:slf4j-api:2.0.11") | ||
compileOnly("com.squareup.okhttp3:okhttp:4.12.0") | ||
compileOnly("ch.qos.logback:logback-core") | ||
|
||
testImplementation("ch.qos.logback:logback-core") | ||
testImplementation("ch.qos.logback:logback-classic") | ||
testImplementation("com.squareup.okhttp3:okhttp:4.12.0") | ||
testImplementation("org.springframework.boot:spring-boot-starter-test") | ||
} |
33 changes: 33 additions & 0 deletions
33
...g-okhttp3/src/main/java/com/raynigon/ecs/logging/okhttp3/EcsTransactionIdInterceptor.java
This file contains 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,33 @@ | ||
package com.raynigon.ecs.logging.okhttp3; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.slf4j.MDC; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
import static com.raynigon.ecs.logging.LoggingConstants.TRANSACTION_ID_HEADER; | ||
import static com.raynigon.ecs.logging.LoggingConstants.TRANSACTION_ID_PROPERTY; | ||
|
||
class EcsTransactionIdInterceptor implements Interceptor { | ||
|
||
@NotNull | ||
@Override | ||
public Response intercept(Interceptor.Chain chain) throws IOException { | ||
Request original = chain.request(); | ||
String transactionId = MDC.get(TRANSACTION_ID_PROPERTY); | ||
// If no transaction id exists, generate custom transaction id | ||
if (transactionId == null) { | ||
transactionId = UUID.randomUUID().toString(); | ||
MDC.put(TRANSACTION_ID_PROPERTY, transactionId); | ||
} | ||
// Update Request with transaction id header | ||
Request updated = original.newBuilder() | ||
.header(TRANSACTION_ID_HEADER, transactionId) | ||
.build(); | ||
return chain.proceed(updated); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...3/src/test/groovy/com/raynigon/ecs/logging/okhttp3/EcsTransactionIdInterceptorSpec.groovy
This file contains 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,67 @@ | ||
package com.raynigon.ecs.logging.okhttp3 | ||
|
||
import static com.raynigon.ecs.logging.LoggingConstants.TRANSACTION_ID_HEADER | ||
import static com.raynigon.ecs.logging.LoggingConstants.TRANSACTION_ID_PROPERTY | ||
|
||
import okhttp3.Protocol | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import org.slf4j.MDC | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
import okhttp3.Interceptor | ||
|
||
class EcsTransactionIdInterceptorSpec extends Specification { | ||
|
||
@Subject | ||
EcsTransactionIdInterceptor interceptor = new EcsTransactionIdInterceptor() | ||
|
||
def "transaction id header exists"() { | ||
given: | ||
Interceptor.Chain chain = Mock() | ||
Request request = new Request.Builder().url("https://example.com").build() | ||
Response response = new Response.Builder().request(request).code(200) | ||
.protocol(Protocol.HTTP_1_1) | ||
.message("dummy") | ||
.build() | ||
|
||
when: | ||
def result = interceptor.intercept(chain) | ||
|
||
then: | ||
1 * chain.request() >> request | ||
1 * chain.proceed({ Request r -> r.header(TRANSACTION_ID_HEADER) != null }) >> response | ||
|
||
and: | ||
result == response | ||
|
||
cleanup: | ||
MDC.clear() | ||
} | ||
|
||
def "transaction id header is set from MDC Tag"() { | ||
given: | ||
Interceptor.Chain chain = Mock() | ||
Request request = new Request.Builder().url("https://example.com").build() | ||
Response response = new Response.Builder().request(request).code(200) | ||
.protocol(Protocol.HTTP_1_1) | ||
.message("dummy") | ||
.build() | ||
|
||
and: | ||
MDC.put(TRANSACTION_ID_PROPERTY, "my-value") | ||
|
||
when: | ||
def result = interceptor.intercept(chain) | ||
|
||
then: | ||
1 * chain.request() >> request | ||
1 * chain.proceed({ Request r -> r.header(TRANSACTION_ID_HEADER) == "my-value" }) >> response | ||
|
||
and: | ||
result == response | ||
|
||
cleanup: | ||
MDC.clear() | ||
} | ||
} |
This file contains 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