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

Provide urllib3 implementation of HTTPClient. #879

Merged
merged 3 commits into from
Dec 24, 2024

Conversation

purple4reina
Copy link
Contributor

@purple4reina purple4reina commented Dec 21, 2024

Requirements for Contributing to this repository

  • Fill out the template below. Any pull request that does not include enough information to be reviewed in a timely manner may be closed at the maintainers' discretion.
  • The pull request must only fix one issue, or add one feature, at the time.
  • The pull request must update the test suite to demonstrate the changed functionality.
  • After you create the pull request, all status checks must be pass before a maintainer reviews your contribution. For more details, please see CONTRIBUTING.

What does this PR do?

Adds a third implementation for HTTPClient using urllib3.

Description of the Change

The datadog-lambda-python package depends on datadogpy for submitting metrics in certain cases. The package is used in AWS Lambda environments where resources are very constrained. There is a hard limit of 250MB for all code plus dependencies. Customers are often hitting this limit and so keeping our package size down is important.

One of the largest dependencies of datadog-lambda-python is the requests package and it's dependencies. Removing this dependency would reduce the size of our installed package by roughly 8.5%.

This PR offers a new solution to this problem. We know that urllib3 is installed by default on all python lambda runtime containers, and thus does not count toward the 250MB limit. Switching from requests to urllib3 therefore would allow us to benefit from the 8.5% size reduction.

Alternate Designs

Possible Drawbacks

Verification Process

I created a testing python lambda layer using these changes and then removing requests and dependencies. It is deployed to the serverless sandbox as arn:aws:lambda:sa-east-1:425362996713:layer:Python39-REY:40.

I then created a handler that submits a metric using the datadogpy package. It then returns a list of installed packages and which http client class was used.

import json
import time

from datadog.api.http_client import resolve_http_client
from datadog_lambda.metric import lambda_metric

def get_version(module_name, *version_path):
    if not version_path:
        version_path = ['__version__']
    try:
        module = __import__(module_name)
        for attr in version_path:
            module = getattr(module, attr)
        return module
    except ImportError:
        return None

body = json.dumps(dict(
    requests_version=get_version('requests'),
    protobuf_version=get_version('google.protobuf', 'protobuf', '__version__'),
    urllib3_version=get_version('urllib3'),
    certifi_version=get_version('certifi'),
    idna_version=get_version('idna'),
    chardet_version=get_version('chardet'),
    charset_normalizer_version=get_version('charset_normalizer'),
    http_client_class=resolve_http_client().__name__,
))

def handler(event, context):
    lambda_metric('rey.kittens', 1, timestamp=time.time() - 60)
    return {'statusCode': 200, 'body': body}

The response shows that requests and dependencies -- certifi, idna, chardet, and charset-normalizer -- are no longer installed. Despite removing urllib3 from our layer, we can see that it is still found in the lambda runtime. Lastly, we see that the newly added Urllib3Client is used to submit metrics.

{
  "requests_version": null,
  "protobuf_version": "5.29.2",
  "urllib3_version": "1.26.19",
  "certifi_version": null,
  "idna_version": null,
  "chardet_version": null,
  "charset_normalizer_version": null,
  "http_client_class": "Urllib3Client"
}

The metric explorer shows the custom metric rey.kittens has been ingested.

Screenshot 2024-12-23 at 1 11 48 PM

Additional Notes

This change will have many additional positive impacts as well. We see a 12% reduction in cold start time for example.

Screenshot 2024-12-23 at 1 40 24 PM

Release Notes

Review checklist (to be filled by reviewers)

  • Feature or bug fix MUST have appropriate tests (unit, integration, etc...)
  • PR title must be written as a CHANGELOG entry (see why)
  • Files changes must correspond to the primary purpose of the PR as described in the title (small unrelated changes should have their own PR)
  • PR must have one changelog/ label attached. If applicable it should have the backward-incompatible label attached.
  • PR should not have do-not-merge/ label attached.
  • If Applicable, issue must have kind/ and severity/ labels attached at least.

@purple4reina purple4reina force-pushed the rey.abolofia/urllib3-imp branch 4 times, most recently from cf7b603 to fa5ce4e Compare December 23, 2024 18:52
@purple4reina purple4reina force-pushed the rey.abolofia/urllib3-imp branch from fa5ce4e to f8b29e4 Compare December 23, 2024 18:56
@purple4reina purple4reina marked this pull request as ready for review December 23, 2024 22:08
@purple4reina purple4reina requested a review from a team as a code owner December 23, 2024 22:08
@purple4reina purple4reina added the changelog/Added Added features results into a minor version bump label Dec 23, 2024
@purple4reina purple4reina force-pushed the rey.abolofia/urllib3-imp branch from c18bb87 to 88dc020 Compare December 24, 2024 18:24
@purple4reina purple4reina force-pushed the rey.abolofia/urllib3-imp branch from 88dc020 to 3d084ac Compare December 24, 2024 19:12
@purple4reina purple4reina merged commit fc4c147 into master Dec 24, 2024
10 checks passed
@purple4reina purple4reina deleted the rey.abolofia/urllib3-imp branch December 24, 2024 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/Added Added features results into a minor version bump
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants