Skip to content

Commit

Permalink
Merge pull request #931 from MarathonLabs/feature/analytics-read-only
Browse files Browse the repository at this point in the history
feat(core): add readOnly property for analytics
  • Loading branch information
Malinskiy authored May 8, 2024
2 parents 0fbc3c5 + 8170555 commit 6a8fe84
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sealed class AnalyticsConfiguration {
val dbName: String,
val retentionPolicyConfiguration: RetentionPolicyConfiguration,
val defaults: Defaults = Defaults(),
val readOnly: Boolean = false,
) : AnalyticsConfiguration() {
data class RetentionPolicyConfiguration(
val name: String,
Expand Down Expand Up @@ -52,6 +53,7 @@ sealed class AnalyticsConfiguration {
val bucket: String,
val retentionPolicyConfiguration: RetentionPolicyConfiguration = RetentionPolicyConfiguration.default,
val defaults: Defaults = Defaults(),
val readOnly: Boolean = false,
) : AnalyticsConfiguration() {
data class RetentionPolicyConfiguration(
val everySeconds: Int,
Expand All @@ -74,5 +76,6 @@ sealed class AnalyticsConfiguration {
val port: Int?,
val prefix: String?,
val defaults: Defaults = Defaults(),
val readOnly: Boolean = false,
) : AnalyticsConfiguration()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class GraphiteMetricsProviderIntegrationTest : BaseMetricsProviderIntegrationTes
@JvmStatic
fun `start container and prepare data`() {
container.start()
val tracker = GraphiteTracker(BasicGraphiteClient(host, container.getMappedPort(port), prefix))
val tracker = GraphiteTracker(BasicGraphiteClient(host, container.getMappedPort(port), prefix), readOnly = false)
getTestEvents().forEach { tracker.track(it) }
// Graphite needs a couple of seconds to process the data. There's no way to wait for it in a more intelligent manner.
Thread.sleep(5_000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class InfluxDbProviderIntegrationTest {


val secondDbInstance = provider.createDb()
val tracker = InfluxDbTracker(secondDbInstance, database, rpName)
val tracker = InfluxDbTracker(secondDbInstance, database, rpName, readOnly = false)
getTestEvents().forEach { tracker.track(it) }
secondDbInstance.close()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class InfluxMetricsProviderIntegrationTest : BaseMetricsProviderIntegrationTest(
@JvmStatic
fun `start influx and insert test data`() {
container.start()
val tracker = InfluxDbTracker(influxDb, dbName, rpName)
val tracker = InfluxDbTracker(influxDb, dbName, rpName, readOnly = false)
getTestEvents().forEach { tracker.track(it) }
influxDb.flush()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Influx2MetricsProviderIntegrationTest : BaseMetricsProviderIntegrationTest
@JvmStatic
fun `start influx and insert test data`() {
container.start()
val tracker = InfluxDb2Tracker(influxDb)
val tracker = InfluxDb2Tracker(influxDb, readOnly = false)
getTestEvents().forEach { tracker.track(it) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class InfluxDb2ProviderIntegrationTest {
firstDbInstance.close()

val secondDbInstance = provider.createDb()
val tracker = InfluxDb2Tracker(secondDbInstance)
val tracker = InfluxDb2Tracker(secondDbInstance, readOnly = false)
val test1 = com.malinskiy.marathon.test.Test("pkg", "clazz", "method", emptyList())
val test2 = com.malinskiy.marathon.test.Test("pkg", "clazz", "method2", emptyList())
getTestEvents(test = test1).forEach { tracker.track(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal class TrackerFactory(
log.warn(e) { "Failed to reach InfluxDB at ${config.url}" }
null
}
return db?.let { InfluxDbTracker(it, config.dbName, config.retentionPolicyConfiguration.name) }
return db?.let { InfluxDbTracker(it, config.dbName, config.retentionPolicyConfiguration.name, config.readOnly) }
}

private fun createInfluxDb2Tracker(config: InfluxDb2Configuration): InfluxDb2Tracker? {
Expand All @@ -80,11 +80,11 @@ internal class TrackerFactory(
log.warn(e) { "Failed to reach InfluxDB at ${config.url}" }
null
}
return db?.let { InfluxDb2Tracker(it) }
return db?.let { InfluxDb2Tracker(it, config.readOnly) }
}

private fun createGraphiteTracker(config: GraphiteConfiguration): GraphiteTracker {
return GraphiteTracker(BasicGraphiteClient(config.host, config.port ?: 2003, config.prefix))
return GraphiteTracker(BasicGraphiteClient(config.host, config.port ?: 2003, config.prefix), config.readOnly)
}

private fun createExecutionReportGenerator(): ExecutionReportGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import com.malinskiy.marathon.test.Test
import com.malinskiy.marathon.test.toSafeTestName

class GraphiteTracker(
private val graphite: GraphiteClient
private val graphite: GraphiteClient,
private val readOnly: Boolean,
) : TrackerInternalAdapter() {

override fun trackTest(event: TestEvent) {
if (readOnly) return

if (event.testResult.status in arrayOf(TestStatus.FAILURE, TestStatus.PASSED)) {
val testResult = event.testResult
val device = event.device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import java.util.concurrent.TimeUnit
class InfluxDbTracker(
private val influxDb: InfluxDB,
private val dbName: String,
private val rpName: String
private val rpName: String,
private val readOnly: Boolean,
) : TrackerInternalAdapter() {

override fun trackTest(event: TestEvent) {
if (readOnly) return

//Report only success and failure
if (event.testResult.status in arrayOf(TestStatus.FAILURE, TestStatus.PASSED)) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import com.malinskiy.marathon.log.MarathonLogging
import com.malinskiy.marathon.test.toSafeTestName

class InfluxDb2Tracker(
private val client: InfluxDBClient
private val client: InfluxDBClient,
private val readOnly: Boolean,
) : TrackerInternalAdapter() {
private val writeApi by lazy { client.writeApiBlocking }
private val logger = MarathonLogging.logger {}

override fun trackTest(event: TestEvent) {
if (readOnly) return

val testResult = event.testResult
val device = event.device
try {
Expand Down
8 changes: 8 additions & 0 deletions docs/runner/configuration/analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ analyticsConfiguration:
defaults:
successRate: 0.1
duration: "PT300S"
readOnly: false
```
</TabItem>
Expand All @@ -57,6 +58,7 @@ marathon {
organization = "starlabs"
bucket = "marathon"
defaults = Defaults(0.0, Duration.ofMinutes(5))
readOnly = false
}
}
}
Expand Down Expand Up @@ -97,6 +99,7 @@ analyticsConfiguration:
defaults:
successRate: 0.1
duration: "PT300S"
readOnly: false
```
</TabItem>
Expand All @@ -111,6 +114,7 @@ marathon {
password = "root"
dbName = "marathon"
defaults = Defaults(0.0, Duration.ofMinutes(5))
readOnly = false
}
}
}
Expand All @@ -128,6 +132,7 @@ marathon {
password = "root"
dbName = "marathon"
defaults = Defaults(0.0, Duration.ofMinutes(5))
readOnly = false
}
}
}
Expand Down Expand Up @@ -156,6 +161,7 @@ analyticsConfiguration:
defaults:
successRate: 0.1
duration: "PT300S"
readOnly: false
```
</TabItem>
Expand All @@ -169,6 +175,7 @@ marathon {
port = "8080"
prefix = "prf"
defaults = Defaults(0.0, Duration.ofMinutes(5))
readOnly = false
}
}
}
Expand All @@ -185,6 +192,7 @@ marathon {
port = "8080"
prefix = "prf"
defaults = Defaults(0.0, Duration.ofMinutes(5))
readOnly = false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class InfluxConfig {
var dbName: String = ""
var retentionPolicy: RetentionPolicy? = null
var defaults: Defaults = Defaults()
var readOnly: Boolean = false
}

class Influx2Config {
Expand All @@ -46,6 +47,7 @@ class Influx2Config {
var bucket: String = ""
var retentionPolicy: Influx2RetentionPolicy? = null
var defaults: Defaults = Defaults()
var readOnly: Boolean = false
}

class Influx2RetentionPolicy {
Expand All @@ -66,6 +68,7 @@ class GraphiteConfig {
var port: String? = null
var prefix: String? = null
var defaults: Defaults = Defaults()
var readOnly: Boolean = false
}

fun AnalyticsConfig.toAnalyticsConfiguration(): AnalyticsConfiguration {
Expand All @@ -80,7 +83,8 @@ fun AnalyticsConfig.toAnalyticsConfiguration(): AnalyticsConfiguration {
bucket = influx2.bucket,
retentionPolicyConfiguration = influx2.retentionPolicy?.toRetentionPolicy()
?: AnalyticsConfiguration.InfluxDb2Configuration.RetentionPolicyConfiguration.default,
defaults = influx2.defaults
defaults = influx2.defaults,
readOnly = influx2.readOnly
)
influx != null -> AnalyticsConfiguration.InfluxDbConfiguration(
dbName = influx.dbName,
Expand All @@ -89,13 +93,15 @@ fun AnalyticsConfig.toAnalyticsConfiguration(): AnalyticsConfiguration {
url = influx.url,
retentionPolicyConfiguration = influx.retentionPolicy?.toRetentionPolicy()
?: AnalyticsConfiguration.InfluxDbConfiguration.RetentionPolicyConfiguration.default,
defaults = influx.defaults
defaults = influx.defaults,
readOnly = influx.readOnly
)
graphite != null -> AnalyticsConfiguration.GraphiteConfiguration(
host = graphite.host,
port = graphite.port?.toIntOrNull(),
prefix = graphite.prefix,
defaults = graphite.defaults
defaults = graphite.defaults,
readOnly = graphite.readOnly
)
else -> AnalyticsConfiguration.DisabledAnalytics
}
Expand Down

0 comments on commit 6a8fe84

Please sign in to comment.