Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .chloggen/fix-issue-39646.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: spanmetricsconnector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate the unused configuration `dimensions_cache_size`

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [39646]

# (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: |
Deprecated configuration `dimensions_cache_size`, please use `aggregation_cardinality_limit` instead

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
2 changes: 1 addition & 1 deletion connector/spanmetricsconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ The following settings can be optionally configured:

If no `default` is provided, this dimension will be **omitted** from the metric.
- `exclude_dimensions`: the list of dimensions to be excluded from the default set of dimensions. Use to exclude unneeded data from metrics.
- `dimensions_cache_size` (default: `1000`): the size of cache for storing Dimensions to improve collectors memory usage. Must be a positive number.
- `dimensions_cache_size`: this setting is deprecated, please use aggregation_cardinality_limit instead.
- `include_instrumentation_scope`: a list of instrumentation scope names to include from the traces.
- `resource_metrics_cache_size` (default: `1000`): the size of the cache holding metrics for a service. This is mostly relevant for
cumulative temporality to avoid memory leaks and correct metric timestamp resets.
Expand Down
1 change: 1 addition & 0 deletions connector/spanmetricsconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Config struct {
// DimensionsCacheSize defines the size of cache for storing Dimensions, which helps to avoid cache memory growing
// indefinitely over the lifetime of the collector.
// Optional. See defaultDimensionsCacheSize in connector.go for the default value.
// Deprecated: Please use AggregationCardinalityLimit instead
DimensionsCacheSize int `mapstructure:"dimensions_cache_size"`

// ResourceMetricsCacheSize defines the size of the cache holding metrics for a service. This is mostly relevant for
Expand Down
62 changes: 26 additions & 36 deletions connector/spanmetricsconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ type connectorImp struct {

keyBuf *bytes.Buffer

// An LRU cache of dimension key-value maps keyed by a unique identifier formed by a concatenation of its values:
// e.g. { "foo/barOK": { "serviceName": "foo", "span.name": "/bar", "status_code": "OK" }}
metricKeyToDimensions *cache.Cache[metrics.Key, pcommon.Map]

clock clockwork.Clock
ticker clockwork.Ticker
done chan struct{}
Expand Down Expand Up @@ -113,11 +109,6 @@ func newConnector(logger *zap.Logger, config component.Config, clock clockwork.C
logger.Info("Building spanmetrics connector")
cfg := config.(*Config)

metricKeyToDimensionsCache, err := cache.NewCache[metrics.Key, pcommon.Map](cfg.DimensionsCacheSize)
if err != nil {
return nil, err
}

resourceMetricsCache, err := cache.NewCache[resourceKey, *resourceMetrics](cfg.ResourceMetricsCacheSize)
if err != nil {
return nil, err
Expand Down Expand Up @@ -146,7 +137,6 @@ func newConnector(logger *zap.Logger, config component.Config, clock clockwork.C
resourceMetricsKeyAttributes: resourceMetricsKeyAttributes,
dimensions: newDimensions(cfg.Dimensions),
keyBuf: bytes.NewBuffer(make([]byte, 0, 1024)),
metricKeyToDimensions: metricKeyToDimensionsCache,
lastDeltaTimestamps: lastDeltaTimestamps,
clock: clock,
ticker: clock.NewTicker(cfg.MetricsFlushInterval),
Expand Down Expand Up @@ -331,10 +321,8 @@ func (p *connectorImp) resetState() {
// If delta metrics, reset accumulated data
if p.config.GetAggregationTemporality() == pmetric.AggregationTemporalityDelta {
p.resourceMetrics.Purge()
p.metricKeyToDimensions.Purge()
} else {
p.resourceMetrics.RemoveEvictedItems()
p.metricKeyToDimensions.RemoveEvictedItems()

// If none of these features are enabled then we can skip the remaining operations.
// Enabling either of these features requires to go over resource metrics and do operation on each.
Expand Down Expand Up @@ -399,43 +387,45 @@ func (p *connectorImp) aggregateMetrics(traces ptrace.Traces) {
if endTime > startTime {
duration = float64(endTime-startTime) / float64(unitDivider)
}
key := p.buildKey(serviceName, span, p.dimensions, resourceAttr)

var attributes pcommon.Map
key := p.buildKey(serviceName, span, p.dimensions, resourceAttr)
var attributesFun metrics.BuildAttributesFun

// Note: we check cardinality limit here for sums metrics but it is the same
// for histograms because both use the same key and attributes.
if rm.sums.IsCardinalityLimitReached() {
attributes = pcommon.NewMap()
for _, d := range p.dimensions {
if v, exists := utilattri.GetDimensionValue(d, span.Attributes(), resourceAttr); exists {
v.CopyTo(attributes.PutEmpty(d.Name))
attributesFun = func() pcommon.Map {
attributes := pcommon.NewMap()
for _, d := range p.dimensions {
if v, exists := utilattri.GetDimensionValue(d, span.Attributes(), resourceAttr); exists {
v.CopyTo(attributes.PutEmpty(d.Name))
}
}
attributes.PutBool(overflowKey, true)

return attributes
}
attributes.PutBool(overflowKey, true)
} else {
var cached bool
attributes, cached = p.metricKeyToDimensions.Get(key)
if !cached {
attributes = p.buildAttributes(
attributesFun = func() pcommon.Map {
attributes := p.buildAttributes(
serviceName,
span,
resourceAttr,
p.dimensions,
ils.Scope(),
)
p.metricKeyToDimensions.Add(key, attributes)
return attributes
}
}

if !p.config.Histogram.Disable {
// aggregate histogram metrics
h := histograms.GetOrCreate(key, attributes, startTimestamp)
h := histograms.GetOrCreate(key, attributesFun, startTimestamp)
p.addExemplar(span, duration, h)
h.Observe(duration)
}
// aggregate sums metrics
s := sums.GetOrCreate(key, attributes, startTimestamp)
s := sums.GetOrCreate(key, attributesFun, startTimestamp)
if p.config.Exemplars.Enabled && !span.TraceID().IsEmpty() {
s.AddExemplar(span.TraceID(), span.SpanID(), duration)
}
Expand All @@ -459,20 +449,20 @@ func (p *connectorImp) aggregateMetrics(traces ptrace.Traces) {
})

eKey := p.buildKey(serviceName, span, eDimensions, rscAndEventAttrs)

var eAttributes pcommon.Map
if rm.events.IsCardinalityLimitReached() {
eAttributes = pcommon.NewMap()
rscAndEventAttrs.CopyTo(eAttributes)
eAttributes.PutBool(overflowKey, true)
attributesFun = func() pcommon.Map {
attributes := pcommon.NewMap()
rscAndEventAttrs.CopyTo(attributes)
attributes.PutBool(overflowKey, true)

return attributes
}
} else {
eAttributes, ok = p.metricKeyToDimensions.Get(eKey)
if !ok {
eAttributes = p.buildAttributes(serviceName, span, rscAndEventAttrs, eDimensions, ils.Scope())
p.metricKeyToDimensions.Add(eKey, eAttributes)
attributesFun = func() pcommon.Map {
return p.buildAttributes(serviceName, span, rscAndEventAttrs, eDimensions, ils.Scope())
}
}
e := events.GetOrCreate(eKey, eAttributes, startTimestamp)
e := events.GetOrCreate(eKey, attributesFun, startTimestamp)
if p.config.Exemplars.Enabled && !span.TraceID().IsEmpty() {
e.AddExemplar(span.TraceID(), span.SpanID(), duration)
}
Expand Down
29 changes: 0 additions & 29 deletions connector/spanmetricsconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,35 +914,6 @@ func TestConsumeTraces(t *testing.T) {
}
}

func TestMetricKeyCache(t *testing.T) {
p, err := newConnectorImp(stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, 0, []string{}, 1000, clockwork.NewFakeClock())
require.NoError(t, err)
traces := buildSampleTrace()

// Test
ctx := metadata.NewIncomingContext(context.Background(), nil)

// 0 key was cached at beginning
assert.Zero(t, p.metricKeyToDimensions.Len())

err = p.ConsumeTraces(ctx, traces)
// Validate
require.NoError(t, err)
// 2 key was cached, 1 key was evicted and cleaned after the processing
assert.Eventually(t, func() bool {
return p.metricKeyToDimensions.Len() == dimensionsCacheSize
}, 10*time.Second, time.Millisecond*100)

// consume another batch of traces
err = p.ConsumeTraces(ctx, traces)
require.NoError(t, err)

// 2 key was cached, other keys were evicted and cleaned after the processing
assert.Eventually(t, func() bool {
return p.metricKeyToDimensions.Len() == dimensionsCacheSize
}, 10*time.Second, time.Millisecond*100)
}

func TestResourceMetricsCache(t *testing.T) {
p, err := newConnectorImp(stringp("defaultNullValue"), explicitHistogramsConfig, disabledExemplarsConfig, disabledEventsConfig, cumulative, 0, []string{}, 1000, clockwork.NewFakeClock())
require.NoError(t, err)
Expand Down
16 changes: 9 additions & 7 deletions connector/spanmetricsconnector/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type Key string

type HistogramMetrics interface {
GetOrCreate(key Key, attributes pcommon.Map, startTimestamp pcommon.Timestamp) Histogram
GetOrCreate(key Key, attributesFun BuildAttributesFun, startTimestamp pcommon.Timestamp) Histogram
BuildMetrics(pmetric.Metric, pcommon.Timestamp, func(Key, pcommon.Timestamp) pcommon.Timestamp, pmetric.AggregationTemporality)
ClearExemplars()
}
Expand Down Expand Up @@ -62,6 +62,8 @@ type exponentialHistogram struct {
startTimestamp pcommon.Timestamp
}

type BuildAttributesFun func() pcommon.Map

func NewExponentialHistogramMetrics(maxSize int32, maxExemplarCount *int) HistogramMetrics {
return &exponentialHistogramMetrics{
metrics: make(map[Key]*exponentialHistogram),
Expand All @@ -78,11 +80,11 @@ func NewExplicitHistogramMetrics(bounds []float64, maxExemplarCount *int) Histog
}
}

func (m *explicitHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Map, startTimestamp pcommon.Timestamp) Histogram {
func (m *explicitHistogramMetrics) GetOrCreate(key Key, attributesFun BuildAttributesFun, startTimestamp pcommon.Timestamp) Histogram {
h, ok := m.metrics[key]
if !ok {
h = &explicitHistogram{
attributes: attributes,
attributes: attributesFun(),
exemplars: pmetric.NewExemplarSlice(),
bounds: m.bounds,
bucketCounts: make([]uint64, len(m.bounds)+1),
Expand Down Expand Up @@ -126,7 +128,7 @@ func (m *explicitHistogramMetrics) ClearExemplars() {
}
}

func (m *exponentialHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Map, startTimeStamp pcommon.Timestamp) Histogram {
func (m *exponentialHistogramMetrics) GetOrCreate(key Key, attributesFun BuildAttributesFun, startTimeStamp pcommon.Timestamp) Histogram {
h, ok := m.metrics[key]
if !ok {
histogram := new(structure.Histogram[float64])
Expand All @@ -137,7 +139,7 @@ func (m *exponentialHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Ma

h = &exponentialHistogram{
histogram: histogram,
attributes: attributes,
attributes: attributesFun(),
exemplars: pmetric.NewExemplarSlice(),
maxExemplarCount: m.maxExemplarCount,
startTimestamp: startTimeStamp,
Expand Down Expand Up @@ -277,11 +279,11 @@ func (m *SumMetrics) IsCardinalityLimitReached() bool {
return m.cardinalityLimit > 0 && len(m.metrics) >= m.cardinalityLimit
}

func (m *SumMetrics) GetOrCreate(key Key, attributes pcommon.Map, startTimestamp pcommon.Timestamp) *Sum {
func (m *SumMetrics) GetOrCreate(key Key, attributesFun BuildAttributesFun, startTimestamp pcommon.Timestamp) *Sum {
s, ok := m.metrics[key]
if !ok {
s = &Sum{
attributes: attributes,
attributes: attributesFun(),
exemplars: pmetric.NewExemplarSlice(),
maxExemplarCount: m.maxExemplarCount,
startTimestamp: startTimestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,10 @@ func TestSumMetrics_GetOrCreate(t *testing.T) {
sm := SumMetrics{
metrics: tt.metrics,
}
sum := sm.GetOrCreate(tt.key, tt.attributes, pcommon.Timestamp(0))
attributesFun := func() pcommon.Map {
return tt.attributes
}
sum := sm.GetOrCreate(tt.key, attributesFun, pcommon.Timestamp(0))
assert.Len(t, sm.metrics, tt.expectedCount)
if tt.expectedCreated {
assert.Equal(t, tt.attributes, sum.attributes)
Expand Down
Loading