Skip to content

Commit

Permalink
[Aishwarya|Jenson] integrate with sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
AishwaryaRK authored and jensoncs committed Feb 12, 2019
1 parent 0b3a773 commit 8d7ae20
Show file tree
Hide file tree
Showing 17 changed files with 137 additions and 22 deletions.
2 changes: 1 addition & 1 deletion proctord/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export PROCTOR_MAIL_PASSWORD="password"
export PROCTOR_MAIL_SERVER_HOST="smtp.mail.com"
export PROCTOR_MAIL_SERVER_PORT="123"
export PROCTOR_JOB_POD_ANNOTATIONS="{\"key.one\":\"true\"}"

export PROCTOR_SENTRY_DSN="foo"
2 changes: 1 addition & 1 deletion proctord/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ export PROCTOR_MAIL_PASSWORD="password"
export PROCTOR_MAIL_SERVER_HOST="smtp.mail.com"
export PROCTOR_MAIL_SERVER_PORT="123"
export PROCTOR_JOB_POD_ANNOTATIONS="{\"key.one\":\"true\"}"

export PROCTOR_SENTRY_DSN="foo"
3 changes: 2 additions & 1 deletion proctord/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@
* `PROCTOR_MIN_CLIENT_VERSION` is minimum client version allowed to communicate with proctord
* `PROCTOR_SCHEDULED_JOBS_FETCH_INTERVAL_IN_MINS` is the interval at which the scheduler fetches updated jobs from database
* `PROCTOR_MAIL_USERNAME`, `PROCTOR_MAIL_PASSWORD`, `PROCTOR_MAIL_SERVER_HOST`, `PROCTOR_MAIL_SERVER_PORT` are the creds required to send notification to users on scheduled jobs execution
* `PROCTOR_JOB_POD_ANNOTATIONS` is used to set any kubernetes pod specific annotations.
* `PROCTOR_JOB_POD_ANNOTATIONS` is used to set any kubernetes pod specific annotations.
* `PROCTOR_SENTRY_DSN` is used to set sentry DSN.
9 changes: 9 additions & 0 deletions proctord/audit/auditor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package audit

