-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #58 send caddy and system metrics to ptah-server
- Loading branch information
1 parent
7d0d8bb
commit 7fed58c
Showing
16 changed files
with
933 additions
and
74 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
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,125 @@ | ||
package ptah_agent | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"log" | ||
"slices" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/expfmt" | ||
|
||
caddyClient "github.com/ptah-sh/ptah-agent/internal/pkg/caddy-client" | ||
) | ||
|
||
type MetricsAgent struct { | ||
client *SafeClient | ||
caddyClient *caddyClient.Client | ||
interval time.Duration | ||
stopChan chan struct{} | ||
wg sync.WaitGroup | ||
} | ||
|
||
func NewMetricsAgent(client *SafeClient, caddyClient *caddyClient.Client, interval time.Duration) *MetricsAgent { | ||
return &MetricsAgent{ | ||
client: client, | ||
caddyClient: caddyClient, | ||
interval: interval, | ||
stopChan: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (m *MetricsAgent) ScrapeMetrics(ctx context.Context) ([]string, error) { | ||
// TODO: adjust the timestamp value to stick to a resolution of 5 seconds (or whatever else server wants) | ||
// something like time.Now().Truncate(5 * time.Second).UnixMilli() | ||
timestamp := time.Now().UnixMilli() | ||
|
||
caddyMetrics, err := m.caddyClient.GetMetrics(ctx) | ||
if err != nil { | ||
log.Printf("failed to get caddy metrics: %v", err) | ||
} | ||
|
||
err = scrapeSystemMetrics() | ||
if err != nil { | ||
log.Printf("failed to scrape system metrics: %v", err) | ||
} | ||
|
||
sysMetrics, err := prometheus.DefaultGatherer.Gather() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to gather metrics") | ||
} | ||
|
||
buf := bytes.NewBuffer([]byte{}) | ||
|
||
for _, mf := range sysMetrics { | ||
_, err = expfmt.MetricFamilyToText(buf, mf) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to expfmt to temporary buffer") | ||
} | ||
} | ||
|
||
sysMetricsSlice := strings.Split(buf.String(), "\n") | ||
allMetrics := slices.Concat(caddyMetrics, sysMetricsSlice) | ||
|
||
result := make([]string, 0, len(allMetrics)) | ||
for _, s := range allMetrics { | ||
if strings.Contains(s, "ptah_") { | ||
if s[0] != '#' { | ||
s = fmt.Sprintf("%s %d", s, timestamp) | ||
} | ||
|
||
result = append(result, s) | ||
} | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (m *MetricsAgent) Start(ctx context.Context) error { | ||
metrics, err := m.ScrapeMetrics(ctx) | ||
if err != nil { | ||
log.Printf("failed to collect metrics: %v", err) | ||
} | ||
|
||
err = m.client.SendMetrics(ctx, metrics) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-time.After(m.interval): | ||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second) | ||
defer cancel() | ||
|
||
metrics, err := m.ScrapeMetrics(ctx) | ||
if err != nil { | ||
log.Printf("failed to collect metrics: %v", err) | ||
|
||
continue | ||
} | ||
|
||
err = m.client.SendMetrics(ctx, metrics) | ||
if err != nil { | ||
log.Printf("failed to send metrics: %v", err) | ||
} | ||
} | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func (m *MetricsAgent) Stop() { | ||
close(m.stopChan) | ||
m.wg.Wait() | ||
} |
Oops, something went wrong.