Skip to content

Commit 288d0e7

Browse files
OliverMcBReedyuk
andauthored
[trace-expansion] Add remaining functionality to Trace class (#359)
* [trace-expansion] Add remaining functions to Trace * [trace-expansion] Add Trace Tests * [trace-expansion] Fix type error tests * [trace-expansion] Fix type error tests * [trace-expansion] Convert to Number * [trace-expansion] Fix attribute name error * Removed unused expects. Adjusted trace tests to start and stop on each run * Added rethrows to js calls * [trace-expansion] Fix trace creation [trace-expansion] rebase fix [trace-expansion] fix * [trace-expansion] Remove unavailable iOS functionality around attributes * [trace-expansion] Remove actual from PerfSession * [trace-expansion] Fix android test --------- Co-authored-by: Andrew Reed <[email protected]>
1 parent b3b6f4a commit 288d0e7

File tree

10 files changed

+285
-12
lines changed
  • firebase-common/src/jsMain/kotlin/dev/gitlive/firebase
  • firebase-perf/src
    • androidAndroidTest/kotlin/dev/gitlive/firebase/perf/metrics
    • androidMain/kotlin/dev/gitlive/firebase/perf
    • commonMain/kotlin/dev/gitlive/firebase/perf/metrics
    • commonTest/kotlin/dev/gitlive/firebase/perf/metrics
    • iosMain/kotlin/dev/gitlive/firebase/perf/metrics
    • jsMain/kotlin/dev/gitlive/firebase/perf
    • jsTest/kotlin/dev/gitlive/firebase/perf/metrics

10 files changed

+285
-12
lines changed

firebase-common/src/jsMain/kotlin/dev/gitlive/firebase/externals.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,28 @@ external object firebase {
525525
}
526526
}
527527

528-
fun performance(app: App? = definedExternally): performance.Performance
528+
fun performance(app: App? = definedExternally): performance
529529

