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

Telemetry Metrics #214

Merged
merged 21 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f8463b8
First pass at adding telemetry
MichaelGHSeg Feb 28, 2024
5ab0c18
Fixing metric name
MichaelGHSeg Feb 28, 2024
8f13b6d
More tags, singleton, other improvements
MichaelGHSeg Mar 4, 2024
c5170b3
Fixing missing values
MichaelGHSeg Mar 5, 2024
1adbbba
Many features added and bugs fixed. Currently does not send log data.
MichaelGHSeg Mar 22, 2024
ffde914
A few improvements around enable
MichaelGHSeg Mar 22, 2024
f1c1d73
Adding empty header to missing parameter of HTTPExceptions in tests
MichaelGHSeg Mar 22, 2024
2902722
Addressing a bunch of smaller PR comments
MichaelGHSeg Mar 23, 2024
21398d6
Custom scope & dispatcher
MichaelGHSeg Mar 25, 2024
daa39b5
Tests and some fixes
MichaelGHSeg Apr 12, 2024
9ceb010
A couple more tests
MichaelGHSeg Apr 12, 2024
c2e51cc
Fixing queue async issue
MichaelGHSeg Apr 23, 2024
4961f89
Merge branch 'main' of ssh://github.com/segmentio/analytics-kotlin in…
MichaelGHSeg Apr 23, 2024
93e4d6e
Fixing unit test
MichaelGHSeg Apr 26, 2024
c119e29
Merge branch 'MichaelGHSeg/Telemetry' of ssh://github.com/segmentio/a…
MichaelGHSeg May 1, 2024
b0809c9
Several PR fixes for concurrency and structure
MichaelGHSeg May 1, 2024
03a356f
Attempt to combine repetative error code, and metric const rename
MichaelGHSeg May 1, 2024
c14bc56
Changing tag building to a trailing lambda
MichaelGHSeg May 2, 2024
8189a58
Last few PR fixes plus a few extras
MichaelGHSeg May 2, 2024
c8d29d2
Setting inital enabled state to default, fixing even type value
MichaelGHSeg May 3, 2024
edeb8c8
Adjusting default behavior for limited release and fixing some associ…
MichaelGHSeg May 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.segment.analytics

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.segment.analytics.kotlin.core.Telemetry

import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -15,6 +16,10 @@ import org.junit.Assert.*
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
init {
Telemetry.enable = false
}

