Skip to content
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

feat: Add support for profiling on iOS #1652

Merged
merged 150 commits into from
Mar 21, 2022

Conversation

armcknight
Copy link
Member

@armcknight armcknight commented Jan 27, 2022

📜 Description

Integrates a subset of the Specto iOS SDK code to implement support for profiling on iOS. This PR contains the changes to bring in that existing code and also integrate it with the Sentry SDK such that profiles are captured alongside transactions.

💡 Motivation and Context

The profiling product will first be available on iOS & Android, based on tech acquired from Specto. This is the first attempt at integrating that functionality into the Sentry Cocoa SDK.

💚 How did you test it?

Add new automated tests, build and run in the iOS-Swift test app and ensure that the envelope generated with the new profiling envelope item contains the right data.

📝 Checklist

  • I reviewed the submitted code
  • I added tests to verify the changes
  • I updated the docs if needed
  • Review from the native team if needed
  • No breaking changes

🔮 Next steps

  • Add tests for SentryProfiler
  • Add the debug_meta field so the backend can symbolicate
  • Add other metadata fields (device/OS metadata etc.) to the profile payload
  • Improve the efficiency of the profiler -- right now the entire thing is buffered in memory using inefficient data structures

to disambiguate from log/src/Log.{h,cpp}; they became ambiguous after
    putting them in the same target vs separate bazel targets, as well
    as the includes being the same name after removing the bazel path prefixes
it seemed to become ambiguous with stdlib <time.h> and caused a build
    error: 'chrono' file not found
- change all files from pure C++ to Objective-C++ (.mm extensions)
- using the polyfills instead of protobufs
- add a few more needed files
- remove spdlog and fmt for now, remove all log logic and define the macros to no-ops
- fix the cast to dispatch_queue_t in ThreadHandle that was breaking under ARC
at the point where Specto used to serialize the protobuf to data and
    write it to packets to the ring buffer, we now create a
    NSDictionary containing all the same fields with original names
    and serialize that to NSData that is then written to a
    SentryEnvelope

add old configuration options like sampling rate, wether profiling is
    enabled etc into SentryOptions
@github-actions
Copy link

github-actions bot commented Jan 27, 2022

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against c3a5bc7

Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

I'm thrilled to see the first draft PR from Specto 🎉, @armcknight.

FYI, we use the logaf scale for PR review comments. In short, we start the comments with l,m,h standing for the importance of the comment low, medium, high. Low are just nitpicks, medium is worth addressing and discussing, and high is something really important.

I didn't take a close look at the profiling code, because I guess this code was already reviewed. If you have tests for the profiling code I think we should also add them and run them in CI.

@armcknight CI is complaining, so I think we still need to adapt the Sentry.podspec and Package.swift.

For the SentryOptions we still need tests. For the BOOL properties, you can add tests as simple as

- (void)testEnableNetworkBreadcrumbs
{
[self testBooleanField:@"enableNetworkBreadcrumbs"];
}

Sources/Sentry/Profiling/MemorySafety.mm Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/SpectoProtoPolyfills.mm Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/SpectoTime.mm Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/ThreadMetadataCache.mm Outdated Show resolved Hide resolved
Sources/Sentry/Public/SentryOptions.h Outdated Show resolved Hide resolved
Sources/Sentry/SentryOptions.m Outdated Show resolved Hide resolved
Sources/Sentry/SentryOptions.m Outdated Show resolved Hide resolved
Sources/Configuration/Sentry.xcconfig Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/Backtrace.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/Backtrace.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/BacktracePlugin.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/BlockSink.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/CPU.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/Exception.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/LogHookSink.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/SpectoProtoPolyfills.h Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/SpectoTime.mm Outdated Show resolved Hide resolved
Sources/Sentry/Profiling/ThreadPriority.h Outdated Show resolved Hide resolved
to try to roll back from C++17, as that was when this was first supported
- remove __APPLE__ and __ANDROID__ specific gates
- change NDEBUG (bazel) usages to DEBUG (Xcode)
@codecov-commenter
Copy link

codecov-commenter commented Feb 1, 2022

Codecov Report

Merging #1652 (7a97916) into master (bf46d0a) will decrease coverage by 2.34%.
The diff coverage is 72.77%.

❗ Current head 7a97916 differs from pull request most recent head c3a5bc7. Consider uploading reports for the commit c3a5bc7 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1652      +/-   ##
==========================================
- Coverage   94.87%   92.53%   -2.35%     
==========================================
  Files         171      187      +16     
  Lines        7751     8657     +906     
==========================================
+ Hits         7354     8011     +657     
- Misses        397      646     +249     
Impacted Files Coverage Δ
Sources/Sentry/include/SentryAsyncSafeLogging.h 0.00% <0.00%> (ø)
Sources/Sentry/SentryMachLogging.cpp 30.25% <30.25%> (ø)
Sources/Sentry/SentryTime.mm 60.00% <60.00%> (ø)
Sources/Sentry/include/SentryProfilingLogging.hpp 61.90% <61.90%> (ø)
Sources/Sentry/SentryThreadHandle.cpp 69.06% <69.06%> (ø)
Sources/Sentry/include/SentryStackFrame.hpp 72.72% <72.72%> (ø)
Sources/Sentry/include/SentryMachLogging.hpp 77.77% <77.77%> (ø)
Sources/Sentry/SentryProfilingLogging.mm 78.57% <78.57%> (ø)
Sources/Sentry/SentrySamplingProfiler.cpp 88.50% <88.50%> (ø)
Sources/Sentry/SentryHub.m 97.87% <90.00%> (-0.67%) ⬇️
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bf46d0a...c3a5bc7. Read the comment docs.

@indragiek
Copy link
Member

@philipphofmann I'm taking over this PR and will address all the comments since @armcknight is now out on paternity, will have an update soon! Thank you.

@brustolin
Copy link
Contributor

@philipphofmann I'm taking over this PR and will address all the comments since @armcknight is now out on paternity, will have an update soon! Thank you.

Hey @indragiek, Phillip is out on vocation the entire month. If you need something, you can ping me or @bruno-garcia

@indragiek indragiek changed the title feat: iOS Profiler feat: Add support for profiling on iOS Mar 16, 2022
Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

A few minor things. Thanks 🙏

@@ -395,6 +395,42 @@ class SentryTracerTests: XCTestCase {
XCTAssertEqual(["key": 0], sut.data as! [String: Int])
}

#if os(iOS)
Copy link
Member

Choose a reason for hiding this comment

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

l:

Suggested change
#if os(iOS)
#if os(iOS) || os(tvOS)

Copy link
Member

Choose a reason for hiding this comment

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

Profiling doesn't work on tvOS because the necessary APIs are not exported, same with watchOS. See SENTRY_TARGET_PROFILING_SUPPORTED. I did notice this should have macOS added to it, I will make that change.

Copy link
Member

Choose a reason for hiding this comment

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

Changed this check to support macOS, iOS, and mac catalyst (but not tvOS or watchOS)

Sources/Sentry/SentryTracer.m Outdated Show resolved Hide resolved
Sources/Sentry/SentryTracer.m Outdated Show resolved Hide resolved
Copy link
Member

@philipphofmann philipphofmann left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@philipphofmann philipphofmann changed the base branch from master to profiling March 21, 2022 15:48
@philipphofmann philipphofmann merged commit cf5bbbb into profiling Mar 21, 2022
@philipphofmann philipphofmann deleted the armcknight/specto-cpp-port branch March 21, 2022 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants