Skip to content

Commit 085e456

Browse files
committed
parse metricUnit in util
1 parent 4831a5c commit 085e456

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

plugins/amazonq/shared/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImpl.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
1919
import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection
2020
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentials.ConnectionMetadata
2121
import software.aws.toolkits.jetbrains.services.amazonq.lsp.model.aws.credentials.SsoProfileData
22+
import software.aws.toolkits.jetbrains.services.amazonq.lsp.util.TelemetryParsingUtil
2223
import software.aws.toolkits.jetbrains.services.telemetry.TelemetryService
2324
import software.aws.toolkits.jetbrains.settings.CodeWhispererSettings
2425
import java.time.Instant
@@ -28,13 +29,8 @@ import java.util.concurrent.CompletableFuture
2829
* Concrete implementation of [AmazonQLanguageClient] to handle messages sent from server
2930
*/
3031
class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageClient {
31-
override fun telemetryEvent(`object`: Any) {
32-
when (`object`) {
33-
is Map<*, *> -> handleTelemetryMap(`object`)
34-
}
35-
}
3632

37-
private fun handleTelemetryMap(telemetryMap: MutableMap<*, *>) {
33+
private fun handleTelemetryMap(telemetryMap: Map<*, *>) {
3834
try {
3935
val name = telemetryMap["name"] as? String ?: return
4036

@@ -44,7 +40,7 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
4440
TelemetryService.getInstance().record(project) {
4541
datum(name) {
4642
createTime(Instant.now())
47-
unit(telemetryMap["unit"] as? MetricUnit ?: MetricUnit.NONE)
43+
unit(TelemetryParsingUtil.parseMetricUnit(telemetryMap["unit"]))
4844
value(telemetryMap["value"] as? Double ?: 1.0)
4945
passive(telemetryMap["passive"] as? Boolean ?: false)
5046

@@ -62,6 +58,12 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
6258
}
6359
}
6460

61+
override fun telemetryEvent(`object`: Any) {
62+
when (`object`) {
63+
is Map<*, *> -> handleTelemetryMap(`object`)
64+
}
65+
}
66+
6567
override fun publishDiagnostics(diagnostics: PublishDiagnosticsParams) {
6668
println(diagnostics)
6769
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package software.aws.toolkits.jetbrains.services.amazonq.lsp.util
5+
6+
import software.amazon.awssdk.services.toolkittelemetry.model.MetricUnit
7+
8+
object TelemetryParsingUtil {
9+
10+
fun parseMetricUnit(value: Any?): MetricUnit {
11+
return when (value) {
12+
is String -> MetricUnit.fromValue(value) ?: MetricUnit.NONE
13+
is MetricUnit -> value
14+
else -> MetricUnit.NONE
15+
}
16+
}
17+
}

plugins/amazonq/shared/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonq/lsp/AmazonQLanguageClientImplTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package software.aws.toolkits.jetbrains.services.amazonq.lsp
66

77
import com.google.gson.Gson
8+
import com.google.gson.GsonBuilder
9+
import com.google.gson.ToNumberPolicy
810
import com.intellij.openapi.components.service
911
import com.intellij.openapi.project.Project
1012
import com.intellij.testFramework.ApplicationExtension
@@ -252,6 +254,47 @@ class AmazonQLanguageClientImplTest {
252254
assertThat(datum.metadata).contains(entry("key1", "value1"))
253255
}
254256

257+
@Test
258+
fun `test GSON deserialization behavior for telemetryEvent`() {
259+
val gson = GsonBuilder()
260+
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
261+
.create()
262+
263+
val jsonString = """
264+
{
265+
"name": "test_event",
266+
"value": 3.0,
267+
"passive": true,
268+
"unit": "Milliseconds",
269+
"data": {
270+
"key1": "value1"
271+
}
272+
}
273+
""".trimIndent()
274+
275+
val result = gson.fromJson(jsonString, Map::class.java)
276+
277+
val telemetryService = mockk<TelemetryService>(relaxed = true)
278+
mockkObject(TelemetryService)
279+
every { TelemetryService.getInstance() } returns telemetryService
280+
281+
val builderCaptor = slot<MetricEvent.Builder.() -> Unit>()
282+
every { telemetryService.record(project, capture(builderCaptor)) } returns Unit
283+
284+
sut.telemetryEvent(result)
285+
286+
val builder = DefaultMetricEvent.builder()
287+
builderCaptor.captured.invoke(builder)
288+
289+
val metricEvent = builder.build()
290+
val datum = metricEvent.data.first()
291+
292+
assertThat(datum.passive).isTrue()
293+
assertThat(datum.unit).isEqualTo(MetricUnit.MILLISECONDS)
294+
assertThat(datum.value).isEqualTo(3.0)
295+
assertThat(datum.metadata).contains(entry("key1", "value1"))
296+
}
297+
255298
@Test
256299
fun `getConnectionMetadata returns connection metadata with start URL for bearer token connection`() {
257300
val mockConnectionManager = mockk<ToolkitConnectionManager>()

0 commit comments

Comments
 (0)