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

feat: Allow to get Probe logs by target #1063

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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)
druanoor marked this conversation as resolved.
Show resolved Hide resolved
if err == nil && target != "" {
http.Error(w, "Probe id and target can't be defined at the same time", 500)
druanoor marked this conversation as resolved.
Show resolved Hide resolved
return
} else if id == -1 && target == "" {
druanoor marked this conversation as resolved.
Show resolved Hide resolved
http.Error(w, "Probe id or target must be defined as http query parameters", 500)
return
}
if target != "" {
druanoor marked this conversation as resolved.
Show resolved Hide resolved
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 {
electron0zero marked this conversation as resolved.
Show resolved Hide resolved
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
}