Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: posting metrics fails due to stop being null #73

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/kotlin/io/getunleash/metrics/MetricsReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import java.io.Closeable
import java.io.IOException
import java.util.Date
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.TimeUnit

interface MetricsReporter {
/**
Expand Down Expand Up @@ -84,11 +83,15 @@ class HttpMetricsReporter(
private var bucket: Bucket = Bucket(start = started)

override fun reportMetrics() {
val stop = Date()
val bucketRef = bucket
bucket = Bucket(start = stop)
val clonedMetrics = bucketRef.copy(stop = stop)
Comment on lines +87 to +89
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediately swap variables so new counts are against the new bucket, only then make a copy. If due to a race condition a count is still counted against the old bucket it'll still be counted, so we should not lose data

val report = Report(
appName = config.appName ?: "unknown",
instanceId = config.instanceId ?: "not-set",
environment = config.environment ?: "not-set",
bucket = bucket
bucket = clonedMetrics
)
val request = Request.Builder().header("Authorization", config.clientKey).url(metricsUrl).post(
Parser.jackson.writeValueAsString(report).toRequestBody("application/json".toMediaType())
Expand All @@ -103,7 +106,7 @@ class HttpMetricsReporter(
}
}
})
bucket = Bucket(start = Date())

}

override fun log(featureName: String, enabled: Boolean): Boolean {
Expand Down
39 changes: 39 additions & 0 deletions src/test/kotlin/io/getunleash/metrics/HttpMetricsReporterTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package io.getunleash.metrics

import com.fasterxml.jackson.databind.ObjectMapper
import io.getunleash.UnleashConfig
import io.getunleash.UnleashContext
import io.getunleash.data.Variant
import io.getunleash.polling.PollingModes
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.File
import java.time.format.DateTimeFormatter
import java.util.*
import java.util.concurrent.TimeUnit

class HttpMetricsReporterTest {
Expand All @@ -19,4 +26,36 @@ class HttpMetricsReporterTest {
}
}

@Test
fun metricsFormatIsCorrect() {
val testResponse = File(MetricsTest::class.java.classLoader.getResource("proxyresponse.json")!!.file)
val server = MockWebServer()
server.start()

val config = UnleashConfig.newBuilder()
.pollingMode(PollingModes.fileMode(testResponse))
.proxyUrl(server.url("/proxy").toString())
.clientKey("some-key")
.appName("metrics-test")
.instanceId("test-instance")
.environment("test")
.build()
val okHttpClient = OkHttpClient.Builder().build()
HttpMetricsReporter(config, okHttpClient).reportMetrics()
// This line blocks until a request is received or the timeout expires
val recordedRequest = server.takeRequest()
val requestBody = recordedRequest.body.readUtf8()
val objectMapper = ObjectMapper()
val response = objectMapper.readTree(requestBody)
assertThat(response.get("appName").asText()).isEqualTo("metrics-test")
assertThat(response.get("environment").asText()).isEqualTo("test")
assertThat(response.get("instanceId").asText()).isEqualTo("test-instance")

val today = Date().toInstant().atZone(TimeZone.getTimeZone("UTC").toZoneId()).toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
// format today as yyyy-MM-dd
assertThat(response.get("bucket").get("start").asText()).startsWith(today)
assertThat(response.get("bucket").get("stop").asText()).startsWith(today)
assertThat(response.get("bucket").get("toggles").toString()).isEqualTo("{}")
}

}
Loading