Skip to content

Commit

Permalink
review comments: added metrics docs, licence header and made cache st…
Browse files Browse the repository at this point in the history
…ruct private

Signed-off-by: Ashish Naware <ashishnaware3@gmail.com>
  • Loading branch information
AshishNaware committed Sep 17, 2024
1 parent 28458c9 commit 09fb765
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
20 changes: 20 additions & 0 deletions docs/content/en/docs/reference/metrics.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions pkg/observer/cache.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package observer

import (
Expand All @@ -7,14 +10,14 @@ import (
lru "github.com/hashicorp/golang-lru/v2"
)

type Cache struct {
type cache struct {
cache *lru.Cache[dataapi.DataEventId, []byte]
size int
}

func NewCache(
func newCache(
dataCacheSize int,
) (*Cache, error) {
) (*cache, error) {
lruCache, err := lru.NewWithEvict(
dataCacheSize,
func(_ dataapi.DataEventId, _ []byte) {
Expand All @@ -24,14 +27,14 @@ func NewCache(
if err != nil {
return nil, err
}
cache := &Cache{
cache := &cache{
cache: lruCache,
size: dataCacheSize,
}
return cache, nil
}

func (c *Cache) get(dataEventId dataapi.DataEventId) ([]byte, error) {
func (c *cache) get(dataEventId dataapi.DataEventId) ([]byte, error) {
data, ok := c.cache.Get(dataEventId)
if !ok {
dataCacheMisses.WithLabelValues("get").Inc()
Expand All @@ -40,15 +43,15 @@ func (c *Cache) get(dataEventId dataapi.DataEventId) ([]byte, error) {
return data, nil
}

func (c *Cache) add(id dataapi.DataEventId, msgData []byte) bool {
func (c *cache) add(id dataapi.DataEventId, msgData []byte) bool {
evicted := c.cache.Add(id, msgData)
if !evicted {
dataCacheTotal.Inc()
}
return evicted
}

func (c *Cache) remove(desc dataapi.DataEventDesc) bool {
func (c *cache) remove(desc dataapi.DataEventDesc) bool {
present := c.cache.Remove(desc.Id)
if present {
dataCacheTotal.Dec()
Expand Down
4 changes: 2 additions & 2 deletions pkg/observer/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func init() {
}

var (
dataCache *Cache
dataCache *cache
)

func InitDataCache(size int) error {
var err error
dataCache, err = NewCache(size)
dataCache, err = newCache(size)
return err
}

Expand Down

0 comments on commit 09fb765

Please sign in to comment.