diff --git a/configs/application.yml b/configs/application.yml index 09d257f..2d752b0 100644 --- a/configs/application.yml +++ b/configs/application.yml @@ -42,7 +42,7 @@ http: # Kafka config kafka: - password: XX + password: kDia1uC.GI;)Al5eQ)+Q username: varnitgoyal/varnitgoyal95@gmail.com/ocid1.streampool.oc1.ap-mumbai-1.amaaaaaaotdslraanepwp54txqqxkmg4l6dghrhufiezqkx2lqhndgxoq7pa brokers: cell-1.streaming.ap-mumbai-1.oci.oraclecloud.com:9092 group: diff --git a/internal/transport/router/exception.go b/internal/transport/router/exception.go index e12042b..bd057c6 100644 --- a/internal/transport/router/exception.go +++ b/internal/transport/router/exception.go @@ -8,8 +8,8 @@ import ( func ExceptionRouter(r *gin.Engine, dep *dependencies.Dependencies) { exceptionHandler := handler.NewExceptionHandler(dep.Service.ExceptionService) - exceptionRouterGroup := r.Group("/api/v1") + exceptionRouterGroup := r.Group("/api") { - exceptionRouterGroup.POST("/catch-errors", exceptionHandler.CatchErrors) + exceptionRouterGroup.POST("/:projectId/envelope", exceptionHandler.CatchErrors) } } diff --git a/models/db/project.go b/models/db/project.go index b68a527..1823dcc 100644 --- a/models/db/project.go +++ b/models/db/project.go @@ -1,7 +1,6 @@ package db import ( - "github.com/google/uuid" "time" ) @@ -10,7 +9,7 @@ type Project struct { CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` DeletedAt time.Time `json:"deletedAt"` - ProjectReferenceId uuid.UUID `json:"projectReferenceId" gorm:"primaryKey"` + ProjectReferenceId string `json:"projectReferenceId" gorm:"primaryKey"` Name string `json:"name" gorm:"unique"` Team string `json:"team"` } diff --git a/service/ExceptionService.go b/service/ExceptionService.go index 5753e53..4a42559 100644 --- a/service/ExceptionService.go +++ b/service/ExceptionService.go @@ -1,13 +1,13 @@ package service import ( - "cybertron/configs" + "bufio" "cybertron/pkg/encoder" "cybertron/pkg/kafka/producer" "cybertron/pkg/log" + "encoding/json" "fmt" "github.com/gin-gonic/gin" - "github.com/google/uuid" "gorm.io/gorm" "net/http" ) @@ -18,6 +18,33 @@ type ExceptionService struct { kafkaProducer producer.KProducer } +type Frame struct { + Filename string `json:"filename"` + Function string `json:"function"` + InApp bool `json:"in_app"` + Lineno int `json:"lineno,omitempty"` + Colno int `json:"colno,omitempty"` +} + +type Stacktrace struct { + Frames []Frame `json:"frames"` +} + +type ExceptionValue struct { + Type string `json:"type"` + Value string `json:"value"` + Stacktrace Stacktrace `json:"stacktrace"` + ProjectId string `json:"project_id,omitempty"` + ReleaseId string `json:"release_id,omitempty"` +} + +type Exception struct { + Values []ExceptionValue `json:"values"` +} +type Payload struct { + Exception Exception `json:"exception"` +} + func NewExceptionService(logger *log.Logger, dbClient *gorm.DB, kafkaProducer producer.KProducer) *ExceptionService { return &ExceptionService{ logger: logger, @@ -27,17 +54,27 @@ func NewExceptionService(logger *log.Logger, dbClient *gorm.DB, kafkaProducer pr } func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) { - var errorsPayload []interface{} + scanner := bufio.NewScanner(c.Request.Body) + var lines []string - if err := c.BindJSON(&errorsPayload); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON payload"}) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + var jsonData Payload + projectID := c.Param("projectId") + + err := json.Unmarshal([]byte(lines[2]), &jsonData) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Second line is not valid JSON"}) return } - headerMap := make(map[string]string) + for _, errorItem := range jsonData.Exception.Values { + errorItem.ProjectId = projectID + errorItem.ReleaseId = "release-1" + err := exceptionService.kafkaProducer.PublishEvent(errorItem, "kafka-stream", "", nil, encoder.JsonEncoderInstance) - for _, errorItem := range errorsPayload { - err := exceptionService.kafkaProducer.PublishEvent(errorItem, configs.GetKafkaConfig().GetTopic("js-error-topic"), uuid.NewString(), headerMap, encoder.JsonEncoderInstance) if err != nil { fmt.Println("Failed to push error to kafka") } diff --git a/service/ProjectCreator.go b/service/ProjectCreator.go index 5532e08..5c50edc 100644 --- a/service/ProjectCreator.go +++ b/service/ProjectCreator.go @@ -3,12 +3,15 @@ package service import ( "cybertron/internal/client/aws" "cybertron/models/db" + "cybertron/pkg/encoder" "cybertron/pkg/kafka/producer" "cybertron/pkg/log" "github.com/gin-gonic/gin" "github.com/google/uuid" "gorm.io/gorm" + "math/big" "net/http" + "strings" ) type ProjectCreator struct { @@ -40,10 +43,13 @@ func (pc *ProjectCreator) CreateProject(ctx *gin.Context) { }) return } + var uuidGenerated = uuid.New() + var i big.Int + i.SetString(strings.Replace(uuidGenerated.String(), "-", "", 4), 16) // Write to database result := pc.dbClient.Create(&db.Project{ - ProjectReferenceId: uuid.New(), + ProjectReferenceId: i.String(), Name: projectBody.Name, Team: projectBody.Team, }) @@ -79,10 +85,11 @@ func (pc *ProjectCreator) ProjectGet(c *gin.Context) { //generate Random numbers in go - //err := pc.kafkaProducer.PublishEvent(rand.Int(), "kafka-stream", "", nil, encoder.JsonEncoderInstance) - //if err != nil { - // pc.logger.Info(err.Error()) - //} + err := pc.kafkaProducer.PublishEvent(`{"hello" :"world"}`, "kafka-stream", "", nil, encoder.JsonEncoderInstance) + if err != nil { + pc.logger.Info(err.Error()) + } + pc.logger.Info("published kafka message") var projects []db.Project pc.dbClient.Find(&projects) diff --git a/service/ReleaseService.go b/service/ReleaseService.go index de63137..de0cec7 100644 --- a/service/ReleaseService.go +++ b/service/ReleaseService.go @@ -4,7 +4,6 @@ import ( "cybertron/models/db" "cybertron/pkg/log" "github.com/gin-gonic/gin" - "github.com/google/uuid" "gorm.io/gorm" "net/http" ) @@ -38,10 +37,8 @@ func (releaseService *ReleaseService) AddRelease(c *gin.Context) { } releaseToBeAdded := db.Release{ - ReleaseId: uuid.New(), ProjectReferenceId: releaseBody.ProjectId, ReleaseVersion: releaseBody.Version, - SourceMapUrl: releaseBody.SourceMapUrl, } if result := releaseService.dbClient.Create(&releaseToBeAdded); result.Error != nil {