import (
"github.com/getsentry/raven-go"
"github.com/gojektech/proctor/proctord/kubernetes"
"github.com/gojektech/proctor/proctord/logger"
"github.com/gojektech/proctor/proctord/storage"
Expand Down Expand Up @@ -39,16 +40,24 @@ func (auditor *auditor) JobsExecution(jobsExecutionAuditLog *postgres.JobsExecut
err := auditor.store.AuditJobsExecution(jobsExecutionAuditLog)
if err != nil {
logger.Error("Error auditing jobs execution", err)
raven.CaptureError(err, nil)
}
}

func (auditor *auditor) JobsExecutionStatus(jobExecutionID string) (string, error) {
status, err := auditor.kubeClient.JobExecutionStatus(jobExecutionID)
if err != nil {
logger.Error("Error getting job execution status", err)
raven.CaptureError(err, nil)
return "", err
}

err = auditor.store.UpdateJobsExecutionAuditLog(jobExecutionID, status)
if err != nil {
logger.Error("Error updating job status", err)
raven.CaptureError(err, nil)
return "", err
}

return status, err
}
4 changes: 4 additions & 0 deletions proctord/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ func JobPodAnnotations() map[string]string {

return annotations
}

func SentryDSN() string {
return viper.GetString("SENTRY_DSN")
}
8 changes: 8 additions & 0 deletions proctord/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,11 @@ func TestJobPodAnnotations(t *testing.T) {

assert.Equal(t, map[string]string{"key.one": "true"}, JobPodAnnotations())
}

func TestSentryDSN(t *testing.T) {
os.Setenv("PROCTOR_SENTRY_DSN", "domain")

viper.AutomaticEnv()

assert.Equal(t, "domain", SentryDSN())
}
16 changes: 12 additions & 4 deletions proctord/glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion proctord/glide.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package: github.com/gojektech/proctor/proctord
import:
- package: github.com/Sirupsen/logrus
- package: github.com/davecgh/go-spew
subpackages:
- spew
Expand Down Expand Up @@ -43,3 +42,5 @@ import:
- package: github.com/robfig/cron
version: v1.1
- package: github.com/badoux/checkmail
- package: github.com/getsentry/raven-go
- package: github.com/sirupsen/logrus
7 changes: 7 additions & 0 deletions proctord/jobs/execution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package execution
import (
"encoding/json"
"fmt"
"github.com/getsentry/raven-go"
"net/http"

"github.com/gojektech/proctor/proctord/audit"
Expand Down Expand Up @@ -41,6 +42,8 @@ func (handler *executionHandler) Status() http.HandlerFunc {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
logger.Error(fmt.Sprintf("Error getting job status for job_id: %s", jobExecutionID), err.Error())
raven.CaptureError(err, map[string]string{"job_id": jobExecutionID})

return
}

Expand Down Expand Up @@ -68,6 +71,8 @@ func (handler *executionHandler) Handle() http.HandlerFunc {
defer req.Body.Close()
if err != nil {
logger.Error(fmt.Sprintf("User: %s: Error parsing request body", userEmail), err.Error())
raven.CaptureError(err, map[string]string{"user_email": userEmail})

jobsExecutionAuditLog.Errors = fmt.Sprintf("Error parsing request body: %s", err.Error())
jobsExecutionAuditLog.JobSubmissionStatus = utility.JobSubmissionClientError

Expand All @@ -79,6 +84,8 @@ func (handler *executionHandler) Handle() http.HandlerFunc {
jobExecutionID, err := handler.executioner.Execute(jobsExecutionAuditLog, job.Name, job.Args)
if err != nil {
logger.Error(fmt.Sprintf("%s: User %s: Error executing job: ", job.Name, userEmail), err.Error())
raven.CaptureError(err, map[string]string{"user_email": userEmail, "job_name": job.Name})

jobsExecutionAuditLog.Errors = fmt.Sprintf("Error executing job: %s", err.Error())
jobsExecutionAuditLog.JobSubmissionStatus = utility.JobSubmissionServerError

Expand Down
10 changes: 10 additions & 0 deletions proctord/jobs/logs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logs

import (
"bufio"
"github.com/getsentry/raven-go"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -37,6 +38,7 @@ func CloseWebSocket(message string, conn *websocket.Conn) {
err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, message))
if err != nil {
_logger.Error("Error closing connection with client after logs are read")
raven.CaptureError(err, nil)
}
return
}
Expand All @@ -46,6 +48,8 @@ func (l *logger) Stream() http.HandlerFunc {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
_logger.Error("Error upgrading connection to websocket protocol: ", err)
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(utility.ClientError))
return
Expand All @@ -62,6 +66,8 @@ func (l *logger) Stream() http.HandlerFunc {
logStream, err := l.kubeClient.StreamJobLogs(jobName)
if err != nil {
_logger.Error("Error streaming logs from kube client: ", err)
raven.CaptureError(err, map[string]string{"job_name": jobName})

CloseWebSocket("Something went wrong", conn)
return
}
Expand All @@ -79,6 +85,8 @@ func (l *logger) Stream() http.HandlerFunc {
}

_logger.Error("Error reading from reader: ", err.Error())
raven.CaptureError(err, nil)

CloseWebSocket("Something went wrong", conn)
return
}
Expand All @@ -87,6 +95,8 @@ func (l *logger) Stream() http.HandlerFunc {
err = conn.WriteMessage(websocket.TextMessage, jobLogSingleLine[:])
if err != nil {
_logger.Error("Error writing logs to client: ", err)
raven.CaptureError(err, nil)

CloseWebSocket("Something went wrong", conn)
return
}
Expand Down
4 changes: 4 additions & 0 deletions proctord/jobs/metadata/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package metadata

import (
"encoding/json"
"github.com/getsentry/raven-go"
"net/http"

"github.com/gojektech/proctor/proctord/logger"
Expand Down Expand Up @@ -40,6 +41,7 @@ func (handler *handler) HandleSubmission() http.HandlerFunc {
err = handler.store.CreateOrUpdateJobMetadata(metadata)
if err != nil {
logger.Error("Error updating metadata", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -57,6 +59,7 @@ func (handler *handler) HandleBulkDisplay() http.HandlerFunc {
jobMetadata, err := handler.store.GetAllJobsMetadata()
if err != nil {
logger.Error("Error fetching metadata", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -66,6 +69,7 @@ func (handler *handler) HandleBulkDisplay() http.HandlerFunc {
jobsMetadataInJSON, err := json.Marshal(jobMetadata)
if err != nil {
logger.Error("Error marshalling jobs metadata in json", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand Down
35 changes: 32 additions & 3 deletions proctord/jobs/schedule/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schedule
import (
"encoding/json"
"fmt"
"github.com/getsentry/raven-go"
"github.com/gorilla/mux"
"net/http"
"strings"
Expand Down Expand Up @@ -43,6 +44,7 @@ func (scheduler *scheduler) Schedule() http.HandlerFunc {
defer req.Body.Close()
if err != nil {
logger.Error("Error parsing request body for scheduling jobs: ", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(utility.ClientError))
Expand Down Expand Up @@ -94,6 +96,7 @@ func (scheduler *scheduler) Schedule() http.HandlerFunc {
w.Write([]byte(utility.NonExistentProcClientError))
} else {
logger.Error(fmt.Sprintf("Error fetching metadata for proc %s ", scheduledJob.Tags), scheduledJob.Name, err.Error())
raven.CaptureError(err, map[string]string{"job_tags": scheduledJob.Tags, "job_name": scheduledJob.Name})

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -107,13 +110,15 @@ func (scheduler *scheduler) Schedule() http.HandlerFunc {
if err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
logger.Error(fmt.Sprintf("Client provided duplicate combination of scheduled job name and args: %s ", scheduledJob.Tags), scheduledJob.Name, scheduledJob.Args)
raven.CaptureError(err, map[string]string{"job_tags": scheduledJob.Tags, "job_name": scheduledJob.Name})

w.WriteHeader(http.StatusConflict)
w.Write([]byte(utility.DuplicateJobNameArgsClientError))

return
} else {
logger.Error(fmt.Sprintf("Error persisting scheduled job %s ", scheduledJob.Tags), scheduledJob.Name, err.Error())
raven.CaptureError(err, map[string]string{"job_tags": scheduledJob.Tags, "job_name": scheduledJob.Name})

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -124,7 +129,8 @@ func (scheduler *scheduler) Schedule() http.HandlerFunc {

responseBody, err := json.Marshal(scheduledJob)
if err != nil {
logger.Error(fmt.Sprintf("Error marshaling response body %s ", scheduledJob.Tags),scheduledJob.Name, err.Error())
logger.Error(fmt.Sprintf("Error marshaling response body %s ", scheduledJob.Tags), scheduledJob.Name, err.Error())
raven.CaptureError(err, map[string]string{"job_tags": scheduledJob.Tags, "job_name": scheduledJob.Name})

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -143,6 +149,7 @@ func (scheduler *scheduler) GetScheduledJobs() http.HandlerFunc {
scheduledJobsStoreFormat, err := scheduler.store.GetEnabledScheduledJobs()
if err != nil {
logger.Error("Error fetching scheduled jobs", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -151,15 +158,25 @@ func (scheduler *scheduler) GetScheduledJobs() http.HandlerFunc {

if len(scheduledJobsStoreFormat) == 0 {
logger.Error(utility.NoScheduledJobsError, nil)

w.WriteHeader(http.StatusNoContent)
return
}

scheduledJobs := FromStoreToHandler(scheduledJobsStoreFormat)
scheduledJobs, err := FromStoreToHandler(scheduledJobsStoreFormat)
if err != nil {
logger.Error("Error deserializing scheduled job args to map: ", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
return
}

scheduledJobsJson, err := json.Marshal(scheduledJobs)
if err != nil {
logger.Error("Error marshalling scheduled jobs", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -182,6 +199,7 @@ func (scheduler *scheduler) GetScheduledJob() http.HandlerFunc {
return
}
logger.Error("Error fetching scheduled job", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -196,11 +214,20 @@ func (scheduler *scheduler) GetScheduledJob() http.HandlerFunc {
return
}

job := GetScheduledJob(scheduledJob[0])
job, err := GetScheduledJob(scheduledJob[0])
if err != nil {
logger.Error("Error deserializing scheduled job args to map: ", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
return
}

scheduledJobJson, err := json.Marshal(job)
if err != nil {
logger.Error("Error marshalling scheduled job", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand All @@ -218,11 +245,13 @@ func (scheduler *scheduler) RemoveScheduledJob() http.HandlerFunc {
if err != nil {
if strings.Contains(err.Error(), "invalid input syntax") {
logger.Error(err.Error())

w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid Job ID"))
return
}
logger.Error("Error fetching scheduled job", err.Error())
raven.CaptureError(err, nil)

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(utility.ServerError))
Expand Down
Loading

0 comments on commit 8d7ae20

Please sign in to comment.