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 hooks for easy performance tracing #3735

Open
ueman opened this issue Feb 9, 2023 · 2 comments
Open

feat: Add hooks for easy performance tracing #3735

ueman opened this issue Feb 9, 2023 · 2 comments
Assignees
Labels
enhancement candidate Candidate for enhancement but additional research is needed pkg:bloc This issue is related to the bloc package

Comments

@ueman
Copy link
Contributor

ueman commented Feb 9, 2023

Description

I'm currently experimenting with automatic performance tracing for blocs. For that I would like to hook somehow into blocs easily.

I'm especially interested in tracing the time an EventHandler takes to execute. That method basically represents the code which is executed after adding an event until that work is handled. At least that's my understanding.

By performance tracing I mean to trace it with tools like Sentry, Datadog, etc. I don't mean the Dart Dev Tools.

Desired Solution

I think I need some hooks around this code:

try {
_emitters.add(emitter);
await handler(event as E, emitter);
} catch (error, stackTrace) {
onError(error, stackTrace);
rethrow;
} finally {
onDone();
}

try {
  _emitters.add(emitter);
  // I want to start tracing here
  await handler(event as E, emitter);
  // I want to end tracing here
} catch (error, stackTrace) {
  onError(error, stackTrace);
  rethrow;
} finally {
  onDone();
}

Ideally, I'm also able to wrap await handler(event as E, emitter); somehow myself in a try catch so that I can set the status of the trace to something like success/failure.

Something like the following pseudo code would be sufficient:

typedef Executor = FutureOr<void> Function(handler, event, emitter);
class Bloc {
  static Executor? executor;

  /// ...
  try {
    _emitters.add(emitter);
    if(executor != null) {
      await executor(handler, event, emitter);
    } else {
      await handler(event as E, emitter);
    }
  } catch (error, stackTrace) {
    onError(error, stackTrace);
    rethrow;
  } finally {
    onDone();
  }
  /// ...
}

Bloc.executor = (handler, event, emitter) {
  startTransaction();
  try {
    await handler(event as E, emitter);
    stopTransactionWithSuccess();
  } catch(_) {
    stopTransactionWithFailure();
    rethrow;
  }
}

Alternatives Considered

I'm not really well versed with bloc so I'm not aware of any other possible approach. I'm therefore open for any alternative approaches or suggestions.

Additional Context

@felangel felangel added the needs triage This issue requires triage label Mar 11, 2023
@felangel
Copy link
Owner

Hi @ueman 👋
Thanks for opening an issue!

The following is invoking your custom event handlers registered in the bloc via on<E>(handler).

await handler(event as E, emitter);

You can create a profiling handler like:

import 'package:bloc/bloc.dart';

void main() {
  final bloc = CounterBloc()
    ..stream.listen(print)
    ..add(CounterEvent.increment);
}

enum CounterEvent {
  increment,
  decrement,
}

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<CounterEvent>(
      profilingEventHandler((event, emit) {
        emit(state + 1);
      }),
    );
  }
}

EventHandler<E, S> profilingEventHandler<E, S>(EventHandler<E, S> handler) {
  return (event, emit) async {
    print('start transaction');
    try {
      await handler(event, emit);
      print('stop transaction');
    } catch (error) {
      print('stop transaction with error');
    }
  };
}

Let me know if that helps and sorry for the delayed response 👍

@felangel felangel self-assigned this Apr 15, 2023
@felangel felangel added question Further information is requested waiting for response Waiting for follow up pkg:bloc This issue is related to the bloc package and removed needs triage This issue requires triage labels Apr 15, 2023
@ueman
Copy link
Contributor Author

ueman commented Apr 16, 2023

While this is definitely working, I was hoping for a more general solution for which I don't have to edit every single bloc class.

@felangel felangel added enhancement candidate Candidate for enhancement but additional research is needed and removed waiting for response Waiting for follow up labels May 5, 2023
@felangel felangel removed the question Further information is requested label May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement candidate Candidate for enhancement but additional research is needed pkg:bloc This issue is related to the bloc package
Projects
None yet
Development

No branches or pull requests

2 participants