Skip to content

Commit

Permalink
Merge pull request #306 from raynigon/feature/okhttp-interceptor
Browse files Browse the repository at this point in the history
Add Interceptor for OkHttp3
  • Loading branch information
raynigon committed Jan 30, 2024
2 parents 0276126 + b0d3cee commit 4bb8faa
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
1 change: 0 additions & 1 deletion ecs-logging-access/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ dependencies {
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-starter-web")
testImplementation("org.springframework.boot:spring-boot-starter-webflux")

}
11 changes: 11 additions & 0 deletions ecs-logging-okhttp3/build.gradle
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")
}
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);
}
}
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()
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ include 'ecs-logging-access'
include 'ecs-logging-async'
include 'ecs-logging-audit'
include 'ecs-logging-kafka'
include 'ecs-logging-okhttp3'
include 'gzip-request-filter-starter'

0 comments on commit 4bb8faa

Please sign in to comment.