Skip to content

Commit

Permalink
Merge branch 'feat/kotlin-migration' of github.com:FireTail-io/fireta…
Browse files Browse the repository at this point in the history
…il-java-lib into feat/kotlin-migration
  • Loading branch information
muhammadn committed Dec 5, 2023
2 parents bfd9baf + 87b1186 commit 69fe1c5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
11 changes: 10 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

Requires Java 17

You will require an `application-local.yaml` file. It will look something like this:
You will require an `application-local.yml` file. It will look something like this:

```yaml
firetail:
apikey: "PS-02....441b09761c3"
url: "https://your-apiapi.logging.eu-north-west-99.sandbox.firetail.app"
## Cache control before dispatching logs to API
buffer:
# Millis
interval: 100000
# Max capacity
capacity: 5

```

Firstly, build the Firetail-Java-Library
Expand All @@ -19,6 +26,8 @@ cd ..
# Run the example
cd examples
./gradlew bootRun
# By default, you'll want to hit this endpoint 5 times before the logs are dispatched
# Otherwise hit it < 5 and wait for 10 seconds
curl http://localhost:8080/hello
```

Expand Down
31 changes: 23 additions & 8 deletions src/main/kotlin/io/firetail/logging/core/FiretailBuffer.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.firetail.logging.core

import io.firetail.logging.core.FiretailLogger.Companion.LOGGER
import io.firetail.logging.servlet.FiretailMapper
import io.firetail.logging.spring.FiretailConfig
import org.slf4j.LoggerFactory
import java.util.*
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock

class FiretailBuffer(
private val firetailConfig: FiretailConfig,
Expand All @@ -16,6 +18,7 @@ class FiretailBuffer(
private val buffer: MutableList<FiretailData> = mutableListOf()
private val scheduler: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
private val flushCallback = mutableListOf<FiretailData>()
private val bufferLock = ReentrantLock()

init {
// Schedule the periodic flush task
Expand All @@ -31,19 +34,31 @@ class FiretailBuffer(
)
}

// Caller sycronizes access before calling this function
fun add(item: FiretailData) {
buffer.add(item)
if (buffer.size >= firetailConfig.capacity) {
flush()
bufferLock.lock()
try {
buffer.add(item)
if (buffer.size >= firetailConfig.capacity) {
flush()
}
} finally {
bufferLock.unlock()
}
}

// Threadsafe - write and reset the cached data
fun flush(): String {
if (buffer.isNotEmpty()) {
LOGGER.debug("Buffer flushing ${buffer.size}")
val result = firetailTemplate.send(buffer.toList())
buffer.clear()
return firetailMapper.getResult(result)
bufferLock.lock()
try {
if (buffer.isNotEmpty()) {
LOGGER.debug("Buffer flushing ${buffer.size}")
val result = firetailTemplate.send(buffer)
buffer.clear()
return firetailMapper.getResult(result)
}
} finally {
bufferLock.unlock()
}
return ""
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/io/firetail/logging/servlet/FiretailFilter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import io.firetail.logging.core.Constants.Companion.CORRELATION_ID
import io.firetail.logging.core.Constants.Companion.OP_NAME
import io.firetail.logging.core.Constants.Companion.REQUEST_ID
import io.firetail.logging.core.FiretailBuffer
import io.firetail.logging.spring.FiretailConfig
import io.firetail.logging.core.FiretailLogger
import io.firetail.logging.core.FiretailTemplate
import io.firetail.logging.spring.FiretailConfig
import io.firetail.logging.util.FiretailMDC
import jakarta.servlet.FilterChain
import jakarta.servlet.http.HttpServletRequest
Expand Down Expand Up @@ -75,9 +75,7 @@ class FiretailFilter(
)
CompletableFuture.runAsync {
try {
synchronized(firetailBuffer) {
firetailBuffer.add(firetailLog)
}
firetailBuffer.add(firetailLog)
} catch (e: Exception) {
LOGGER.error(e.message)
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package io.firetail.logging

import io.firetail.logging.core.Constants
import io.firetail.logging.core.FiretailBuffer
import io.firetail.logging.spring.EnableFiretail
import io.firetail.logging.core.FiretailLogger
import io.firetail.logging.core.FiretailTemplate
import io.firetail.logging.servlet.FiretailFilter
import io.firetail.logging.servlet.FiretailHeaderInterceptor
import io.firetail.logging.servlet.FiretailMapper
import io.firetail.logging.spring.EnableFiretail
import io.firetail.logging.util.FiretailMDC
import io.firetail.logging.util.StringUtils
import org.assertj.core.api.Assertions.assertThat
Expand Down Expand Up @@ -37,6 +37,7 @@ import org.springframework.web.client.RestTemplate
@TestPropertySource(
properties = [
"firetail.url=http://localhost:\${wiremock.server.port}",
"firetail.buffer.capacity=5",
],
)
@EnableFiretail
Expand Down

0 comments on commit 69fe1c5

Please sign in to comment.