Skip to content

Commit

Permalink
enhancement: quick refactor for HostFactCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
fgouteroux committed Dec 15, 2023
1 parent f761688 commit 875bafc
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions hostfact_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c HostFactCollector) Collect(ch chan<- prometheus.Metric) {
}
if hostsFactsError == nil {
// update the cache
updateHostsFactsKV(c.RingConfig.kvStore, string(content), c.CacheConfig.ExpiresTTL)
c.updateKV(string(content))
}
} else {
localCache.Set(hostsFactsKey, data, c.CacheConfig.ExpiresTTL)
Expand Down Expand Up @@ -169,32 +169,25 @@ func hostFactCollectorScrapeError(ch chan<- prometheus.Metric, errVal float64) {
)
}

func updateHostsFactsKV(store *memberlist.KV, content string, expiresTTL time.Duration) {
func (c HostFactCollector) updateKV(content string) {
cache := &Cache{
Content: content,
CreatedAt: time.Now(),
ExpiresAt: time.Now().Add(expiresTTL),
}
val, _ := json.Marshal(cache)
msg := memberlist.KeyValuePair{
Key: hostsFactsKey,
Value: val,
Codec: "jsonCodec",
ExpiresAt: time.Now().Add(c.CacheConfig.ExpiresTTL),
}

msgBytes, _ := msg.Marshal()
store.NotifyMsg(msgBytes)
}
val, err := JSONCodec.Encode(cache)
if err != nil {
level.Error(c.Logger).Log("msg", fmt.Sprintf("failed to encode data with '%s'", JSONCodec.CodecID()), "err", err) // #nosec G104
return
}

/*
func deleteHostsFactsKV(store *memberlist.KV) {
msg := memberlist.KeyValuePair{
Key: hostsFactsKey,
Value: []byte("{}"),
Codec: "jsonCodec",
Value: val,
Codec: JSONCodec.CodecID(),
}

msgBytes, _ := msg.Marshal()
store.NotifyMsg(msgBytes)
c.RingConfig.kvStore.NotifyMsg(msgBytes)
}
*/

0 comments on commit 875bafc

Please sign in to comment.