530530
object performance {
531-
interface Performance {
532-
fun trace(
533-
performance: Performance,
534-
name: String
535-
): Trace
531+
532+
fun trace(
533+
name: String
534+
): PerformanceTrace
535+
536+
open class Performance {
537+
536538
}
537539

538-
interface Trace {
540+
open class PerformanceTrace {
539541
fun start()
540542
fun stop()
543+
544+
fun getAttribute(attr: String): String?
545+
fun getMetric(metricName: String): Number
546+
fun incrementMetric(metricName: String, num: Number)
547+
fun putAttribute(attr: String, value: String)
548+
fun putMetric(metricName: String, num: Number)
549+
fun removeAttribute(attr: String)
541550
}
542551
}
543552
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package dev.gitlive.firebase.perf.metrics
2+
3+
import dev.gitlive.firebase.Firebase
4+
import dev.gitlive.firebase.FirebaseOptions
5+
import dev.gitlive.firebase.apps
6+
import dev.gitlive.firebase.initialize
7+
import dev.gitlive.firebase.perf.FirebasePerformance
8+
import dev.gitlive.firebase.perf.performance
9+
import kotlin.test.BeforeTest
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
13+
class AndroidTraceTest {
14+
15+
private lateinit var performance: FirebasePerformance
16+
17+
@BeforeTest
18+
fun initializeFirebase() {
19+
Firebase
20+
.takeIf { Firebase.apps(dev.gitlive.firebase.perf.context).isEmpty() }
21+
?.apply {
22+
initialize(
23+
dev.gitlive.firebase.perf.context,
24+
FirebaseOptions(
25+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
26+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
27+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
28+
storageBucket = "fir-kotlin-sdk.appspot.com",
29+
projectId = "fir-kotlin-sdk",
30+
gcmSenderId = "846484016111"
31+
)
32+
)
33+
}
34+
35+
performance = Firebase.performance
36+
37+
38+
}
39+
40+
@Test
41+
fun testGetAttributes() {
42+
val trace = performance.newTrace("testGetAttributes")
43+
trace.start()
44+
val values = listOf(1, 2, 3)
45+
46+
values.forEach {
47+
trace.putAttribute("Test_Get_Attributes_$it", "Test Get Attributes Value $it")
48+
}
49+
50+
val attributes = trace.getAttributes()
51+
52+
assertEquals(3, attributes.size)
53+
54+
attributes.onEachIndexed { index, entry ->
55+
56+
assertEquals(entry.key.last(), entry.value.last())
57+
}
58+
59+
trace.stop()
60+
}
61+
62+
@Test
63+
fun testGetAttribute() {
64+
val trace = performance.newTrace("testGetAttribute")
65+
trace.start()
66+
trace.putAttribute("Test_Get_Attribute", "Test Get Attribute Value")
67+
68+
assertEquals("Test Get Attribute Value", trace.getAttribute("Test_Get_Attribute"))
69+
trace.stop()
70+
}
71+
72+
@Test
73+
fun testPutAttribute() {
74+
val trace = performance.newTrace("testPutAttribute")
75+
trace.start()
76+
trace.putAttribute("Test_Put_Attribute", "Test Put Attribute Value")
77+
78+
assertEquals("Test Put Attribute Value", trace.getAttribute("Test_Put_Attribute"))
79+
trace.stop()
80+
}
81+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
package dev.gitlive.firebase.perf.metrics
22

33
import com.google.firebase.perf.metrics.Trace
4+
import dev.gitlive.firebase.perf.session.PerfSession
45

56
actual class Trace internal constructor(private val android: Trace) {
67

78
actual fun start() = android.start()
89

910
actual fun stop() = android.stop()
11+
12+
actual fun getLongMetric(metricName: String): Long = android.getLongMetric(metricName)
13+
14+
actual fun incrementMetric(metricName: String, incrementBy: Long) = android.incrementMetric(metricName, incrementBy)
15+
16+
actual fun putMetric(metricName: String, value: Long) = android.putMetric(metricName, value)
17+
18+
fun getAttributes(): Map<String, String> = android.attributes
19+
20+
fun getAttribute(attribute: String): String? = android.getAttribute(attribute)
21+
22+
fun putAttribute(attribute: String, value: String) = android.putAttribute(attribute, value)
23+
24+
fun removeAttribute(attribute: String) = android.removeAttribute(attribute)
25+
26+
fun updateSession(session: PerfSession) = android.updateSession(session.android)
1027
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dev.gitlive.firebase.perf.session
2+
3+
import com.google.firebase.perf.session.PerfSession
4+
5+
class PerfSession internal constructor(val android: PerfSession) {
6+
}

firebase-perf/src/commonMain/kotlin/dev/gitlive/firebase/perf/metrics/Trace.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ expect class Trace {
44

55
fun start()
66
fun stop()
7+
fun getLongMetric(metricName: String): Long
8+
fun incrementMetric(metricName: String, incrementBy: Long)
9+
fun putMetric(metricName: String, value: Long)
710
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dev.gitlive.firebase.perf.metrics
2+
3+
import dev.gitlive.firebase.Firebase
4+
import dev.gitlive.firebase.FirebaseOptions
5+
import dev.gitlive.firebase.apps
6+
import dev.gitlive.firebase.initialize
7+
import dev.gitlive.firebase.perf.FirebasePerformance
8+
import dev.gitlive.firebase.perf.performance
9+
import kotlin.test.BeforeTest
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
13+
class TraceTest {
14+
15+
private lateinit var performance: FirebasePerformance
16+
17+
@BeforeTest
18+
fun initializeFirebase() {
19+
Firebase
20+
.takeIf { Firebase.apps(dev.gitlive.firebase.perf.context).isEmpty() }
21+
?.apply {
22+
initialize(
23+
dev.gitlive.firebase.perf.context,
24+
FirebaseOptions(
25+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
26+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
27+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
28+
storageBucket = "fir-kotlin-sdk.appspot.com",
29+
projectId = "fir-kotlin-sdk",
30+
gcmSenderId = "846484016111"
31+
)
32+
)
33+
}
34+
35+
performance = Firebase.performance
36+
37+
38+
}
39+
40+
@Test
41+
fun testGetLongMetric() {
42+
val trace = performance.newTrace("testGetLongMetric")
43+
trace.start()
44+
trace.putMetric("Get Long Metric Test", 1L)
45+
46+
assertEquals(1L, trace.getLongMetric("Get Long Metric Test"))
47+
trace.stop()
48+
}
49+
50+
@Test
51+
fun testIncrementMetric() {
52+
val trace = performance.newTrace("testIncrementMetric")
53+
trace.start()
54+
trace.putMetric("Get Increment Metric Test", 1L)
55+
56+
trace.incrementMetric("Get Increment Metric Test", 1L)
57+
58+
assertEquals(2L, trace.getLongMetric("Get Increment Metric Test"))
59+
trace.stop()
60+
}
61+
62+
@Test
63+
fun testPutMetric() {
64+
val trace = performance.newTrace("testPutMetric")
65+
trace.start()
66+
trace.putMetric("Get Put Metric Test", 1L)
67+
68+
assertEquals(1L, trace.getLongMetric("Get Put Metric Test"))
69+
trace.stop()
70+
}
71+
}

firebase-perf/src/iosMain/kotlin/dev/gitlive/firebase/perf/metrics/Trace.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,14 @@ actual class Trace internal constructor(val ios: FIRTrace?) {
1111
actual fun stop() {
1212
ios?.stop()
1313
}
14+
15+
actual fun getLongMetric(metricName: String): Long = ios?.valueForIntMetric(metricName) ?: 0L
16+
17+
actual fun incrementMetric(metricName: String, incrementBy: Long) {
18+
ios?.incrementMetric(metricName, incrementBy)
19+
}
20+
21+
actual fun putMetric(metricName: String, value: Long) {
22+
ios?.setIntValue(value, metricName)
23+
}
1424
}
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
11
package dev.gitlive.firebase.perf.metrics
22

33
import dev.gitlive.firebase.firebase
4+
import dev.gitlive.firebase.perf.rethrow
45

56

6-
actual class Trace internal constructor(private val js: firebase.performance.Trace) {
7+
actual class Trace internal constructor(private val js: firebase.performance.PerformanceTrace) {
78

8-
actual fun start() = js.start()
9+
actual fun start() = rethrow { js.start() }
10+
actual fun stop() = rethrow { js.stop() }
11+
actual fun getLongMetric(metricName: String) = rethrow { js.getMetric(metricName).toLong() }
12+
actual fun incrementMetric(metricName: String, incrementBy: Long) = rethrow { js.incrementMetric(metricName, incrementBy.toInt()) }
13+
actual fun putMetric(metricName: String, value: Long) = rethrow { js.putMetric(metricName, value.toInt()) }
14+
fun getAttribute(attribute: String): String? = rethrow { js.getAttribute(attribute) }
15+
fun putAttribute(attribute: String, value: String) = rethrow { js.putAttribute(attribute, value) }
16+
fun removeAttribute(attribute: String) = rethrow { js.removeAttribute(attribute) }
17+
18+
fun primitiveHashMap(container: dynamic): HashMap<String, String> {
19+
val m = HashMap<String, String>().asDynamic()
20+
m.map = container
21+
val keys = js("Object.keys")
22+
m.`$size` = keys(container).length
23+
return m
24+
}
925

10-
actual fun stop() = js.stop()
1126
}

firebase-perf/src/jsMain/kotlin/dev/gitlive/firebase/perf/performance.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ actual fun Firebase.performance(app: FirebaseApp): FirebasePerformance = rethrow
1717
FirebasePerformance(firebase.performance(app.js))
1818
}
1919

20-
actual class FirebasePerformance internal constructor(val js: firebase.performance.Performance) {
20+
actual class FirebasePerformance internal constructor(val js: firebase.performance) {
2121

22-
actual fun newTrace(traceName: String): Trace = Trace(js.trace(js, traceName))
22+
actual fun newTrace(traceName: String): Trace = rethrow {
23+
Trace(js.trace(traceName))
24+
}
2325
}
2426

2527
actual open class FirebasePerformanceException(code: String, cause: Throwable) :
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package dev.gitlive.firebase.perf.metrics
2+
3+
import dev.gitlive.firebase.Firebase
4+
import dev.gitlive.firebase.FirebaseOptions
5+
import dev.gitlive.firebase.apps
6+
import dev.gitlive.firebase.initialize
7+
import dev.gitlive.firebase.perf.FirebasePerformance
8+
import dev.gitlive.firebase.perf.performance
9+
import kotlin.test.BeforeTest
10+
import kotlin.test.Test
11+
import kotlin.test.assertEquals
12+
13+
class JsTraceTest {
14+
15+
private lateinit var performance: FirebasePerformance
16+
17+
@BeforeTest
18+
fun initializeFirebase() {
19+
Firebase
20+
.takeIf { Firebase.apps(dev.gitlive.firebase.perf.context).isEmpty() }
21+
?.apply {
22+
initialize(
23+
dev.gitlive.firebase.perf.context,
24+
FirebaseOptions(
25+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
26+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
27+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
28+
storageBucket = "fir-kotlin-sdk.appspot.com",
29+
projectId = "fir-kotlin-sdk",
30+
gcmSenderId = "846484016111"
31+
)
32+
)
33+
}
34+
35+
performance = Firebase.performance
36+
37+
38+
}
39+
40+
@Test
41+
fun testGetAttribute() {
42+
val trace = performance.newTrace("testGetAttribute")
43+
trace.start()
44+
trace.putAttribute("Test_Get_Attribute", "Test Get Attribute Value")
45+
46+
assertEquals("Test Get Attribute Value", trace.getAttribute("Test_Get_Attribute"))
47+
trace.stop()
48+
}
49+
50+
@Test
51+
fun testPutAttribute() {
52+
val trace = performance.newTrace("testPutAttribute")
53+
trace.start()
54+
trace.putAttribute("Test_Put_Attribute", "Test Put Attribute Value")
55+
56+
assertEquals("Test Put Attribute Value", trace.getAttribute("Test_Put_Attribute"))
57+
trace.stop()
58+
}
59+
}

0 commit comments

Comments
 (0)