:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(error-tracking): support posthog-kmp stack traces (#83618)
Hugues Pouillotf01a057edfb6
diff --git a/frontend/src/lib/components/Errors/types.ts b/frontend/src/lib/components/Errors/types.tsindex 3ba0455f55b4..5f1cbd8a174d 100644--- a/frontend/src/lib/components/Errors/types.ts+++ b/frontend/src/lib/components/Errors/types.ts@@ -24,6 +24,7 @@ export type ErrorTrackingRuntime =| 'ruby'| 'php'| 'java'+ | 'kotlin'| 'react-native'| 'android'| 'ios'diff --git a/frontend/src/lib/components/Errors/utils.test.ts b/frontend/src/lib/components/Errors/utils.test.tsindex 5fff5560f897..aab32cc71739 100644--- a/frontend/src/lib/components/Errors/utils.test.ts+++ b/frontend/src/lib/components/Errors/utils.test.ts@@ -4,10 +4,15 @@ import {getExceptionList,getExceptionRelease,getInstructionAddress,+ getRuntimeFromLib,getSessionId,} from './utils'describe('Error Display', () => {+ it('recognizes the Kotlin Multiplatform SDK runtime', () => {+ expect(getRuntimeFromLib('posthog-kmp')).toBe('kotlin')+ })+it('can read sentry stack trace when $exception_list is not present', () => {const eventProperties = {'should not be in the': 'result',diff --git a/frontend/src/lib/components/Errors/utils.ts b/frontend/src/lib/components/Errors/utils.tsindex 48c5a3021cfb..43874c8d5d3c 100644--- a/frontend/src/lib/components/Errors/utils.ts+++ b/frontend/src/lib/components/Errors/utils.ts@@ -52,6 +52,8 @@ export function getRuntimeFromLib(lib?: string | null): ErrorTrackingRuntime {case 'posthog-java':case 'analytics-java':return 'java'+ case 'posthog-kmp':+ return 'kotlin'default:return 'unknown'}diff --git a/products/error_tracking/frontend/components/RuntimeIcon.tsx b/products/error_tracking/frontend/components/RuntimeIcon.tsxindex 831c528d31ae..0813aaf77572 100644--- a/products/error_tracking/frontend/components/RuntimeIcon.tsx+++ b/products/error_tracking/frontend/components/RuntimeIcon.tsx@@ -8,6 +8,7 @@ import {IconGo,IconJava,IconJavascript,+ IconKotlin,IconLogomark,IconNode,IconPHP,@@ -31,6 +32,7 @@ const RuntimeIconMap: Record<ErrorTrackingRuntime, React.FC> = {dotnet: IconCSharp,php: IconPHP,java: IconJava,+ kotlin: IconKotlin,android: IconAndroid,ios: IconApple,elixir: IconElixir,diff --git a/rust/cymbal/src/core/types/langs/apple.rs b/rust/cymbal/src/core/types/langs/apple.rsindex be1b2a774338..42c636d5e3e4 100644--- a/rust/cymbal/src/core/types/langs/apple.rs+++ b/rust/cymbal/src/core/types/langs/apple.rs@@ -10,7 +10,7 @@ use crate::{error::{AppleError, FrameError, ResolveError, UnhandledError},frames::{record_frame_resolution_failure, Frame},langs::native::{self, DebugImage},- langs::utils::{add_raw_to_junk, get_context_lines},+ langs::utils::{add_raw_to_junk, get_context_lines, is_kotlin_compose_source},langs::CommonFrameMetadata,symbolication::symbol_store::{apple::AppleRef,@@ -68,6 +68,16 @@ fn is_system_module(module: &Option<String>) -> bool {})}+fn resolved_in_app(+ module: &Option<String>,+ source_path: Option<&str>,+ client_in_app: bool,+) -> bool {+ let generated_or_dependency = source_path+ .is_some_and(|path| path == "<compiler-generated>" || is_kotlin_compose_source(path));+ client_in_app && !is_system_module(module) && !generated_or_dependency+}+#[derive(Debug, Clone, Deserialize, Serialize)]pub struct RawAppleFrame {pub instruction_addr: Option<String>,@@ -196,16 +206,13 @@ impl RawAppleFrame {}fn build_resolved_frame(&self, symbol_info: &SymbolInfo, _debug_image: &DebugImage) -> Frame {- // Override in_app to false for system frameworks or compiler-generated code- let is_compiler_generated = symbol_info- .filename- .as_ref()- .is_some_and(|f| f == "<compiler-generated>");- let in_app = if is_system_module(&self.module) || is_compiler_generated {- false- } else {- self.meta.in_app- };+ // Kotlin/Native links Compose into the application binary.+ // Only the resolved source path identifies those frames as dependency code.+ let source_path = symbol_info+ .full_path+ .as_deref()+ .or(symbol_info.filename.as_deref());+ let in_app = resolved_in_app(&self.module, source_path, self.meta.in_app);let mut f = Frame {frame_id: FrameId::placeholder(),@@ -250,16 +257,7 @@ impl RawAppleFrame {None};- // Override in_app to false for system frameworks or compiler-generated code- let is_compiler_generated = self- .filename- .as_ref()- .is_some_and(|f| f == "<compiler-generated>");- let in_app = if is_system_module(&self.module) || is_compiler_generated {- false- } else {- self.meta.in_app- };+ let in_app = resolved_in_app(&self.module, self.filename.as_deref(), self.meta.in_app);// For unresolved frames without a filename, show "Module +image_addr" as source.// This is typically for Apple system frameworks (CoreFoundation, UIKitCore, etc.)@@ -358,6 +356,7 @@ impl RawAppleFrame {fn lang_from_filename(filename: Option<&str>) -> &'static str {match filename.and_then(|f| f.rsplit('.').next()) {Some("swift") => "swift",+ Some("kt" | "kts") => "kotlin",Some("m") => "objectivec",Some("mm") => "objectivecpp",Some("c") => "c",@@ -400,6 +399,32 @@ mod test {use super::*;use crate::core::symbolication::resolve::Resolve;+ #[test]+ fn resolved_kotlin_frame_classification() {+ let module = Some("ExampleApp".to_string());+ assert!(resolved_in_app(+ &module,+ Some("/project/src/commonMain/kotlin/com/example/App.kt"),+ true+ ));+ assert!(!resolved_in_app(+ &module,+ Some("/dependencies/compose/ui/src/commonMain/kotlin/androidx/compose/ui/Button.kt"),+ true+ ));+ assert!(!resolved_in_app(+ &module,+ Some("/project/src/commonMain/kotlin/com/example/App.kt"),+ false+ ));+ }++ #[test]+ fn identifies_kotlin_source_language() {+ assert_eq!(lang_from_filename(Some("App.kt")), "kotlin");+ assert_eq!(lang_from_filename(Some("Build.kts")), "kotlin");+ }+#[sqlx::test(migrations = "./tests/test_migrations")]async fn test_apple_symbolication(db: sqlx::PgPool) {use chrono::Utc;diff --git a/rust/cymbal/src/core/types/langs/js.rs b/rust/cymbal/src/core/types/langs/js.rsindex d8d674677586..2dd74571a20d 100644--- a/rust/cymbal/src/core/types/langs/js.rs+++ b/rust/cymbal/src/core/types/langs/js.rs@@ -15,7 +15,7 @@ use crate::{},};-use super::utils::{add_raw_to_junk, get_sourcelocation_context};+use super::utils::{add_raw_to_junk, get_sourcelocation_context, is_kotlin_compose_source};// A minifed JS stack frame. Just the minimal information needed to lookup some// sourcemap for it and produce a "real" stack frame.@@ -224,10 +224,7 @@ impl From<(&RawJSFrame, SourceLocation<'_>, usize)> for Frame {.and_then(|f| f.name()).map(|s| sanitize_string(s.to_string()));- let in_app = source- .as_ref()- .map(|s| !s.contains("node_modules"))- .unwrap_or(raw_frame.meta.in_app);+ let in_app = raw_frame.meta.in_app && !source.as_deref().is_some_and(is_dependency_source);let suspicious = source.as_ref().is_some_and(|s| s.contains("posthog-js@"));@@ -354,8 +351,41 @@ impl From<&RawJSFrame> for Frame {}}+fn is_dependency_source(source: &str) -> bool {+ source.contains("node_modules") || is_kotlin_compose_source(source)+}+#[cfg(test)]mod test {+ use super::is_dependency_source;++ #[test]+ fn detects_dependency_sources() {+ let cases = [+ ("webpack:///app/node_modules/react/index.js", true),+ (+ "webpack:///app/../dependencies/compose/ui/src/commonMain/kotlin/androidx/compose/ui/Button.kt",+ true,+ ),+ (+ "webpack:///app/../dependencies/compose/ui/src/skikoMain/kotlin/androidx/compose/ui/RootNodeOwner.kt",+ true,+ ),+ (+ "webpack:///app/../dependencies/compose/resources/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt",+ true,+ ),+ (+ "webpack:///app/src/commonMain/kotlin/com/example/checkout/App.kt",+ false,+ ),+ ];++ for (source, expected) in cases {+ assert_eq!(is_dependency_source(source), expected, "{source}");+ }+ }+#[test]fn source_ref_generation() {let frame = super::RawJSFrame {diff --git a/rust/cymbal/src/core/types/langs/utils.rs b/rust/cymbal/src/core/types/langs/utils.rsindex 269046fddd06..eccac75fc6bf 100644--- a/rust/cymbal/src/core/types/langs/utils.rs+++ b/rust/cymbal/src/core/types/langs/utils.rs@@ -17,6 +17,12 @@ pub fn get_sourcelocation_context(token: &SourceLocation, context_lines: usize)get_context_lines(src.lines(), token_line_num as usize, context_lines)}+pub fn is_kotlin_compose_source(source: &str) -> bool {+ let normalized = source.replace('\\', "/");+ normalized.contains("/kotlin/androidx/compose/")+ || normalized.contains("/kotlin/org/jetbrains/compose/")+}+pub fn get_token_context(token: &Token<'_>, line: usize, context_lines: usize) -> Option<Context> {let src = token.get_source_view()?;let lines = src.lines();