sentry_link 0.1.0 copy "sentry_link: ^0.1.0" to clipboard
sentry_link: ^0.1.0 copied to clipboard

Automatic capture of exceptions and GraphQL errors for the gql eco-system, like graphql and ferry

Sentry Link (GraphQL) #

pub package likes popularity pub points

Integration for the gql_link package to collect error reports for GraphQL requests. This is used by a wide variety of GraphQL libraries like ferry or graphql.

Usage #

Just add SentryLink.link() and/or SentryTracingLink to your links.

final link = Link.from([
    AuthLink(getToken: () async => 'Bearer $personalAccessToken'),
    // SentryLink records exceptions
    SentryLink.link(),
    // SentryTracingLink adds performance tracing with Sentry
    SentryTracingLink(shouldStartTransaction: true),
    HttpLink('https://api.github.com/graphql'),
]);

In addition to that, you can add GqlEventProcessor to Sentry's event processor, to improve support for nested LinkExceptions.

A GraphQL error will be reported like the following screenshot:

SentryResponseParser and SentryRequestSerializer #

The SentryResponseParser and SentryRequestSerializer classes can be used to trace the serialization process. Both classes work with HttpLink and DioLink. When using the HttpLink, you can additionally use the sentryResponseDecoder function.

import 'package:sentry_link/sentry_link.dart';

final link = Link.from([
    SentryLink.link(),
    AuthLink(getToken: () async => 'Bearer $personalAccessToken'),
    SentryTracingLink(shouldStartTransaction: true),
    HttpLink(
      'https://api.github.com/graphql',
      httpClient: SentryHttpClient(networkTracing: true),
      serializer: SentryRequestSerializer(),
      parser: SentryResponseParser(),
    ),
  ]);

  final client = GraphQLClient(
    cache: GraphQLCache(),
    link: link,
  );
HttpLink

Bonus HttpLink tracing #

import 'dart:async';
import 'dart:convert';

import 'package:sentry/sentry.dart';
import 'package:http/http.dart' as http;

import 'package:sentry_link/sentry_link.dart';

final link = Link.from([
  SentryLink.link(),
  AuthLink(getToken: () async => 'Bearer $personalAccessToken'),
  SentryTracingLink(shouldStartTransaction: true),
  HttpLink(
    'https://api.github.com/graphql',
    httpClient: SentryHttpClient(networkTracing: true),
    serializer: SentryRequestSerializer(),
    parser: SentryResponseParser(),
    httpResponseDecoder: sentryResponseDecoder,
  ),
]);

final client = GraphQLClient(
  cache: GraphQLCache(),
  link: link,
);

Map<String, dynamic>? sentryResponseDecoder(
  http.Response response, {
  Hub? hub,
}) {
  final currentHub = hub ?? HubAdapter();
  final span = currentHub.getSpan()?.startChild(
        'serialize.http.client',
        description: 'http response deserialization',
      );
  Map<String, dynamic>? result;
  try {
    result = _defaultHttpResponseDecoder(response);
    span?.status = const SpanStatus.ok();
  } catch (e) {
    span?.status = const SpanStatus.unknownError();
    span?.throwable = e;
    rethrow;
  } finally {
    unawaited(span?.finish());
  }
  return result;
}

Map<String, dynamic>? _defaultHttpResponseDecoder(http.Response httpResponse) {
  return json.decode(utf8.decode(httpResponse.bodyBytes))
      as Map<String, dynamic>?;
}

📣 About the author #

  • Twitter Follow
  • GitHub followers
7
likes
0
pub points
77%
popularity

Publisher

verified publisheruekoetter.dev

Automatic capture of exceptions and GraphQL errors for the gql eco-system, like graphql and ferry

Repository (GitHub)
View/report issues

Funding

Consider supporting this project:

github.com
www.buymeacoffee.com

License

unknown (LICENSE)

Dependencies

gql, gql_error_link, gql_exec, gql_link, sentry

More

Packages that depend on sentry_link