Skip to content

Commit

Permalink
feat: Add optional /logs?module= filter paramater
Browse files Browse the repository at this point in the history
This allows to uniquely identify a probe by target and module.

Previously with the `?target=` selector only,  it was impossible to
select the correct probe log, when multiple modules had probed a target.

Signed-off-by: Norwin Roosen <[email protected]>
  • Loading branch information
noerw committed Jun 18, 2024
1 parent 909dd33 commit dabf913
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,14 @@ func run() int {
http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusBadRequest)
return
}
module := r.URL.Query().Get("module")
if target == "" && module != "" {
http.Error(w, "Probe module filter only works in conjunction with target parameter", http.StatusBadRequest)
return
}
result := new(prober.Result)
if target != "" {
result = rh.GetByTarget(target)
result = rh.GetByTarget(target, module)
if result == nil {
http.Error(w, "Probe target not found", http.StatusNotFound)
return
Expand Down
8 changes: 4 additions & 4 deletions prober/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ func (rh *ResultHistory) GetById(id int64) *Result {
return nil
}

// Get returns a given result by url.
func (rh *ResultHistory) GetByTarget(target string) *Result {
// Get returns a given result by url, optionally filtered by a module name.
func (rh *ResultHistory) GetByTarget(target string, module string) *Result {
rh.mu.Lock()
defer rh.mu.Unlock()

for _, r := range rh.preservedFailedResults {
if r.Target == target {
if r.Target == target && (module == "" || module == r.ModuleName) {
return r
}
}
for _, r := range rh.results {
if r.Target == target {
if r.Target == target && (module == "" || module == r.ModuleName) {
return r
}
}
Expand Down
33 changes: 27 additions & 6 deletions prober/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ func TestHistoryGetById(t *testing.T) {
}

func TestHistoryGetByTarget(t *testing.T) {
history := &ResultHistory{MaxResults: 2}
history := &ResultHistory{MaxResults: 3}

history.Add("module", "target-0", fmt.Sprintf("result %d", history.nextId), true)
history.Add("module", "target-1", fmt.Sprintf("result %d", history.nextId), false)
history.Add("module-0", "target-0", fmt.Sprintf("result %d", history.nextId), true)
history.Add("module-1", "target-1", fmt.Sprintf("result %d", history.nextId), false)
history.Add("module-0", "target-1", fmt.Sprintf("result %d", history.nextId), false)

// Get a Result object for a target that exists
resultTrue := history.GetByTarget("target-0")
resultTrue := history.GetByTarget("target-0", "")
if resultTrue == nil {
t.Errorf("Error finding the result in history by target for target-0")
} else {
Expand All @@ -127,17 +128,37 @@ func TestHistoryGetByTarget(t *testing.T) {
}
}

resultFalse := history.GetByTarget("target-1")
resultFalse := history.GetByTarget("target-1", "")
if resultFalse == nil {
t.Errorf("Error finding the result in history by target for target-1")
} else {
if resultFalse.Target != "target-1" {
t.Errorf("Error finding the result in history by target for target: expected \"%s\" and got \"%s\"", "target-1", resultFalse.Target)
}
if resultFalse.ModuleName != "module-1" {
t.Errorf("Error finding the result in history by target for target: expected \"%s\" and got \"%s\"", "module-1", resultFalse.ModuleName)
}
}

// Get a Result object for a target that doesn't exist
if history.GetByTarget("target-5") != nil {
if history.GetByTarget("target-5", "") != nil {
t.Errorf("Error finding the result in history by target for target-5")
}

// Get a result by existing target and non-matching module
if history.GetByTarget("target-1", "module-5") != nil {
t.Errorf("Incorrectly found a result in history by target for [target-1,module-5]")
}

// Get a result by existing target and matching module
if result := history.GetByTarget("target-1", "module-1"); result == nil {
t.Errorf("Incorrectly found no result in history by target for [target-1,module-1]")
} else {
if result.Target != "target-1" {
t.Errorf("Error finding the result in history by target and module for target: expected \"%s\" and got \"%s\"", "target-1", result.Target)
}
if result.ModuleName != "module-1" {
t.Errorf("Error finding the result in history by target and module for target: expected \"%s\" and got \"%s\"", "module-1", result.ModuleName)
}
}
}

0 comments on commit dabf913

Please sign in to comment.