Skip to content

Add Ktor client integration #4527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Jul 29, 2025
Merged

Add Ktor client integration #4527

merged 37 commits into from
Jul 29, 2025

Conversation

lcian
Copy link
Member

@lcian lcian commented Jun 27, 2025

📜 Description

Add a Ktor client integration.
Functionality, options and tests are similar to our OkHttp integration.

💡 Motivation and Context

Part of #2684

💚 How did you test it?

End to end and manual tests

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

Copy link
Contributor

github-actions bot commented Jun 27, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 417.00 ms 447.33 ms 30.33 ms
Size 1.58 MiB 2.09 MiB 519.49 KiB

Previous results on branch: lcian/feat/ktor-client

Startup times

Revision Plain With Sentry Diff
94e993e 404.49 ms 439.62 ms 35.13 ms
df1fbd1 407.84 ms 424.93 ms 17.09 ms
b5f9205 492.51 ms 542.98 ms 50.47 ms
569c88a 406.86 ms 419.57 ms 12.72 ms
9647976 403.48 ms 420.36 ms 16.88 ms
1df63ce 429.52 ms 485.02 ms 55.50 ms
e2d324e 400.33 ms 423.66 ms 23.33 ms
c3b3ac8 433.74 ms 490.08 ms 56.34 ms
f316c06 427.39 ms 464.94 ms 37.55 ms
bc4c3ab 420.27 ms 462.92 ms 42.65 ms

App size

Revision Plain With Sentry Diff
94e993e 1.58 MiB 2.09 MiB 519.31 KiB
df1fbd1 1.58 MiB 2.09 MiB 519.08 KiB
b5f9205 1.58 MiB 2.09 MiB 519.31 KiB
569c88a 1.58 MiB 2.09 MiB 519.08 KiB
9647976 1.58 MiB 2.09 MiB 519.31 KiB
1df63ce 1.58 MiB 2.09 MiB 518.90 KiB
e2d324e 1.58 MiB 2.09 MiB 519.49 KiB
c3b3ac8 1.58 MiB 2.09 MiB 518.90 KiB
f316c06 1.58 MiB 2.09 MiB 519.08 KiB
bc4c3ab 1.58 MiB 2.09 MiB 518.90 KiB

@lcian lcian changed the title [WIP] Add Ktor client integration \Add Ktor client integration Jul 3, 2025
@lcian lcian changed the title \Add Ktor client integration Add Ktor client integration Jul 3, 2025
@lcian lcian marked this pull request as ready for review July 3, 2025 10:55
@lcian lcian force-pushed the lcian/feat/ktor-client branch from 28e039a to bb52a1b Compare July 17, 2025 13:03
@lcian lcian force-pushed the lcian/feat/ktor-client branch from 10fa401 to 8a42315 Compare July 17, 2025 13:26
@lcian
Copy link
Member Author

lcian commented Jul 17, 2025

@romtsn Please let me know what you think about this approach: 8a42315 (#4527)
As far as I can tell it's not possible to do this without reflection, as the config property has internal visibility.
I've also added a ProGuard rule that should ensure this works when ProGuard is being used, as we rely on those class/property names.

The approach is still not perfect because if the config block has some side effects, we would trigger them when creating the fake client.
However this seems to be the only approach to do this successfully.
Otherwise I guess another option would be to somehow detect the usage of OkHttp as engine for Ktor and conditionally disable the instrumentation in SAGP, and leave the responsibility to the user to not double instrument when not using SAGP.

As a next step I want to set up an app using ProGuard and SAGP to ensure this works as intended.

@lcian lcian requested a review from romtsn July 17, 2025 13:42
@lcian
Copy link
Member Author

lcian commented Jul 18, 2025

I've set up an Android sample with SAGP and it seems this approach to detect our OkHttp instrumentation doesn't work. As far as I understand, that's because the bytecode manipulation only adds the interceptor when we're in okhttp3.internal.connection.RealCall::getResponseWithInterceptorChain, so it cannot be detected outside of that method.

@lcian
Copy link
Member Author

lcian commented Jul 21, 2025

The OkHttp instrumentation is automatically added by SAGP, but users would have to manually set up this Ktor integration.
So I think a viable option would be to just state very clearly in the docs that if you're using OkHttp as the engine and already using the OkHttp integration or SAGP with auto instrumentation enabled, you should not use the Ktor plugin.
What do you think @romtsn ?

@romtsn
Copy link
Member

romtsn commented Jul 21, 2025

The OkHttp instrumentation is automatically added by SAGP, but users would have to manually set up this Ktor integration. So I think a viable option would be to just state very clearly in the docs that if you're using OkHttp as the engine and already using the OkHttp integration or SAGP with auto instrumentation enabled, you should not use the Ktor plugin. What do you think @romtsn ?

oh dang, sorry I made you go through this. Yeah, this sounds like a job for the gradle plugin to do. Could you file an issue there please? And highlighting this in the docs for now sounds good too 👍

@lcian
Copy link
Member Author

lcian commented Jul 24, 2025

Okay, opened the issue in SAGP: getsentry/sentry-android-gradle-plugin#949
Tested with a sample app and functionality looks good.
I'm going to prepare docs and then merge this.

Copy link
Member

@adinauer adinauer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM one question about reported integration name.

@lcian lcian merged commit d1fd194 into main Jul 29, 2025
42 checks passed
@lcian lcian deleted the lcian/feat/ktor-client branch July 29, 2025 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants