Skip to content

Commit

Permalink
refactor: data encapsulation for log Record
Browse files Browse the repository at this point in the history
  • Loading branch information
amircybersec committed Oct 26, 2023
1 parent 91be76f commit b8f24eb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 43 deletions.
79 changes: 43 additions & 36 deletions x/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,35 @@ import (
var debugLog log.Logger = *log.New(io.Discard, "", 0)
var httpClient = &http.Client{}

func sendReport(record map[string]interface{}, collectorURL string) error {
jsonData, err := json.Marshal(record)
if err != nil {
log.Printf("Error encoding JSON: %s\n", err)
return err
}

req, err := http.NewRequest("POST", collectorURL, bytes.NewReader(jsonData))
if err != nil {
debugLog.Printf("Error creating the HTTP request: %s\n", err)
return err
}

req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("Error sending the HTTP request: %s\n", err)
return err
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
debugLog.Printf("Error reading the HTTP response body: %s\n", err)
return err
}
debugLog.Printf("Response: %s\n", respBody)
return nil
type Record struct {
collectorURL string
successSampleRate float64
failureSampleRate float64
success bool
record interface{}
}

func sendReportRandomly(record map[string]interface{}, collectorURL string, success bool, successSampleRate float64, failureSampleRate float64) error {
func Report(r Record) error {

// Perform custom range validation for sampling rate
if successSampleRate < 0.0 || successSampleRate > 1.0 {
if r.successSampleRate < 0.0 || r.successSampleRate > 1.0 {
return errors.New("Error: successSampleRate must be between 0 and 1.")
}

if failureSampleRate < 0.0 || failureSampleRate > 1.0 {
if r.failureSampleRate < 0.0 || r.failureSampleRate > 1.0 {
return errors.New("Error: failureSampleRate must be between 0 and 1.")
}

var samplingRate float64
if success {
samplingRate = successSampleRate
if r.success {
samplingRate = r.successSampleRate
} else {
samplingRate = failureSampleRate
samplingRate = r.failureSampleRate
}
// Generate a random number between 0 and 1
random := rand.Float64()
if random < samplingRate {
// Run your function here
err := sendReport(record, collectorURL)
err := sendReport(r.record, r.collectorURL)
if err != nil {
log.Printf("HTTP request failed: %v", err)
return err
Expand All @@ -92,3 +69,33 @@ func sendReportRandomly(record map[string]interface{}, collectorURL string, succ
return nil
}
}

func sendReport(record interface{}, collectorURL string) error {
jsonData, err := json.Marshal(record)
if err != nil {
log.Printf("Error encoding JSON: %s\n", err)
return err
}

req, err := http.NewRequest("POST", collectorURL, bytes.NewReader(jsonData))
if err != nil {
debugLog.Printf("Error creating the HTTP request: %s\n", err)
return err
}

req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := httpClient.Do(req)
if err != nil {
log.Printf("Error sending the HTTP request: %s\n", err)
return err
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
debugLog.Printf("Error reading the HTTP response body: %s\n", err)
return err
}
debugLog.Printf("Response: %s\n", respBody)
return nil
}
14 changes: 7 additions & 7 deletions x/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ func TestSendReportSuccessfully(t *testing.T) {
"msg": "i/o timeout"
}
}`
var record map[string]interface{}
err := json.Unmarshal([]byte(jsonData), &record)
var testRecord Record
err := json.Unmarshal([]byte(jsonData), &testRecord.record)
if err != nil {
fmt.Println(err)
t.Errorf("Expected no error, but got: %v", err)
}
collectorURL := "example.com"
success := true
successSampleRate := 1.0
failureSampleRate := 0.0
testRecord.collectorURL = "https://example.com"
testRecord.success = true
testRecord.successSampleRate = 1.0
testRecord.failureSampleRate = 0.0

err = sendReportRandomly(record, collectorURL, success, successSampleRate, failureSampleRate)
err = Report(testRecord)

if err != nil {
t.Errorf("Expected no error, but got: %v", err)
Expand Down

0 comments on commit b8f24eb

Please sign in to comment.