Skip to content

Would be great to have a test mode that stub out the library #17

Closed

Description

@jackyjjc-canva

It would be nice to have a stub version of the wrapper. Currently when I'm running test on the lambda function code locally it will trigger error in two places:

  1. extract_dd_trace_context() in the class _LambdaDecorator will throw exception because missing trace segment.

  2. When metric.lambda_metric() is run in tests, it throws exception because it cannot send metrics to datadog.

Activity

  1. tianchu commented on Oct 1, 2019

    @tianchu
    Collaborator

    Thanks for the feedback @jackyjjc-canva, we will look into this! Meanwhile, do you mind sharing a little bit more details with us, say a code snippet from your tests that you expect to work?

  2. jackyjjc-canva commented on Oct 2, 2019

    @jackyjjc-canva
    Author

    This is my Lamdba function (I took out the parts with business logic)

    @datadog_lambda.wrapper.datadog_lambda_wrapper
    def lambda_handler(event, _):
      try:
        return _lambda_handler(event)
      except AuthenticationError as e:
          metric.lambda_metric("auth.failure", 1, tags=["error:%s" % e.short_message, DATADOG_ENV_TAG])
        return {
          "statusCode": 403
        }
    

    This is my test:

    import unittest
    class TestAuthModule(unittest.TestCase):
      def test_lambda_handler_rejects_user(self) -> None:
          event = {
             "username": "user"
             "password": "invalid-password"
          }
          self.assertEqual(auth.lambda_handler(event, {})["body"], "{}")
    

    The first issue happens when I import the module to be tested because _LambdaDecorator is invoked at that point in time. The exact exception was:

    cannot find the current segment/subsegment, please make sure you have a segment open
    Traceback (most recent call last):
      File "/lambda/.ve/lib/python3.7/site-packages/datadog_lambda/wrapper.py", line 60, in _before
        set_correlation_ids()
      File "/lambda/.ve/lib/python3.7/site-packages/datadog_lambda/tracing.py", line 127, in set_correlation_ids
        context = get_dd_trace_context()
      File "/lambda/.ve/lib/python3.7/site-packages/datadog_lambda/tracing.py", line 98, in get_dd_trace_context
        xray_trace_entity = xray_recorder.get_trace_entity()  # xray (sub)segment
      File "/lambda/.ve/lib/python3.7/site-packages/aws_xray_sdk/core/recorder.py", line 369, in get_trace_entity
        return self.context.get_trace_entity()
      File "/lambda/.ve/lib/python3.7/site-packages/aws_xray_sdk/core/context.py", line 93, in get_trace_entity
        return self.handle_context_missing()
      File "/lambda/.ve/lib/python3.7/site-packages/aws_xray_sdk/core/context.py", line 118, in handle_context_missing
        raise SegmentNotFoundException(MISSING_SEGMENT_MSG)
    aws_xray_sdk.core.exceptions.exceptions.SegmentNotFoundException: cannot find the current segment/subsegment, please make sure you have a segment open
    

    The second issue happens when I run the tests. Of course someone can make the case that I should test the business logic separated and I don't need to test the Lambda code. However I would still like to have an end-to-end test that test the whole Lambda code path besides my business logic tests.

  3. tianchu commented on Oct 2, 2019

    @tianchu
    Collaborator

    @jackyjjc-canva end-to-end tests make perfect sense for Lambda. We will have a fix soon.

  4. tianchu commented on Oct 2, 2019

    @tianchu
    Collaborator

    @jackyjjc-canva after a quick look, I believe the issue was introduced in the latest version (v6). While we are fixing the issue, you can probably work around this issue by using version 5 (or 0.5.0 for pip).

  5. tianchu commented on Oct 3, 2019

    @tianchu
    Collaborator

    #18 should fix the specific issue caused the exception. Do you mind trying it out and let us know if it works for you?

    You can install it using pip install git+https://github.com/DataDog/datadog-lambda-layer-python@tian.chu/check-for-lambda-context

    I agree, in the long run, there should be a toggle to short-circuit (turn off) all datadog operations for testing purpose. I will queue a ticket in our internal backlog and leave this issue open for tracking.

  6. reopened this on Oct 3, 2019
  7. jackyjjc-canva commented on Oct 4, 2019

    @jackyjjc-canva
    Author

    Thank you @tianchu I will take a look.

  8. jackyjjc-canva commented on Oct 8, 2019

    @jackyjjc-canva
    Author

    Sorry for the delay but I can confirm it has resolved the issue. Thank you! @tianchu

  9. tedyu commented on Nov 8, 2023

    @tedyu

    @tianchu
    Can you tell me how to stub out datadog handler for unit testing (with node.js) ?
    Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions