-
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.
Merge pull request #1 from luissimas/feature/backfill-data
Feature/backfill data
- Loading branch information
Showing
20 changed files
with
799 additions
and
289 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 |
---|---|---|
@@ -1,64 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/luissimas/zettelkasten-exporter/internal/collector" | ||
"github.com/luissimas/zettelkasten-exporter/internal/config" | ||
"github.com/luissimas/zettelkasten-exporter/internal/metrics" | ||
"github.com/luissimas/zettelkasten-exporter/internal/zettel" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/luissimas/zettelkasten-exporter/internal/storage" | ||
"github.com/luissimas/zettelkasten-exporter/internal/zettelkasten" | ||
) | ||
|
||
func main() { | ||
// Setup | ||
cfg, err := config.LoadConfig() | ||
slog.SetLogLoggerLevel(cfg.LogLevel) | ||
if err != nil { | ||
slog.Error("Error loading config", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
slog.Debug("Loaded config", slog.Any("config", cfg)) | ||
zettelkasten := zettel.NewZettel(cfg) | ||
err = zettelkasten.Ensure() | ||
if err != nil { | ||
slog.Error("Error ensuring that zettelkasten is ready", slog.Any("error", err)) | ||
os.Exit(1) | ||
storage := storage.NewInfluxDBStorage(cfg.InfluxDBURL, cfg.InfluxDBOrg, cfg.InfluxDBBucket, cfg.InfluxDBToken) | ||
collector := collector.NewCollector(cfg.IgnoreFiles, storage) | ||
var zet zettelkasten.Zettelkasten | ||
if cfg.ZettelkastenGitURL != "" { | ||
zet = zettelkasten.NewGitZettelkasten(cfg.ZettelkastenGitURL, cfg.ZettelkastenGitBranch) | ||
} else { | ||
zet = zettelkasten.NewLocalZettelkasten(cfg.ZettelkastenDirectory) | ||
} | ||
|
||
collector := collector.NewCollector(zettelkasten.GetRoot(), cfg.IgnoreFiles) | ||
promHandler := promhttp.Handler() | ||
http.Handle("/metrics", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
slog.Info("Starting metrics collection") | ||
started := time.Now() | ||
err := zettelkasten.Ensure() | ||
// Collect historical data | ||
if cfg.CollectHistoricalMetrics { | ||
slog.Info("Collecting historical metrics") | ||
start := time.Now() | ||
err = zet.Ensure() | ||
if err != nil { | ||
slog.Error("Error ensuring zettelkasten", slog.Any("error", err)) | ||
metrics.CollectionSuccessful.Set(0) | ||
} else { | ||
err = collector.CollectMetrics() | ||
if err != nil { | ||
slog.Error("Error collecting zettelkasten metrics", slog.Any("error", err)) | ||
metrics.CollectionSuccessful.Set(0) | ||
} else { | ||
metrics.CollectionSuccessful.Set(1) | ||
} | ||
slog.Error("Error ensuring that zettelkasten is ready", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
slog.Info("Walking zettelkasten history") | ||
err = zet.WalkHistory(collector.CollectMetrics) | ||
if err != nil { | ||
slog.Error("Error walking history", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
slog.Info("Collected historical metrics", slog.Duration("duration", time.Since(start))) | ||
} | ||
|
||
elapsed := time.Since(started) | ||
metrics.CollectionDuration.Observe(float64(elapsed)) | ||
slog.Info("Completed metrics collection", slog.Duration("duration", elapsed)) | ||
|
||
promHandler.ServeHTTP(w, r) | ||
})) | ||
|
||
addr := fmt.Sprintf("%s:%d", cfg.IP, cfg.Port) | ||
slog.Info("Starting HTTP server", slog.String("address", addr)) | ||
err = http.ListenAndServe(addr, nil) | ||
if err != nil { | ||
slog.Error("Error on HTTP server", slog.Any("error", err)) | ||
os.Exit(1) | ||
// Periodic collection loop | ||
for { | ||
err = zet.Ensure() | ||
if err != nil { | ||
slog.Error("Error ensuring that zettelkasten is ready", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
root := zet.GetRoot() | ||
err = collector.CollectMetrics(root, time.Now()) | ||
if err != nil { | ||
slog.Error("Error collecting metrics", slog.Any("error", err)) | ||
os.Exit(1) | ||
} | ||
time.Sleep(cfg.CollectionInterval) | ||
} | ||
} |
Oops, something went wrong.