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

[Logs forwarder] Parse enhanced metrics from Lambda telemetry JSON logs. #859

Merged
merged 2 commits into from
Oct 29, 2024

Conversation

dtcaciuc-ggs
Copy link
Contributor

@dtcaciuc-ggs dtcaciuc-ggs commented Oct 22, 2024

What does this PR do?

This change makes forwarder parse enhanced metrics from Lambda telemetry logs which get emitted instead of regular REPORT ones when JSON format is enabled. For schema, see https://docs.aws.amazon.com/lambda/latest/dg/telemetry-schema-reference.html#platform-report

Motivation

I would like to see same basic enhanced metrics I see when Lambda uses plain text logging when I enable JSON logging option.

Testing Guidelines

  1. tests/run_unit_tests.sh
  2. Build forwarder zip, uploaded to sandbox AWS account w/ DD integration, ran lambda w/ LOG_FORMAT=JSON envar set and confirmed enhanced telemetry is showing up

Additional Notes

  1. I wanted to add integration tests but they're current seem to be broken (see Integration tests fail but build pipelines still pass #858)
  2. I replicated how cold_start tag is set for REPORT logs but for some reason neither show up in our DD instance

Types of changes

  • Bug fix
  • New feature
  • Breaking change
  • Misc (docs, refactoring, dependency upgrade, etc.)

Check all that apply

  • This PR's description is comprehensive
  • This PR contains breaking changes that are documented in the description
  • This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
  • This PR impacts documentation, and it has been updated (or a ticket has been logged)
  • This PR's changes are covered by the automated tests
  • This PR collects user input/sensitive content into Datadog
  • This PR passes the integration tests (ask a Datadog member to run the tests)
  • This PR passes the unit tests
  • This PR passes the installation tests (ask a Datadog member to run the tests)

These get emitted instead of regular REPORT logs if log format is set to JSON.
@dtcaciuc-ggs dtcaciuc-ggs marked this pull request as ready for review October 22, 2024 02:10
@dtcaciuc-ggs dtcaciuc-ggs changed the title Parse enhanced metrics from Lambda telemetry JSON logs. [Logs forwarder] Parse enhanced metrics from Lambda telemetry JSON logs. Oct 22, 2024
@ge0Aja ge0Aja self-assigned this Oct 29, 2024
Comment on lines 262 to 270
MEMORY_ALLOCATED_RECORD_KEY = "memorySizeMB"
INIT_DURATION_RECORD_KEY = "initDurationMs"
BILLED_DURATION_RECORD_KEY = "billedDurationMs"
RUNTIME_METRICS_BY_RECORD_KEY = {
# Except INIT_DURATION_RECORD_KEY which is handled separately
"durationMs": DURATION_METRIC_NAME,
BILLED_DURATION_RECORD_KEY: BILLED_DURATION_METRIC_NAME,
"maxMemoryUsedMB": MAX_MEMORY_USED_METRIC_NAME,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could you move these to the top of the file.
nit: let's put dureationMs and maxMemeoryUsedMB as keys as well

Comment on lines 274 to 275
if not log_message.startswith("{"):
return []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no need for this here, we can return at the try/except of parsing json fails

)
)

if record["status"] == "timeout":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if record["status"] == "timeout":
if record.get("status") == "timeout":

f"{MEMORY_ALLOCATED_FIELD_NAME}:{record_metrics[MEMORY_ALLOCATED_RECORD_KEY]}"
]

try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to have less try/except patterns

Suggested change
try:
init_duration = record_metrics.get(INIT_DURATION_RECORD_KEY)
if init_duration:
tags.append("cold_start:true")
metrics.append(
DatadogMetricPoint(
f"{ENHANCED_METRICS_NAMESPACE_PREFIX}.{INIT_DURATION_METRIC_NAME}",
init_duration * METRIC_ADJUSTMENT_FACTORS[INIT_DURATION_METRIC_NAME],
)
)
else:
tags.append("cold_start:false")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, for some reason, suggestion code doesn't have removed lines I'd expected it to have. I'll apply this change with the rest in one commit.

* Less try/catch
* Organize constants
* Remove unnecessary defensive JSON check
@dtcaciuc-ggs dtcaciuc-ggs requested a review from ge0Aja October 29, 2024 15:39
@ge0Aja ge0Aja merged commit 50e34e8 into DataDog:master Oct 29, 2024
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants