GoogleCloudPlatform / functions-framework-dart

FaaS (Function as a service) framework for writing portable Dart functions
https://pub.dev/packages/functions_framework
Apache License 2.0
536 stars 54 forks source link

fix(gcp): allow logging message as jsonPayload #393

Closed felangel closed 1 year ago

felangel commented 1 year ago

Closes #394

Currently, log messages do not get interpreted as jsonPayload even if a JSON object is provided:

log({'hello': 'world'});

results in:

textPayload: "{hello: world}"

This change results in the following log:

jsonPayload: {
  message: {
    hello: world
  }
}

Are there existing tests for the logger? I looked but wasn't able to find any.

kevmoo commented 1 year ago

gah! this is tricky.

Before this change EVERY message is encoded with toString with this change all messages are encoded with jsonEncode. Which might cause failures in a bunch of unexpected cases.

I wonder if we should add a JsonMessage wrapper type that we can test against? See where I'm going?

felangel commented 1 year ago

gah! this is tricky.

Before this change EVERY message is encoded with toString with this change all messages are encoded with jsonEncode. Which might cause failures in a bunch of unexpected cases.

I wonder if we should add a JsonMessage wrapper type that we can test against? See where I'm going?

Just to make sure I understand, you're suggesting introducing a JsonMessage class that developers will need to explicitly wrap their messages in like:

log(JsonMessage({'hello': 'world'}));

Is that correct?

kevmoo commented 1 year ago

@felangel – suggesting/brainstorming/etc – yeah. Thoughts?

felangel commented 1 year ago

@kevmoo what are your thoughts on checking if the message is json encodable and defaulting to string if not? Would that be too expensive of an operation?

kevmoo commented 1 year ago

@kevmoo what are your thoughts on checking if the message is json encodable and defaulting to string if not? Would that be too expensive of an operation?

I guess we could just test is Map. Should be fine. A tiny bit hacky, but I doubt it'd break anyone.

felangel commented 1 year ago

@kevmoo what are your thoughts on checking if the message is json encodable and defaulting to string if not? Would that be too expensive of an operation?

I guess we could just test is Map. Should be fine. A tiny bit hacky, but I doubt it'd break anyone.

@kevmoo updated the PR lmk what you think?

kevmoo commented 1 year ago

@felangel – now the nits.

felangel commented 1 year ago

@felangel – now the nits.

  • [ ] test
  • [ ] changelog entry
  • [ ] Likely want to include some doc comments (maybe on cloudLoggingMiddleware) explaining this behavior?

Sounds great, I didn't see any tests for log but happy to try to add some from scratch.

kevmoo commented 1 year ago

@felangel – crap. If there is no testing, we should leave it for now. Plumbing through the right logic there is more work than you should have to do...

felangel commented 1 year ago

@felangel – crap. If there is no testing, we should leave it for now. Plumbing through the right logic there is more work than you should have to do...

@kevmoo sounds good, I updated api docs and CHANGELOG lmk if there's anything else you want me to do.

kevmoo commented 1 year ago

You're going to have to rebase, now. 😁

felangel commented 1 year ago

@kevmoo, just rebased on main 👍