53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package metrics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// SourceStore persistiert, welche Module ihre Metriken unter welcher URL
|
|
// bereitstellen — dieselbe Postgres-basierte "kein Code-Deploy noetig"-
|
|
// Konvention wie internal/statuspage.Store.RegisterTarget (OPS-02): ein neu
|
|
// registriertes Modul erscheint automatisch in der Aggregation, sobald es
|
|
// hier eingetragen ist (Akzeptanzkriterium 2 / Pruefung 2).
|
|
type SourceStore struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewSourceStore(pool *pgxpool.Pool) *SourceStore {
|
|
return &SourceStore{pool: pool}
|
|
}
|
|
|
|
func (s *SourceStore) RegisterSource(ctx context.Context, moduleName, metricsURL string) error {
|
|
_, err := s.pool.Exec(ctx, `
|
|
INSERT INTO metrics_sources (module_name, metrics_url)
|
|
VALUES ($1, $2)
|
|
ON CONFLICT (module_name) DO UPDATE SET metrics_url = $2
|
|
`, moduleName, metricsURL)
|
|
if err != nil {
|
|
return fmt.Errorf("metrik-quelle speichern: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Provide implementiert SourceProvider direkt aus der Datenbank.
|
|
func (s *SourceStore) Provide(ctx context.Context) ([]Source, error) {
|
|
rows, err := s.pool.Query(ctx, `SELECT module_name, metrics_url FROM metrics_sources ORDER BY module_name`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("metrik-quellen auflisten: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []Source
|
|
for rows.Next() {
|
|
var src Source
|
|
if err := rows.Scan(&src.ModuleName, &src.MetricsURL); err != nil {
|
|
return nil, fmt.Errorf("metrik-quelle lesen: %w", err)
|
|
}
|
|
out = append(out, src)
|
|
}
|
|
return out, rows.Err()
|
|
}
|