94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package service
|
|
|
|
import (
|
|
"bufio"
|
|
"cybertron/pkg/encoder"
|
|
"cybertron/pkg/kafka/producer"
|
|
"cybertron/pkg/log"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"net/http"
|
|
)
|
|
|
|
type ExceptionService struct {
|
|
logger *log.Logger
|
|
dbClient *gorm.DB
|
|
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"`
|
|
Breadcrumbs interface{} `json:"breadcrumbs,omitempty"`
|
|
Extras interface{} `json:"extras,omitempty"`
|
|
Request interface{} `json:"request,omitempty"`
|
|
}
|
|
|
|
type Exception struct {
|
|
Values []ExceptionValue `json:"values"`
|
|
}
|
|
type Payload struct {
|
|
Exception Exception `json:"exception"`
|
|
Breadcrumbs interface{} `json:"breadcrumbs"`
|
|
Request interface{} `json:"request"`
|
|
Extra interface{} `json:"extra"`
|
|
}
|
|
|
|
func NewExceptionService(logger *log.Logger, dbClient *gorm.DB, kafkaProducer producer.KProducer) *ExceptionService {
|
|
return &ExceptionService{
|
|
logger: logger,
|
|
dbClient: dbClient,
|
|
kafkaProducer: kafkaProducer,
|
|
}
|
|
}
|
|
|
|
func (exceptionService *ExceptionService) CatchErrors(c *gin.Context) {
|
|
scanner := bufio.NewScanner(c.Request.Body)
|
|
var lines []string
|
|
|
|
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
|
|
}
|
|
|
|
for _, errorItem := range jsonData.Exception.Values {
|
|
errorItem.ProjectId = projectID
|
|
errorItem.ReleaseId = "release-1"
|
|
errorItem.Breadcrumbs = jsonData.Breadcrumbs
|
|
errorItem.Extras = jsonData.Extra
|
|
errorItem.Request = jsonData.Request
|
|
err := exceptionService.kafkaProducer.PublishEvent(errorItem, "kafka-stream", "", nil, encoder.JsonEncoderInstance)
|
|
|
|
if err != nil {
|
|
fmt.Println("Failed to push error to kafka")
|
|
}
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"status": "success"})
|
|
}
|