Skip to content

Commit

Permalink
Allow access to the metrics slice in the OTTL metric context (#24447)
Browse files Browse the repository at this point in the history
**Description:**
Allow OTTL functions to access the MetricsSlice in the metric context.
This makes it possible for functions to create new metrics without
having to use the datapoint context.


**Link to tracking Issue:**
#24446

**Testing:**
Tested this change with a new function in #24368
  • Loading branch information
swiatekm authored Jul 25, 2023
1 parent 911336c commit 50fe850
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .chloggen/feat_ottl_metrics-context-slice.yaml
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: breaking

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow access to the metrics slice in the metric context

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

# (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 is only a breaking change for users using OTTL in custom components. For all Contrib components this is an enhancement.
2 changes: 1 addition & 1 deletion connector/countconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c *count) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {

for k := 0; k < scopeMetrics.Metrics().Len(); k++ {
metric := scopeMetrics.Metrics().At(k)
mCtx := ottlmetric.NewTransformContext(metric, scopeMetrics.Scope(), resourceMetric.Resource())
mCtx := ottlmetric.NewTransformContext(metric, scopeMetrics.Metrics(), scopeMetrics.Scope(), resourceMetric.Resource())
errors = multierr.Append(errors, metricsCounter.update(ctx, pcommon.NewMap(), mCtx))

switch metric.Type() {
Expand Down
4 changes: 2 additions & 2 deletions internal/filter/filtermetric/filtermetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestMatcherMatches(t *testing.T) {
assert.NotNil(t, matcher)
assert.NoError(t, err)

matches, err := matcher.Eval(context.Background(), ottlmetric.NewTransformContext(test.metric, pcommon.NewInstrumentationScope(), pcommon.NewResource()))
matches, err := matcher.Eval(context.Background(), ottlmetric.NewTransformContext(test.metric, pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource()))
assert.NoError(t, err)
assert.Equal(t, test.shouldMatch, matches)
})
Expand Down Expand Up @@ -188,7 +188,7 @@ func Test_NewSkipExpr_With_Bridge(t *testing.T) {

scope := pcommon.NewInstrumentationScope()

tCtx := ottlmetric.NewTransformContext(metric, scope, resource)
tCtx := ottlmetric.NewTransformContext(metric, pmetric.NewMetricSlice(), scope, resource)

boolExpr, err := NewSkipExpr(tt.include, tt.exclude)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions internal/filter/filterottl/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Test_HasAttrKeyOnDatapoint(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := hasAttributeKeyOnDatapoint(tt.key)
assert.NoError(t, err)
result, err := exprFunc(context.Background(), ottlmetric.NewTransformContext(tt.input(), pcommon.NewInstrumentationScope(), pcommon.NewResource()))
result, err := exprFunc(context.Background(), ottlmetric.NewTransformContext(tt.input(), pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource()))
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
Expand Down Expand Up @@ -351,7 +351,7 @@ func Test_HasAttrOnDatapoint(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := hasAttributeOnDatapoint(tt.key, tt.expectedVal)
assert.NoError(t, err)
result, err := exprFunc(context.Background(), ottlmetric.NewTransformContext(tt.input(), pcommon.NewInstrumentationScope(), pcommon.NewResource()))
result, err := exprFunc(context.Background(), ottlmetric.NewTransformContext(tt.input(), pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource()))
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
Expand Down
8 changes: 7 additions & 1 deletion pkg/ottl/contexts/ottlmetric/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ var _ internal.MetricContext = TransformContext{}

type TransformContext struct {
metric pmetric.Metric
metrics pmetric.MetricSlice
instrumentationScope pcommon.InstrumentationScope
resource pcommon.Resource
cache pcommon.Map
}

type Option func(*ottl.Parser[TransformContext])

func NewTransformContext(metric pmetric.Metric, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
func NewTransformContext(metric pmetric.Metric, metrics pmetric.MetricSlice, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource) TransformContext {
return TransformContext{
metric: metric,
metrics: metrics,
instrumentationScope: instrumentationScope,
resource: resource,
cache: pcommon.NewMap(),
Expand All @@ -41,6 +43,10 @@ func (tCtx TransformContext) GetMetric() pmetric.Metric {
return tCtx.metric
}

func (tCtx TransformContext) GetMetrics() pmetric.MetricSlice {
return tCtx.metrics
}

func (tCtx TransformContext) GetInstrumentationScope() pcommon.InstrumentationScope {
return tCtx.instrumentationScope
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/contexts/ottlmetric/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func Test_newPathGetSetter(t *testing.T) {

metric := createMetricTelemetry()

ctx := NewTransformContext(metric, pcommon.NewInstrumentationScope(), pcommon.NewResource())
ctx := NewTransformContext(metric, pmetric.NewMetricSlice(), pcommon.NewInstrumentationScope(), pcommon.NewResource())

got, err := accessor.Get(context.Background(), ctx)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion processor/attributesprocessor/attributes_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (a *metricAttributesProcessor) processMetrics(ctx context.Context, md pmetr
for k := 0; k < metrics.Len(); k++ {
m := metrics.At(k)
if a.skipExpr != nil {
skip, err := a.skipExpr.Eval(ctx, ottlmetric.NewTransformContext(m, scope, resource))
skip, err := a.skipExpr.Eval(ctx, ottlmetric.NewTransformContext(m, metrics, scope, resource))
if err != nil {
return md, err
}
Expand Down
2 changes: 1 addition & 1 deletion processor/filterprocessor/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (fmp *filterMetricProcessor) processMetrics(ctx context.Context, md pmetric
scope := smetrics.Scope()
smetrics.Metrics().RemoveIf(func(metric pmetric.Metric) bool {
if fmp.skipMetricExpr != nil {
skip, err := fmp.skipMetricExpr.Eval(ctx, ottlmetric.NewTransformContext(metric, scope, resource))
skip, err := fmp.skipMetricExpr.Eval(ctx, ottlmetric.NewTransformContext(metric, smetrics.Metrics(), scope, resource))
if err != nil {
errors = multierr.Append(errors, err)
}
Expand Down
2 changes: 1 addition & 1 deletion processor/transformprocessor/internal/common/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (m metricStatements) ConsumeMetrics(ctx context.Context, md pmetric.Metrics
smetrics := rmetrics.ScopeMetrics().At(j)
metrics := smetrics.Metrics()
for k := 0; k < metrics.Len(); k++ {
tCtx := ottlmetric.NewTransformContext(metrics.At(k), smetrics.Scope(), rmetrics.Resource())
tCtx := ottlmetric.NewTransformContext(metrics.At(k), smetrics.Metrics(), smetrics.Scope(), rmetrics.Resource())
err := m.Execute(ctx, tCtx)
if err != nil {
return err
Expand Down

0 comments on commit 50fe850

Please sign in to comment.