This commit is contained in:
		
							parent
							
								
									0a1b9c1305
								
							
						
					
					
						commit
						5dd38b7f18
					
				
					 3 changed files with 42 additions and 1 deletions
				
			
		
							
								
								
									
										2
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
								
							|  | @ -29,7 +29,7 @@ | ||||||
|             "env": { |             "env": { | ||||||
|                 "DUMP_PATH": "../tmp/nodes.json", |                 "DUMP_PATH": "../tmp/nodes.json", | ||||||
|                 "CONFIG_PATH": "../tmp/config.json", |                 "CONFIG_PATH": "../tmp/config.json", | ||||||
|                 "NODE_DIR_PATH": "../tmp/node" |                 "NODE_PATH": "../tmp/node" | ||||||
|             }, |             }, | ||||||
|             "args": [ |             "args": [ | ||||||
|                 "node" |                 "node" | ||||||
|  |  | ||||||
							
								
								
									
										36
									
								
								api/main.go
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								api/main.go
									
									
									
									
									
								
							|  | @ -3,6 +3,7 @@ package api | ||||||
| import ( | import ( | ||||||
| 	"log" | 	"log" | ||||||
| 	"net/http" | 	"net/http" | ||||||
|  | 	"strings" | ||||||
| 
 | 
 | ||||||
| 	_ "gitea.ceperka.net/rosti/lobby2/docs" // This line is necessary for swag to find your docs!
 | 	_ "gitea.ceperka.net/rosti/lobby2/docs" // This line is necessary for swag to find your docs!
 | ||||||
| 	"gitea.ceperka.net/rosti/lobby2/nodes" | 	"gitea.ceperka.net/rosti/lobby2/nodes" | ||||||
|  | @ -53,6 +54,7 @@ func (a *API) Run() error { | ||||||
| 	a.e.GET("/nodes", a.listHandler) | 	a.e.GET("/nodes", a.listHandler) | ||||||
| 	a.e.GET("/nodes/:hostname", a.getHandler) | 	a.e.GET("/nodes/:hostname", a.getHandler) | ||||||
| 	a.e.POST("/nodes/:hostname", a.refreshHandler) | 	a.e.POST("/nodes/:hostname", a.refreshHandler) | ||||||
|  | 	a.e.GET("/prometheus/:service", a.prometheusHandler) | ||||||
| 
 | 
 | ||||||
| 	// Start the server in a goroutine so that it doesn't block the signal listening
 | 	// Start the server in a goroutine so that it doesn't block the signal listening
 | ||||||
| 	return a.e.Start(a.listen) | 	return a.e.Start(a.listen) | ||||||
|  | @ -111,3 +113,37 @@ func (a *API) refreshHandler(c echo.Context) error { | ||||||
| 
 | 
 | ||||||
| 	return c.NoContent(http.StatusNoContent) | 	return c.NoContent(http.StatusNoContent) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | // @Summary Prometheus service discovery
 | ||||||
|  | // @Description Return one nodes based on given hostname
 | ||||||
|  | // @Produce  application/json
 | ||||||
|  | // @Success 200 {array} []prometheusDiscovery "Node details"
 | ||||||
|  | // @Failure 401 {object} Message "Forbidden access"
 | ||||||
|  | // @Security Bearer
 | ||||||
|  | // @Router /nodes/{hostname} [get]
 | ||||||
|  | func (a *API) prometheusHandler(c echo.Context) error { | ||||||
|  | 	ss := c.QueryParam("service") | ||||||
|  | 
 | ||||||
|  | 	ns := a.np.List() | ||||||
|  | 
 | ||||||
|  | 	pds := []prometheusDiscovery{ | ||||||
|  | 		{ | ||||||
|  | 			Labels:  nodes.Labels{}, | ||||||
|  | 			Targets: []string{}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	for _, node := range ns { | ||||||
|  | 		v, ok := node.KV["prometheus_exporters"] | ||||||
|  | 		if ok { | ||||||
|  | 			services := strings.Split(v, ",") | ||||||
|  | 			for _, service := range services { | ||||||
|  | 				if ss == service { | ||||||
|  | 					pds[0].Targets = append(pds[0].Targets, node.HostName) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return c.JSONPretty(http.StatusOK, pds, "  ") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -10,3 +10,8 @@ type params struct { | ||||||
| 	Labels nodes.Labels `json:"labels"` | 	Labels nodes.Labels `json:"labels"` | ||||||
| 	KV     nodes.KV     `json:"kv"` | 	KV     nodes.KV     `json:"kv"` | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | type prometheusDiscovery struct { | ||||||
|  | 	Labels  nodes.Labels `json:"labels"` | ||||||
|  | 	Targets []string     `json:"targets"` | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue