Skip to content

Drop aws-embedded-metrics from the stats lambda#2154

Merged
mattgodbolt merged 2 commits into
mainfrom
lambda-drop-aws-embedded-metrics
Jun 4, 2026
Merged

Drop aws-embedded-metrics from the stats lambda#2154
mattgodbolt merged 2 commits into
mainfrom
lambda-drop-aws-embedded-metrics

Conversation

@mattgodbolt
Copy link
Copy Markdown
Member

Continues the lambda housekeeping. aws-embedded-metrics dragged in the entire aiohttp subtree (aiohttp, yarl, multidict, frozenlist, propcache, aiohappyeyeballs) -- the bulk of this lambda's recurring Dependabot churn -- yet in the Lambda runtime the library only uses its stdout sink, so aiohttp was never exercised. stats only used set_namespace/set_property/put_metric.

Change

  • Emit the single PageLoad metric as an Embedded Metric Format JSON line on stdout (emit_metric). CloudWatch ingests EMF from the logs the same way the library did.
  • Namespace (CompilerExplorer), metric name (PageLoad) and value (1) are unchanged. Unit kept as the library default "None" so any alarm keyed on it still matches.
  • Behaviour change: the metric is emitted only on the pageload path, instead of the decorator flushing a (mostly empty) EMF record on every invocation. The sponsors property is preserved.
  • Dimensions: emitted with no dimensions (aggregate), vs the library's default service dimensions -- to verify live.

Impact

  • Lambda package: 4.2 MB -> 845 KB; six transitive deps removed.
  • No more aiohttp/yarl/multidict/... Dependabot PRs for this lambda.

Tests

  • emit_metric is unit-tested for EMF shape (namespace, metric, timestamp, property).
  • Existing pageload tests now assert emit_metric is called with the right args.
  • 26 passed; make static-checks clean; package builds.

Verification plan (post-merge, before/after deploy)

Deploy stats then invoke a real POST /pageload and confirm a new datapoint lands on the CompilerExplorer/PageLoad metric via GetMetricStatistics -- i.e. confirm the hand-rolled EMF is actually ingested, not just logged. A brief stats blip during deploy is acceptable.

🤖 Generated with Claude Code

aws-embedded-metrics pulled in the entire aiohttp subtree (aiohttp, yarl,
multidict, frozenlist, propcache, aiohappyeyeballs) -- the bulk of this
lambda's recurring Dependabot churn -- yet in Lambda the library only uses
its stdout sink, so aiohttp was never exercised at runtime.

stats only used set_namespace/set_property/put_metric, so emit the one
PageLoad metric as an Embedded Metric Format JSON line on stdout directly.
CloudWatch ingests EMF from the logs identically; the namespace, metric
name and value are unchanged (unit kept as the library's default "None" so
any alarm keyed on it still matches). The metric is now emitted only on the
pageload path rather than flushed (mostly empty) on every invocation.

Drops the package from 4.2MB to 845KB and removes six transitive
dependencies. emit_metric is unit-tested for EMF shape; the existing
pageload tests assert it is called correctly.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 4, 2026 01:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR removes the aws-embedded-metrics dependency from the stats Lambda and replaces its usage with a minimal, hand-rolled CloudWatch Embedded Metric Format (EMF) JSON line written directly to stdout, substantially reducing the deployed package size and transitive dependency churn.

Changes:

  • Dropped aws-embedded-metrics from the Lambda dependency set (and lockfile), removing the aiohttp dependency subtree.
  • Added an emit_metric() helper to output an EMF record to stdout for the PageLoad metric (preserving the sponsors property).
  • Updated existing pageload tests and added a unit test validating EMF output shape.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
lambda/uv.lock Removes aws-embedded-metrics and its transitive dependency subtree from the Lambda lockfile.
lambda/stats.py Replaces embedded-metrics decorator/logger usage with a direct EMF stdout emit for PageLoad.
lambda/stats_test.py Adds EMF-shape unit test and updates pageload tests to assert emit_metric() usage.
lambda/pyproject.toml Removes aws-embedded-metrics from Lambda project dependencies.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lambda/stats.py Outdated
Two fixes after inspecting the live metric:

- The existing CompilerExplorer/PageLoad metric (queried by a Grafana
  dashboard) carries aws-embedded-metrics' default dimensions
  (LogGroup/ServiceName/ServiceType = stats). Reproduce them so the metric
  stays the same stream rather than forking into a dimensionless series.
- Flush stdout when writing the EMF record: Lambda's stdout is
  block-buffered, so without it the line can be delayed past the
  invocation or dropped on recycle, losing the metric (review feedback).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@mattgodbolt mattgodbolt merged commit 5e22ec7 into main Jun 4, 2026
1 check passed
@mattgodbolt mattgodbolt deleted the lambda-drop-aws-embedded-metrics branch June 4, 2026 02:06
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