├── .bumpversion.cfg ├── .circleci └── config.yml ├── .github └── CODEOWNERS ├── .gitignore ├── .idea └── .gitignore ├── .pre-commit-config.yaml ├── .python-version ├── LICENSE ├── README.md ├── pytest.ini ├── requirements.txt ├── scripts ├── bd_to_prod.sh ├── checks.sh └── ci_deploy.sh ├── setup.py └── src ├── lumigo_log_shipper ├── VERSION ├── __init__.py ├── lumigo_shipper.py ├── models.py └── utils │ ├── __init__.py │ ├── aws_utils.py │ ├── consts.py │ ├── encoder.py │ ├── firehose_dal.py │ ├── log.py │ ├── shipper_utils.py │ ├── sts.py │ └── utils.py └── test ├── conftest.py ├── fixtures.py ├── test_lumigo_shipper.py └── utils ├── test_aws_utils.py ├── test_firehose_dal.py ├── test_sts.py └── test_utils.py /.bumpversion.cfg: -------------------------------------------------------------------------------- 1 | [bumpversion] 2 | current_version = 1.0.30 3 | commit = True 4 | tag = True 5 | 6 | [bumpversion:file:setup.py] 7 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | 3 | orbs: 4 | lumigo-orb: &lumigo_orb_version lumigo/lumigo-orb@volatile 5 | 6 | workflows: 7 | test-deploy: 8 | jobs: 9 | - lumigo-orb/print_orb_versions: 10 | lumigo_orb_version: *lumigo_orb_version 11 | 12 | - lumigo-orb/test: 13 | context: common 14 | 15 | - lumigo-orb/is_environment_available: 16 | context: common 17 | filters: 18 | branches: 19 | ignore: master 20 | 21 | - lumigo-orb/be-deploy: 22 | context: common 23 | requires: 24 | - lumigo-orb/is_environment_available 25 | 26 | - lumigo-orb/integration-test-prep: 27 | context: common 28 | run_test_cleanup: false 29 | requires: 30 | - lumigo-orb/be-deploy 31 | 32 | - lumigo-orb/integration-test-cleanup: 33 | name: pre-test-cleanup 34 | context: common 35 | requires: 36 | - lumigo-orb/integration-test-prep 37 | 38 | - lumigo-orb/integration-test-limited-flows: 39 | context: common 40 | run_test_cleanup: false 41 | requires: 42 | - pre-test-cleanup 43 | 44 | - lumigo-orb/integration-test-parallel: 45 | context: common 46 | run_test_cleanup: false 47 | requires: 48 | - lumigo-orb/integration-test-limited-flows 49 | 50 | - lumigo-orb/integration-test-cleanup: 51 | name: post-test-cleanup 52 | context: common 53 | requires: 54 | - lumigo-orb/integration-test-parallel 55 | 56 | - lumigo-orb/e2e-test: 57 | context: common 58 | requires: 59 | - lumigo-orb/integration-test-limited-flows 60 | 61 | - lumigo-orb/workflow-completed-successfully: 62 | context: common 63 | requires: 64 | - lumigo-orb/test 65 | - lumigo-orb/integration-test-parallel 66 | - lumigo-orb/e2e-test 67 | 68 | - lumigo-orb/deploy: 69 | context: 70 | - common 71 | - fury 72 | - twine 73 | filters: 74 | branches: 75 | only: master 76 | requires: 77 | - lumigo-orb/test 78 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @lumigo-io/angels -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # SageMath parsed files 79 | *.sage.py 80 | 81 | # Environments 82 | .env 83 | .venv 84 | env/ 85 | venv/ 86 | ENV/ 87 | env.bak/ 88 | venv.bak/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://github.com/ambv/black 3 | rev: 19.10b0 4 | hooks: 5 | - id: black 6 | language_version: python3.9 7 | additional_dependencies: ['click==8.0.4'] 8 | 9 | - repo: https://github.com/pre-commit/mirrors-mypy 10 | rev: v0.971 11 | hooks: 12 | - id: mypy 13 | 14 | - repo: https://github.com/pre-commit/pre-commit-hooks 15 | rev: v2.4.0 16 | hooks: 17 | - id: flake8 18 | args: ["--ignore","E501"] 19 | 20 | -------------------------------------------------------------------------------- /.python-version: -------------------------------------------------------------------------------- 1 | 3.9.9 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![CircleCI](https://circleci.com/gh/lumigo-io/lumigo-python-log-shipper/tree/master.svg?style=svg&circle-token=82bcda94717aed3dc5068e1643922ffc0ad039c6) 2 | [![codecov](https://codecov.io/gh/lumigo-io/lumigo-python-log-shipper/branch/master/graph/badge.svg?token=jlGd29sam6)](https://codecov.io/gh/lumigo-io/lumigo-python-log-shipper) 3 | ![Version](https://badge.fury.io/py/lumigo-log-shipper.svg) 4 | 5 | # lumigo-python-log-shipper 6 | 7 | Lumigo Log Shipper API lets you stream your Lambda functions' logs to Lumigo as a part of your custom log shipping function. 8 | 9 | NOTE: Lumigo will automatically try to subscribe your Lambda functions to a Kinesis data stream. In case your log group is already subscribed to a Lambda as a destination, use this library to send logs to Lumigo. 10 | 11 | Please contact Lumigo's support through the platform chat so we can enable this feature for you. 12 | 13 | ## Usage 14 | 15 | Install `lumigo-log-shipper`: 16 | 17 | pip: 18 | 19 | ```bash 20 | > pip install lumigo-log-shipper 21 | ``` 22 | 23 | In your log shipping Lambda's code: 24 | 25 | ```python 26 | from lumigo_log_shipper import lumigo_shipper 27 | 28 | def handler(event, context): 29 | lumigo_shipper.ship_logs(event) 30 | ``` 31 | 32 | If you are using programmatic errors, add your custom error keyword as an additional parameter. 33 | This will also send logs which contains your custom expression for Lumigo to process. 34 | 35 | ```python 36 | from lumigo_log_shipper import lumigo_shipper 37 | 38 | def handler(event, context): 39 | lumigo_shipper.ship_logs(event, ["WARNING-EXAMPLE"]) 40 | ``` 41 | 42 | Add to your lambda's `serverless.yml` 43 | 44 | ```bash 45 | iamRoleStatements: 46 | - Effect: Allow 47 | Action: 48 | - "firehose:PutRecordBatch" 49 | Resource: 50 | - "arn:aws:firehose:[YOUR-REGION]:114300393969:deliverystream/prod_logs-edge-stfl_customer-logs-firehose" 51 | - Effect: Allow 52 | Action: 53 | - "sts:AssumeRole" 54 | Resource: 55 | - "arn:aws:iam::114300393969:role/prod-CustomerLogsWriteRole" 56 | ``` 57 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | testpaths = 3 | src/test 4 | filterwarnings = 5 | ignore::DeprecationWarning:boto 6 | ignore::DeprecationWarning:responses 7 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | aioboto3==6.4.1 2 | aiobotocore==0.10.3 3 | black==19.10b0 4 | boto3==1.9.189 5 | botocore==1.12.189 6 | dacite==1.5.1 7 | cachetools 8 | moto==1.3.10 9 | py==1.11.0 10 | pytest==5.2.1 11 | pytest-cov==2.6.1 12 | -------------------------------------------------------------------------------- /scripts/bd_to_prod.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -Eeo pipefail 3 | 4 | setup_git() { 5 | git config --global user.email "no-reply@build.com" 6 | git config --global user.name "CircleCI" 7 | git checkout master 8 | # Avoid version failure 9 | git stash 10 | } 11 | 12 | push_tags() { 13 | git push origin master --tags 14 | } 15 | 16 | echo ".____ .__ .__ "; 17 | echo "| | __ __ _____ |__| ____ ____ |__| ____ "; 18 | echo "| | | | \/ \| |/ ___\ / _ \ | |/ _ \ "; 19 | echo "| |___| | / Y Y \ / /_/ > <_> ) | ( <_> )"; 20 | echo "|_______ \____/|__|_| /__\___ / \____/ /\ |__|\____/ "; 21 | echo " \/ \/ /_____/ \/ "; 22 | echo 23 | echo "Deploy lumigo-log-shipper to pypi server" 24 | 25 | setup_git 26 | 27 | pip install wheel 28 | 29 | echo "Create package" 30 | python setup.py bdist_wheel 31 | 32 | echo "Getting latest changes from git" 33 | latest_tag="$(git describe --tags --abbrev=0)" 34 | changes=$(git log "${latest_tag}..HEAD" --oneline) 35 | 36 | bumpversion patch --message "{current_version} → {new_version}. Changes: ${changes}" 37 | 38 | echo "Uploading to PyPi" 39 | pip install twine 40 | twine upload dist/* 41 | 42 | push_tags 43 | echo "Done" -------------------------------------------------------------------------------- /scripts/checks.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eo pipefail 3 | 4 | python setup.py develop 5 | pre-commit run -a 6 | python -m pytest src/test --cov=./src 7 | -------------------------------------------------------------------------------- /scripts/ci_deploy.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eo pipefail 3 | 4 | pushd "$(dirname "$0")" &> /dev/null 5 | # Go back one spot because we are on scripts dir. The other scripts assume you are in the root folder 6 | cd .. 7 | ../utils/common_bash/defaults/ci_deploy.sh lumigo-python-log-shipper 8 | popd &> /dev/null 9 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | setuptools.setup( 4 | name="lumigo-log-shipper", 5 | version="1.0.30", 6 | author="Lumigo LTD (https://lumigo.io)", 7 | author_email="support@lumigo.io", 8 | description="Ship logs to Lumigo platform", 9 | long_description=open("README.md").read(), 10 | long_description_content_type="text/markdown", 11 | url="https://github.com/lumigo-io/lumigo-python-log-shipper.git", 12 | package_dir={"": "src"}, 13 | packages=setuptools.find_packages("src"), 14 | install_requires=["dacite==1.5.1"], 15 | classifiers=[ 16 | "Programming Language :: Python :: 3", 17 | "Operating System :: OS Independent", 18 | ], 19 | ) 20 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/VERSION: -------------------------------------------------------------------------------- 1 | 1.0.1 2 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumigo-io/lumigo-python-log-shipper/8c0b347fcb76b02a7e39705ba68ab965e6c44024/src/lumigo_log_shipper/__init__.py -------------------------------------------------------------------------------- /src/lumigo_log_shipper/lumigo_shipper.py: -------------------------------------------------------------------------------- 1 | from dataclasses import asdict 2 | from typing import List, Optional 3 | 4 | from lumigo_log_shipper.models import AwsLogSubscriptionEvent 5 | from lumigo_log_shipper.utils.consts import ( 6 | STREAM_NAME, 7 | LOG_STREAM_KIIL_SWITCH, 8 | FILTER_KEYWORDS, 9 | ) 10 | from lumigo_log_shipper.utils.firehose_dal import FirehoseDal 11 | from lumigo_log_shipper.utils.aws_utils import extract_aws_logs_data 12 | from lumigo_log_shipper.utils.log import get_logger 13 | from lumigo_log_shipper.utils.shipper_utils import filter_logs 14 | from lumigo_log_shipper.utils.sts import ChinaMissingEnvVar 15 | 16 | 17 | def ship_logs( 18 | aws_event: dict, 19 | programmatic_error_keyword: Optional[str] = None, 20 | exclude_filters: Optional[List[str]] = None, 21 | ) -> int: 22 | try: 23 | shipper_output: AwsLogSubscriptionEvent = extract_aws_logs_data(aws_event) 24 | res = _ship_logs_to_lumigo( 25 | shipper_outputs=[shipper_output], 26 | programmatic_error_keyword=programmatic_error_keyword, 27 | exclude_filters=exclude_filters, 28 | ) 29 | get_logger().info(f"Successfully sent {res} logs") 30 | return res 31 | except ChinaMissingEnvVar: 32 | pass 33 | except Exception as e: 34 | # lumigo_shipper will print out the exception but won't raises it 35 | get_logger().critical("Failed to send customer logs", exc_info=e) 36 | return 0 37 | 38 | 39 | def _ship_logs_to_lumigo( 40 | shipper_outputs: List[AwsLogSubscriptionEvent], 41 | programmatic_error_keyword: Optional[str] = None, 42 | exclude_filters: Optional[List[str]] = None, 43 | ) -> int: 44 | get_logger().debug(f"Number of logs before filter {len(shipper_outputs)}") 45 | filter_keywords = FILTER_KEYWORDS.copy() 46 | if programmatic_error_keyword: 47 | filter_keywords.append(programmatic_error_keyword) 48 | shipper_outputs = filter_logs(shipper_outputs, filter_keywords, exclude_filters) 49 | get_logger().debug(f"Number of logs after filter {len(shipper_outputs)}") 50 | if len(shipper_outputs) > 0 and not LOG_STREAM_KIIL_SWITCH: 51 | account_id = shipper_outputs[0].owner 52 | firehose_records = list(map(asdict, shipper_outputs)) 53 | firehose_dal = FirehoseDal(stream_name=STREAM_NAME, account_id=account_id) 54 | return firehose_dal.put_record_batch(firehose_records) 55 | return 0 56 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/models.py: -------------------------------------------------------------------------------- 1 | from typing import List, Optional 2 | from dataclasses import dataclass 3 | 4 | 5 | @dataclass(frozen=True) 6 | class AwsLogEvent: 7 | id: str 8 | timestamp: int 9 | message: str 10 | 11 | 12 | @dataclass(frozen=True) 13 | class AwsLogSubscriptionEvent: 14 | messageType: str 15 | owner: str 16 | logGroup: str 17 | logStream: str 18 | subscriptionFilters: List[str] 19 | logEvents: List[AwsLogEvent] 20 | region: Optional[str] 21 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lumigo-io/lumigo-python-log-shipper/8c0b347fcb76b02a7e39705ba68ab965e6c44024/src/lumigo_log_shipper/utils/__init__.py -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/aws_utils.py: -------------------------------------------------------------------------------- 1 | import gzip 2 | import json 3 | import base64 4 | import os 5 | from typing import Optional 6 | 7 | import dacite 8 | 9 | from lumigo_log_shipper.models import AwsLogSubscriptionEvent 10 | from lumigo_log_shipper.utils.consts import MASTER_REGION 11 | 12 | 13 | def extract_aws_logs_data(event: dict) -> AwsLogSubscriptionEvent: 14 | logs_data_decoded = base64.b64decode(event["awslogs"]["data"]) 15 | logs_data_unzipped = gzip.decompress(logs_data_decoded) 16 | logs_data_dict = json.loads(logs_data_unzipped) 17 | logs_data_dict["region"] = get_current_region() 18 | return dacite.from_dict(AwsLogSubscriptionEvent, logs_data_dict) 19 | 20 | 21 | def is_china_region() -> bool: 22 | return get_current_region() == "cn-northwest-1" 23 | 24 | 25 | def get_current_region() -> Optional[str]: 26 | return os.environ.get("AWS_REGION") 27 | 28 | 29 | def get_dest_region() -> str: 30 | if is_china_region(): 31 | return MASTER_REGION 32 | return get_current_region() or MASTER_REGION 33 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/consts.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | IS_LAMBDA_ENVIRONMENT = bool(os.environ.get("AWS_EXECUTION_ENV")) 4 | 5 | MASTER_REGION = "us-west-2" 6 | 7 | TARGET_ENV = os.environ.get("TARGET_ENV") 8 | if TARGET_ENV == "" or TARGET_ENV is None: 9 | TARGET_ENV = "prod" 10 | 11 | if TARGET_ENV == "SELF": 12 | TARGET_ENV = os.environ["ENV"] if IS_LAMBDA_ENVIRONMENT else os.environ["USER"] 13 | 14 | STREAM_NAME = f"{TARGET_ENV}_logs-edge-stfl_customer-logs-firehose" 15 | 16 | TARGET_ACCOUNT_ID = os.environ.get("TARGET_ACCOUNT_ID", "114300393969") 17 | LOG_STREAM_KIIL_SWITCH = os.environ.get("LOG_STREAM_KIIL_SWITCH", None) == "TRUE" 18 | 19 | SELF_ACCOUNT_ID = "SELF" 20 | 21 | FILTER_KEYWORDS = [ 22 | "Task timed out", 23 | "Process exited before completing request", 24 | "REPORT RequestId", 25 | "[ERROR]", 26 | "[LUMIGO_LOG]", 27 | "@lumigo", 28 | "LambdaRuntimeClientError", 29 | "Invoke Error", 30 | "Uncaught Exception", 31 | "Unhandled Promise Rejection", 32 | "Traceback", 33 | ] 34 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/encoder.py: -------------------------------------------------------------------------------- 1 | import json 2 | from decimal import Decimal 3 | 4 | 5 | class DecimalEncoder(json.JSONEncoder): 6 | def default(self, o): 7 | if isinstance(o, Decimal): 8 | return float(o) 9 | return super(DecimalEncoder, self).default(o) 10 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/firehose_dal.py: -------------------------------------------------------------------------------- 1 | import json 2 | from collections import defaultdict 3 | from dataclasses import dataclass 4 | from typing import List, Dict, Any 5 | import boto3 6 | 7 | from lumigo_log_shipper.utils.aws_utils import get_dest_region, is_china_region 8 | from lumigo_log_shipper.utils.consts import ( 9 | TARGET_ACCOUNT_ID, 10 | SELF_ACCOUNT_ID, 11 | TARGET_ENV, 12 | ) 13 | from lumigo_log_shipper.utils.encoder import DecimalEncoder 14 | from lumigo_log_shipper.utils.log import get_logger 15 | from lumigo_log_shipper.utils.sts import assume_role 16 | from lumigo_log_shipper.utils.utils import split_to_chunks 17 | 18 | MAX_RETRY_COUNT = 2 19 | MAX_MESSAGES_TO_FIREHOSE = 500 # Firehose supports batch up to 500 messages. 20 | MAX_FIREHOSE_RECORD_SIZE = 1024000 21 | EOL = "\n" # Firehose end of line mark 22 | ENCODING = "utf-8" 23 | 24 | 25 | @dataclass(frozen=True) 26 | class Batch: 27 | records: List[Any] 28 | retry_count: int = 1 29 | 30 | 31 | class FirehoseDal: 32 | def __init__( 33 | self, 34 | stream_name: str, 35 | account_id: str, 36 | max_retry_count: int = MAX_RETRY_COUNT, 37 | batch_size: int = MAX_MESSAGES_TO_FIREHOSE, 38 | ): 39 | """ 40 | :param stream_name: Name of the firehose delivery stream. 41 | """ 42 | self.current_account_id = account_id 43 | self._client = FirehoseDal.get_boto_client(account_id, TARGET_ACCOUNT_ID) 44 | self._stream_name = stream_name 45 | self.max_retry_count = max_retry_count 46 | self.failed_by_error_code: Dict[str, int] = defaultdict(int) 47 | self.batch_size = min(batch_size, MAX_MESSAGES_TO_FIREHOSE) 48 | get_logger().debug( 49 | f"Init firehose stream: {stream_name} target account: {TARGET_ACCOUNT_ID}" 50 | ) 51 | 52 | def put_record_batch(self, records: List[dict]) -> int: 53 | """ 54 | :param max_batch_size: max batch size 55 | :param records: The records to put 56 | :return: number of records inserted 57 | """ 58 | number_of_records = 0 59 | firehose_records = self._convert_to_firehose_record(records) 60 | chunks = split_to_chunks(firehose_records, self.batch_size) 61 | batches: List[Batch] = self.create_batches_from_chunks(chunks) 62 | while batches: 63 | current_batch = batches.pop(0) 64 | should_retry = current_batch.retry_count < self.max_retry_count 65 | try: 66 | response = self._client.put_record_batch( 67 | DeliveryStreamName=self._stream_name, Records=current_batch.records 68 | )["RequestResponses"] 69 | failed_items = self.get_failed_items(current_batch, response) 70 | self.update_failed_by_error_code(response) 71 | success_items_len = len(current_batch.records) - len(failed_items) 72 | number_of_records += success_items_len 73 | if any(failed_items) and should_retry: 74 | batches.append(self.create_next_batch(current_batch, failed_items)) 75 | except Exception as e: 76 | get_logger().debug( 77 | "Error while trying to send data to firehose", exc_info=e, 78 | ) 79 | self.failed_by_error_code[str(type(e).__name__)] += 1 80 | if should_retry: 81 | next_records = current_batch.records 82 | batches.append(self.create_next_batch(current_batch, next_records)) 83 | return number_of_records 84 | 85 | def get_failed_items( 86 | self, current_batch: Batch, kinesis_response: List[dict] 87 | ) -> list: 88 | failed_items = [] 89 | for index, response in enumerate(kinesis_response): 90 | if response.get("RecordId") is None: 91 | failed_items.append(current_batch.records[index]) 92 | return failed_items 93 | 94 | def update_failed_by_error_code(self, kinesis_response: List[dict]) -> None: 95 | for response in kinesis_response: 96 | if response.get("RecordId") is None: 97 | error_code = response.get("ErrorCode") 98 | self.failed_by_error_code[str(error_code)] += 1 99 | 100 | @staticmethod 101 | def create_next_batch(current_batch: Batch, next_records: list) -> Batch: 102 | return Batch(records=next_records, retry_count=current_batch.retry_count + 1) 103 | 104 | @staticmethod 105 | def create_batches_from_chunks(chunks: List[list]) -> List[Batch]: 106 | return list(map(lambda b: Batch(records=b), chunks)) 107 | 108 | @staticmethod 109 | def get_boto_client(account_id: str, target_account_id: str): 110 | region = get_dest_region() 111 | if ( 112 | account_id != target_account_id and target_account_id != SELF_ACCOUNT_ID 113 | ) or is_china_region(): 114 | sts_response = assume_role(target_account_id, TARGET_ENV) 115 | return boto3.client( 116 | "firehose", 117 | region_name=region, 118 | aws_access_key_id=sts_response["Credentials"]["AccessKeyId"], 119 | aws_secret_access_key=sts_response["Credentials"]["SecretAccessKey"], 120 | aws_session_token=sts_response["Credentials"]["SessionToken"], 121 | ) 122 | return boto3.client("firehose") 123 | 124 | @staticmethod 125 | def _convert_to_firehose_record(records: List[dict]) -> List[dict]: 126 | fh_records: List[dict] = [] 127 | for record in records: 128 | try: 129 | fh_record = json.dumps(record, cls=DecimalEncoder) 130 | if fh_record is not None: 131 | fh_record += EOL 132 | if len(fh_record) < MAX_FIREHOSE_RECORD_SIZE: 133 | fh_records.append({"Data": fh_record}) 134 | except Exception: 135 | # TODO: log error 136 | print("Failed to convert record to fh record") 137 | return fh_records 138 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/log.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | from typing import Union 4 | 5 | LOG_FORMAT = "#LUMIGO# - %(asctime)s - %(levelname)s - %(message)s" 6 | _logger: Union[logging.Logger, None] = None 7 | 8 | 9 | def get_logger(): 10 | """ 11 | This function returns lumigo's logger. 12 | The logger streams the logs to the stderr in format the explicitly say that those are lumigo's logs. 13 | 14 | This logger is off by default. 15 | Add the environment variable `LUMIGO_DEBUG=true` to activate it. 16 | """ 17 | global _logger 18 | if not _logger: 19 | _logger = logging.getLogger("lumigo") 20 | handler = logging.StreamHandler() 21 | handler.setFormatter(logging.Formatter(LOG_FORMAT)) 22 | if os.environ.get("LUMIGO_DEBUG", "").lower() == "true": 23 | _logger.setLevel(logging.DEBUG) 24 | else: 25 | _logger.setLevel(logging.CRITICAL) 26 | _logger.addHandler(handler) 27 | return _logger 28 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/shipper_utils.py: -------------------------------------------------------------------------------- 1 | from typing import List, Optional 2 | 3 | from lumigo_log_shipper.models import AwsLogSubscriptionEvent 4 | 5 | 6 | def _is_valid_log( 7 | log_message: str, 8 | filter_keywords: List[str], 9 | exclude_filters: Optional[List[str]] = None, 10 | ): 11 | log_message = str(log_message) 12 | log_message_lower = log_message.lower() 13 | if exclude_filters: 14 | for exclude_filter in exclude_filters: 15 | if exclude_filter.lower() in log_message_lower: 16 | return False 17 | for keyword in filter_keywords: 18 | if keyword.lower() in log_message_lower: 19 | return True 20 | return False 21 | 22 | 23 | def filter_logs( 24 | logs: List[AwsLogSubscriptionEvent], 25 | filter_keywords: List[str], 26 | exclude_filters: Optional[List[str]] = None, 27 | ) -> List[AwsLogSubscriptionEvent]: 28 | res_list: List[AwsLogSubscriptionEvent] = [] 29 | for log in logs: 30 | filtered_events = list( 31 | filter( 32 | lambda event: _is_valid_log( 33 | event.message, filter_keywords, exclude_filters 34 | ), 35 | log.logEvents, 36 | ) 37 | ) 38 | if len(filtered_events) > 0: 39 | res_list.append( 40 | AwsLogSubscriptionEvent( 41 | messageType=log.messageType, 42 | owner=log.owner, 43 | logGroup=log.logGroup, 44 | logStream=log.logStream, 45 | subscriptionFilters=log.subscriptionFilters, 46 | logEvents=filtered_events, 47 | region=log.region, 48 | ) 49 | ) 50 | 51 | return res_list 52 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/sts.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | import os 3 | 4 | from lumigo_log_shipper.utils.aws_utils import is_china_region 5 | from lumigo_log_shipper.utils.log import get_logger 6 | 7 | 8 | def _get_china_env_var(env_var_name: str) -> str: 9 | value = os.environ.get(env_var_name) 10 | if value: 11 | return value 12 | get_logger().critical( 13 | f"Failed to send customer logs because {env_var_name} env var is missing and it is needed in China" 14 | ) 15 | raise ChinaMissingEnvVar() 16 | 17 | 18 | def _get_boto_sts_client(): 19 | if not is_china_region(): 20 | return boto3.client("sts") 21 | aws_access_key_id = _get_china_env_var("LUMIGO_LOGS_EDGE_AWS_ACCESS_KEY_ID") 22 | aws_secret_access_key = _get_china_env_var("LUMIGO_LOGS_EDGE_AWS_SECRET_ACCESS_KEY") 23 | return boto3.client( 24 | "sts", 25 | region_name=os.environ.get("LUMIGO_LOGS_EDGE_REGION", "us-west-2"), 26 | aws_access_key_id=aws_access_key_id, 27 | aws_secret_access_key=aws_secret_access_key, 28 | ) 29 | 30 | 31 | def assume_role(target_account_id, target_env): 32 | client = _get_boto_sts_client() 33 | sts_response = client.assume_role( 34 | RoleArn=f"arn:aws:iam::{target_account_id}:role/{target_env}-CustomerLogsWriteRole", 35 | RoleSessionName="AssumeCrossAccountRole", 36 | DurationSeconds=900, 37 | ) 38 | return sts_response 39 | 40 | 41 | class ChinaMissingEnvVar(Exception): 42 | pass 43 | -------------------------------------------------------------------------------- /src/lumigo_log_shipper/utils/utils.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | def split_to_chunks(list_to_split: list, chunk_size: int) -> List[list]: 5 | def chunk(l, n): # noqa E741 6 | for i in range(0, len(l), n): 7 | yield l[i : i + n] # noqa 8 | 9 | chunk_size = max(chunk_size, 1) 10 | result = list(chunk(list_to_split, chunk_size)) 11 | return result 12 | -------------------------------------------------------------------------------- /src/test/conftest.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from pytest import fixture 4 | 5 | from lumigo_log_shipper.utils.firehose_dal import FirehoseDal 6 | 7 | 8 | class MockFirehoseBotoClient: 9 | def put_record_batch(self, DeliveryStreamName: str, Records: List[dict]): 10 | return { 11 | "FailedPutCount": 0, 12 | "RequestResponses": [{"RecordId": "1"} for _ in Records], 13 | } 14 | 15 | 16 | @fixture(autouse=True) 17 | def firehose_dal_mock(monkeypatch, request): 18 | if not request.node.get_closest_marker("skip_firehose_dal_mock"): 19 | monkeypatch.setattr( 20 | FirehoseDal, "get_boto_client", lambda x, y: MockFirehoseBotoClient() 21 | ) 22 | yield 23 | 24 | 25 | @fixture(autouse=True) 26 | def lambda_env(monkeypatch): 27 | monkeypatch.setenv("AWS_REGION", "us-west-2") 28 | -------------------------------------------------------------------------------- /src/test/fixtures.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | 4 | @pytest.fixture() 5 | def simple_aws_event(): 6 | return { 7 | "awslogs": { 8 | "data": "H4sIAAAAAAAAAKVSy27bMBD8FYHo0YrIpcSHbw6sBgHitrBUoEViBJS0cgXo4Uh00yTwv3flpGiPLXqdGc4Mh3xhHU6T22P+dEC2ZOtVvrrfpFm2ukrZgg2PPY4ES5loACkUmITgdthfjcPxQEzkHqeodV1Rucjj5MNv3h/CER9eZZkf0XWkAy5sxG0EMrp9d7PK0yzf8VigjiUvVWXjhKsCilIXYIuKgzBcksV0LKZybA6+Gfr3TetxnNjylt2cA1/N70fXV0PHdufA9Dv2fta8sKaam8fWJsJITeWFVGBBgUiE0IRKKxOQXCdKax0La2TMuYyN0cpQtm9oG+86uqZIlAVppBCC88Wvzcg+/bAOtvhwJOF1tQyshhokqhBrV4SxsTo03PFQQGFq48AIy+96dlr8Xzv7l+226aeP2/yfC/r1cXTz4suADC84D7rpzl82bYtV8JuDN2KD3TA+BVnzjHQATLC5JND9CN6IzxNSsk7O+HXf+D88qPoFN2ebu/7LdvU1yEdX4lxVhElljKtAha7WKMraKFqp5LQAbYJYUdMM9x09+KwHdFbGJRdS6pi+FpG0Tjtn166dcE5gp93pJy17fMXzAgAA" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/test/test_lumigo_shipper.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import gzip 3 | import json 4 | from dataclasses import asdict 5 | import pytest 6 | 7 | from lumigo_log_shipper.lumigo_shipper import ship_logs 8 | from lumigo_log_shipper.models import AwsLogSubscriptionEvent, AwsLogEvent 9 | from src.test.fixtures import * # noqa 10 | 11 | 12 | def test_lumigo_shipper_full_flow(simple_aws_event): 13 | records_send = ship_logs(simple_aws_event) 14 | assert records_send == 1 15 | 16 | 17 | def test_lumigo_shipper_full_flow_with_programtic_error_keyword(simple_aws_event): 18 | records_send = ship_logs(simple_aws_event, "END") 19 | assert records_send == 1 20 | 21 | 22 | def test_lumigo_aws_log_shipper_exception_not_throw_flow_wrapper(): 23 | records_send = ship_logs(None) 24 | assert records_send == 0 25 | 26 | 27 | @pytest.mark.skip_firehose_dal_mock 28 | def test_ship_logs_china_missing_env_var(simple_aws_event, monkeypatch, caplog): 29 | monkeypatch.setenv("AWS_REGION", "cn-northwest-1") 30 | 31 | records_sent = ship_logs(simple_aws_event) 32 | 33 | assert records_sent == 0 34 | assert caplog.text.count("CRITICAL") == 1 35 | assert "LUMIGO_LOGS_EDGE_AWS_ACCESS_KEY_ID" in caplog.text 36 | 37 | 38 | def test_filter_logs_filter_filtering_invalid_items(): 39 | raw_log = AwsLogSubscriptionEvent( 40 | messageType="DATA_MESSAGE", 41 | owner="335722316285", 42 | logGroup="/aws/lambda/test-http-req", 43 | logStream="2019/09/23/[$LATEST]041e7430c6d94506b2bc7b29bd021803", 44 | subscriptionFilters=["LambdaStream_random"], 45 | logEvents=[ 46 | AwsLogEvent( 47 | id="34995183731613629262151179513935230756777419834003488769", 48 | timestamp=1569238311100, 49 | message="NEW_FILTER RequestId: 972f23e6-efab-4897-80a0-12b8f8a28190\tDuration: 100.00 ms\tBilled Duration: 200 ms\tMemory Size: 128 MB\tMax Memory Used: 75 MB\tInit Duration: 156.08 ms\t\nXRAY TraceId: 1-5d88ad26-af7e1cf86161c0887567eed0\tSegmentId: 2ea934c013374041\tSampled: false\t\n", 50 | ), 51 | AwsLogEvent( 52 | id="34995183731613629262151179513935230756777419834003488769", 53 | timestamp=1569238311100, 54 | message="NEW_FILTER RequestId: 972f23e6-efab-4897-80a0-12b8f8a28190\tDuration: 100.00 ms\tBilled Duration: 200 ms\tMemory Size: 128 MB\tMax Memory Used: 75 MB\tInit Duration: 156.08 ms\t\nXRAY TraceId: 1-5d88ad26-af7e1cf86161c0887567eed0\tSegmentId: 2ea934c013374041\tSampled: false\t\n", 55 | ), 56 | ], 57 | region="us-west-2", 58 | ) 59 | 60 | records_send = ship_logs( 61 | _awsLogSubscriptionEvent_to_aws_event(raw_log), "NEW_FILTER" 62 | ) 63 | 64 | assert records_send == 1 65 | 66 | 67 | def test_filter_logs_filter_not_filtering_valid_items(): 68 | raw_log = AwsLogSubscriptionEvent( 69 | messageType="DATA_MESSAGE", 70 | owner="335722316285", 71 | logGroup="/aws/lambda/test-http-req", 72 | logStream="2019/09/23/[$LATEST]041e7430c6d94506b2bc7b29bd021803", 73 | subscriptionFilters=["LambdaStream_random"], 74 | logEvents=[ 75 | AwsLogEvent( 76 | id="34995183731613629262151179513935230756777419834003488769", 77 | timestamp=1569238311100, 78 | message="SHOULD BE FILTERED", 79 | ), 80 | AwsLogEvent( 81 | id="34995183731613629262151179513935230756777419834003488769", 82 | timestamp=1569238311100, 83 | message="SHOULD BE FILTERED", 84 | ), 85 | ], 86 | region="us-west-2", 87 | ) 88 | 89 | records_send = ship_logs(_awsLogSubscriptionEvent_to_aws_event(raw_log), "END") 90 | 91 | assert records_send == 0 92 | 93 | 94 | def test_filter_logs_filter_exclude_filter(): 95 | raw_log = AwsLogSubscriptionEvent( 96 | messageType="DATA_MESSAGE", 97 | owner="335722316285", 98 | logGroup="/aws/lambda/test-http-req", 99 | logStream="2019/09/23/[$LATEST]041e7430c6d94506b2bc7b29bd021803", 100 | subscriptionFilters=["LambdaStream_random"], 101 | logEvents=[ 102 | AwsLogEvent( 103 | id="34995183731613629262151179513935230756777419834003488769", 104 | timestamp=1569238311100, 105 | message="INFO", 106 | ), 107 | AwsLogEvent( 108 | id="34995183731613629262151179513935230756777419834003488769", 109 | timestamp=1569238311100, 110 | message="INFO", 111 | ), 112 | ], 113 | region="us-west-2", 114 | ) 115 | 116 | records_send = ship_logs( 117 | _awsLogSubscriptionEvent_to_aws_event(raw_log), "INFO", ["INFO"] 118 | ) 119 | 120 | assert records_send == 0 121 | 122 | 123 | def test_filter_logs_filter_exclude_filter_not_filtered(): 124 | raw_log = AwsLogSubscriptionEvent( 125 | messageType="DATA_MESSAGE", 126 | owner="335722316285", 127 | logGroup="/aws/lambda/test-http-req", 128 | logStream="2019/09/23/[$LATEST]041e7430c6d94506b2bc7b29bd021803", 129 | subscriptionFilters=["LambdaStream_random"], 130 | logEvents=[ 131 | AwsLogEvent( 132 | id="34995183731613629262151179513935230756777419834003488769", 133 | timestamp=1569238311100, 134 | message="INFO", 135 | ), 136 | AwsLogEvent( 137 | id="34995183731613629262151179513935230756777419834003488769", 138 | timestamp=1569238311100, 139 | message="INFO", 140 | ), 141 | ], 142 | region="us-west-2", 143 | ) 144 | 145 | records_send = ship_logs( 146 | _awsLogSubscriptionEvent_to_aws_event(raw_log), "INFO", ["NOT_EXCLUDE"] 147 | ) 148 | 149 | assert records_send == 1 150 | 151 | 152 | def _awsLogSubscriptionEvent_to_aws_event(event: AwsLogSubscriptionEvent) -> dict: 153 | logs_data = json.dumps(asdict(event)) 154 | logs_data_zipped = gzip.compress(bytes(logs_data, "utf-8")) 155 | logs_data_encoded = base64.b64encode(logs_data_zipped) 156 | return {"awslogs": {"data": logs_data_encoded}} 157 | -------------------------------------------------------------------------------- /src/test/utils/test_aws_utils.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from lumigo_log_shipper.models import AwsLogSubscriptionEvent, AwsLogEvent 4 | from lumigo_log_shipper.utils.aws_utils import ( 5 | extract_aws_logs_data, 6 | is_china_region, 7 | get_dest_region, 8 | ) 9 | from src.test.fixtures import * # noqa 10 | 11 | 12 | def test_extract_aws_logs_data_simple_flow(simple_aws_event): 13 | result = extract_aws_logs_data(simple_aws_event) 14 | 15 | assert result == AwsLogSubscriptionEvent( 16 | messageType="DATA_MESSAGE", 17 | owner="335722316285", 18 | logGroup="/aws/lambda/test-http-req", 19 | logStream="2019/09/23/[$LATEST]041e7430c6d94506b2bc7b29bd021803", 20 | subscriptionFilters=["LambdaStream_random"], 21 | logEvents=[ 22 | AwsLogEvent( 23 | id="34995183731613629262151179513935230756777419834003488768", 24 | timestamp=1_569_238_311_100, 25 | message="END RequestId: 972f23e6-efab-4897-80a0-12b8f8a28190\n", 26 | ), 27 | AwsLogEvent( 28 | id="34995183731613629262151179513935230756777419834003488769", 29 | timestamp=1_569_238_311_100, 30 | message="REPORT RequestId: 972f23e6-efab-4897-80a0-12b8f8a28190\tDuration: 100.00 ms\tBilled Duration: 200 ms\tMemory Size: 128 MB\tMax Memory Used: 75 MB\tInit Duration: 156.08 ms\t\nXRAY TraceId: 1-5d88ad26-af7e1cf86161c0887567eed0\tSegmentId: 2ea934c013374041\tSampled: false\t\n", 31 | ), 32 | ], 33 | region="us-west-2", 34 | ) 35 | 36 | 37 | def test_is_china_region_not_china(): 38 | assert is_china_region() is False 39 | 40 | 41 | def test_is_china_region_in_china(monkeypatch): 42 | monkeypatch.setenv("AWS_REGION", "cn-northwest-1") 43 | assert is_china_region() is True 44 | 45 | 46 | @pytest.mark.parametrize( 47 | ["current_region", "expected_region"], 48 | [["us-east-1", "us-east-1"], ["cn-northwest-1", "us-west-2"]], 49 | ) 50 | def test_get_dest_region(monkeypatch, current_region, expected_region): 51 | monkeypatch.setenv("AWS_REGION", current_region) 52 | assert get_dest_region() == expected_region 53 | -------------------------------------------------------------------------------- /src/test/utils/test_firehose_dal.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | from moto import mock_sts 4 | import pytest 5 | 6 | from lumigo_log_shipper.utils.firehose_dal import FirehoseDal, Batch 7 | 8 | RANDOM_RECORD_ID = "RANDOM_RECORD_ID" 9 | RANDOM_ERROR_CODE = "RANDOM_ERROR_CODE" 10 | 11 | 12 | def test_create_batches_from_chunks_simple(): 13 | chunks = [[1], [2]] 14 | result = FirehoseDal.create_batches_from_chunks(chunks) 15 | assert result == [Batch([1], 1), Batch([2], 1)] 16 | 17 | 18 | def test_create_next_batch(): 19 | current_batch = Batch([1, 2], 1) 20 | next_items = [2] 21 | next_batch = FirehoseDal.create_next_batch(current_batch, next_items) 22 | 23 | assert next_batch == Batch([2], 2) 24 | 25 | 26 | def test_get_failed_items_return_correct_items( 27 | kinesis_success_item, kinesis_failed_item 28 | ): 29 | current_batch = Batch(records=[1, 2]) 30 | kinesis_response = [kinesis_success_item, kinesis_failed_item] 31 | firehose_service = FirehoseDal("random-stream-name", "random") 32 | failed_items = firehose_service.get_failed_items(current_batch, kinesis_response) 33 | 34 | assert failed_items == [2] 35 | 36 | 37 | def test_get_failed_items_add_to_failed_by_error_code( 38 | kinesis_success_item, kinesis_failed_item 39 | ): 40 | kinesis_response = [kinesis_success_item, kinesis_failed_item] 41 | firehose_service = FirehoseDal("random-stream-name", "random") 42 | firehose_service.update_failed_by_error_code(kinesis_response) 43 | 44 | assert firehose_service.failed_by_error_code == {RANDOM_ERROR_CODE: 1} 45 | 46 | 47 | def test_put_records_happy_flow(): 48 | firehose = FirehoseDal(stream_name="stream_name", account_id="random") 49 | assert firehose.put_record_batch(records=[{"id": 1}]) == 1 50 | 51 | 52 | def test_put_records_happy_flow_with_non_default_batch_size(): 53 | firehose = FirehoseDal(stream_name="stream_name", batch_size=1, account_id="random") 54 | 55 | assert firehose.put_record_batch(records=[{"id": 1}]) == 1 56 | 57 | 58 | def test_put_records_with_record_too_big(): 59 | firehose = FirehoseDal(stream_name="stream_name", account_id="random") 60 | 61 | assert firehose.put_record_batch(records=[{"id": ("x" * 2048000)}]) == 0 62 | 63 | 64 | def test_put_records_with_record_invalid(): 65 | firehose = FirehoseDal(stream_name="stream_name", account_id="random") 66 | 67 | assert firehose.put_record_batch(records=[Exception()]) == 0 # noqa 68 | 69 | 70 | def test_put_records_error_on_first_try_success_in_second_try(monkeypatch): 71 | monkeypatch.setattr( 72 | FirehoseDal, 73 | "get_boto_client", 74 | lambda x, y: MockFirehoseBotoClientRetryWithError(), 75 | ) 76 | firehose = FirehoseDal(stream_name="stream_name", account_id="random") 77 | 78 | assert firehose.put_record_batch(records=[{"id": 1}]) == 1 79 | 80 | 81 | def test_put_records_exception_on_first_try_success_in_second_try(monkeypatch): 82 | client = MockFirehoseBotoClientException() 83 | monkeypatch.setattr(FirehoseDal, "get_boto_client", lambda x, y: client) 84 | firehose = FirehoseDal(stream_name="stream_name", account_id="random") 85 | 86 | assert firehose.put_record_batch(records=[{"id": 1}]) == 1 87 | 88 | 89 | def test_put_records_dont_retry_to_many_times(monkeypatch): 90 | monkeypatch.setattr( 91 | FirehoseDal, 92 | "get_boto_client", 93 | lambda x, y: MockFirehoseBotoClientRetryWithError(3), 94 | ) 95 | firehose = FirehoseDal( 96 | stream_name="stream_name", max_retry_count=2, account_id="random" 97 | ) 98 | 99 | assert firehose.put_record_batch(records=[{"id": 1}]) == 0 100 | 101 | 102 | @pytest.mark.skip_firehose_dal_mock 103 | @mock_sts 104 | def test_get_boto_client_in_china_should_connect_to_global(monkeypatch): 105 | monkeypatch.setenv("LUMIGO_LOGS_EDGE_AWS_ACCESS_KEY_ID", "key1") 106 | monkeypatch.setenv("LUMIGO_LOGS_EDGE_AWS_SECRET_ACCESS_KEY", "secret1") 107 | monkeypatch.setenv("AWS_REGION", "cn-northwest-1") 108 | 109 | client = FirehoseDal.get_boto_client("111111111111", "unittests") 110 | 111 | assert client.meta.region_name == "us-west-2" 112 | 113 | 114 | class MockFirehoseBotoClientRetryWithException: 115 | retry = 1 116 | 117 | def put_record_batch(self, DeliveryStreamName: str, Records: List[dict]): 118 | if self.retry > 0: 119 | self.retry -= 1 120 | raise Exception() 121 | return { 122 | "FailedPutCount": 0, 123 | "RequestResponses": [{"RecordId": "1"} for _ in Records], 124 | } 125 | 126 | 127 | class MockFirehoseBotoClientRetryWithError: 128 | def __init__(self, retry=1): 129 | self.retry = retry 130 | 131 | def put_record_batch(self, DeliveryStreamName: str, Records: List[dict]): 132 | if self.retry > 0: 133 | self.retry -= 1 134 | return { 135 | "FailedPutCount": 0, 136 | "RequestResponses": [ 137 | { 138 | "ErrorCode": "ServiceUnavailableException", 139 | "ErrorMessage": "ServiceUnavailableException", 140 | } 141 | for _ in Records 142 | ], 143 | } 144 | return { 145 | "FailedPutCount": 0, 146 | "RequestResponses": [{"RecordId": "1"} for _ in Records], 147 | } 148 | 149 | 150 | class MockFirehoseBotoClientException: 151 | retry = 1 152 | records: List = [] 153 | 154 | def put_record_batch(self, DeliveryStreamName: str, Records: List[dict]): 155 | if self.retry > 0: 156 | self.retry -= 1 157 | raise Exception() 158 | self.records.extend(Records) 159 | return { 160 | "FailedPutCount": 0, 161 | "RequestResponses": [{"RecordId": "1"} for _ in Records], 162 | } 163 | 164 | 165 | @pytest.fixture 166 | def kinesis_success_item() -> dict: 167 | return {"RecordId": RANDOM_RECORD_ID} 168 | 169 | 170 | @pytest.fixture 171 | def kinesis_failed_item() -> dict: 172 | return {"ErrorCode": RANDOM_ERROR_CODE} 173 | -------------------------------------------------------------------------------- /src/test/utils/test_sts.py: -------------------------------------------------------------------------------- 1 | from unittest.mock import MagicMock 2 | 3 | import boto3 4 | from moto import mock_sts 5 | import pytest 6 | 7 | from lumigo_log_shipper.utils.sts import assume_role, ChinaMissingEnvVar 8 | 9 | 10 | def test_assume_role_china_missing_access_key(monkeypatch, caplog): 11 | monkeypatch.setenv("AWS_REGION", "cn-northwest-1") 12 | with pytest.raises(ChinaMissingEnvVar): 13 | assume_role("111111111111", "unitteset") 14 | assert "CRITICAL" in caplog.text 15 | assert "LUMIGO_LOGS_EDGE_AWS_ACCESS_KEY_ID" in caplog.text 16 | 17 | 18 | def test_assume_role_china_missing_secret_key(monkeypatch, caplog): 19 | monkeypatch.setenv("LUMIGO_LOGS_EDGE_AWS_ACCESS_KEY_ID", "111") 20 | monkeypatch.setenv("AWS_REGION", "cn-northwest-1") 21 | with pytest.raises(ChinaMissingEnvVar): 22 | assume_role("111111111111", "unitteset") 23 | assert "CRITICAL" in caplog.text 24 | assert "LUMIGO_LOGS_EDGE_AWS_SECRET_ACCESS_KEY" in caplog.text 25 | 26 | 27 | @mock_sts 28 | def test_assume_role(monkeypatch): 29 | original_client = boto3.client 30 | mocked_client = MagicMock(side_effect=original_client) 31 | monkeypatch.setattr(boto3, "client", mocked_client) 32 | 33 | assume_role("111111111111", "unitteset") 34 | 35 | mocked_client.assert_called_with("sts") 36 | 37 | 38 | @mock_sts 39 | def test_assume_role_china(monkeypatch): 40 | monkeypatch.setenv("LUMIGO_LOGS_EDGE_AWS_ACCESS_KEY_ID", "key1") 41 | monkeypatch.setenv("LUMIGO_LOGS_EDGE_AWS_SECRET_ACCESS_KEY", "secret1") 42 | monkeypatch.setenv("AWS_REGION", "cn-northwest-1") 43 | original_client = boto3.client 44 | mocked_client = MagicMock(side_effect=original_client) 45 | monkeypatch.setattr(boto3, "client", mocked_client) 46 | 47 | assume_role("111111111111", "unitteset") 48 | 49 | mocked_client.assert_called_with( 50 | "sts", 51 | region_name="us-west-2", 52 | aws_access_key_id="key1", 53 | aws_secret_access_key="secret1", 54 | ) 55 | -------------------------------------------------------------------------------- /src/test/utils/test_utils.py: -------------------------------------------------------------------------------- 1 | from lumigo_log_shipper.utils.utils import split_to_chunks 2 | 3 | 4 | def test_split_to_chunks_empty_list(): 5 | lst = [] 6 | chunks = split_to_chunks(lst, 2) 7 | assert chunks == [] 8 | 9 | 10 | def test_split_to_chunks_simple_flow(): 11 | lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 12 | chunks = split_to_chunks(lst, 2) 13 | assert chunks == [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]] 14 | 15 | 16 | def test_split_to_chunks_min_value(): 17 | lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 18 | chunks = split_to_chunks(lst, 0) 19 | assert chunks == [[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]] 20 | 21 | 22 | def test_split_to_chunks_not_equal_chunks(): 23 | lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 24 | chunks = split_to_chunks(lst, 3) 25 | assert chunks == [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] 26 | 27 | 28 | def test_split_to_chunks_smaller_then_chunk_size(): 29 | lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 30 | chunks = split_to_chunks(lst, 11) 31 | assert chunks == [lst] 32 | --------------------------------------------------------------------------------