Skip to content

Commit

Permalink
chore: setup sentry
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima <kathurimakimathi415@gmail.com>
  • Loading branch information
KathurimaKimathi committed Apr 21, 2022
1 parent 7be2a69 commit ca0d105
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ env:
OPENCONCEPTLAB_API_URL: ${{ secrets.OPENCONCEPTLAB_API_URL }}
JWT_KEY: ${{ secrets.JWT_KEY }}
CLOUD_HEALTH_FHIRSTORE_ID: ${{ secrets.CLOUD_HEALTH_FHIRSTORE_ID }}
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}

# Schema Registry CLI command version
CLI_VERSION: v0.0.1
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/aws/aws-sdk-go v1.40.0 // indirect
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/casbin/casbin/v2 v2.37.2
github.com/getsentry/sentry-go v0.11.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
Expand All @@ -36,7 +37,7 @@ require (
github.com/savannahghi/profileutils v0.0.17
github.com/savannahghi/pubsubtools v0.0.2
github.com/savannahghi/scalarutils v0.0.4
github.com/savannahghi/serverutils v0.0.4
github.com/savannahghi/serverutils v0.0.6
github.com/segmentio/ksuid v1.0.4
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ github.com/savannahghi/scalarutils v0.0.4 h1:UT0a5zl1ByzV4+W7hYjGlIyLmILmpJV/Wgc
github.com/savannahghi/scalarutils v0.0.4/go.mod h1:Z+Dl3wc3vy5zKvthctHAtYzol1p8w27zEVRfOYueoks=
github.com/savannahghi/server_utils v0.0.1/go.mod h1:Tldm4Op4ARxk8xaP3BA0V52XVZEJenkq0ck4FCdEp+I=
github.com/savannahghi/serverutils v0.0.2/go.mod h1:sLX0El0i0DKN/9cUkB8xqm5cVMP79qCZvK60EzB7Pa4=
github.com/savannahghi/serverutils v0.0.4 h1:mQGAwhNgS1NPSBXqCeyywuGQFnvRxiOmHJEHPgzhWQE=
github.com/savannahghi/serverutils v0.0.4/go.mod h1:3VCEJ8BTHf/DW3WFjLqV4SznzrXaul/As2RJ5eNOO7U=
github.com/savannahghi/serverutils v0.0.6 h1:RCRHQbp/GtYwPsLmAwYe2gSwrM0R5XPyVUViXqItjDQ=
github.com/savannahghi/serverutils v0.0.6/go.mod h1:QTqJJaNk3Rmtl2RV9ltxERjvv7WPkMJyieX18TKYDG8=
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
github.com/segmentio/ksuid v1.0.3/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
Expand Down Expand Up @@ -564,6 +565,7 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -779,6 +781,7 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
8 changes: 8 additions & 0 deletions pkg/clinical/application/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package utils

import (
"fmt"
"time"

"github.com/asaskevich/govalidator"
"github.com/getsentry/sentry-go"
)

// ValidateEmail returns an error if the supplied string does not have a
Expand All @@ -14,3 +16,9 @@ func ValidateEmail(email string) error {
}
return nil
}

// ReportErrorToSentry captures the exception thrown and registers an issue in sentry
func ReportErrorToSentry(err error) {
defer sentry.Flush(2 * time.Millisecond)
sentry.CaptureException(err)
}
23 changes: 23 additions & 0 deletions pkg/clinical/application/utils/helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"fmt"
"testing"

"github.com/savannahghi/firebasetools"
Expand Down Expand Up @@ -38,3 +39,25 @@ func TestValidateEmail(t *testing.T) {
})
}
}

func TestReportErrorToSentry(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
}{
{
name: "Happy case",
args: args{
err: fmt.Errorf("test error"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ReportErrorToSentry(tt.args.err)
})
}
}
23 changes: 23 additions & 0 deletions pkg/clinical/infrastructure/datastore/fhir/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"log"

