-
Notifications
You must be signed in to change notification settings - Fork 487
/
http.go
84 lines (73 loc) · 2.7 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package logs
import (
"net/http"
"sort"
"github.com/go-kit/log/level"
"github.com/gorilla/mux"
"github.com/grafana/agent/pkg/metrics/cluster/configapi"
"github.com/grafana/loki/clients/pkg/promtail/targets/target"
"github.com/prometheus/common/model"
)
// WireAPI adds API routes to the provided mux router.
func (l *Logs) WireAPI(r *mux.Router) {
r.HandleFunc("/agent/api/v1/logs/instances", l.ListInstancesHandler).Methods("GET")
r.HandleFunc("/agent/api/v1/logs/targets", l.ListTargetsHandler).Methods("GET")
}
// ListInstancesHandler writes the set of currently running instances to the http.ResponseWriter.
func (l *Logs) ListInstancesHandler(w http.ResponseWriter, _ *http.Request) {
instances := l.instances
instanceNames := make([]string, 0, len(instances))
for instance := range instances {
instanceNames = append(instanceNames, instance)
}
sort.Strings(instanceNames)
err := configapi.WriteResponse(w, http.StatusOK, instanceNames)
if err != nil {
level.Error(l.l).Log("msg", "failed to write response", "err", err)
}
}
// ListTargetsHandler retrieves the full set of targets across all instances and shows
// information on them.
func (l *Logs) ListTargetsHandler(w http.ResponseWriter, r *http.Request) {
instances := l.instances
allTagets := make(map[string]TargetSet, len(instances))
for instName, inst := range instances {
allTagets[instName] = inst.promtail.ActiveTargets()
}
listTargetsHandler(allTagets).ServeHTTP(w, r)
}
func listTargetsHandler(targets map[string]TargetSet) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
resp := ListTargetsResponse{}
for instance, tset := range targets {
for key, targets := range tset {
for _, tgt := range targets {
resp = append(resp, TargetInfo{
InstanceName: instance,
TargetGroup: key,
Type: tgt.Type(),
DiscoveredLabels: tgt.DiscoveredLabels(),
Labels: tgt.Labels(),
Ready: tgt.Ready(),
Details: tgt.Details(),
})
}
}
}
_ = configapi.WriteResponse(rw, http.StatusOK, resp)
})
}
// TargetSet is a set of targets for an individual scraper.
type TargetSet map[string][]target.Target
// ListTargetsResponse is returned by the ListTargetsHandler.
type ListTargetsResponse []TargetInfo
// TargetInfo describes a specific target.
type TargetInfo struct {
InstanceName string `json:"instance"`
TargetGroup string `json:"target_group"`
Type target.TargetType `json:"type"`
Labels model.LabelSet `json:"labels"`
DiscoveredLabels model.LabelSet `json:"discovered_labels"`
Ready bool `json:"ready"`
Details interface{} `json:"details"`
}