112 lines
3.0 KiB
Go
112 lines
3.0 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log-enricher/models/es"
|
|
dbPackage "log-enricher/pkg/db"
|
|
"log-enricher/pkg/encoder"
|
|
"log-enricher/pkg/log"
|
|
"log-enricher/pkg/symbolicator"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type ErrorProcessor struct {
|
|
logger *log.Logger
|
|
elasticSearchClient *dbPackage.ElasticSearchClient
|
|
sourceMapFetcher *SourceMapService
|
|
}
|
|
|
|
type Frame struct {
|
|
Filename string `json:"filename"`
|
|
Function string `json:"function"`
|
|
InApp bool `json:"in_app"`
|
|
Lineno int `json:"lineno"`
|
|
Colno int `json:"colno"`
|
|
SourceMapPath string `json:"source_map_path,omitempty"`
|
|
}
|
|
|
|
type Stacktrace struct {
|
|
Frames []Frame `json:"frames"`
|
|
}
|
|
|
|
type MechanismData struct {
|
|
Function string `json:"function"`
|
|
Handler string `json:"handler"`
|
|
Target string `json:"target"`
|
|
}
|
|
|
|
type Mechanism struct {
|
|
Type string `json:"type"`
|
|
Handled bool `json:"handled"`
|
|
Data MechanismData `json:"data"`
|
|
}
|
|
|
|
type Exception struct {
|
|
Type string `json:"type"`
|
|
Value string `json:"value"`
|
|
Stacktrace Stacktrace `json:"stacktrace"`
|
|
Mechanism Mechanism `json:"mechanism"`
|
|
ProjectId string `json:"project_id,omitempty"`
|
|
ReleaseId string `json:"release_id,omitempty"`
|
|
}
|
|
|
|
func NewErrorProcessor(logger *log.Logger, elasticSearchClient *dbPackage.ElasticSearchClient, sourceMapFetcherService *SourceMapService) *ErrorProcessor {
|
|
return &ErrorProcessor{
|
|
logger: logger,
|
|
elasticSearchClient: elasticSearchClient,
|
|
sourceMapFetcher: sourceMapFetcherService,
|
|
}
|
|
|
|
}
|
|
|
|
func extractFilename(url string) string {
|
|
// Remove any trailing characters you don't want (in case there are any)
|
|
url = strings.Trim(url, `"`)
|
|
|
|
// Extract the base (filename) from the URL path
|
|
filename := path.Base(url)
|
|
return filename + ".map"
|
|
}
|
|
|
|
func (ep *ErrorProcessor) ProcessError(error []byte) {
|
|
fmt.Println("got error in consumer processing the same...")
|
|
var payload Exception
|
|
json.Unmarshal(error, &payload)
|
|
//getting source map path
|
|
for i, frame := range payload.Stacktrace.Frames {
|
|
fileName := extractFilename(frame.Filename)
|
|
projectId := payload.ProjectId
|
|
frame := &payload.Stacktrace.Frames[i]
|
|
//releaseId := payload.ReleaseId
|
|
sourceMapPath, err := ep.sourceMapFetcher.GetSourceMap(fileName, projectId, "release-1")
|
|
if err != nil {
|
|
fmt.Printf("error occured in fetching source map")
|
|
}
|
|
frame.SourceMapPath = sourceMapPath
|
|
|
|
}
|
|
var frames, _ = json.Marshal(&payload.Stacktrace)
|
|
command := &symbolicator.Command{
|
|
Cmd: "bins/source-map",
|
|
Args: []string{string(frames)},
|
|
}
|
|
|
|
output, err := symbolicator.SymbolicatorClient(command)
|
|
if err != nil {
|
|
fmt.Printf("error occured in symbolicator client %s", err)
|
|
}
|
|
//make md5 hash of error
|
|
hash := encoder.Md5Encode(string(error))
|
|
//creating es document
|
|
errorDocument := &es.ErrorDocument{
|
|
Error: payload.Value,
|
|
StackTrace: output.Frames,
|
|
ErrorHash: hash,
|
|
ProjectId: payload.ProjectId,
|
|
ReleaseVersion: payload.ReleaseId,
|
|
}
|
|
ep.elasticSearchClient.IndexDocument(errorDocument)
|
|
}
|