Skip to content

Commit

Permalink
report error to coralogix initial push
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Apr 1, 2024
1 parent 74da120 commit 01e9766
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func init() {
RtUrl = flag.String("rt.url", "http://localhost:8081/artifactory", "Artifactory url")
DistUrl = flag.String("ds.url", "", "Distribution url")
XrayUrl = flag.String("xr.url", "", "Xray url")
XscUrl = flag.String("xsc.url", "", "Xray url")
XscUrl = flag.String("xsc.url", "", "Xsc url")
PipelinesUrl = flag.String("pipe.url", "", "Pipelines url")
AccessUrl = flag.String("access.url", "http://localhost:8081/access", "Access url")
RtUser = flag.String("rt.user", "admin", "Artifactory username")
Expand Down
58 changes: 58 additions & 0 deletions tests/xsclogerrorevent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tests

import (
"encoding/json"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/xsc/services"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

const errorMessageContentForTest = "THIS IS NOT A REAL ERROR! This Error is posted as part of TestXscSendLogErrorEvent test"

func TestXscSendLogErrorEvent(t *testing.T) {
initXscTest(t)
mockServer, logErrorService := createXscMockServer(t)
defer mockServer.Close()

event := &services.ExternalErrorLog{
Log_level: "error",
Source: "cli",
Message: errorMessageContentForTest,
}

assert.NoError(t, logErrorService.SendLogErrorEvent(event))
}

func createXscMockServer(t *testing.T) (mockServer *httptest.Server, logErrorService *services.LogErrorEventService) {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/xsc/api/v1/event/logMessage" && r.Method == http.MethodPost {
var reqBody services.ExternalErrorLog
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&reqBody)
assert.NoError(t, err, "Invalid JSON request body")
if err != nil {
return
}

assert.Equal(t, "error", reqBody.Log_level)
assert.Equal(t, "cli", reqBody.Source)
assert.Equal(t, errorMessageContentForTest, reqBody.Message)
w.WriteHeader(http.StatusCreated)
return
}
assert.Fail(t, "received an unexpected request")
}))

xscDetails := GetXscDetails()
xscDetails.SetUrl(mockServer.URL + "/xsc")

client, err := jfroghttpclient.JfrogClientBuilder().Build()
assert.NoError(t, err)

logErrorService = services.NewLogErrorEventService(client)
logErrorService.XscDetails = xscDetails
return
}
6 changes: 6 additions & 0 deletions xsc/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ func (sm *XscServicesManager) AddAnalyticsGeneralEvent(event services.XscAnalyti
return eventService.AddGeneralEvent(event)
}

func (sm *XscServicesManager) SendXscLogErrorRequest(errorLog *services.ExternalErrorLog) error {
logErrorService := services.NewLogErrorEventService(sm.client)
logErrorService.XscDetails = sm.config.GetServiceDetails()
return logErrorService.SendLogErrorEvent(errorLog)
}

// UpdateAnalyticsGeneralEvent Upon completion of the scan and we have all the results to report on,
// we send a finalized analytics metrics event with information matching an existing event's msi.
func (sm *XscServicesManager) UpdateAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEventFinalize) error {
Expand Down
42 changes: 42 additions & 0 deletions xsc/services/logerrorevent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package services

import (
"encoding/json"
"fmt"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"net/http"
)

const postLogErrorAPI = "api/v1/event/logMessage"

type LogErrorEventService struct {
client *jfroghttpclient.JfrogHttpClient
XscDetails auth.ServiceDetails
}

type ExternalErrorLog struct {
Log_level string `json:"log_level"`
Source string `json:"source"`
Message string `json:"message"`
}

func NewLogErrorEventService(client *jfroghttpclient.JfrogHttpClient) *LogErrorEventService {
return &LogErrorEventService{client: client}
}

func (les *LogErrorEventService) SendLogErrorEvent(errorLog *ExternalErrorLog) error {
httpClientDetails := les.XscDetails.CreateHttpClientDetails()
requestContent, err := json.Marshal(errorLog)
if err != nil {
return fmt.Errorf("failed to convert POST request body's struct into JSON: %q", err)
}
url := utils.AddTrailingSlashIfNeeded(les.XscDetails.GetUrl()) + postLogErrorAPI
response, body, err := les.client.SendPost(url, requestContent, &httpClientDetails)
if err != nil {
return fmt.Errorf("failed to send POST query to '%s': %s", url, err.Error())
}
return errorutils.CheckResponseStatusWithBody(response, body, http.StatusCreated)
}

0 comments on commit 01e9766

Please sign in to comment.