Skip to content

Commit

Permalink
feat: write aggregated metrics to storage
Browse files Browse the repository at this point in the history
  • Loading branch information
luissimas committed Jul 2, 2024
1 parent 9b1c91f commit 2dbf3ae
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
5 changes: 1 addition & 4 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ func (c *Collector) CollectMetrics(root fs.FS, collectionTime time.Time) error {
return err
}

// Write metrics to storage
for name, metric := range collected.Notes {
c.storage.WriteMetric(name, metric, collectionTime)
}
c.storage.WriteMetrics(collected, collectionTime)
slog.Debug("Collected metrics", slog.Duration("duration", time.Since(start)))

return nil
Expand Down
3 changes: 1 addition & 2 deletions internal/storage/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ func NewFakeStorage() FakeStorage {
return FakeStorage{}
}

func (f FakeStorage) WriteMetric(noteName string, metric metrics.NoteMetrics, timestamp time.Time) {

func (f FakeStorage) WriteMetrics(zettelkastenMetrics metrics.Metrics, timestamp time.Time) {
}

func (f FakeStorage) IsEmpty() bool {
Expand Down
34 changes: 26 additions & 8 deletions internal/storage/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (

influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api"

"github.com/luissimas/zettelkasten-exporter/internal/metrics"
)

// The measurement name to be used for all metrics within the InfluxDB bucket.
const measurementName = "notes"
// The measurement names to be used for metrics within the InfluxDB bucket.
const notesMeasurementName = "notes"
const totalMeasurementName = "total"

// InfluxDBStorage represents the implementation of a metric storage using InfluxDB.
type InfluxDBStorage struct {
Expand All @@ -26,16 +28,32 @@ func NewInfluxDBStorage(url, org, bucket, token string) InfluxDBStorage {
}

// WriteMetric writes `metric` for `noteName` to the storage with `timestamp`.
func (i InfluxDBStorage) WriteMetric(noteName string, metric metrics.NoteMetrics, timestamp time.Time) {
func (i InfluxDBStorage) WriteMetrics(zettelkastenMetrics metrics.Metrics, timestamp time.Time) {
// Aggregated metrics
point := influxdb2.NewPoint(
measurementName,
map[string]string{"name": noteName},
totalMeasurementName,
map[string]string{},
map[string]interface{}{
"link_count": metric.LinkCount,
"word_count": metric.WordCount,
"backlink_count": metric.BacklinkCount,
"note_count": zettelkastenMetrics.NoteCount,
"link_count": zettelkastenMetrics.LinkCount,
"word_count": zettelkastenMetrics.WordCount,
},
timestamp,
)
i.writeAPI.WritePoint(point)

// Individual note metrics
for name, metric := range zettelkastenMetrics.Notes {
point := influxdb2.NewPoint(
notesMeasurementName,
map[string]string{"name": name},
map[string]interface{}{
"link_count": metric.LinkCount,
"word_count": metric.WordCount,
"backlink_count": metric.BacklinkCount,
},
timestamp,
)
i.writeAPI.WritePoint(point)
}
}
4 changes: 2 additions & 2 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (

// Storage represents a storage for metrics.
type Storage interface {
// WriteMetric writes the note metric to the storage.
WriteMetric(noteName string, metric metrics.NoteMetrics, timestamp time.Time)
// WriteMetric writes the `zettelkastenMetrics` to the storage.
WriteMetrics(zettelkastenMetrics metrics.Metrics, timestamp time.Time)
}

0 comments on commit 2dbf3ae

Please sign in to comment.