"github.com/savannahghi/clinical/pkg/clinical/application/utils"
"github.com/savannahghi/serverutils"
"google.golang.org/api/healthcare/v1"
)
Expand All @@ -28,6 +29,7 @@ func NewFHIRRepository() *Repository {
ctx := context.Background()
hsv, err := healthcare.NewService(ctx)
if err != nil {
utils.ReportErrorToSentry(err)
log.Panicf("unable to initialize new Google Cloud Healthcare Service: %s", err)
}
return &Repository{
Expand All @@ -46,6 +48,7 @@ func (fr Repository) CreateDataset() (*healthcare.Operation, error) {
parent := fmt.Sprintf("projects/%s/locations/%s", fr.projectID, fr.location)
resp, err := datasetsService.Create(parent, &healthcare.Dataset{}).DatasetId(fr.datasetID).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("create Data Set: %v", err)
}
return resp, nil
Expand All @@ -58,6 +61,7 @@ func (fr Repository) GetDataset() (*healthcare.Dataset, error) {
name := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", fr.projectID, fr.location, fr.datasetID)
resp, err := datasetsService.Get(name).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("get Data Set: %v", err)
}
return resp, nil
Expand All @@ -76,6 +80,7 @@ func (fr Repository) CreateFHIRStore() (*healthcare.FhirStore, error) {
parent := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", fr.projectID, fr.location, fr.datasetID)
resp, err := storesService.Create(parent, store).FhirStoreId(fr.fhirStoreID).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("create FHIR Store: %v", err)
}
return resp, nil
Expand All @@ -90,6 +95,7 @@ func (fr Repository) GetFHIRStore() (*healthcare.FhirStore, error) {
fr.projectID, fr.location, fr.datasetID, fr.fhirStoreID)
store, err := storesService.Get(name).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("get FHIR Store: %v", err)
}
return store, nil
Expand Down Expand Up @@ -119,6 +125,7 @@ func (fr Repository) CreateFHIRResource(resourceType string, payload map[string]
payload["language"] = "EN"
jsonPayload, err := json.Marshal(payload)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("json.Encode: %v", err)
}

Expand All @@ -131,6 +138,7 @@ func (fr Repository) CreateFHIRResource(resourceType string, payload map[string]
call.Header().Set("Content-Type", "application/fhir+json;charset=utf-8")
resp, err := call.Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("create: %v", err)
}
defer func() {
Expand All @@ -139,6 +147,7 @@ func (fr Repository) CreateFHIRResource(resourceType string, payload map[string]

respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}
if resp.StatusCode > 299 {
Expand All @@ -159,10 +168,12 @@ func (fr Repository) DeleteFHIRResource(resourceType, fhirResourceID string) ([]
resourceType, fhirResourceID)
resp, err := fhirService.Delete(name).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("delete: %v", err)
}
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}
if resp.StatusCode > 299 {
Expand Down Expand Up @@ -193,6 +204,7 @@ func (fr Repository) PatchFHIRResource(
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("json.Encode: %v", err)
}
name := fmt.Sprintf(
Expand All @@ -204,6 +216,7 @@ func (fr Repository) PatchFHIRResource(
call.Header().Set("Content-Type", "application/json-patch+json")
resp, err := call.Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("patch: %v", err)
}
defer func() {
Expand All @@ -214,6 +227,7 @@ func (fr Repository) PatchFHIRResource(
log.Printf("Patch FHIR Resource %d Response: %s", resp.StatusCode, string(respBytes))
}
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}
if resp.StatusCode > 299 {
Expand All @@ -231,6 +245,7 @@ func (fr Repository) UpdateFHIRResource(
payload["resourceType"] = resourceType
jsonPayload, err := json.Marshal(payload)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("json.Encode: %v", err)
}
if serverutils.IsDebug() {
Expand All @@ -244,13 +259,15 @@ func (fr Repository) UpdateFHIRResource(
call.Header().Set("Content-Type", "application/fhir+json;charset=utf-8")
resp, err := call.Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("update: %v", err)
}
defer func() {
_ = resp.Body.Close()
}()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}
if resp.StatusCode > 299 {
Expand All @@ -275,6 +292,7 @@ func (fr Repository) GetFHIRPatientAllData(fhirResourceID string) ([]byte, error

resp, err := fhirService.PatientEverything(name).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("PatientAllData: %v", err)
}

Expand All @@ -284,6 +302,7 @@ func (fr Repository) GetFHIRPatientAllData(fhirResourceID string) ([]byte, error

respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}

Expand All @@ -306,13 +325,15 @@ func (fr Repository) GetFHIRResource(resourceType, fhirResourceID string) ([]byt
call.Header().Set("Content-Type", "application/fhir+json;charset=utf-8")
resp, err := call.Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("read: %v", err)
}
defer func() {
_ = resp.Body.Close()
}()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}
if resp.StatusCode > 299 {
Expand All @@ -336,6 +357,7 @@ func (fr Repository) GetFHIRPatientEverything(fhirResourceID string) ([]byte, er

resp, err := fhirService.PatientEverything(name).Do()
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("PatientEverything: %v", err)
}

Expand All @@ -345,6 +367,7 @@ func (fr Repository) GetFHIRPatientEverything(fhirResourceID string) ([]byte, er

respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
utils.ReportErrorToSentry(err)
return nil, fmt.Errorf("could not read response: %v", err)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/clinical/presentation/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"

"github.com/savannahghi/clinical/pkg/clinical/application/utils"
"github.com/savannahghi/clinical/pkg/clinical/domain"
"github.com/savannahghi/clinical/pkg/clinical/infrastructure"
"github.com/savannahghi/clinical/pkg/clinical/usecases"
Expand Down Expand Up @@ -49,6 +50,7 @@ func (p PresentationHandlersImpl) DeleteFHIRPatientByPhone() http.HandlerFunc {
}
deleted, err := p.usecases.DeleteFHIRPatientByPhone(ctx, payload.PhoneNumber)
if err != nil {
utils.ReportErrorToSentry(err)
err := fmt.Sprintf("unable to delete patient: %v", err.Error())
serverutils.WriteJSONResponse(
w,
Expand Down
Loading

0 comments on commit ca0d105

Please sign in to comment.