Skip to content

Instantly share code, notes, and snippets.

@ueman
Created October 11, 2022 15:52
Show Gist options
  • Save ueman/bac93b435dfe2c09fef6a9a218137675 to your computer and use it in GitHub Desktop.
Save ueman/bac93b435dfe2c09fef6a9a218137675 to your computer and use it in GitHub Desktop.
Sentry Dart integrations
// Integration for http_link / GraphQL
// The following code can be used as
// final httpLink = HttpLink(
// appConfig.graphQL,
// httpClient: httpClient,
// useGETForQueries: true,
// serializer: SentryRequestSerializer(),
// httpResponseDecoder: sentryResponseDecoder,
// );
import 'dart:async';
import 'dart:convert';
import 'package:graphql/client.dart';
import 'package:sentry/sentry.dart';
import 'package:http/http.dart';
class SentryRequestSerializer implements RequestSerializer {
SentryRequestSerializer({RequestSerializer? inner})
: inner = inner ?? const RequestSerializer();
final RequestSerializer inner;
@override
Map<String, dynamic> serializeRequest(Request request) {
final span = Sentry.getSpan()?.startChild(
'serialize.http.client',
description: 'graph-ql-request-serialization',
);
Map<String, dynamic> result;
try {
result = inner.serializeRequest(request);
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>? sentryResponseDecoder(Response response) {
final span = Sentry.getSpan()?.startChild(
'serialize.http.client',
description: '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(Response httpResponse) {
return json.decode(utf8.decode(httpResponse.bodyBytes))
as Map<String, dynamic>?;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment