-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reporter package #116
Merged
Merged
Reporter package #116
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8f9b09c
feat: add reporter package
amircybersec 7d58f75
initializing http client only once
amircybersec 91be76f
test: add one test
amircybersec b8f24eb
refactor: data encapsulation for log Record
amircybersec 1b3eadd
fix: change error type to any
amircybersec deedd11
refactor: some encapsulation wip
amircybersec 6708954
define interface and methods for reporter
amircybersec bce8a31
feat: define collector interface and implement it
amircybersec f674228
refactor: fallback and retry collector + context
amircybersec b9beb9f
test: httptest
amircybersec 9716758
enhance: docs + clean up
amircybersec ad23b7a
improved docs
amircybersec ebb9a59
fixed types
amircybersec af5201f
improve docs
amircybersec e934f4e
fix: marshal error
amircybersec 571c675
fix: moved connectivityreport to test
amircybersec 6725d28
initialDelay with exponential backoff
amircybersec 7a5d1c2
removed multiplecollector
amircybersec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,101 @@ | ||||
// Copyright 2023 Jigsaw Operations LLC | ||||
// | ||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||
// you may not use this file except in compliance with the License. | ||||
// You may obtain a copy of the License at | ||||
// | ||||
// https://www.apache.org/licenses/LICENSE-2.0 | ||||
// | ||||
// Unless required by applicable law or agreed to in writing, software | ||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
// See the License for the specific language governing permissions and | ||||
// limitations under the License. | ||||
|
||||
package reporter | ||||
|
||||
import ( | ||||
"bytes" | ||||
"encoding/json" | ||||
"errors" | ||||
"fmt" | ||||
"io" | ||||
"log" | ||||
"math/rand" | ||||
"net/http" | ||||
) | ||||
|
||||
var debugLog log.Logger = *log.New(io.Discard, "", 0) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remove all logging from this library.
Suggested change
|
||||
var httpClient = &http.Client{} | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass the client to the RemoteReporter
Suggested change
|
||||
|
||||
type Record struct { | ||||
collectorURL string | ||||
successSampleRate float64 | ||||
failureSampleRate float64 | ||||
success bool | ||||
record interface{} | ||||
} | ||||
amircybersec marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
func Report(r Record) error { | ||||
|
||||
// Perform custom range validation for sampling rate | ||||
if r.successSampleRate < 0.0 || r.successSampleRate > 1.0 { | ||||
return errors.New("Error: successSampleRate must be between 0 and 1.") | ||||
} | ||||
|
||||
if r.failureSampleRate < 0.0 || r.failureSampleRate > 1.0 { | ||||
return errors.New("Error: failureSampleRate must be between 0 and 1.") | ||||
} | ||||
|
||||
var samplingRate float64 | ||||
if r.success { | ||||
samplingRate = r.successSampleRate | ||||
} else { | ||||
samplingRate = r.failureSampleRate | ||||
} | ||||
// Generate a random number between 0 and 1 | ||||
random := rand.Float64() | ||||
if random < samplingRate { | ||||
err := sendReport(r.record, r.collectorURL) | ||||
if err != nil { | ||||
log.Printf("HTTP request failed: %v", err) | ||||
return err | ||||
} else { | ||||
fmt.Println("Report sent") | ||||
return nil | ||||
} | ||||
} else { | ||||
fmt.Println("Report was not sent this time") | ||||
return nil | ||||
} | ||||
} | ||||
|
||||
func sendReport(record any, 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) | ||||
amircybersec marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
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 | ||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2023 Jigsaw Operations LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package reporter | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
// When success is true and random number is less than successSampleRate, report is sent successfully | ||
func TestSendReportSuccessfully(t *testing.T) { | ||
// Example JSON data | ||
jsonData := `{ | ||
"proxy": "192.168.1.1:65000", | ||
"resolver": "8.8.8.8:53", | ||
"proto": "tcp", | ||
"prefix": "HTTP1/1", | ||
"time": "2021-01-01T00:00:00Z", | ||
"durationMs": 100, | ||
"error": { | ||
"operation": "read", | ||
"posixError": "ETIMEDOUT", | ||
"msg": "i/o timeout" | ||
} | ||
}` | ||
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) | ||
} | ||
testRecord.collectorURL = "https://example.com" | ||
testRecord.success = true | ||
testRecord.successSampleRate = 1.0 | ||
testRecord.failureSampleRate = 0.0 | ||
|
||
err = Report(testRecord) | ||
|
||
if err != nil { | ||
t.Errorf("Expected no error, but got: %v", err) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.