Git Product home page Git Product logo

datadog_flutter's Introduction

Pub

Datadog Flutter

Community implementation of native bindings for Datadog's SDK. This is not an official package.

Setup

  1. Generate a client token from Datadog through the Settings > API panel (under Client Tokens). If you're using RUM, do not toggle between the RUM platform client tokens.
  2. Initialize:
    await DatadogFlutter.initialize(
      clientToken: myDatadogClientToken,
      serviceName: 'my-app-name',
      environment: 'production',
    )
  3. Associate RUM and log events (optional):
    await DatadogFlutter.setUserInfo(id: <YOUR_USER_ID>);
  4. Acknowledge TrackingConsent at initialization or later within your application. Events will not be logged until trackingConsent is .granted. This value can be updated via DatadogFlutter.updateTrackingConsent.

โš ๏ธ Your Podfile must have use_frameworks! (Flutter includes this by default) and your minimum iOS target must be >= 11. This is a requirement from the Datadog SDK.

Flutter Web Caveats

The Datadog scripts need to be inserted in your index.html document. Please add both the logging script and the RUM script to their own <script> tags. DO NOT add the .init on .onReady code.

Logging

In its default implementation, log data will only be transmitted to Datadog through Logger records. print statements are not guaranteed to be captured.

ddLogger = DatadogLogger(loggerName: 'orders');
// optionally set a value for HOST
// ddLogger.addAttribute('hostname', <DEVICE IDENTIFIER>);

ddLogger.addTag('restaurant_type', 'pizza');
ddLogger.removeTag('restaurant_type');

// add attribute to every log
ddLogger.addAttribute('toppings', 'extra_cheese');

// add atttributes to some logs
ddLogger.log('time to cook pizza', Level.FINE, attributes: {
  'durationInMilliseconds': timer.elapsedMilliseconds,
});

Flutter Web Caveats

  • addTag and removeTag are not invoked and resolve silently when using Flutter web. This is a missing feature in Datadog's JS SDK.

Real User Monitoring

RUM adds support for error, event, and screen tracking. The integration requires additional configuration for each service.

  1. Supply an application ID to initialize:
    await DatadogFlutter.initialize(
      clientToken: myDatadogClientToken,
      serviceName: 'my-app-name',
      environment: 'production',
      iosRumApplicationId: myiOSRumApplicationId,
      androidRumApplicationId: myAndroidRumApplicationId,
      webRumApplicationId: myWebRumApplicationId,
    )
  2. Automatically track screens:
    MaterialApp(
      // ...your material config...
      home: HomeScreen(),
      navigatorObservers: [
        DatadogObserver(),
      ],
    );
  3. Automatically report errors:
    void main() async {
      // Capture Flutter errors automatically:
      FlutterError.onError = DatadogRum.instance.addFlutterError;
    
      // Catch errors without crashing the app:
      runZonedGuarded(() {
        runApp(MyApp());
      }, (error, stackTrace) {
        DatadogRum.instance.addError(error, stackTrace);
      });
    }
  4. Manually track additional events (please note that Android will only report RUMAction.custom events):
    GestureDetector(onTap: () {
      DatadogRum.instance.addUserAction('EventTapped');
    })
  5. Manually track additional errors:
    try {
      throw StateError();
    } catch (e, st) {
      DatadogRum.instance.addError(e, st);
    }
  6. Manually track network requests or resources:
    await DatadogRum.startResourceLoading(
      aUniqueIdentifier,
      url: 'https://example.com',
      method: RUMResources.get,
    );
    await DatadogRum.stopResourceLoading(
      aUniqueIdentifier,
      statusCode: 500,
      errorMessage: 'Internal Server Error' ,
    )

Flutter Web Caveats

  • addUserAction ignores the action when using Flutter web.
  • updateTrackingConsent is not invoked and fails silently when using Flutter web. This is a missing feature in Datadog's JS SDK.
  • stopView is not invoked and fails silently when using Flutter web. This is a missing feature in Datadog's JS SDK.
  • startUserAction and stopStopUserAction are not invoked and fail silently when using Flutter web. This is a missing feature in Datadog's JS SDK.
  • startResourceLoading and stopResourceLoading are not invoked and resolve silently when using Flutter web. This is a missing feature in Datadog's JS SDK.
  • setUserInfo does not support custom attributes when using Flutter web. This is due to Dart's method of strongly typing JS. Only name, id, and email are supported.

Tracing

Associate your HTTP requests with your backend service. Be sure to setup (usually immediately after DatadogFlutter.initialize):

await DatadogTracing.initialize();

For one-off requests, instantiate a fresh client:

final httpClient = DatadogTracingHttpClient();
// make requests
final response = await httpClient.get(Uri(string: 'http://example.com');

For frameworks that use an internal client like Brick, compose the client:

RestProvider(
  client: DatadogTracingHttpClient();
)
// or compose if another client is already being used:
RestProvider(
  client: DatadogTracingHttpClient(GZipHttpClient());
)

FAQ

How do I disable logging when I'm developing locally?

By default, the DatadogFlutter default constructor will send all logs from Logger to Datadog. To ignore, set bindOnRecord:

DatadogLogger(bindOnRecord: false)

And log conditionally later:

Logger.root.onRecord.listen((record) async {
  if (shouldSendToDatadog) {
    ddLogger.onRecordCallback(record)
  } else {
    print(record);
  }
});

datadog_flutter's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.