-
Notifications
You must be signed in to change notification settings - Fork 103
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
Add attrs for aws lambda entity #1227
Open
hmstepanek
wants to merge
22
commits into
main
Choose a base branch
from
support-lambda-entities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
61c9ebb
Add attrs for aws lambda entity
hmstepanek deb74bf
Merge branch 'main' into support-lambda-entities
mergify[bot] b99c886
Merge branch 'main' into support-lambda-entities
mergify[bot] 14e3e5e
Merge branch 'main' into support-lambda-entities
mergify[bot] a855845
Merge branch 'main' into support-lambda-entities
mergify[bot] 088b8fb
Merge branch 'main' into support-lambda-entities
mergify[bot] 91a2b75
Merge branch 'main' into support-lambda-entities
mergify[bot] 84d977a
Store arn on events obj instead of transaction
hmstepanek 616eed9
Merge branch 'support-lambda-entities' of github.com:newrelic/newreli…
hmstepanek 456660a
Merge branch 'main' into support-lambda-entities
mergify[bot] 678a35c
Reformat iam create role
hmstepanek d22eeb6
Merge branch 'main' into support-lambda-entities
mergify[bot] c2f4f20
Merge branch 'main' into support-lambda-entities
mergify[bot] 50aebae
Merge branch 'main' into support-lambda-entities
mergify[bot] 1b8425c
Merge branch 'main' into support-lambda-entities
mergify[bot] 4e363ea
Merge branch 'main' into support-lambda-entities
mergify[bot] 5c97248
Add attrs for aws lambda entity
hmstepanek 71ce408
Store arn on events obj instead of transaction
hmstepanek 86c8362
Reformat iam create role
hmstepanek d6347c9
Attach arn to api_params & request_dict
hmstepanek c1fde51
Merge branch 'support-lambda-entities' of github.com:newrelic/newreli…
hmstepanek ec94f91
Merge branch 'main' into support-lambda-entities
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import io | ||
import json | ||
import zipfile | ||
|
||
import boto3 | ||
import pytest | ||
from moto import mock_aws | ||
from testing_support.fixtures import dt_enabled | ||
from testing_support.validators.validate_span_events import validate_span_events | ||
from testing_support.validators.validate_transaction_metrics import ( | ||
validate_transaction_metrics, | ||
) | ||
|
||
from newrelic.api.background_task import background_task | ||
from newrelic.common.package_version_utils import get_package_version_tuple | ||
|
||
MOTO_VERSION = get_package_version_tuple("moto") | ||
BOTOCORE_VERSION = get_package_version_tuple("botocore") | ||
|
||
AWS_ACCESS_KEY_ID = "AAAAAAAAAAAACCESSKEY" | ||
AWS_SECRET_ACCESS_KEY = "AAAAAASECRETKEY" # nosec | ||
AWS_REGION_NAME = "us-west-2" | ||
|
||
LAMBDA_URL = "lambda.us-west-2.amazonaws.com" | ||
EXPECTED_LAMBDA_URL = f"https://{LAMBDA_URL}/2015-03-31/functions" | ||
LAMBDA_ARN = f"arn:aws:lambda:{AWS_REGION_NAME}:383735328703:function:lambdaFunction" | ||
|
||
|
||
_lambda_scoped_metrics = [ | ||
(f"External/{LAMBDA_URL}/botocore/POST", 2), | ||
] | ||
|
||
_lambda_rollup_metrics = [ | ||
("External/all", 3), | ||
("External/allOther", 3), | ||
(f"External/{LAMBDA_URL}/all", 2), | ||
(f"External/{LAMBDA_URL}/botocore/POST", 2), | ||
] | ||
|
||
|
||
@dt_enabled | ||
@validate_span_events(exact_agents={"aws.operation": "CreateFunction"}, count=1) | ||
@validate_span_events( | ||
exact_agents={"aws.operation": "Invoke", "cloud.platform": "aws_lambda", "cloud.resource_id": LAMBDA_ARN}, count=1 | ||
) | ||
@validate_span_events(exact_agents={"aws.operation": "Invoke"}, count=1) | ||
@validate_span_events(exact_agents={"http.url": EXPECTED_LAMBDA_URL}, count=1) | ||
@validate_transaction_metrics( | ||
"test_boto3_lambda:test_lambda", | ||
scoped_metrics=_lambda_scoped_metrics, | ||
rollup_metrics=_lambda_rollup_metrics, | ||
background_task=True, | ||
) | ||
@background_task() | ||
@mock_aws | ||
def test_lambda(iam_role_arn, lambda_zip): | ||
role_arn = iam_role_arn() | ||
|
||
client = boto3.client( | ||
"lambda", | ||
aws_access_key_id=AWS_ACCESS_KEY_ID, | ||
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | ||
region_name=AWS_REGION_NAME, | ||
) | ||
|
||
# Create lambda | ||
resp = client.create_function( | ||
FunctionName="lambdaFunction", Runtime="python3.9", Role=role_arn, Code={"ZipFile": lambda_zip} | ||
) | ||
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 201 | ||
|
||
# Invoke lambda | ||
client.invoke(FunctionName=LAMBDA_ARN, InvocationType="RequestResponse", Payload=json.dumps({})) | ||
assert resp["ResponseMetadata"]["HTTPStatusCode"] == 201 | ||
|
||
|
||
@pytest.fixture | ||
def lambda_zip(): | ||
code = """ | ||
def lambda_handler(event, context): | ||
return event | ||
""" | ||
zip_output = io.BytesIO() | ||
zip_file = zipfile.ZipFile(zip_output, "w", zipfile.ZIP_DEFLATED) | ||
zip_file.writestr("lambda_function.py", code) | ||
zip_file.close() | ||
zip_output.seek(0) | ||
return zip_output.read() | ||
|
||
|
||
@pytest.fixture | ||
def iam_role_arn(): | ||
def create_role(): | ||
iam = boto3.client( | ||
"iam", | ||
aws_access_key_id=AWS_ACCESS_KEY_ID, | ||
aws_secret_access_key=AWS_SECRET_ACCESS_KEY, | ||
region_name=AWS_REGION_NAME, | ||
) | ||
# Create IAM role | ||
role = iam.create_role( | ||
RoleName="my-role", | ||
AssumeRolePolicyDocument="some policy", | ||
Path="/my-path/", | ||
) | ||
return role["Role"]["Arn"] | ||
|
||
return create_role |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the docker dependency for? Lambda/moto?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it's imported inside the lambda invoke in moto.