freelruotel

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2025 License: MIT Imports: 7 Imported by: 0

README

freelru-otel

Go Reference Go Report Card codecov

OpenTelemetry instrumentation for elastic/go-freelru LRU cache implementations.

Features

  • Universal Instrumentation: Works with freelru.LRU, freelru.SyncedLRU and freelru.ShardedLRU
  • OpenTelemetry Integration: Automatic metrics export for cache performance monitoring
  • Low Overhead: Uses OpenTelemetry Observable Counter callbacks for efficient metrics collection
  • Non-intrusive: Metrics collection doesn't impact cache performance

Installation

go get github.com/sweet-tv/freelru-otel

Usage

Basic Usage with freelru.LRU
package main

import (
    "github.com/cespare/xxhash/v2"
    "github.com/elastic/go-freelru"
    "github.com/sweet-tv/freelru-otel"
)

func hashString(s string) uint32 {
    return uint32(xxhash.Sum64String(s))
}

func main() {
    // Create a single-threaded LRU cache
    cache, err := freelru.New[string, string](100, hashString)
    if err != nil {
        panic(err)
    }

    // Instrument the cache (this starts collecting metrics)
    err = freelruotel.InstrumentCache(cache, "my_cache")
    if err != nil {
        panic(err)
    }

    // Use the cache normally
    cache.Add("key1", "value1")
    if value, ok := cache.Get("key1"); ok {
        // Metrics are automatically collected
    }
}
Usage with SyncedLRU
// Create a SyncedLRU cache for thread safety
cache, err := freelru.NewSynced[string, string](100, hashString)
if err != nil {
    panic(err)
}

// Instrument the cache
err = freelruotel.InstrumentCache(cache, "synced_cache")
if err != nil {
    panic(err)
}

// Use cache normally - metrics are collected automatically
cache.Add("key1", "value1")
cache.Get("key1")
Usage with ShardedLRU
// Create a ShardedLRU cache for high concurrency
cache, err := freelru.NewSharded[string, string](1024, hashString)
if err != nil {
    panic(err)
}

// Instrument the cache
err = freelruotel.InstrumentCache(cache, "high_perf_cache")
if err != nil {
    panic(err)
}

// Use cache normally - metrics are collected automatically
cache.Add("key1", "value1")
cache.Get("key1")
Using Custom MeterProvider
// Create custom MeterProvider
provider := metric.NewMeterProvider()

// Create a cache
cache, err := freelru.NewSynced[string, string](100, hashString)
if err != nil {
    panic(err)
}

// Instrument the cache with custom MeterProvider
err = freelruotel.InstrumentCache(cache, "my_cache", 
    freelruotel.WithMeterProvider(provider))
if err != nil {
    panic(err)
}

Exported Metrics

The instrumentation automatically exports the following OpenTelemetry metrics:

Metric Name Type Description Attributes
cache.hit Int64ObservableCounter Number of cache hits cache_name
cache.miss Int64ObservableCounter Number of cache misses cache_name
cache.insert Int64ObservableCounter Number of cache inserts cache_name
cache.eviction Int64ObservableCounter Number of cache evictions cache_name
cache.collision Int64ObservableCounter Number of cache collisions cache_name
cache.removal Int64ObservableCounter Number of cache removals cache_name

All metrics include the cache_name attribute to distinguish between different cache instances.

Requirements

  • Go 1.22+
  • OpenTelemetry configured in your application

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InstrumentCache

func InstrumentCache(cache MetricsProvider, name string, opts ...Option) error

InstrumentCache registers OpenTelemetry Observable Counter metrics of any instance of freelru cache.

Types

type MetricsProvider

type MetricsProvider interface {
	Metrics() freelru.Metrics
}

MetricsProvider is an interface for freelru cache implementations that can provide metrics. freelru.LRU, freelru.SyncedLRU and freelru.ShardedLRU implement this interface.

type Option

type Option func(*config)

Option is a functional option for configuring cache instrumentation.

func WithMeterProvider

func WithMeterProvider(provider metric.MeterProvider) Option

WithMeterProvider sets a custom MeterProvider for the instrumentation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL