-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
197 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,15 +45,3 @@ class ApiService { | |
.create(ApiInterface::class.java) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
29 changes: 29 additions & 0 deletions
29
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/model/Metrics.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,29 @@ | ||
package com.amazon.connect.chat.sdk.model | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
enum class MetricName { | ||
CreateParticipantConnection, | ||
SendMessage | ||
} | ||
|
||
data class Metric( | ||
val dimensions: List<Dimension>, | ||
val metricName: String, | ||
val namespace: String, | ||
val optionalDimensions: List<Dimension>, | ||
val timestamp: String, | ||
val unit: String, | ||
val value: Int | ||
) | ||
|
||
data class Dimension( | ||
val name: String, | ||
val value: String | ||
) | ||
|
||
data class MetricRequestBody( | ||
val metricList: List<Metric>, | ||
val metricNamespace: String | ||
) |
21 changes: 19 additions & 2 deletions
21
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/network/APIClient.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,12 +1,29 @@ | ||
package com.amazon.connect.chat.sdk.network | ||
|
||
import com.amazon.connect.chat.sdk.model.MetricRequestBody | ||
import retrofit2.Call | ||
import retrofit2.Callback | ||
|
||
import retrofit2.Response | ||
import javax.inject.Inject | ||
|
||
class APIClient @Inject constructor( | ||
private val metricsInterface: MetricsInterface | ||
) { | ||
fun sendMetrics(){ | ||
// metricsInterface.sendMetrics | ||
fun sendMetrics(metricRequestBody: MetricRequestBody, callback: (Response<Any>?) -> Unit) { | ||
val call = metricsInterface.sendMetrics(metricRequestBody) | ||
|
||
call.enqueue(object: Callback<Any> { | ||
override fun onResponse( | ||
call: Call<Any>, | ||
response: Response<Any> | ||
) { | ||
callback(response) | ||
} | ||
|
||
override fun onFailure(call: Call<Any>, t: Throwable) { | ||
callback(null) | ||
} | ||
}) | ||
} | ||
} |
10 changes: 5 additions & 5 deletions
10
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/network/MetricsInterface.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,13 +1,13 @@ | ||
package com.amazon.connect.chat.sdk.network | ||
|
||
import com.amazon.connect.chat.sdk.model.ConnectionDetails | ||
import com.amazon.connect.chat.sdk.model.MetricRequestBody | ||
import retrofit2.Call | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
|
||
@ApiUrl("https://api.aws.example.com/") | ||
interface MetricsInterface { | ||
|
||
@GET("someAwsEndpoint") | ||
fun getAwsData(@Query("param") param: String): Call<ConnectionDetails> | ||
@POST("put-metrics/") | ||
fun sendMetrics(@Body metricRequestBody: MetricRequestBody): Call<Any> | ||
} |
92 changes: 90 additions & 2 deletions
92
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/network/MetricsManager.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,95 @@ | ||
package com.amazon.connect.chat.sdk.network | ||
|
||
import com.amazon.connect.chat.sdk.model.MetricRequestBody | ||
import javax.inject.Inject | ||
import java.util.* | ||
import kotlin.concurrent.timer | ||
import com.amazon.connect.chat.sdk.utils.MetricsUtils | ||
import com.amazon.connect.chat.sdk.model.MetricName | ||
import com.amazon.connect.chat.sdk.model.Metric | ||
import com.amazon.connect.chat.sdk.model.Dimension | ||
|
||
class MetricsManager @Inject constructor(private val metricsInterface: MetricsInterface) { | ||
class MetricsManager @Inject constructor( | ||
private var apiClient: APIClient | ||
) { | ||
private var metricList: MutableList<Metric> = mutableListOf() | ||
private var isMonitoring: Boolean = false | ||
private var timer: Timer? = null | ||
private var shouldRetry: Boolean = true | ||
|
||
} | ||
init { | ||
if (!MetricsUtils.isCsmDisabled()) { | ||
monitorAndSendMetrics() | ||
} | ||
} | ||
|
||
@Synchronized | ||
private fun monitorAndSendMetrics() { | ||
if (isMonitoring) return | ||
isMonitoring = true | ||
|
||
timer = timer(initialDelay = 10000, period = 10000) { | ||
if (metricList.isNotEmpty()) { | ||
val metricRequestBody = createMetricRequestBody() | ||
apiClient.sendMetrics(metricRequestBody) { response -> | ||
if (response != null && response.isSuccessful) { | ||
metricList = mutableListOf() | ||
isMonitoring = false | ||
timer?.cancel() | ||
} else { | ||
// We should retry once after 10s delay, otherwise we will send the missed | ||
// payload with the next batch of metrics | ||
if (shouldRetry) { | ||
shouldRetry = false | ||
} else { | ||
isMonitoring = false | ||
shouldRetry = true | ||
timer?.cancel() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun createMetricRequestBody(): MetricRequestBody { | ||
return MetricRequestBody( | ||
metricNamespace = "chat-widget", | ||
metricList = metricList | ||
) | ||
} | ||
|
||
private fun getCountMetricDimensions(): List<Dimension> { | ||
return listOf( | ||
Dimension(name = "WidgetType", value = "MobileChatSDK"), | ||
Dimension(name = "SDKPlatform", value = "Android"), | ||
Dimension(name = "Category", value = "API"), | ||
Dimension(name = "Metric", value = "Count") | ||
) | ||
} | ||
|
||
fun addCountMetric(metricName: MetricName) { | ||
val currentTime = MetricsUtils.getCurrentMetricTimestamp() | ||
val countMetricDimensions = getCountMetricDimensions() | ||
val countMetric = Metric( | ||
dimensions = countMetricDimensions, | ||
metricName = metricName.name, | ||
namespace = "chat-widget", | ||
optionalDimensions = emptyList(), | ||
timestamp = currentTime, | ||
unit = "Count", | ||
value = 1 | ||
) | ||
|
||
addMetric(countMetric) | ||
} | ||
|
||
private fun addMetric(metric: Metric) { | ||
if (MetricsUtils.isCsmDisabled()) { | ||
return | ||
} | ||
|
||
metricList.add(0, metric) | ||
monitorAndSendMetrics() | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
chat-sdk/src/main/java/com/amazon/connect/chat/sdk/utils/MetricsUtils.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 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.amazon.connect.chat.sdk.utils | ||
|
||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
import com.amazon.connect.chat.sdk.Config | ||
|
||
object MetricsUtils { | ||
fun getCurrentMetricTimestamp(): String { | ||
val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault()) | ||
formatter.timeZone = TimeZone.getTimeZone("UTC") | ||
val now = Date() | ||
return formatter.format(now) | ||
} | ||
|
||
fun isCsmDisabled(): Boolean { | ||
return Config.disableCsm | ||
} | ||
} |