From afa26f85864d49ee6d87142a6afb425b88743fc4 Mon Sep 17 00:00:00 2001 From: ruuushhh <66899387+ruuushhh@users.noreply.github.com> Date: Tue, 18 Apr 2023 12:15:40 +0530 Subject: [PATCH] Sentry integration (#227) * Sentry integration * Pytest updated * codecove config updated * Codecov GA added --- .github/workflows/codecov.yml | 35 ++++++++++++++++++++ .github/workflows/production_deployment.yml | 18 +++++++++++ .github/workflows/pytest.yml | 5 +-- .github/workflows/staging_deployment.yml | 18 +++++++++++ codecov.yml | 17 ++++++++++ fyle_xero_api/sentry.py | 36 +++++++++++++++++++++ fyle_xero_api/settings.py | 5 +++ requirements.txt | 3 +- 8 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/codecov.yml create mode 100644 codecov.yml create mode 100644 fyle_xero_api/sentry.py diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml new file mode 100644 index 00000000..2ef7e706 --- /dev/null +++ b/.github/workflows/codecov.yml @@ -0,0 +1,35 @@ +name: Codecov Report Upload + +on: + push: + branches: + - master + pull_request: + types: [labeled] + +jobs: + pytest: + runs-on: ubuntu-latest + environment: CI Environment + steps: + - uses: actions/checkout@v2 + - uses: satackey/action-docker-layer-caching@v0.0.11 + continue-on-error: true + - name: Bring up Services and Run Tests + run: | + docker-compose -f docker-compose-pipeline.yml build + docker-compose -f docker-compose-pipeline.yml up -d + docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov --cov-report=xml --cov-fail-under=94 + echo "STATUS=$(cat pytest-coverage.txt | grep 'Required test' | awk '{ print $1 }')" >> $GITHUB_ENV + echo "FAILED=$(cat test-reports/report.xml | awk -F'=' '{print $5}' | awk -F' ' '{gsub(/"/, "", $1); print $1}')" >> $GITHUB_ENV + env: + FYLE_BASE_URL: ${{ secrets.FYLE_BASE_URL }} + FYLE_TOKEN_URI: ${{ secrets.FYLE_TOKEN_URI }} + NS_ACCOUNT_ID: ${{ secrets.NS_ACCOUNT_ID }} + - name: Upload coverage reports to Codecov with GitHub Action + uses: codecov/codecov-action@v3 + - name: Stop Services + run: docker compose -f docker-compose-pipeline.yml down + - name: Evaluate Coverage + if: ${{ (env.STATUS == 'FAIL') || (env.FAILED > 0) }} + run: exit 1 \ No newline at end of file diff --git a/.github/workflows/production_deployment.yml b/.github/workflows/production_deployment.yml index 5eaabc2a..f526d8dd 100644 --- a/.github/workflows/production_deployment.yml +++ b/.github/workflows/production_deployment.yml @@ -32,6 +32,24 @@ jobs: AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} KUBERNETES_MANIFEST_FILE_PATH: deployment/production/controller.yml + - name: Create new Sentry release + env: + SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} + SENTRY_ORG: fyle-technologies-private-limi + SENTRY_PROJECT: xero-api + SENTRY_DEPLOY_ENVIRONMENT: production + run: | + # Install Sentry CLI + curl -sL https://sentry.io/get-cli/ | bash + + # Create new Sentry release + export SENTRY_RELEASE=$(sentry-cli releases propose-version) + sentry-cli releases new -p $SENTRY_PROJECT $SENTRY_RELEASE + sentry-cli releases set-commits --auto $SENTRY_RELEASE + sentry-cli releases finalize $SENTRY_RELEASE + + # Create new deploy for this Sentry release + sentry-cli releases deploys $SENTRY_RELEASE new -e $SENTRY_DEPLOY_ENVIRONMENT - name: Slack Notification uses: rtCamp/action-slack-notify@v2 env: diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index d5f449be..9e56bb0b 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -21,7 +21,7 @@ jobs: run: | docker-compose -f docker-compose-pipeline.yml build docker-compose -f docker-compose-pipeline.yml up -d - docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov=. --junit-xml=test-reports/report.xml --cov-report=term-missing --cov-fail-under=97 | tee pytest-coverage.txt + docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov=. --cov-report=xml --cov-fail-under=97 --junit-xml=test-reports/report.xml echo "STATUS=$(cat pytest-coverage.txt | grep 'Required test' | awk '{ print $1 }')" >> $GITHUB_ENV echo "FAILED=$(cat test-reports/report.xml | awk -F'=' '{print $5}' | awk -F' ' '{gsub(/"/, "", $1); print $1}')" >> $GITHUB_ENV env: @@ -37,12 +37,13 @@ jobs: XERO_REDIRECT_URI: ${{ secrets.XERO_REDIRECT_URI }} XERO_TOKEN_URI: ${{ secrets.XERO_TOKEN_URI }} continue-on-error: true + - name: Upload coverage reports to Codecov with GitHub Action + uses: codecov/codecov-action@v3 - name: Pytest coverage comment uses: MishaKav/pytest-coverage-comment@main if: ${{ always() && github.ref != 'refs/heads/master' }} with: create-new-comment: true - pytest-coverage-path: ./pytest-coverage.txt junitxml-path: ./test-reports/report.xml - name: Stop Services run: docker compose -f docker-compose-pipeline.yml down diff --git a/.github/workflows/staging_deployment.yml b/.github/workflows/staging_deployment.yml index 808f1231..ff156027 100644 --- a/.github/workflows/staging_deployment.yml +++ b/.github/workflows/staging_deployment.yml @@ -36,6 +36,24 @@ jobs: AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} KUBERNETES_MANIFEST_FILE_PATH: deployment/staging/controller.yml + - name: Create new Sentry release + env: + SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} + SENTRY_ORG: fyle-technologies-private-limi + SENTRY_PROJECT: xero-api + SENTRY_DEPLOY_ENVIRONMENT: staging + run: | + # Install Sentry CLI + curl -sL https://sentry.io/get-cli/ | bash + + # Create new Sentry release + export SENTRY_RELEASE=$(sentry-cli releases propose-version) + sentry-cli releases new -p $SENTRY_PROJECT $SENTRY_RELEASE + sentry-cli releases set-commits --auto $SENTRY_RELEASE + sentry-cli releases finalize $SENTRY_RELEASE + + # Create new deploy for this Sentry release + sentry-cli releases deploys $SENTRY_RELEASE new -e $SENTRY_DEPLOY_ENVIRONMENT - name: Slack Notification uses: rtCamp/action-slack-notify@v2 env: diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..c8dac6b1 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,17 @@ +codecov: + require_ci_to_pass: false #post comment only if ci checks have passed +comment: + behavior: True #comment post behavior (update existing, post new, delete and post new) + layout: reach, diff, flags, files #comment format + require_base: true #base report is mandatory to post comment + require_changes: false #only post comment if chages are done + require_head: false #head commit is mandatory + show_carryforward_flags: false +coverage: #code coverage range + precision: 2 + range: + - 97.0 + - 98.0 + round: down +github_checks: + annotations: false \ No newline at end of file diff --git a/fyle_xero_api/sentry.py b/fyle_xero_api/sentry.py new file mode 100644 index 00000000..21320e33 --- /dev/null +++ b/fyle_xero_api/sentry.py @@ -0,0 +1,36 @@ +import os + +import sentry_sdk + +from sentry_sdk.integrations.django import DjangoIntegration + +class Sentry: + + @staticmethod + def init(): + sentry_sdk.init( + dsn=os.environ.get('SENTRY_DSN'), + send_default_pii=True, + integrations=[DjangoIntegration()], + environment=os.environ.get('SENTRY_ENV'), + traces_sampler=Sentry.traces_sampler, + attach_stacktrace=True, + request_bodies='small', + in_app_include=['apps.users', + 'apps.workspaces', + 'apps.mappings', + 'apps.fyle', + 'apps.xero', + 'apps.tasks', + 'fyle_rest_auth', + 'fyle_accounting_mappings'], + ) + + @staticmethod + def traces_sampler(sampling_context): + # avoiding ready APIs in performance tracing + if sampling_context.get('wsgi_environ') is not None: + if sampling_context['wsgi_environ']['PATH_INFO'] in ['/ready']: + return 0 + + return 0.5 diff --git a/fyle_xero_api/settings.py b/fyle_xero_api/settings.py index a1c47d78..0dc83934 100644 --- a/fyle_xero_api/settings.py +++ b/fyle_xero_api/settings.py @@ -14,6 +14,8 @@ import dj_database_url +from .sentry import Sentry + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -292,6 +294,9 @@ CORS_ORIGIN_ALLOW_ALL = True +# Sentry +Sentry.init() + CORS_ALLOW_HEADERS = [ 'sentry-trace', 'authorization', diff --git a/requirements.txt b/requirements.txt index 6adc87e4..3ff8e97c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -33,11 +33,12 @@ pylint==2.7.4 python-dateutil==2.8.1 pytz==2019.3 requests==2.26.0 +sentry-sdk==1.19.1 six==1.13.0 sqlparse==0.3.0 toml==0.10.2 Unidecode==1.1.2 -urllib3==1.26.5 +urllib3==1.26.11 wcwidth==0.1.8 wrapt==1.12.1 xerosdk==0.13.0