Files
cybertron/service/ExceptionService.go

101 lines
2.6 KiB
Go
Raw Normal View History

package service
import (
2024-08-16 06:26:21 +05:30
"bufio"
2024-09-18 16:42:54 +05:30
"cybertron/models/instrumentation"
"cybertron/pkg/encoder"
"cybertron/pkg/kafka/producer"
"cybertron/pkg/log"
2024-09-18 16:42:54 +05:30
"cybertron/pkg/metrics"
2024-08-16 06:26:21 +05:30
"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
}
2024-08-16 06:26:21 +05:30
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"`
2024-09-18 16:42:54 +05:30
Extra interface{} `json:"extra,omitempty"`
Request interface{} `json:"request,omitempty"`
2024-08-16 06:26:21 +05:30
}
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"`
2024-08-16 06:26:21 +05:30
}
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) {
2024-08-16 06:26:21 +05:30
scanner := bufio.NewScanner(c.Request.Body)
var lines []string
2024-09-18 16:42:54 +05:30
//error metric firing
2024-08-16 06:26:21 +05:30
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
var jsonData Payload
projectID := c.Param("projectId")
2024-08-16 06:26:21 +05:30
err := json.Unmarshal([]byte(lines[2]), &jsonData)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Second line is not valid JSON"})
return
}
2024-09-18 16:42:54 +05:30
metrics.PublishErrorConsumptionMetric(instrumentation.ErrorConsumptionMetric{ProjectId: projectID})
2024-08-16 06:26:21 +05:30
for _, errorItem := range jsonData.Exception.Values {
errorItem.ProjectId = projectID
2024-09-10 10:04:08 +05:30
//todo update release id
2024-08-16 06:26:21 +05:30
errorItem.ReleaseId = "release-1"
errorItem.Breadcrumbs = jsonData.Breadcrumbs
2024-09-18 16:42:54 +05:30
errorItem.Extra = jsonData.Extra
errorItem.Request = jsonData.Request
2024-08-16 06:26:21 +05:30
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"})
}