Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
feat(labels): use fnv1a metric fingerprint as label ID
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Nov 28, 2019
1 parent 7061ca9 commit f18baa2
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions postgres/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
package postgres

import (
"crypto/sha1"
"database/sql"
"encoding/base64"
"encoding/json"
"math"
"time"
Expand All @@ -34,7 +32,7 @@ import (
type Client struct {
logger log.Logger

cache *lru.TwoQueueCache
cache *lru.Cache
cron *cron.Cron
db *sql.DB
}
Expand Down Expand Up @@ -151,7 +149,7 @@ func NewClient(logger log.Logger, conn string, idle int, open int, cacheSize int
db.SetMaxOpenConns(open)

level.Info(logger).Log("msg", "creating cache", "size", cacheSize)
cache, err := lru.New2Q(cacheSize)
cache, err := lru.New(cacheSize)
if err != nil {
level.Error(logger).Log("msg", "error creating lid cache", "err", err)
return nil
Expand Down Expand Up @@ -209,31 +207,27 @@ func (c *Client) WriteLabels(samples model.Samples, txn *sql.Tx) error {
skipLabels := 0

for _, s := range samples {
l := s.Metric.String()
if c.cache.Contains(l) {
lid := s.Metric.Fingerprint()
if c.cache.Contains(lid) {
skipLabels++
level.Debug(c.logger).Log("msg", "skipping duplicate labels", "labels", l)
level.Debug(c.logger).Log("msg", "skipping duplicate labels", "lid", lid)
continue
}

h := sha1.New()
h.Write([]byte(l))
lid := base64.StdEncoding.EncodeToString(h.Sum(nil))
t := time.Unix(0, s.Timestamp.UnixNano())

labels, err := json.Marshal(s.Metric)
if err != nil {
continue
}

t := time.Unix(0, s.Timestamp.UnixNano())
ql := string(labels)
_, err = stmt.Exec(lid, t, ql)
if err != nil {
level.Error(c.logger).Log("msg", "error in single label execution", "err", err, "labels", ql, "lid", lid)
continue
}

c.cache.Add(l, lid)
c.cache.Add(lid, nil)
newLabels++
}

Expand Down

0 comments on commit f18baa2

Please sign in to comment.