Skip to content

Commit

Permalink
integrate dart & cocoa profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Aug 26, 2023
1 parent a848454 commit f50a5df
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 78 deletions.
2 changes: 2 additions & 0 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ class Hub {
Future<SentryId> captureTransaction(
SentryTransaction transaction, {
SentryTraceContextHeader? traceContext,
ProfileInfo? profileInfo,
}) async {
var sentryId = SentryId.empty();

Expand Down Expand Up @@ -538,6 +539,7 @@ class Hub {
transaction,
scope: item.scope,
traceContext: traceContext,
profileInfo: profileInfo,
);
} catch (exception, stackTrace) {
_options.logger(
Expand Down
1 change: 1 addition & 0 deletions dart/lib/src/hub_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class HubAdapter implements Hub {
Future<SentryId> captureTransaction(
SentryTransaction transaction, {
SentryTraceContextHeader? traceContext,
ProfileInfo? profileInfo,
}) =>
Sentry.currentHub.captureTransaction(
transaction,
Expand Down
1 change: 1 addition & 0 deletions dart/lib/src/noop_hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class NoOpHub implements Hub {
Future<SentryId> captureTransaction(
SentryTransaction transaction, {
SentryTraceContextHeader? traceContext,
ProfileInfo? profileInfo,
}) async =>
SentryId.empty();

Expand Down
6 changes: 3 additions & 3 deletions dart/lib/src/profiling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import '../sentry.dart';

@internal
abstract class ProfilerFactory {
Profiler startProfiling(SentryTransactionContext context);
Profiler? startProfiling(SentryTransactionContext context);
}

@internal
abstract class Profiler {
Future<ProfileInfo> finishFor(SentryTransaction transaction);
Future<ProfileInfo?> finishFor(SentryTransaction transaction);
void dispose();
}

// See https://develop.sentry.dev/sdk/profiles/
@internal
abstract class ProfileInfo {
FutureOr<SentryEnvelopeItem> asEnvelopeItem();
SentryEnvelopeItem asEnvelopeItem();
}
3 changes: 0 additions & 3 deletions dart/lib/src/protocol/sentry_transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ class SentryTransaction extends SentryEvent {
late final Map<String, SentryMeasurement> measurements;
late final SentryTransactionInfo? transactionInfo;

@internal
late final ProfileInfo? profileInfo;

SentryTransaction(
this._tracer, {
SentryId? eventId,
Expand Down
5 changes: 5 additions & 0 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:math';
import 'package:meta/meta.dart';
import 'profiling.dart';
import 'sentry_attachment/sentry_attachment.dart';

import 'event_processor.dart';
Expand Down Expand Up @@ -284,6 +285,7 @@ class SentryClient {
SentryTransaction transaction, {
Scope? scope,
SentryTraceContextHeader? traceContext,
ProfileInfo? profileInfo,
}) async {
SentryTransaction? preparedTransaction =
_prepareEvent(transaction) as SentryTransaction;
Expand Down Expand Up @@ -331,6 +333,9 @@ class SentryClient {
traceContext: traceContext,
attachments: attachments,
);
if (profileInfo != null) {
envelope.items.add(profileInfo.asEnvelopeItem());
}
final id = await captureEnvelope(envelope);

return id ?? SentryId.empty();
Expand Down
2 changes: 2 additions & 0 deletions dart/lib/src/sentry_envelope_item.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:meta/meta.dart';

import 'client_reports/client_report.dart';
import 'protocol.dart';
import 'utils.dart';
Expand Down
1 change: 1 addition & 0 deletions dart/lib/src/sentry_item_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ class SentryItemType {
static const String attachment = 'attachment';
static const String transaction = 'transaction';
static const String clientReport = 'client_report';
static const String profile = 'profile';
static const String unknown = '__unknown__';
}
9 changes: 4 additions & 5 deletions dart/lib/src/sentry_tracer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,14 @@ class SentryTracer extends ISentrySpan {
final transaction = SentryTransaction(this);
transaction.measurements.addAll(_measurements);

if (profiler != null) {
if (status == null || status == SpanStatus.ok()) {
transaction.profileInfo = await profiler?.finishFor(transaction);
}
}
final profileInfo = (status == null || status == SpanStatus.ok())
? await profiler?.finishFor(transaction)
: null;

await _hub.captureTransaction(
transaction,
traceContext: traceContext(),
profileInfo: profileInfo,
);
} finally {
profiler?.dispose();
Expand Down
76 changes: 20 additions & 56 deletions dart/test/mocks.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import 'dart:async' as _i4;

import 'package:mockito/mockito.dart' as _i1;
import 'package:sentry/sentry.dart' as _i3;
import 'package:sentry/src/profiling.dart' as _i2;
import 'package:sentry/sentry.dart' as _i2;
import 'package:sentry/src/profiling.dart' as _i3;

// ignore_for_file: type=lint
// ignore_for_file: avoid_redundant_argument_values
Expand All @@ -20,29 +20,9 @@ import 'package:sentry/src/profiling.dart' as _i2;
// ignore_for_file: camel_case_types
// ignore_for_file: subtype_of_sealed_class

class _FakeProfiler_0 extends _i1.SmartFake implements _i2.Profiler {
_FakeProfiler_0(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

class _FakeProfileInfo_1 extends _i1.SmartFake implements _i2.ProfileInfo {
_FakeProfileInfo_1(
Object parent,
Invocation parentInvocation,
) : super(
parent,
parentInvocation,
);
}

class _FakeSentryEnvelopeItem_2 extends _i1.SmartFake
implements _i3.SentryEnvelopeItem {
_FakeSentryEnvelopeItem_2(
class _FakeSentryEnvelopeItem_0 extends _i1.SmartFake
implements _i2.SentryEnvelopeItem {
_FakeSentryEnvelopeItem_0(
Object parent,
Invocation parentInvocation,
) : super(
Expand All @@ -54,51 +34,36 @@ class _FakeSentryEnvelopeItem_2 extends _i1.SmartFake
/// A class which mocks [ProfilerFactory].
///
/// See the documentation for Mockito's code generation for more information.
class MockProfilerFactory extends _i1.Mock implements _i2.ProfilerFactory {
class MockProfilerFactory extends _i1.Mock implements _i3.ProfilerFactory {
MockProfilerFactory() {
_i1.throwOnMissingStub(this);
}

@override
_i2.Profiler startProfiling(_i3.SentryTransactionContext? context) =>
(super.noSuchMethod(
Invocation.method(
#startProfiling,
[context],
),
returnValue: _FakeProfiler_0(
this,
Invocation.method(
#startProfiling,
[context],
),
),
) as _i2.Profiler);
_i3.Profiler? startProfiling(_i2.SentryTransactionContext? context) =>
(super.noSuchMethod(Invocation.method(
#startProfiling,
[context],
)) as _i3.Profiler?);
}

/// A class which mocks [Profiler].
///
/// See the documentation for Mockito's code generation for more information.
class MockProfiler extends _i1.Mock implements _i2.Profiler {
class MockProfiler extends _i1.Mock implements _i3.Profiler {
MockProfiler() {
_i1.throwOnMissingStub(this);
}

@override
_i4.Future<_i2.ProfileInfo> finishFor(_i3.SentryTransaction? transaction) =>
_i4.Future<_i3.ProfileInfo?> finishFor(_i2.SentryTransaction? transaction) =>
(super.noSuchMethod(
Invocation.method(
#finishFor,
[transaction],
),
returnValue: _i4.Future<_i2.ProfileInfo>.value(_FakeProfileInfo_1(
this,
Invocation.method(
#finishFor,
[transaction],
),
)),
) as _i4.Future<_i2.ProfileInfo>);
returnValue: _i4.Future<_i3.ProfileInfo?>.value(),
) as _i4.Future<_i3.ProfileInfo?>);
@override
void dispose() => super.noSuchMethod(
Invocation.method(
Expand All @@ -112,24 +77,23 @@ class MockProfiler extends _i1.Mock implements _i2.Profiler {
/// A class which mocks [ProfileInfo].
///
/// See the documentation for Mockito's code generation for more information.
class MockProfileInfo extends _i1.Mock implements _i2.ProfileInfo {
class MockProfileInfo extends _i1.Mock implements _i3.ProfileInfo {
MockProfileInfo() {
_i1.throwOnMissingStub(this);
}

@override
_i4.FutureOr<_i3.SentryEnvelopeItem> asEnvelopeItem() => (super.noSuchMethod(
_i2.SentryEnvelopeItem asEnvelopeItem() => (super.noSuchMethod(
Invocation.method(
#asEnvelopeItem,
[],
),
returnValue:
_i4.Future<_i3.SentryEnvelopeItem>.value(_FakeSentryEnvelopeItem_2(
returnValue: _FakeSentryEnvelopeItem_0(
this,
Invocation.method(
#asEnvelopeItem,
[],
),
)),
) as _i4.FutureOr<_i3.SentryEnvelopeItem>);
),
) as _i2.SentryEnvelopeItem);
}
2 changes: 2 additions & 0 deletions dart/test/mocks/mock_hub.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/profiling.dart';

import '../mocks.dart';
import 'mock_sentry_client.dart';
Expand Down Expand Up @@ -110,6 +111,7 @@ class MockHub with NoSuchMethodProvider implements Hub {
Future<SentryId> captureTransaction(
SentryTransaction transaction, {
SentryTraceContextHeader? traceContext,
ProfileInfo? profileInfo,
}) async {
captureTransactionCalls
.add(CaptureTransactionCall(transaction, traceContext));
Expand Down
2 changes: 2 additions & 0 deletions dart/test/mocks/mock_sentry_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:sentry/sentry.dart';
import 'package:sentry/src/profiling.dart';

import 'no_such_method_provider.dart';

Expand Down Expand Up @@ -84,6 +85,7 @@ class MockSentryClient with NoSuchMethodProvider implements SentryClient {
SentryTransaction transaction, {
Scope? scope,
SentryTraceContextHeader? traceContext,
ProfileInfo? profileInfo,
}) async {
captureTransactionCalls
.add(CaptureTransactionCall(transaction, traceContext));
Expand Down
1 change: 1 addition & 0 deletions flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Future<void> setupSentry(AppRunner appRunner, String dsn,
await SentryFlutter.init((options) {
options.dsn = exampleDsn;
options.tracesSampleRate = 1.0;
options.profilesSampleRate = 1.0;
options.reportPackages = false;
options.addInAppInclude('sentry_flutter_example');
options.considerInAppFramesByDefault = false;
Expand Down
Loading

0 comments on commit f50a5df

Please sign in to comment.