Skip to content

Commit

Permalink
reproducing dns timeout bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyghe committed Feb 2, 2022
1 parent 533bb95 commit 8c727cc
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 5 deletions.
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM openjdk:11-slim as builder

ENV OTEL_RESOURCE_ATTRIBUTES=service.name=ratpack-kotlin-otel
ENV OTEL_INSTRUMENTATION_KAFKA_ENABLED=false
ENV OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://jaegerallinone:9411/api/v2/spans
ENV OTEL_PROPAGATORS=b3,tracecontext
ENV OTEL_METRICS_EXPORTER=none
ENV OTEL_INSTRUMENTATION_CASSANDRA_ENABLED=false
ENV OTEL_TRACES_EXPORTER=zipkin
ENV OTEL_INSTRUMENTATION_NETTY_ALWAYS_CREATE_SPAN=false
ENV RATPACK_SERVER__ADDRESS=0.0.0.0

COPY run.sh /
COPY build/distributions/*.tar /
COPY otel /otel
RUN cd /tmp/ \
&& tar xvf /*.tar \
&& mv ratpack-kotlin-otel* /src
WORKDIR /src

ENTRYPOINT ["/run.sh"]
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# ratpack-kotlin-otel
Simple project to debug ratpack+kotlin+otel libraries

# Dependent services
```
docker-compose up
```

View traces in [Jaeger UI](http://localhost:16686)
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ services:
build: downstream-app
ports:
- "9999:9999"

otel-ratpack-kotlin:
build: .
ports:
- "5050:5050"
9 changes: 9 additions & 0 deletions downstream-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Simple api to do timeouts easily

```
docker build -t testing . && docker run --rm -it -p 9999:9999 testing
```

```
curl http://localhost:9999/api/v1/timeout?duration=10s
```
6 changes: 6 additions & 0 deletions downstream-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ func timeoutHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"status\": \"up\"}"))
w.WriteHeader(200)
}

func addRoutes() {
http.HandleFunc("/api/v1/timeout", timeoutHandler)
http.HandleFunc("/health", healthHandler)
}

func main() {
Expand Down
Binary file added otel/otel.1.10.1.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion otel/otel.jar
5 changes: 5 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Why does java have to be so hard?

java -javaagent:/otel/otel.jar -classpath /src/app:$(find /src/lib | xargs | tr ' ' ':') sample.MainKt
5 changes: 5 additions & 0 deletions src/main/kotlin/exampleapp/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ratpack.service.StartEvent
import ratpackkotlinotel.promisesession.DefaultPromiseSession
import ratpackkotlinotel.promisesession.PromiseSession
import ratpackkotlinotel.promisesession.PromiseSessionProvider
import java.net.InetAddress
import java.net.InetSocketAddress
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -30,6 +31,10 @@ fun main() {
fun app(bindings: List<Any> = listOf()) = ratpack {
System.setProperty("log4j2.contextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector")

serverConfig {
env()
}

bindings {
for (b in bindings) {
bindInstance(b)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/exampleapp/data/RefreshItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class RefreshItem(
var userLast: User,
var timeout: Int,
var hostUnknown: Int,
var connectError: Int
var connectError: Int,
var hostUnreachable: Int
)
32 changes: 31 additions & 1 deletion src/main/kotlin/exampleapp/service/JsonPlaceholder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,31 @@ class JsonPlaceholder @Inject constructor(
.map {
1
}.mapError {
println("an error was seen")
println("Caught error trying to do an http request to ${host}:${port}")
1
}
}

fun getURLBody(host: String, path: String, port: Int, params: Map<String, *>): Promise<ReceivedResponse> {
val url = HttpUrlBuilder
.http()
.host(host)
.port(port)
.path(path)
.params(params)
.build()
println(url)

return httpClient.copyWith {
it.readTimeout(Duration.ofSeconds(1))
it.connectTimeout( Duration.ofSeconds(1))
}
.get(url)
.map {
it
}
}

fun getURLThatHasTimeout(): Promise<Int> {
return getErrorURL("localhost", "api/v1/timeout", 9999, mapOf("duration" to "60s"))
}
Expand All @@ -116,4 +136,14 @@ class JsonPlaceholder @Inject constructor(
fun getURLThatHasConnectError(): Promise<Int> {
return getErrorURL("localhost", "", 9998, emptyMap<String, String>())
}

fun getHostUnreachableError(): Promise<Int> {
return getErrorURL("192.168.5.1", "", 9999, emptyMap<String, String>())
}

fun getLocalURL(hostname: String): Promise<String> {
return getURLBody(hostname, "/health", 9999, mapOf("duration" to "1ms")).map {
it.body.text
}
}
}
10 changes: 8 additions & 2 deletions src/main/kotlin/exampleapp/service/Refresh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class Refresh (
userLast = userLast.await(),
timeout = doHttpTimeout.await(),
hostUnknown = doHttpHostNotFound.await(),
connectError = doHttpConnectError.await()
connectError = doHttpConnectError.await(),
hostUnreachable = doHttpHostUnreachable.await()
)
}

Expand Down Expand Up @@ -73,6 +74,10 @@ class Refresh (
refreshService.jsonPlaceholder.getURLThatHasConnectError().await()
}

private val doHttpHostUnreachable: Deferred<Int> by lazyAsync {
refreshService.jsonPlaceholder.getHostUnreachableError().await()
}

private fun startCoroutines() {
val coroutines = listOf(
createPost,
Expand All @@ -82,7 +87,8 @@ class Refresh (
userLast,
doHttpTimeout,
doHttpHostNotFound,
doHttpConnectError
doHttpConnectError,
doHttpHostUnreachable
)
coroutines.forEach { deferredFunc ->
deferredFunc.start()
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/http/V1Chain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,32 @@ class V1Chain @Inject constructor(
get("hi") {
render("hey there")
}
path("connectiontimeout") {
refreshService.jsonPlaceholder.getURLThatHasConnectError().then {
render("fetched conn error host")
}
}
path("unknownhost") {
refreshService.jsonPlaceholder.getURLThatHasHostUnknown().then {
render("fetched unknown host")
}
}
path("onlyhttp/:id") {
val id = allPathTokens.getOrDefault("id", "1")
refreshService.refresh(id.toInt()).then {
render(json(it))
}
}
path("localthing") {
val qParams = this.request.queryParams
var hostname = "localhost"
if ("hostname" in qParams) {
hostname = qParams["hostname"].toString()
}
refreshService.jsonPlaceholder.getLocalURL(hostname).then {
render("did local url for $hostname<br/>$it")
}
}
// path("cassandrahttp/:id") {
// val id = allPathTokens.getOrDefault("id", "1")
// val promises = listOf(
Expand Down

0 comments on commit 8c727cc

Please sign in to comment.