我已联系 Firebase 支持,他们的回答是:
...
对于我上面提到的第四点,还有一件事要记住。假设 N 是代表我前面提到的“几十”阈值的确定数值。而且,我们正在监控以下 3 个不同的路径段: 1. www.something.com/a/ 2. www.something.com/b/ 3. www.something.com/c/
在给定的时间范围内,如果上述所有 3 条路径都收到 N-1 个命中。也就是说,不符合门槛要求。虽然从 www.something.com 的整体来看,点击次数可能几乎是 N 的 3 倍(这就是仪表板将显示的内容),但在给定的时间范围内,每条路径的单独点击未达到阈值因此,仅显示汇总的数字统计信息。
...
我用来拦截改造请求并监控请求时间的代码是:(为了安全起见,我删除了查询参数数据)
val builder = OkHttpClient.Builder()
.addInterceptor(FirebasePerformanceInterceptor(FirebasePerformance.getInstance()))
.build()
// Adapted from: http://qaru.site/questions/15007824/how-can-i-get-the-url-and-method-of-retrofit-request-on-onsubscribe-of-rxjava-2-custom-operator
class FirebasePerformanceInterceptor(private val performanceInstance: FirebasePerformance) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
//Get request values
val url = request.url().url()
val urlPath = getUrlWithoutQuery(url)
val requestPayloadSize = request.body()?.contentLength() ?: 0L
val httpMethod = request.method()
//Initialize http trace
val trace = performanceInstance.newHttpMetric(urlPath, httpMethod)
trace.setRequestPayloadSize(requestPayloadSize)
trace.start()
//Proceed
val response = chain.proceed(chain.request())
//Get response values
val responseCode = response.code()
//Add response values to trace and close it
trace.setHttpResponseCode(responseCode)
trace.stop()
return response
}
}
private fun getUrlWithoutQuery(url: URL): URL {
val uri = URI(url.protocol, url.host, url.path, null)
return uri.toURL()
}
要测试它是否正确记录:按照这个调试教程:你会看到类似的东西:
10-24 19:48:21.162 23037 24411 I FirebasePerformance: Logging NetworkRequestMetric - https://your-api-domain.com/cart 0b 147809ms,