Skip to content

Commit

Permalink
[chore] fix update to golang-lru/v2 (open-telemetry#35847)
Browse files Browse the repository at this point in the history
#### Description

Fixes incomplete mechanical update in
open-telemetry#30890.
The new version of golang-lru uses type params.

Recreates
open-telemetry#35824,
which Renovate kindly closed without merging 🥲

#### Link to tracking issue

Supersedes
open-telemetry#30890

#### Testing

Ran the unit tests.

#### Documentation

N/A

Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
  • Loading branch information
axw and codeboten authored Oct 18, 2024
1 parent 17225bb commit aff4d9a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 19 deletions.
1 change: 0 additions & 1 deletion connector/spanmetricsconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanm
go 1.22.0

require (
github.com/hashicorp/golang-lru v1.0.2
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/jonboulle/clockwork v0.4.0
github.com/lightstep/go-expohisto v1.0.0
Expand Down
2 changes: 0 additions & 2 deletions connector/spanmetricsconnector/go.sum

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

12 changes: 6 additions & 6 deletions connector/spanmetricsconnector/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package cache // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/spanmetricsconnector/internal/cache"

import (
"github.com/hashicorp/golang-lru/simplelru"
"github.com/hashicorp/golang-lru/v2/simplelru"
)

// Cache consists of an LRU cache and the evicted items from the LRU cache.
Expand All @@ -15,15 +15,15 @@ import (
//
// Important: This implementation is non-thread safe.
type Cache[K comparable, V any] struct {
lru simplelru.LRUCache
lru *simplelru.LRU[K, V]
evictedItems map[K]V
}

// NewCache creates a Cache.
func NewCache[K comparable, V any](size int) (*Cache[K, V], error) {
evictedItems := make(map[K]V)
lruCache, err := simplelru.NewLRU(size, func(key any, value any) {
evictedItems[key.(K)] = value.(V)
lruCache, err := simplelru.NewLRU(size, func(key K, value V) {
evictedItems[key] = value
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -51,7 +51,7 @@ func (c *Cache[K, V]) Add(key K, value V) bool {
// Get an item from the LRU cache or evicted items.
func (c *Cache[K, V]) Get(key K) (V, bool) {
if val, ok := c.lru.Get(key); ok {
return val.(V), ok
return val, ok
}
val, ok := c.evictedItems[key]

Expand Down Expand Up @@ -85,7 +85,7 @@ func (c *Cache[K, V]) ForEach(fn func(k K, v V)) {
for _, k := range c.lru.Keys() {
v, ok := c.lru.Get(k)
if ok {
fn(k.(K), v.(V))
fn(k, v)
}
}

Expand Down
14 changes: 7 additions & 7 deletions extension/observer/ecsobserver/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/hashicorp/golang-lru/v2/simplelru"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -50,8 +50,8 @@ type taskFetcher struct {
ecs ecsClient
ec2 ec2Client
cluster string
taskDefCache simplelru.LRUCache
ec2Cache simplelru.LRUCache
taskDefCache *simplelru.LRU[string, *ecs.TaskDefinition]
ec2Cache *simplelru.LRU[string, *ec2.Instance]
serviceNameFilter serviceNameFilter
}

Expand Down Expand Up @@ -81,11 +81,11 @@ func newTaskFetcherFromConfig(cfg Config, logger *zap.Logger) (*taskFetcher, err

func newTaskFetcher(opts taskFetcherOptions) (*taskFetcher, error) {
// Init cache
taskDefCache, err := simplelru.NewLRU(taskDefCacheSize, nil)
taskDefCache, err := simplelru.NewLRU[string, *ecs.TaskDefinition](taskDefCacheSize, nil)
if err != nil {
return nil, err
}
ec2Cache, err := simplelru.NewLRU(ec2CacheSize, nil)
ec2Cache, err := simplelru.NewLRU[string, *ec2.Instance](ec2CacheSize, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -205,7 +205,7 @@ func (f *taskFetcher) attachTaskDefinition(ctx context.Context, tasks []*ecs.Tas
}
var def *ecs.TaskDefinition
if cached, ok := f.taskDefCache.Get(arn); ok {
def = cached.(*ecs.TaskDefinition)
def = cached
} else {
res, err := svc.DescribeTaskDefinitionWithContext(ctx, &ecs.DescribeTaskDefinitionInput{
TaskDefinition: aws.String(arn),
Expand Down Expand Up @@ -251,7 +251,7 @@ func (f *taskFetcher) attachContainerInstance(ctx context.Context, tasks []*task
for instanceArn := range ciToEC2 {
cached, ok := f.ec2Cache.Get(instanceArn)
if ok {
ciToEC2[instanceArn] = cached.(*ec2.Instance) // use value from cache
ciToEC2[instanceArn] = cached // use value from cache
} else {
instanceList = append(instanceList, aws.String(instanceArn))
}
Expand Down
2 changes: 1 addition & 1 deletion extension/observer/ecsobserver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.0

require (
github.com/aws/aws-sdk-go v1.55.5
github.com/hashicorp/golang-lru v1.0.2
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.111.1-0.20241008154146-ea48c09c31ae
go.opentelemetry.io/collector/component/componentstatus v0.111.1-0.20241008154146-ea48c09c31ae
Expand Down
4 changes: 2 additions & 2 deletions extension/observer/ecsobserver/go.sum

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

0 comments on commit aff4d9a

Please sign in to comment.