@@ -18,19 +18,19 @@ import (
18
18
"github.com/grafana/alloy/internal/service"
19
19
"github.com/grafana/alloy/internal/service/cluster"
20
20
"github.com/grafana/alloy/internal/service/livedebugging"
21
+ "github.com/grafana/alloy/internal/service/remotecfg"
21
22
"github.com/prometheus/prometheus/util/httputil"
22
23
)
23
24
24
25
// AlloyAPI is a wrapper around the component API.
25
26
type AlloyAPI struct {
26
27
alloy service.Host
27
- remotecfg service.Host
28
28
CallbackManager livedebugging.CallbackManager
29
29
}
30
30
31
31
// NewAlloyAPI instantiates a new Alloy API.
32
- func NewAlloyAPI (alloy , remotecfg service.Host , CallbackManager livedebugging.CallbackManager ) * AlloyAPI {
33
- return & AlloyAPI {alloy : alloy , remotecfg : remotecfg , CallbackManager : CallbackManager }
32
+ func NewAlloyAPI (alloy service.Host , CallbackManager livedebugging.CallbackManager ) * AlloyAPI {
33
+ return & AlloyAPI {alloy : alloy , CallbackManager : CallbackManager }
34
34
}
35
35
36
36
// RegisterRoutes registers all the API's routes.
@@ -40,67 +40,99 @@ func (a *AlloyAPI) RegisterRoutes(urlPrefix string, r *mux.Router) {
40
40
// component IDs.
41
41
42
42
r .Handle (path .Join (urlPrefix , "/modules/{moduleID:.+}/components" ), httputil.CompressionHandler {Handler : listComponentsHandler (a .alloy )})
43
- r .Handle (path .Join (urlPrefix , "/remotecfg/modules/{moduleID:.+}/components" ), httputil.CompressionHandler {Handler : listComponentsHandler (a .remotecfg )})
43
+ r .Handle (path .Join (urlPrefix , "/remotecfg/modules/{moduleID:.+}/components" ), httputil.CompressionHandler {Handler : listComponentsHandlerRemoteCfg (a .alloy )})
44
44
45
45
r .Handle (path .Join (urlPrefix , "/components" ), httputil.CompressionHandler {Handler : listComponentsHandler (a .alloy )})
46
- r .Handle (path .Join (urlPrefix , "/remotecfg/components" ), httputil.CompressionHandler {Handler : listComponentsHandler (a .remotecfg )})
46
+ r .Handle (path .Join (urlPrefix , "/remotecfg/components" ), httputil.CompressionHandler {Handler : listComponentsHandlerRemoteCfg (a .alloy )})
47
47
48
48
r .Handle (path .Join (urlPrefix , "/components/{id:.+}" ), httputil.CompressionHandler {Handler : getComponentHandler (a .alloy )})
49
- r .Handle (path .Join (urlPrefix , "/remotecfg/components/{id:.+}" ), httputil.CompressionHandler {Handler : getComponentHandler (a .remotecfg )})
49
+ r .Handle (path .Join (urlPrefix , "/remotecfg/components/{id:.+}" ), httputil.CompressionHandler {Handler : getComponentHandlerRemoteCfg (a .alloy )})
50
50
51
51
r .Handle (path .Join (urlPrefix , "/peers" ), httputil.CompressionHandler {Handler : getClusteringPeersHandler (a .alloy )})
52
52
r .Handle (path .Join (urlPrefix , "/debug/{id:.+}" ), liveDebugging (a .alloy , a .CallbackManager ))
53
53
}
54
54
55
55
func listComponentsHandler (host service.Host ) http.HandlerFunc {
56
56
return func (w http.ResponseWriter , r * http.Request ) {
57
- // moduleID is set from the /modules/{moduleID:.+}/components route above
58
- // but not from the /components route.
59
- var moduleID string
60
- if vars := mux .Vars (r ); vars != nil {
61
- moduleID = vars ["moduleID" ]
62
- }
57
+ listComponentsHandlerInternal (host , w , r )
58
+ }
59
+ }
63
60
64
- components , err := host . ListComponents ( moduleID , component. InfoOptions {
65
- GetHealth : true ,
66
- } )
67
- if err != nil {
68
- http .Error (w , err . Error () , http .StatusInternalServerError )
61
+ func listComponentsHandlerRemoteCfg ( host service. Host ) http. HandlerFunc {
62
+ return func ( w http. ResponseWriter , r * http. Request ) {
63
+ svc , found := host . GetService ( remotecfg . ServiceName )
64
+ if ! found {
65
+ http .Error (w , "remote config service not available" , http .StatusInternalServerError )
69
66
return
70
67
}
71
68
72
- bb , err := json .Marshal (components )
73
- if err != nil {
74
- http .Error (w , err .Error (), http .StatusInternalServerError )
75
- return
76
- }
77
- _ , _ = w .Write (bb )
69
+ listComponentsHandlerInternal (svc .Data ().(remotecfg.Data ).Host , w , r )
78
70
}
79
71
}
80
72
73
+ func listComponentsHandlerInternal (host service.Host , w http.ResponseWriter , r * http.Request ) {
74
+ // moduleID is set from the /modules/{moduleID:.+}/components route above
75
+ // but not from the /components route.
76
+ var moduleID string
77
+ if vars := mux .Vars (r ); vars != nil {
78
+ moduleID = vars ["moduleID" ]
79
+ }
80
+
81
+ components , err := host .ListComponents (moduleID , component.InfoOptions {
82
+ GetHealth : true ,
83
+ })
84
+ if err != nil {
85
+ http .Error (w , err .Error (), http .StatusInternalServerError )
86
+ return
87
+ }
88
+
89
+ bb , err := json .Marshal (components )
90
+ if err != nil {
91
+ http .Error (w , err .Error (), http .StatusInternalServerError )
92
+ return
93
+ }
94
+ _ , _ = w .Write (bb )
95
+ }
96
+
81
97
func getComponentHandler (host service.Host ) http.HandlerFunc {
82
98
return func (w http.ResponseWriter , r * http.Request ) {
83
- vars := mux .Vars (r )
84
- requestedComponent := component .ParseID (vars ["id" ])
99
+ getComponentHandlerInternal (host , w , r )
100
+ }
101
+ }
85
102
86
- component , err := host .GetComponent (requestedComponent , component.InfoOptions {
87
- GetHealth : true ,
88
- GetArguments : true ,
89
- GetExports : true ,
90
- GetDebugInfo : true ,
91
- })
92
- if err != nil {
93
- http .NotFound (w , r )
103
+ func getComponentHandlerRemoteCfg (host service.Host ) http.HandlerFunc {
104
+ return func (w http.ResponseWriter , r * http.Request ) {
105
+ svc , found := host .GetService (remotecfg .ServiceName )
106
+ if ! found {
107
+ http .Error (w , "remote config service not available" , http .StatusInternalServerError )
94
108
return
95
109
}
96
110
97
- bb , err := json .Marshal (component )
98
- if err != nil {
99
- http .Error (w , err .Error (), http .StatusInternalServerError )
100
- return
101
- }
102
- _ , _ = w .Write (bb )
111
+ getComponentHandlerInternal (svc .Data ().(remotecfg.Data ).Host , w , r )
112
+ }
113
+ }
114
+
115
+ func getComponentHandlerInternal (host service.Host , w http.ResponseWriter , r * http.Request ) {
116
+ vars := mux .Vars (r )
117
+ requestedComponent := component .ParseID (vars ["id" ])
118
+
119
+ component , err := host .GetComponent (requestedComponent , component.InfoOptions {
120
+ GetHealth : true ,
121
+ GetArguments : true ,
122
+ GetExports : true ,
123
+ GetDebugInfo : true ,
124
+ })
125
+ if err != nil {
126
+ http .NotFound (w , r )
127
+ return
128
+ }
129
+
130
+ bb , err := json .Marshal (component )
131
+ if err != nil {
132
+ http .Error (w , err .Error (), http .StatusInternalServerError )
133
+ return
103
134
}
135
+ _ , _ = w .Write (bb )
104
136
}
105
137
106
138
func getClusteringPeersHandler (host service.Host ) http.HandlerFunc {
0 commit comments