-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/jaegerremotesampling] Support reload_interval option in re…
…mote mode of usage (#24981) Updates the jaegerremotesampling extension's `remote` mode of usage to support the `reload_interval` caching option already supported in `file` mode of usage. Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
- Loading branch information
Showing
9 changed files
with
499 additions
and
25 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
.chloggen/zcross_jaegerremotesampling_support-remote-policy-ttl-caching.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
# If your change doesn't affect end users, such as a test fix or a tooling change, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: extension/jaegerremotesampling | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: gRPC remote source usage in jaegerremotesampling extension supports optional caching via existing `reload_interval` config | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [24840] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
extension/jaegerremotesampling/internal/remote_strategy_cache.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/jaegerremotesampling/internal" | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/jaegertracing/jaeger/thrift-gen/sampling" | ||
"github.com/tilinna/clock" | ||
) | ||
|
||
type serviceStrategyCache interface { | ||
get(ctx context.Context, serviceName string) (*sampling.SamplingStrategyResponse, bool) | ||
put(ctx context.Context, serviceName string, response *sampling.SamplingStrategyResponse) | ||
Close() error | ||
} | ||
|
||
// serviceStrategyCacheEntry is a timestamped sampling strategy response | ||
type serviceStrategyCacheEntry struct { | ||
retrievedAt time.Time | ||
strategyResponse *sampling.SamplingStrategyResponse | ||
} | ||
|
||
// serviceStrategyTTLCache is a naive in-memory TTL serviceStrategyTTLCache of service-specific sampling strategies | ||
// returned from the remote source. Each cached item has its own TTL used to determine whether it is valid for read | ||
// usage (based on the time of write). | ||
type serviceStrategyTTLCache struct { | ||
itemTTL time.Duration | ||
|
||
stopCh chan struct{} | ||
rw sync.RWMutex | ||
items map[string]serviceStrategyCacheEntry | ||
} | ||
|
||
// Initial size of cache's underlying map | ||
const initialRemoteResponseCacheSize = 32 | ||
|
||
func newServiceStrategyCache(itemTTL time.Duration) serviceStrategyCache { | ||
result := &serviceStrategyTTLCache{ | ||
itemTTL: itemTTL, | ||
items: make(map[string]serviceStrategyCacheEntry, initialRemoteResponseCacheSize), | ||
stopCh: make(chan struct{}), | ||
} | ||
|
||
// Launches a "cleaner" goroutine that naively blows away stale items with a frequency equal to the item TTL. | ||
// Note that this is for memory usage and not for correctness (the get() function checks item validity). | ||
go result.periodicallyClearCache(context.Background(), itemTTL) | ||
return result | ||
} | ||
|
||
// get returns a cached sampling strategy if one is present and is no older than the serviceStrategyTTLCache's per-item TTL. | ||
func (c *serviceStrategyTTLCache) get( | ||
ctx context.Context, | ||
serviceName string, | ||
) (*sampling.SamplingStrategyResponse, bool) { | ||
c.rw.RLock() | ||
defer c.rw.RUnlock() | ||
found, ok := c.items[serviceName] | ||
if !ok { | ||
return nil, false | ||
} | ||
if c.staleItem(ctx, found) { | ||
return nil, false | ||
} | ||
return found.strategyResponse, true | ||
} | ||
|
||
// put unconditionally overwrites the given service's serviceStrategyTTLCache item entry and resets its timestamp used for TTL checks. | ||
func (c *serviceStrategyTTLCache) put( | ||
ctx context.Context, | ||
serviceName string, | ||
response *sampling.SamplingStrategyResponse, | ||
) { | ||
c.rw.Lock() | ||
defer c.rw.Unlock() | ||
c.items[serviceName] = serviceStrategyCacheEntry{ | ||
strategyResponse: response, | ||
retrievedAt: clock.Now(ctx), | ||
} | ||
} | ||
|
||
// periodicallyClearCache periodically clears expired items from the cache and replaces the backing map with only | ||
// valid (fresh) items. Note that this is not necessary for correctness, just preferred for memory usage hygiene. | ||
// Client request activity drives the replacement of stale items with fresh items upon cache misses for any service. | ||
func (c *serviceStrategyTTLCache) periodicallyClearCache( | ||
ctx context.Context, | ||
schedulingPeriod time.Duration, | ||
) { | ||
ticker := clock.NewTicker(ctx, schedulingPeriod) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
c.rw.Lock() | ||
newItems := make(map[string]serviceStrategyCacheEntry, initialRemoteResponseCacheSize) | ||
for serviceName, item := range c.items { | ||
if !c.staleItem(ctx, item) { | ||
newItems[serviceName] = item | ||
} | ||
} | ||
// Notice that we swap the map rather than using map's delete (which doesn't reduce its allocated size). | ||
c.items = newItems | ||
c.rw.Unlock() | ||
case <-c.stopCh: | ||
return | ||
} | ||
} | ||
} | ||
|
||
func (c *serviceStrategyTTLCache) Close() error { | ||
close(c.stopCh) | ||
return nil | ||
} | ||
|
||
func (c *serviceStrategyTTLCache) staleItem(ctx context.Context, item serviceStrategyCacheEntry) bool { | ||
return clock.Now(ctx).After(item.retrievedAt.Add(c.itemTTL)) | ||
} | ||
|
||
type noopStrategyCache struct{} | ||
|
||
func (n *noopStrategyCache) get(_ context.Context, _ string) (*sampling.SamplingStrategyResponse, bool) { | ||
return nil, false | ||
} | ||
|
||
func (n *noopStrategyCache) put(_ context.Context, _ string, _ *sampling.SamplingStrategyResponse) { | ||
} | ||
|
||
func (n *noopStrategyCache) Close() error { | ||
return nil | ||
} | ||
|
||
func newNoopStrategyCache() serviceStrategyCache { | ||
return &noopStrategyCache{} | ||
} |
Oops, something went wrong.