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

Added trace ID to error events #251

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## TBD

- Getting the correlation trace ID and span ID through `flutter_bridge` and adding it to events.
[251](https://github.com/bugsnag/bugsnag-flutter/pull/251)

## 3.1.1 (2024-04-24)

- Fixed: Navigator.pushAndRemoveUntil throws exception [#242](https://github.com/bugsnag/bugsnag-flutter/pull/242)
Expand Down
39 changes: 28 additions & 11 deletions features/fixtures/app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
bugsnag_bridge:
dependency: transitive
description:
path: "packages/bugsnag-flutter-bridge"
ref: HEAD
resolved-ref: "0a2b35f8b0a5908082ce0f57e85fa9c027a0f0ad"
url: "https://github.com/bugsnag/bugsnag-flutter-common"
source: git
version: "0.1.0"
bugsnag_flutter:
dependency: "direct main"
description:
Expand Down Expand Up @@ -58,10 +67,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.17.2"
cupertino_icons:
dependency: "direct main"
description:
Expand Down Expand Up @@ -140,18 +149,18 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
url: "https://pub.dev"
source: hosted
version: "0.12.15"
version: "0.12.16"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.5.0"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -241,10 +250,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -281,10 +290,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.6.0"
typed_data:
dependency: transitive
description:
Expand All @@ -301,6 +310,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
win32:
dependency: transitive
description:
Expand All @@ -318,5 +335,5 @@ packages:
source: hosted
version: "1.0.4"
sdks:
dart: ">=3.0.0 <4.0.0"
dart: ">=3.1.0-185.0.dev <4.0.0"
flutter: ">=3.10.0"
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.UUID;

class BugsnagFlutter {

Expand All @@ -43,6 +44,8 @@ class BugsnagFlutter {
private static boolean isAnyStarted = false;
private boolean isStarted = false;

static final int HEX_LONG_LENGTH = 16;

/*
***********************************************************************************************
* All methods listed here must also be registered in the BugsnagFlutterPlugin otherwise they
Expand Down Expand Up @@ -406,6 +409,26 @@ JSONObject createEvent(@Nullable JSONObject args) throws JSONException {
event.addMetadata("flutter", (Map<String, Object>) flutterMetadata);
}

JSONObject correlation = args.optJSONObject("correlation");
if (correlation != null) {
try {
String traceId = getString(correlation, "traceId");
String spanId = getString(correlation, "spanId");
if (traceId != null &&
traceId.length() == HEX_LONG_LENGTH * 2 &&
spanId != null &&
spanId.length() == HEX_LONG_LENGTH
) {
long traceIdMostSignificantBits = hexToLong(traceId.substring(0, HEX_LONG_LENGTH));
long traceIdLeastSignificantBits = hexToLong(traceId.substring(HEX_LONG_LENGTH));
long spanIdAsLong = hexToLong(spanId);
event.setTraceCorrelation(new UUID(traceIdMostSignificantBits, traceIdLeastSignificantBits), spanIdAsLong);
}
} catch(Exception e) {
// ignore the error, the error correlation will be missing
}
}

if (deliver) {
// Flutter layer has asked us to deliver the Event immediately
client.deliverEvent(event);
Expand Down Expand Up @@ -442,4 +465,9 @@ JSONObject deliverEvent(@Nullable JSONObject eventJson) {
boolean hasString(JSONObject args, String key) {
return getString(args, key) != null;
}

private static long hexToLong(String hex) {
return Long.parseLong(hex.substring(0, 2), 16) << 56 |
Long.parseLong(hex.substring(2), 16);
}
}
9 changes: 9 additions & 0 deletions packages/bugsnag_flutter/ios/Classes/BugsnagFlutterPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ - (NSDictionary *)createEvent:(NSDictionary *)json {
[event addMetadata:DartCodeBuildId withKey:@"buildID" toSection:@"flutter"];
}
}

NSDictionary *correlation = json[@"correlation"];
if (correlation != nil) {
NSString *traceId = correlation[@"traceId"];
NSString *spanId = correlation[@"spanId"];
if (traceId != nil && spanId != nil) {
[event setCorrelationTraceId:traceId spanId:spanId];
}
}

if ([json[@"deliver"] boolValue]) {
[client notifyInternal:event block:nil];
Expand Down
2 changes: 1 addition & 1 deletion packages/bugsnag_flutter/ios/bugsnag_flutter.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Bugsnag crash monitoring and reporting tool for Flutter apps
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }

s.dependency 'Flutter'
s.dependency 'Bugsnag', '6.28.0'
s.dependency 'Bugsnag', '6.30.0'
end
13 changes: 12 additions & 1 deletion packages/bugsnag_flutter/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:io';
import 'dart:ui';

import 'package:bugsnag_bridge/bugsnag_bridge.dart';
import 'package:bugsnag_flutter/src/error_factory.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
Expand Down Expand Up @@ -421,6 +422,8 @@ class ChannelClient extends BugsnagClient {

final CallbackCollection<BugsnagEvent> _onErrorCallbacks = {};

final contextProvider = BugsnagContextProviderImpl();

@override
void Function(dynamic error, StackTrace? stack) get errorHandler =>
_notifyUnhandled;
Expand Down Expand Up @@ -616,13 +619,21 @@ class ChannelClient extends BugsnagClient {
PlatformDispatcher.instance.initialLifecycleState,
if (lifecycleState != null) 'lifecycleState': lifecycleState,
};
final traceContext = contextProvider.getCurrentTraceContext();
final correlation = traceContext != null
? {
'spanId': traceContext.spanId,
'traceId': traceContext.traceId,
}
: null;
final eventJson = await _channel.invokeMethod(
'createEvent',
{
'error': error,
'flutterMetadata': metadata,
'unhandled': unhandled,
'deliver': deliver
'deliver': deliver,
if (correlation != null) 'correlation': correlation,
},
);

Expand Down
1 change: 1 addition & 0 deletions packages/bugsnag_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ environment:
dependencies:
flutter:
sdk: flutter
bugsnag_bridge: ^2.0.0

dev_dependencies:
flutter_test:
Expand Down