firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

Support performance HTTP auto tracing with OkHTTP v4+

Open mikehardy opened this issue 2 years ago • 10 comments

What feature would you like to see?

Hi there! This came up in react-native-firebase repo: https://github.com/invertase/react-native-firebase/issues/6082#issuecomment-1044683057 It appears per your docs that HTTP auto tracing is only supported with OkHTTP v3: https://firebase.google.com/docs/perf-mon/network-traces?platform=android#:~:text=Performance%20Monitoring%20automatically%20collects%20metrics,specifically%20HTTP%20client%20v3.x.x

But react-native ecosystem has moved on at long last to OkHTTP v4+

Is it possible to update performance auto-tracing to support OkHTTP v4+?

How would you use it?

We would use it to automatically trace HTTP requests in our react-native projects, but with up to date react-native versions that now depend on OkHTTP v4+

cc @OrkhanAlikhanov

mikehardy avatar Feb 18 '22 15:02 mikehardy

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Feb 18 '22 15:02 google-oss-bot

In related issue in react-native-firebase user confirms auto-tracing is working on iOS (implying project is integrated well, an important thing to know), but not on android with OkHTTP v4+ (also important to know - it appears it is not just a documentation issue but actually OkHTTP v4+ really isn't working for some reason)

I did search PR list here and issues and nothing turned up for me at least so it did not appear to be work in progress or work already released that we have not integrated correctly, though I'm always open to being wrong on that, and as maintainer over there I can alter react-native-firebase as needed, if needed

mikehardy avatar Feb 18 '22 16:02 mikehardy

Hi @mikehardy ,

Thanks for surfacing this. You are right the OkHttp version we support is V3, and we haven't worked on the support for V4+ yet.

yalunqin avatar Feb 18 '22 20:02 yalunqin

Probably wasn't even a possibility until recently because of minSdkVersion support requirements but react native moved to a min of 21 so the future is finally now 😂

mikehardy avatar Feb 18 '22 20:02 mikehardy

It seems there is no need to support okhttp v4 specifically, at least for react native. It might be that okhttp v4 is backwards compatible or it uses something under the hood that firebase perf already traces. I cannot confirm that firebase perf will auto trace non-react-native android app but definitely auto traces our react native app after we correctly integrated it. We had forgotten one step for android to make automatic network tracking work, which is "applying gradle plugin". After fixing that, it started to collect http requests. I'm sorry for misreporting.

@mikehardy @yalunqin

OrkhanAlikhanov avatar Feb 23 '22 13:02 OrkhanAlikhanov

Hey @OrkhanAlikhanov i also found a solution if you have a requestBuilder just do : requestBuilder.removeHeader("User-Agent"); requestBuilder.addHeader("User-Agent","okhttp/3.14");

So you dont have to downgrade and just make FireBase think that it's on the good version

Otherwise you could "addNetworkInterceptor" and do the same thing as above

It works for me and started to track

ThomasBuresi avatar Feb 24 '22 10:02 ThomasBuresi

same question.Why not let the network as a separate library or custom inject?

Haoxiqiang avatar Jun 03 '22 07:06 Haoxiqiang

... just do : requestBuilder.removeHeader("User-Agent"); requestBuilder.addHeader("User-Agent","okhttp/3.14");

Why don't you use setHeader method? It removes the header first and then add it automacally

hungntv avatar Aug 02 '22 04:08 hungntv

or we can custom track in an Interceptor

class FirebasePerfTrackInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()

        val metric: HttpMetric = FirebasePerformance.getInstance().newHttpMetric(request.url.toString(), request.method)
        val requestBody: RequestBody? = request.body
        metric.setRequestPayloadSize(requestBody?.contentLength() ?: 0)
        metric.start()

        val response = chain.proceed(request)

        try {
            metric.setHttpResponseCode(response.code)
            val responseBody = response.body
            if (responseBody != null) {
                val mediaType = responseBody.contentType()
                metric.setResponseContentType(mediaType?.toString() ?: "")
                var contentLength = responseBody.contentLength()
                if (contentLength < 0) {
                    val source = responseBody.source()
                    source.request(Long.MAX_VALUE)
                    val buffer = source.buffer
                    contentLength = buffer.size
                }
                metric.setResponsePayloadSize(contentLength)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            metric.stop()
        }
        
        return response
    }
}

xckevin avatar Nov 02 '22 10:11 xckevin

android 项目中,firebase性能监控没有收集到网络请求okhttp3 的链接。请求返回200,是成功的。尝试使用HttpMetric后,还是没有上报到firebase后台。

ZhangZhizhen avatar Dec 30 '22 07:12 ZhangZhizhen