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

Allow nested objects to be used in logging context data #142

Closed
scopendo opened this issue Jun 7, 2022 · 6 comments
Closed

Allow nested objects to be used in logging context data #142

scopendo opened this issue Jun 7, 2022 · 6 comments
Labels
awaiting response Waiting for response from the reporter of the issue enhancement New feature or request

Comments

@scopendo
Copy link

scopendo commented Jun 7, 2022

Is your feature request related to a problem? Please describe.
It does not appear to be possible to log a data object if that object contains Dart enum values. For example, I have a data object where the class has been generated by the popular Freezed package, which includes attributes that are enum values. If I try to log such an object then I get an error:

flutter: [Datadog 🐶⚠️ ] ArgumentError when calling logs.info: parameter null could not be properly converted to a supported native type.

Here is a simplified example.

enum PlaceName {
  organisation,
  reception,
}

@freezed
class HomeSettings with _$HomeSettings {
  const factory HomeSettings({
    @Default(PlaceName.organisation) PlaceName placeName,
  }) = _HomeSettings;

  const HomeSettings._();

  factory HomeSettings.fromJson(Map<String, dynamic> json) =>
      _$HomeSettingsFromJson(json);
}
final settings = HomeSettings(placeName: PlaceName.reception);

// This won't work as the settings object contains a enum value
DatadogSdk.instance.logs?.info(
  'example', 
  {
    'settings': settings.toJson(),
  },
);

Describe the solution you'd like
Add support for logging of values that can be encoded as JSON.

@scopendo scopendo added the enhancement New feature or request label Jun 7, 2022
@fuzzybinary
Copy link
Member

Hi @scopendo!

I tried to replicate your example and with freezed 2.0.3+1 and json_serializable 6.2.0 this works as expected so long as you do what's listed and convert the object to Json first, since json_serialziable converts those properly to strings.

image

Generally, anything that is valid JSON will work when passed as an attribute to any call that needs accepts context.

Can you take another look at the Map resulting from your .toJson call and see if there's another possible cause other than the enum? A nested object maybe?

@fuzzybinary fuzzybinary added the awaiting response Waiting for response from the reporter of the issue label Jun 7, 2022
@scopendo
Copy link
Author

scopendo commented Jun 7, 2022

Thanks for checking @fuzzybinary – I'll take a deeper look and see what's going on with my example that's failing. It'd be great if the error message was able to better identify the map key that's causing the issue, rather than parameter null....

@fuzzybinary
Copy link
Member

@scopendo Agreed, I will look into that as a separate issue.

@scopendo scopendo changed the title Allow enum values to be used in logging context data Allow nested objects to be used in logging context data Jun 9, 2022
@scopendo
Copy link
Author

scopendo commented Jun 9, 2022

It looks like the issue isn't really to do with enums but more to do with nested (freezed) sub-objects. I'm still to prepare an example, however, I've found a workaround to be encoding and decoding:

Datadog.instance.rum?.addTapAction('setup_done', {
  'settings': jsonDecode(jsonEncode(settings.toJson())),
});

@fuzzybinary
Copy link
Member

I think this is just a weird behavior of json_serializable that it doesn't actually serialize nested objects by default. Here's a bit more detail:
https://docs.flutter.dev/development/data-and-backend/json#generating-code-for-nested-classes

It looks like if you add:

@JsonSerializable(explicitToJson: true)

above your class is will correctly navigate to children and serialize them to JSON with just the .toJson() call.

Let me know if that works and we can close this issue and I'll open a new one for properly logging these types of errors.

@fuzzybinary
Copy link
Member

Closing in favor of #149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting response Waiting for response from the reporter of the issue enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants