Skip to content

Commit 4f0f1f6

Browse files
committed
feat: Add optional /logs?module= filter parameter
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]>
1 parent 909dd33 commit 4f0f1f6

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,14 @@ func run() int {
232232
http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusBadRequest)
233233
return
234234
}
235+
module := r.URL.Query().Get("module")
236+
if target == "" && module != "" {
237+
http.Error(w, "Probe module filter only works in conjunction with target parameter", http.StatusBadRequest)
238+
return
239+
}
235240
result := new(prober.Result)
236241
if target != "" {
237-
result = rh.GetByTarget(target)
242+
result = rh.GetByTarget(target, module)
238243
if result == nil {
239244
http.Error(w, "Probe target not found", http.StatusNotFound)
240245
return

prober/history.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,18 @@ func (rh *ResultHistory) GetById(id int64) *Result {
9797
return nil
9898
}
9999

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

105105
for _, r := range rh.preservedFailedResults {
106-
if r.Target == target {
106+
if r.Target == target && (module == "" || module == r.ModuleName) {
107107
return r
108108
}
109109
}
110110
for _, r := range rh.results {
111-
if r.Target == target {
111+
if r.Target == target && (module == "" || module == r.ModuleName) {
112112
return r
113113
}
114114
}

prober/history_test.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ func TestHistoryGetById(t *testing.T) {
112112
}
113113

114114
func TestHistoryGetByTarget(t *testing.T) {
115-
history := &ResultHistory{MaxResults: 2}
115+
history := &ResultHistory{MaxResults: 3}
116116

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

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

130-
resultFalse := history.GetByTarget("target-1")
131+
resultFalse := history.GetByTarget("target-1", "")
131132
if resultFalse == nil {
132133
t.Errorf("Error finding the result in history by target for target-1")
133134
} else {
134135
if resultFalse.Target != "target-1" {
135136
t.Errorf("Error finding the result in history by target for target: expected \"%s\" and got \"%s\"", "target-1", resultFalse.Target)
136137
}
138+
if resultFalse.ModuleName != "module-1" {
139+
t.Errorf("Error finding the result in history by target for target: expected \"%s\" and got \"%s\"", "module-1", resultFalse.ModuleName)
140+
}
137141
}
138142

139143
// Get a Result object for a target that doesn't exist
140-
if history.GetByTarget("target-5") != nil {
144+
if history.GetByTarget("target-5", "") != nil {
141145
t.Errorf("Error finding the result in history by target for target-5")
142146
}
147+
148+
// Get a result by existing target and non-matching module
149+
if history.GetByTarget("target-1", "module-5") != nil {
150+
t.Errorf("Incorrectly found a result in history by target for [target-1,module-5]")
151+
}
152+
153+
// Get a result by existing target and matching module
154+
if result := history.GetByTarget("target-1", "module-1"); result == nil {
155+
t.Errorf("Incorrectly found no result in history by target for [target-1,module-1]")
156+
} else {
157+
if result.Target != "target-1" {
158+
t.Errorf("Error finding the result in history by target and module for target: expected \"%s\" and got \"%s\"", "target-1", result.Target)
159+
}
160+
if result.ModuleName != "module-1" {
161+
t.Errorf("Error finding the result in history by target and module for target: expected \"%s\" and got \"%s\"", "module-1", result.ModuleName)
162+
}
163+
}
143164
}

0 commit comments

Comments
 (0)