@Test
fun useAppContext() {
// Context of the app under test.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.segment.analytics.kotlin.android

import com.segment.analytics.kotlin.core.Telemetry
import org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

internal class AndroidAnalyticsKtTest {
init {
Telemetry.enable = false
}

@Test
fun `jvm initializer in android platform should failed`() {
val exception = assertThrows<Exception> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class AndroidContextCollectorTests {
private val testDispatcher = UnconfinedTestDispatcher()
private val testScope = TestScope(testDispatcher)

init {
Telemetry.enable = false
}

@Before
fun setUp() {
appContext = spyk(InstrumentationRegistry.getInstrumentation().targetContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AndroidLifecyclePluginTests {
private val testScope = TestScope(testDispatcher)

init {
Telemetry.enable = false
val packageInfo = PackageInfo()
packageInfo.versionCode = 100
packageInfo.versionName = "1.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.segment.analytics.kotlin.core.emptyJsonObject
import com.segment.analytics.kotlin.core.utilities.EncodeDefaultsJson
import com.segment.analytics.kotlin.core.utilities.EventsFileManager
import com.segment.analytics.kotlin.core.utilities.SegmentInstant
import com.segment.analytics.kotlin.core.Telemetry
import io.mockk.every
import io.mockk.mockkObject
import kotlinx.coroutines.test.runTest
Expand All @@ -32,6 +33,7 @@ class EventsFileTests {
private val directory = File("/tmp/analytics-android-test/")

init {
Telemetry.enable = false
mockkObject(SegmentInstant)
every { SegmentInstant.now() } returns Date(0).toInstant().toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.segment.analytics.kotlin.core.Configuration
import com.segment.analytics.kotlin.core.Settings
import com.segment.analytics.kotlin.core.Storage
import com.segment.analytics.kotlin.core.System
import com.segment.analytics.kotlin.core.Telemetry
import com.segment.analytics.kotlin.core.TrackEvent
import com.segment.analytics.kotlin.core.UserInfo
import com.segment.analytics.kotlin.core.emptyJsonObject
Expand Down Expand Up @@ -43,6 +44,10 @@ class StorageTests {
private lateinit var androidStorage: AndroidStorage
private var mockContext: Context = mockContext()

init {
Telemetry.enable = false
}

@BeforeEach
fun setup() = runTest {
clearPersistentStorage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ open class Analytics protected constructor(

init {
require(configuration.isValid()) { "invalid configuration" }
Telemetry.increment(Telemetry.INVOKE,
MichaelGHSeg marked this conversation as resolved.
Show resolved Hide resolved
mapOf("message" to "configured", "apihost" to configuration.apiHost, "cdnhost" to configuration.cdnHost,
"flush" to "at:${configuration.flushAt} int:${configuration.flushInterval} pol:${configuration.flushPolicies.count()}"))
build()
}

Expand All @@ -96,6 +99,10 @@ open class Analytics protected constructor(
Analytics.segmentLog(
"Caught Exception in Analytics Scope: ${t}"
)
Telemetry.error(Telemetry.INVOKE_ERROR,
mapOf("error" to t.toString(), "message" to "Exception in Analytics Scope"),
t.stackTraceToString()
)
}
override val analyticsScope = CoroutineScope(SupervisorJob() + exceptionHandler)
override val analyticsDispatcher: CloseableCoroutineDispatcher =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class HTTPClient(
} catch (e: MalformedURLException) {
val error = IOException("Attempted to use malformed url: $url", e)
Analytics.reportInternalError(error)
Telemetry.error(Telemetry.INVOKE_ERROR,
mapOf("error" to e.toString(), "writekey" to writeKey, "message" to "Malformed url"),
e.stackTraceToString()
)
throw error
}
val connection = requestedURL.openConnection() as HttpURLConnection
Expand Down Expand Up @@ -105,7 +109,7 @@ internal fun HttpURLConnection.createPostConnection(): Connection {
inputStream?.close()
}
throw HTTPException(
responseCode, connection.responseMessage, responseBody
responseCode, connection.responseMessage, responseBody, connection.headerFields
)
}
} finally {
Expand All @@ -119,7 +123,8 @@ internal fun HttpURLConnection.createPostConnection(): Connection {
internal class HTTPException(
val responseCode: Int,
val responseMessage: String,
val responseBody: String?
val responseBody: String?,
val responseHeaders: MutableMap<String, MutableList<String>>
) :
IOException("HTTP $responseCode: $responseMessage. Response: ${responseBody ?: "No response"}") {
fun is4xx(): Boolean {
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/com/segment/analytics/kotlin/core/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.double
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.serializer
import java.io.BufferedReader

Expand All @@ -22,6 +24,7 @@ data class Settings(
var plan: JsonObject = emptyJsonObject,
var edgeFunction: JsonObject = emptyJsonObject,
var middlewareSettings: JsonObject = emptyJsonObject,
var metrics: JsonObject = emptyJsonObject,
) {
inline fun <reified T : Any> destinationSettings(
name: String,
Expand Down Expand Up @@ -97,6 +100,13 @@ suspend fun Analytics.checkSettings() {
update(settingsObj)
}

settingsObj?.metrics?.get("sampleRate")?.jsonPrimitive?.double.also {
MichaelGHSeg marked this conversation as resolved.
Show resolved Hide resolved
if (it != null) {
Telemetry.sampleRate = it
Telemetry.start()
}
}

// we're good to go back to a running state.
store.dispatch(System.ToggleRunningAction(running = true), System::class)
}
Expand All @@ -118,5 +128,9 @@ internal fun Analytics.fetchSettings(
"${ex.message}: failed to fetch settings",
kind = LogKind.ERROR
)
Telemetry.error(Telemetry.INVOKE_ERROR,
MichaelGHSeg marked this conversation as resolved.
Show resolved Hide resolved
mapOf("error" to ex.toString(), "writekey" to writeKey, "message" to "Error retrieving settings"),
ex.stackTraceToString()
)
null
}
Loading
Loading