フラッタにおけるGRPC性能


Firebase Performance Monitoring迅速かつ簡単な方法をあなたのアプリのパフォーマンスを監視を開始することです.それは自動的にあなたのサーバー側のメトリックを褒め称賛し、どのようにあなたのアプリケーションのユーザーが実際に感じているかを理解するための素晴らしい方法ですHTTP(s)の要求を含む様々なメトリックを監視します.
あなたがフラッタアプリで要求をするためにGRPCを使用している場合、その魔法は自動的にキックしません.代わりに、GRPCリクエストのパフォーマンスを測定し、FireBaseに報告するために簡単なインターセプターにドロップすることができます.lib/performance_interceptor.dart
import 'package:firebase_performance/firebase_performance.dart';
import 'package:grpc/grpc.dart';

class PerformanceInterceptor implements ClientInterceptor {
  FirebasePerformance _performance = FirebasePerformance.instance;
  Map<String, String> attributes;

  PerformanceInterceptor([this.attributes = const {}]);

  @override
  ResponseFuture<R> interceptUnary<Q, R>(ClientMethod<Q, R> method, Q request,
      CallOptions options, ClientUnaryInvoker<Q, R> invoker) {
    Trace metric = _performance.newTrace(method.path);
    metric.start();
    this.attributes.forEach((key, value) => metric.putAttribute(key, value));

    ResponseFuture<R> resp = invoker(method, request, options);
    resp.then((_) {
      metric.stop();
    });
    return resp;
  }

  @override
  ResponseStream<R> interceptStreaming<Q, R>(
      ClientMethod<Q, R> method,
      Stream<Q> requests,
      CallOptions options,
      ClientStreamingInvoker<Q, R> invoker) {
    return invoker(method, requests, options);
  }
}
このクラスはスーパーシンプルで、GRPCクライアントを構築する際にオプションとチャネルと一緒にパスします.
import 'performance_interceptor.dart';

...

client = GreeterClient(
  channel,
  interceptors: [
    PerformanceInterceptor()
  ],
);
また、各リクエストで渡される属性(ディメンション)を渡すこともできます.私は接続しているサーバーを渡すのが好きです.
client = GreeterClient(
  channel,
  interceptors: [
    PerformanceInterceptor({'host': hostname})
  ],
);
プレスト!今、すべてのあなたの単純なGRPCの要求のためのパフォーマンスメトリックを持っている!