Skip to content

Commit

Permalink
Sentry integration (#227)
Browse files Browse the repository at this point in the history
* Sentry integration

* Pytest updated

* codecove config updated

* Codecov GA added
  • Loading branch information
ruuushhh authored Apr 18, 2023
1 parent 68f9661 commit afa26f8
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 3 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
18 changes: 18 additions & 0 deletions .github/workflows/production_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/staging_deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions fyle_xero_api/sentry.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions fyle_xero_api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)))

Expand Down Expand Up @@ -292,6 +294,9 @@

CORS_ORIGIN_ALLOW_ALL = True

# Sentry
Sentry.init()

CORS_ALLOW_HEADERS = [
'sentry-trace',
'authorization',
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit afa26f8

Please sign in to comment.