Skip to content
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

Fix retrieving probe logs by target name when probed by multiple modules #1257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 5 additions & 5 deletions prober/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (rh *ResultHistory) List() []*Result {
return append(rh.preservedFailedResults[:], rh.results...)
}

// Get returns a given result by id.
// GetById returns a given result by id.
func (rh *ResultHistory) GetById(id int64) *Result {
rh.mu.Lock()
defer rh.mu.Unlock()
Expand All @@ -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 {
// GetByTarget 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)
}
}
}