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 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
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,32 @@ 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)
id = -1
}
target := r.URL.Query().Get("target")
if err == nil && target != "" {
http.Error(w, "Probe id and target can't be defined at the same time", http.StatusBadRequest)
return
}
result := rh.Get(id)
if result == nil {
http.Error(w, "Probe id not found", 404)
if id == -1 && target == "" {
http.Error(w, "Probe id or target must be defined as http query parameters", http.StatusBadRequest)
return
}
result := new(prober.Result)
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", http.StatusNotFound)
return
}
} else {
result = rh.GetById(id)
if result == nil {
http.Error(w, "Probe id not found", http.StatusNotFound)
return
}
}

w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(result.DebugOutput))
})
Expand All @@ -230,7 +248,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")
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
}
62 changes: 62 additions & 0 deletions prober/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}