From 66f27bc3fd78cbb9f814e1931296a457009d7462 Mon Sep 17 00:00:00 2001 From: Daniel Ruano Date: Thu, 4 May 2023 16:07:24 +0200 Subject: [PATCH 01/10] feat: Allow to get Probe logs by target Signed-off-by: Daniel Ruano --- main.go | 27 ++++++++++++++++++++++----- prober/history.go | 23 +++++++++++++++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index f3e09e1c..859ac698 100644 --- a/main.go +++ b/main.go @@ -212,14 +212,31 @@ func run() int { http.HandleFunc(path.Join(*routePrefix, "/logs"), func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.URL.Query().Get("id"), 10, 64) if err != nil { - http.Error(w, "Invalid probe id", 500) - return + id = -1 } - result := rh.Get(id) - if result == nil { - http.Error(w, "Probe id not found", 404) + target := r.URL.Query().Get("target") + result := new(prober.Result) + if err == nil && target != "" { + http.Error(w, "Probe id and target can't be defined at the same time", 500) + return + } else if id == -1 && target == "" { + http.Error(w, "Probe id or target must be defined as http query parameters", 500) return } + if target != "" { + result = rh.GetByTarget(target) + if result == nil { + http.Error(w, "Probe target not found", 404) + return + } + } else { + result = rh.GetById(id) + if result == nil { + http.Error(w, "Probe id not found", 404) + return + } + } + w.Header().Set("Content-Type", "text/plain") w.Write([]byte(result.DebugOutput)) }) diff --git a/prober/history.go b/prober/history.go index 306eb419..c5009c2f 100644 --- a/prober/history.go +++ b/prober/history.go @@ -78,8 +78,8 @@ func (rh *ResultHistory) List() []*Result { return append(rh.preservedFailedResults[:], rh.results...) } -// Get returns a given result. -func (rh *ResultHistory) Get(id int64) *Result { +// Get returns a given result by id. +func (rh *ResultHistory) GetById(id int64) *Result { rh.mu.Lock() defer rh.mu.Unlock() @@ -96,3 +96,22 @@ func (rh *ResultHistory) Get(id int64) *Result { return nil } + +// Get returns a given result by url. +func (rh *ResultHistory) GetByTarget(target string) *Result { + rh.mu.Lock() + defer rh.mu.Unlock() + + for _, r := range rh.preservedFailedResults { + if r.Target == target { + return r + } + } + for _, r := range rh.results { + if r.Target == target { + return r + } + } + + return nil +} From 514819888dfce8673d555239bc3112f16021f4cf Mon Sep 17 00:00:00 2001 From: Daniel Ruano Date: Thu, 4 May 2023 16:12:05 +0200 Subject: [PATCH 02/10] Commit for signing Signed-off-by: Daniel Ruano From 27ec99baa9ed076504d2d62afbd5e4747c5b7a9c Mon Sep 17 00:00:00 2001 From: Daniel Ruano Date: Tue, 16 May 2023 13:01:29 +0200 Subject: [PATCH 03/10] Use net/http constants instead of hardcoded ints as http codes Signed-off-by: Daniel Ruano --- main.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 859ac698..5fb79041 100644 --- a/main.go +++ b/main.go @@ -217,22 +217,22 @@ func run() int { target := r.URL.Query().Get("target") result := new(prober.Result) if err == nil && target != "" { - http.Error(w, "Probe id and target can't be defined at the same time", 500) + http.Error(w, "Probe id and target can't be defined at the same time", http.StatusInternalServerError) return } else if id == -1 && target == "" { - http.Error(w, "Probe id or target must be defined as http query parameters", 500) + http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusInternalServerError) return } if target != "" { result = rh.GetByTarget(target) if result == nil { - http.Error(w, "Probe target not found", 404) + http.Error(w, "Probe target not found", http.StatusNotFound) return } } else { result = rh.GetById(id) if result == nil { - http.Error(w, "Probe id not found", 404) + http.Error(w, "Probe id not found", http.StatusNotFound) return } } @@ -247,7 +247,7 @@ func run() int { sc.RUnlock() if err != nil { level.Warn(logger).Log("msg", "Error marshalling configuration", "err", err) - http.Error(w, err.Error(), 500) + http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/plain") From 9cd2ca242a82002e6d9adae8ba52e6331b1969bb Mon Sep 17 00:00:00 2001 From: Daniel Ruano Date: Thu, 18 May 2023 12:55:48 +0200 Subject: [PATCH 04/10] Add unit testing for GetByTarget and GetById methods Signed-off-by: Daniel Ruano --- prober/history_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/prober/history_test.go b/prober/history_test.go index b964c537..6ab9cdc1 100644 --- a/prober/history_test.go +++ b/prober/history_test.go @@ -79,3 +79,65 @@ func TestHistoryPreservesExpiredFailedResults(t *testing.T) { } } } + +func TestHistoryGetById(t *testing.T) { + history := &ResultHistory{MaxResults: 2} + + history.Add("module", "target-0", fmt.Sprintf("result %d", history.nextId), true) + history.Add("module", "target-1", fmt.Sprintf("result %d", history.nextId), false) + + // Get a Result object for a target that exists + resultTrue := history.GetById(0) + if resultTrue == nil { + t.Errorf("Error finding the result in history by id for id: 1") + } else { + if resultTrue.Id != 0 { + t.Errorf("Error finding the result in history by id: expected \"%d\" and got \"%d\"", 0, resultTrue.Id) + } + } + + resultFalse := history.GetById(1) + if resultFalse == nil { + t.Errorf("Error finding the result in history by id for id: 1") + } else { + if resultFalse.Id != 1 { + t.Errorf("Error finding the result in history by id: expected \"%d\" and got \"%d\"", 1, resultFalse.Id) + } + } + + // Get a Result object for a target that doesn't exist + if history.GetById(5) != nil { + t.Errorf("Error finding the result in history by id for id: 5") + } +} + +func TestHistoryGetByTarget(t *testing.T) { + history := &ResultHistory{MaxResults: 2} + + history.Add("module", "target-0", fmt.Sprintf("result %d", history.nextId), true) + history.Add("module", "target-1", fmt.Sprintf("result %d", history.nextId), false) + + // Get a Result object for a target that exists + resultTrue := history.GetByTarget("target-0") + if resultTrue == nil { + t.Errorf("Error finding the result in history by target for target-0") + } else { + if resultTrue.Target != "target-0" { + t.Errorf("Error finding the result in history by target for target: expected \"%s\" and got \"%s\"", "target-0", resultTrue.Target) + } + } + + 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) + } + } + + // Get a Result object for a target that doesn't exist + if history.GetByTarget("target-5") != nil { + t.Errorf("Error finding the result in history by target for target-5") + } +} From dd608efca13c347ff5c70a6f652fd288b105ece3 Mon Sep 17 00:00:00 2001 From: druanoor <51286702+druanoor@users.noreply.github.com> Date: Fri, 7 Jul 2023 10:29:53 +0200 Subject: [PATCH 05/10] Update main.go Co-authored-by: Suraj Nath <9503187+electron0zero@users.noreply.github.com> Signed-off-by: druanoor <51286702+druanoor@users.noreply.github.com> --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 5fb79041..582f2467 100644 --- a/main.go +++ b/main.go @@ -220,7 +220,7 @@ func run() int { http.Error(w, "Probe id and target can't be defined at the same time", http.StatusInternalServerError) return } else if id == -1 && target == "" { - http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusInternalServerError) + http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusBadRequest) return } if target != "" { From 58016e05ba5feba3844cf26e3f082091c3cbff49 Mon Sep 17 00:00:00 2001 From: druanoor <51286702+druanoor@users.noreply.github.com> Date: Fri, 7 Jul 2023 10:30:00 +0200 Subject: [PATCH 06/10] Update main.go Co-authored-by: Suraj Nath <9503187+electron0zero@users.noreply.github.com> Signed-off-by: druanoor <51286702+druanoor@users.noreply.github.com> --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 582f2467..ead32cbf 100644 --- a/main.go +++ b/main.go @@ -219,7 +219,8 @@ func run() int { if err == nil && target != "" { http.Error(w, "Probe id and target can't be defined at the same time", http.StatusInternalServerError) return - } else if id == -1 && target == "" { + } + if id == -1 && target == "" { http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusBadRequest) return } From cc301f864bb4eb697e100942d26f31ee656a0d0e Mon Sep 17 00:00:00 2001 From: druanoor <51286702+druanoor@users.noreply.github.com> Date: Fri, 7 Jul 2023 10:30:07 +0200 Subject: [PATCH 07/10] Update main.go Co-authored-by: Suraj Nath <9503187+electron0zero@users.noreply.github.com> Signed-off-by: druanoor <51286702+druanoor@users.noreply.github.com> --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index ead32cbf..71caf7ae 100644 --- a/main.go +++ b/main.go @@ -217,7 +217,7 @@ func run() int { target := r.URL.Query().Get("target") result := new(prober.Result) if err == nil && target != "" { - http.Error(w, "Probe id and target can't be defined at the same time", http.StatusInternalServerError) + http.Error(w, "Probe id and target can't be defined at the same time", http.StatusBadRequest) return } if id == -1 && target == "" { From 698f90c2e87b2293ad28d7b0303c0d0f0451ec26 Mon Sep 17 00:00:00 2001 From: druanoor <51286702+druanoor@users.noreply.github.com> Date: Fri, 7 Jul 2023 10:30:26 +0200 Subject: [PATCH 08/10] Update main.go Co-authored-by: Suraj Nath <9503187+electron0zero@users.noreply.github.com> Signed-off-by: druanoor <51286702+druanoor@users.noreply.github.com> --- main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/main.go b/main.go index 71caf7ae..02aade44 100644 --- a/main.go +++ b/main.go @@ -215,7 +215,6 @@ func run() int { id = -1 } target := r.URL.Query().Get("target") - result := new(prober.Result) if err == nil && target != "" { http.Error(w, "Probe id and target can't be defined at the same time", http.StatusBadRequest) return From d35a7b592722cc5ba0c8b84c1e32212af96d9845 Mon Sep 17 00:00:00 2001 From: druanoor <51286702+druanoor@users.noreply.github.com> Date: Fri, 7 Jul 2023 10:30:31 +0200 Subject: [PATCH 09/10] Update main.go Co-authored-by: Suraj Nath <9503187+electron0zero@users.noreply.github.com> Signed-off-by: druanoor <51286702+druanoor@users.noreply.github.com> --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 02aade44..2b95d781 100644 --- a/main.go +++ b/main.go @@ -223,7 +223,8 @@ func run() int { http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusBadRequest) return } - if target != "" { + result := new(prober.Result) + if target != "" { result = rh.GetByTarget(target) if result == nil { http.Error(w, "Probe target not found", http.StatusNotFound) From a18806efd97912c6e27d2a8c9f8713800180fa91 Mon Sep 17 00:00:00 2001 From: Daniel Ruano Date: Fri, 7 Jul 2023 10:49:00 +0200 Subject: [PATCH 10/10] Fix format Signed-off-by: Daniel Ruano --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 2b95d781..912084ed 100644 --- a/main.go +++ b/main.go @@ -224,7 +224,7 @@ func run() int { return } result := new(prober.Result) - if target != "" { + if target != "" { result = rh.GetByTarget(target) if result == nil { http.Error(w, "Probe target not found", http.StatusNotFound)