-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add node exporter proxy feature
- Loading branch information
1 parent
5bea6f7
commit d3c13b7
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package exporter | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
"time" | ||
"context" | ||
"net/http" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
|
||
/* | ||
* --- Node Exporter Metric Handler --- | ||
*/ | ||
func ExporterMetricsHandler(service ExporterService) fiber.Handler { | ||
return func(c *fiber.Ctx) error { | ||
ctx, cancel := context.WithTimeout(c.Context(), 120 * time.Second) | ||
defer cancel() | ||
|
||
ch := make(chan string, 1) | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
|
||
go func() { | ||
defer wg.Done() | ||
|
||
apiResponse, err := service.ExporterMetricsService(ctx) | ||
if err != nil { | ||
log.Printf("[%s] %v", time.Now().Format("2006-01-02 15:04:05"), err) | ||
ch <- apiResponse | ||
return | ||
} | ||
|
||
ch <- apiResponse | ||
}() | ||
|
||
go func() { | ||
wg.Wait() | ||
close(ch) | ||
}() | ||
|
||
select { | ||
case apiResponse, ok := <-ch: | ||
if !ok { | ||
return c.Status(http.StatusInternalServerError).JSON(fiber.Map{ | ||
"error": "Failed to get a response from the server", | ||
}) | ||
} | ||
return c.SendString(apiResponse) | ||
|
||
case <-ctx.Done(): | ||
log.Printf("[%s] Timeout: %v", time.Now().Format("2006-01-02 15:04:05"), ctx.Err()) | ||
return c.Status(http.StatusRequestTimeout).JSON(fiber.Map{ | ||
"error": "Request timeout", | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package exporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
|
||
/* | ||
* --- Node Exporter Metric Service --- | ||
*/ | ||
func (s *ExporterServiceImpl) ExporterMetricsService(ctx context.Context) (string, error) { | ||
endpoint := fmt.Sprintf("%s/metrics", "http://localhost:9100") | ||
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to create request: %v", err) | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to perform request: %v", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("request failed with status code: %d", resp.StatusCode) | ||
} | ||
|
||
// Read the response body as plain text | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read response body: %v", err) | ||
} | ||
|
||
// Convert []byte to string | ||
metricsResponse := string(body) | ||
|
||
return metricsResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package exporter | ||
|
||
import "context" | ||
|
||
type ExporterService interface { | ||
ExporterMetricsService(ctx context.Context) (string, error) | ||
// ExporterVersionInfoService(ctx context.Context) (string, error) | ||
// ExporterHealthCheckService(ctx context.Context) (string, error) | ||
} | ||
|
||
type ExporterServiceImpl struct{} |