-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #792 from MarathonLabs/feature/grafana-cloud
feat(analytics): migrate to grafana cloud
- Loading branch information
Showing
29 changed files
with
233 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
analytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/Constants.kt
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
analytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/Event.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.malinskiy.marathon.usageanalytics | ||
|
||
sealed class Event { | ||
data class TestsTotal( | ||
val total: Int | ||
) : Event() | ||
|
||
data class TestsRun( | ||
val value: Int | ||
) : Event() | ||
|
||
data class Devices( | ||
val total: Int | ||
) : Event() | ||
|
||
data class Executed( | ||
val seconds: Long | ||
) : Event() | ||
} |
12 changes: 0 additions & 12 deletions
12
analytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/TrackActionType.kt
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
analytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/UsageAnalytics.kt
This file was deleted.
Oops, something went wrong.
10 changes: 6 additions & 4 deletions
10
...ytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/tracker/EmptyTracker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package com.malinskiy.marathon.usageanalytics.tracker | ||
|
||
internal class EmptyTracker : UsageTracker { | ||
override fun trackEvent(event: Event) { | ||
import com.malinskiy.marathon.usageanalytics.Event | ||
|
||
} | ||
} | ||
class EmptyTracker : UsageTracker { | ||
override fun trackEvent(event: Event) = Unit | ||
override fun meta(version: String, vendor: String, releaseMode: String) = Unit | ||
override fun close() = Unit | ||
} |
8 changes: 0 additions & 8 deletions
8
analytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/tracker/Event.kt
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
...e/src/main/kotlin/com/malinskiy/marathon/usageanalytics/tracker/GoogleAnalyticsTracker.kt
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
...ytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/tracker/GrafanaCloud.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.malinskiy.marathon.usageanalytics.tracker | ||
|
||
import com.malinskiy.marathon.usageanalytics.Event | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import java.lang.Exception | ||
import java.util.UUID | ||
|
||
class GrafanaCloud : UsageTracker { | ||
private val client = OkHttpClient() | ||
private val key = | ||
"967310:eyJrIjoiMmEzNWM4ZmI2ZTExNjg1OWJiZDUxOWE3YWU1N2NiMTI5MDRhOTk2ZiIsIm4iOiJtYXJhdGhvbi1vc3MtcHVibGlzaGluZyIsImlkIjo4NDcyNDZ9" | ||
private val accumulator = mutableListOf<Event>() | ||
private val tags = mutableMapOf<String, String>() | ||
|
||
override fun trackEvent(event: Event) { | ||
accumulator.add(event) | ||
} | ||
|
||
override fun meta(version: String, vendor: String, releaseMode: String) { | ||
tags["version"] = version | ||
tags["releaseMode"] = releaseMode | ||
tags["vendor"] = vendor | ||
tags["id"] = UUID.randomUUID().toString() | ||
} | ||
|
||
private fun sendEvents(events: Collection<Event>) { | ||
val body = convertToJson(events) | ||
send(body) | ||
} | ||
|
||
private fun convertToJson(events: Collection<Event>): String { | ||
return StringBuilder().apply { | ||
append("[") | ||
append( | ||
events.map { event -> | ||
when (event) { | ||
is Event.Devices -> Metric(name = "testing.device", value = event.total, tags = tags) | ||
is Event.Executed -> Metric(name = "testing.duration", value = event.seconds, tags = tags) | ||
is Event.TestsTotal -> Metric(name = "testing.test", value = event.total, tags = tags) | ||
is Event.TestsRun -> Metric(name = "testing.executed", value = event.value, tags = tags) | ||
} | ||
}.joinToString(separator = ",") { it.toJson() } | ||
) | ||
append("]") | ||
}.toString() | ||
} | ||
|
||
override fun close() { | ||
sendEvents(accumulator) | ||
} | ||
|
||
private fun send(body: String) { | ||
try { | ||
val request = Request.Builder() | ||
.url("https://graphite-prod-13-prod-us-east-0.grafana.net/graphite/metrics") | ||
.header("Authorization", "Bearer $key") | ||
.post(body.toByteArray().toRequestBody("application/json".toMediaType())) | ||
.build() | ||
|
||
client.newCall(request).execute().use { response -> | ||
if (!response.isSuccessful) { | ||
//se la vie, we shouldn't handle analytics errors because users don't care about them | ||
} | ||
} | ||
} catch (e: Exception) { | ||
//se la vie, we shouldn't handle analytics errors because users don't care about them | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
analytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/tracker/Metric.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.malinskiy.marathon.usageanalytics.tracker | ||
|
||
data class Metric( | ||
val name: String, | ||
val value: Number, | ||
val time: Long = System.currentTimeMillis() / 1000, | ||
val interval: Int = 1, | ||
val tags: Map<String, String> = emptyMap(), | ||
) { | ||
fun toJson() = """{"name":"$name","interval":$interval,"value":$value,"time":$time,"tags":[${ | ||
tags.map { "${it.key}=${it.value}" }.joinToString(separator = ",") { "\"$it\""} | ||
}]}""" | ||
} |
6 changes: 5 additions & 1 deletion
6
...ytics/usage/src/main/kotlin/com/malinskiy/marathon/usageanalytics/tracker/UsageTracker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package com.malinskiy.marathon.usageanalytics.tracker | ||
|
||
import com.malinskiy.marathon.usageanalytics.Event | ||
|
||
interface UsageTracker { | ||
fun trackEvent(event: Event) | ||
} | ||
fun meta(version: String, vendor: String, releaseMode: String) | ||
fun close() | ||
} |
20 changes: 20 additions & 0 deletions
20
analytics/usage/src/test/kotlin/com/malinskiy/marathon/usageanalytics/tracker/MetricTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.malinskiy.marathon.usageanalytics.tracker | ||
|
||
import org.amshove.kluent.`should be equal to` | ||
import org.junit.jupiter.api.Test | ||
|
||
|
||
class MetricTest { | ||
|
||
@Test | ||
fun toJson() { | ||
val metric = Metric( | ||
name = "testing.count", | ||
interval = 1, | ||
value = 42, | ||
time = 1000, | ||
tags = mapOf("id" to "123") | ||
) | ||
metric.toJson().`should be equal to`("""{"name":"testing.count","interval":1,"value":42,"time":1000,"tags":["id=123"]}""") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.