Skip to content

Commit

Permalink
feat: Allow to get Probe logs by target (prometheus#1063)
Browse files Browse the repository at this point in the history
* feat: Allow to get Probe logs by target

Signed-off-by: Daniel Ruano <[email protected]>

* Commit for signing

Signed-off-by: Daniel Ruano <[email protected]>

* Use net/http constants instead of hardcoded ints as http codes

Signed-off-by: Daniel Ruano <[email protected]>

* Add unit testing for GetByTarget and GetById methods

Signed-off-by: Daniel Ruano <[email protected]>

* Update main.go

Co-authored-by: Suraj Nath <[email protected]>
Signed-off-by: druanoor <[email protected]>

* Update main.go

Co-authored-by: Suraj Nath <[email protected]>
Signed-off-by: druanoor <[email protected]>

* Update main.go

Co-authored-by: Suraj Nath <[email protected]>
Signed-off-by: druanoor <[email protected]>

* Update main.go

Co-authored-by: Suraj Nath <[email protected]>
Signed-off-by: druanoor <[email protected]>

* Update main.go

Co-authored-by: Suraj Nath <[email protected]>
Signed-off-by: druanoor <[email protected]>

* Fix format

Signed-off-by: Daniel Ruano <[email protected]>

---------

Signed-off-by: Daniel Ruano <[email protected]>
Signed-off-by: druanoor <[email protected]>
Co-authored-by: Suraj Nath <[email protected]>
Signed-off-by: Arash Hatami <[email protected]>
  • Loading branch information
2 people authored and hatamiarash7 committed Aug 27, 2023
1 parent 99b86fc commit eef78c2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 7 deletions.
28 changes: 23 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,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 != "" {
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 @@ -234,7 +252,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 {
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")
}
}

0 comments on commit eef78c2

Please sign in to comment.