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