Skip to content

Commit

Permalink
feat: add node exporter proxy feature
Browse files Browse the repository at this point in the history
  • Loading branch information
belajarqywok committed Jul 8, 2024
1 parent 5bea6f7 commit d3c13b7
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (

proxy "tebakaja_lb_proxy/proxy"

// Main Features
stock_proxy "tebakaja_lb_proxy/proxy/stock"
crypto_proxy "tebakaja_lb_proxy/proxy/crypto"
national_currency_proxy "tebakaja_lb_proxy/proxy/national_currency"

// Node Exporter
// exporter_proxy "tebakaja_lb_proxy/proxy/node_exporter"
)

func main() {
Expand Down Expand Up @@ -42,6 +46,11 @@ func main() {
national_currency_proxy.NationalCurrencyPredictionHandler(
&national_currency_proxy.NationalCurrencyServiceImpl{}))

// exporterGroup := proxyService.Group("/exporter")
// exporterGroup.Get("/metrics",
// exporter_proxy.ExporterMetricsHandler(
// &exporter_proxy.ExporterServiceImpl{}))

port := 7860
log.Fatal(proxyService.Listen(fmt.Sprintf("0.0.0.0:%d", port)))
}
60 changes: 60 additions & 0 deletions proxy/node_exporter/exporter_handler.go
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",
})
}
}
}
41 changes: 41 additions & 0 deletions proxy/node_exporter/exporter_service.go
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
}
11 changes: 11 additions & 0 deletions proxy/node_exporter/interfaces.go
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{}

0 comments on commit d3c13b7

Please sign in to comment.