Skip to content

Commit

Permalink
feat: Allow to get Probe logs by target
Browse files Browse the repository at this point in the history
  • Loading branch information
druanoor committed May 4, 2023
1 parent ae8455d commit 8bfc30c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
23 changes: 21 additions & 2 deletions prober/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}

0 comments on commit 8bfc30c

Please sign in to comment.