Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl] add event_index to available paths in span event context #37092

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
27 changes: 27 additions & 0 deletions .chloggen/ottl-span-event-index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `event_index` to the available paths of the span event context

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

# (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:

# 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: []
39 changes: 20 additions & 19 deletions pkg/ottl/contexts/ottlspanevent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@ In general, the Span Event Context supports accessing pdata using the field name

The following paths are supported.

| path | field accessed | type |
|----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|
| cache | the value of the current transform context's temporary cache. cache can be used as a temporary placeholder for data during complex transformations | pcommon.Map |
| cache\[""\] | the value of an item in cache. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| resource | resource of the span event being processed | pcommon.Resource |
| resource.attributes | resource attributes of the span event being processed | pcommon.Map |
| resource.attributes\[""\] | the value of the resource attribute of the span event being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| instrumentation_scope | instrumentation scope of the span event being processed | pcommon.InstrumentationScope |
| instrumentation_scope.name | name of the instrumentation scope of the span event being processed | string |
| instrumentation_scope.version | version of the instrumentation scope of the span event being processed | string |
| instrumentation_scope.attributes | instrumentation scope attributes of the span event being processed | pcommon.Map |
| instrumentation_scope.attributes\[""\] | the value of the instrumentation scope attribute of the span event being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| span | span of the span event being processed | ptrace.Span |
| path | field accessed | type |
|----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|
| cache | the value of the current transform context's temporary cache. cache can be used as a temporary placeholder for data during complex transformations | pcommon.Map |
| cache\[""\] | the value of an item in cache. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| resource | resource of the span event being processed | pcommon.Resource |
| resource.attributes | resource attributes of the span event being processed | pcommon.Map |
| resource.attributes\[""\] | the value of the resource attribute of the span event being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| instrumentation_scope | instrumentation scope of the span event being processed | pcommon.InstrumentationScope |
| instrumentation_scope.name | name of the instrumentation scope of the span event being processed | string |
| instrumentation_scope.version | version of the instrumentation scope of the span event being processed | string |
| instrumentation_scope.attributes | instrumentation scope attributes of the span event being processed | pcommon.Map |
| instrumentation_scope.attributes\[""\] | the value of the instrumentation scope attribute of the span event being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| span | span of the span event being processed | ptrace.Span |
| span.* | All fields exposed by the [ottlspan context](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/contexts/ottlspan) can accessed via `span.` | varies |
| attributes | attributes of the span event being processed | pcommon.Map |
| attributes\[""\] | the value of the attribute of the span event being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| time_unix_nano | time_unix_nano of the span event being processed | int64 |
| time | time of the span event being processed | `time.Time` |
| name | name of the span event being processed | string |
| dropped_attributes_count | dropped_attributes_count of the span event being processed | int64 |
| attributes | attributes of the span event being processed | pcommon.Map |
| attributes\[""\] | the value of the attribute of the span event being processed. Supports multiple indexes to access nested fields. | string, bool, int64, float64, pcommon.Map, pcommon.Slice, []byte or nil |
| time_unix_nano | time_unix_nano of the span event being processed | int64 |
| time | time of the span event being processed | `time.Time` |
| name | name of the span event being processed | string |
| dropped_attributes_count | dropped_attributes_count of the span event being processed | int64 |
| event_index | index of the span event within the span | int64 |

## Enums

Expand Down
28 changes: 28 additions & 0 deletions pkg/ottl/contexts/ottlspanevent/span_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type TransformContext struct {
cache pcommon.Map
scopeSpans ptrace.ScopeSpans
resouceSpans ptrace.ResourceSpans
eventIndex int64
}

func (tCtx TransformContext) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
Expand All @@ -47,12 +48,21 @@ func (tCtx TransformContext) MarshalLogObject(encoder zapcore.ObjectEncoder) err
err = errors.Join(err, encoder.AddObject("span", logging.Span(tCtx.span)))
err = errors.Join(err, encoder.AddObject("spanevent", logging.SpanEvent(tCtx.spanEvent)))
err = errors.Join(err, encoder.AddObject("cache", logging.Map(tCtx.cache)))
encoder.AddInt64("eventindex", tCtx.eventIndex)
return err
}

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

func NewTransformContext(spanEvent ptrace.SpanEvent, span ptrace.Span, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource, scopeSpans ptrace.ScopeSpans, resourceSpans ptrace.ResourceSpans) TransformContext {
// calculate the event index
index := -1
for i := 0; i < span.Events().Len(); i++ {
if span.Events().At(i) == spanEvent {
index = i
break
}
}
return TransformContext{
spanEvent: spanEvent,
span: span,
Expand All @@ -61,6 +71,7 @@ func NewTransformContext(spanEvent ptrace.SpanEvent, span ptrace.Span, instrumen
cache: pcommon.NewMap(),
scopeSpans: scopeSpans,
resouceSpans: resourceSpans,
eventIndex: int64(index),
}
}

Expand Down Expand Up @@ -92,6 +103,10 @@ func (tCtx TransformContext) GetResourceSchemaURLItem() internal.SchemaURLItem {
return tCtx.resouceSpans
}

func (tCtx TransformContext) GetEventIndex() int64 {
return tCtx.eventIndex
}

func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySettings component.TelemetrySettings, options ...Option) (ottl.Parser[TransformContext], error) {
pep := pathExpressionParser{telemetrySettings}
p, err := ottl.NewParser[TransformContext](
Expand Down Expand Up @@ -206,6 +221,8 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot
return accessSpanEventAttributesKey(path.Keys()), nil
case "dropped_attributes_count":
return accessSpanEventDroppedAttributeCount(), nil
case "event_index":
return accessSpanEventIndex(), nil
default:
return nil, internal.FormatDefaultErrorMessage(path.Name(), path.String(), contextNameDescription, internal.SpanEventRef)
}
Expand Down Expand Up @@ -333,3 +350,14 @@ func accessSpanEventDroppedAttributeCount() ottl.StandardGetSetter[TransformCont
},
}
}

func accessSpanEventIndex() ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(_ context.Context, tCtx TransformContext) (any, error) {
return tCtx.eventIndex, nil
},
Setter: func(_ context.Context, _ TransformContext, _ any) error {
return nil
bacherfl marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
18 changes: 14 additions & 4 deletions pkg/ottl/contexts/ottlspanevent/span_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ func Test_newPathGetSetter(t *testing.T) {
spanEvent.SetDroppedAttributesCount(20)
},
},
{
name: "event_index",
path: &internal.TestPath[TransformContext]{
N: "event_index",
},
orig: int64(0),
newVal: int64(1),
modified: func(_ ptrace.SpanEvent, _ ptrace.Span, _ pcommon.InstrumentationScope, _ pcommon.Resource, _ pcommon.Map) {
},
},
}
// Copy all tests cases and sets the path.Context value to the generated ones.
// It ensures all exiting field access also work when the path context is set.
Expand Down Expand Up @@ -521,7 +531,10 @@ func Test_newPathGetSetter_higherContextPath(t *testing.T) {
}

func createTelemetry() (ptrace.SpanEvent, ptrace.Span, pcommon.InstrumentationScope, pcommon.Resource) {
spanEvent := ptrace.NewSpanEvent()
span := ptrace.NewSpan()
span.SetName("test")

spanEvent := span.Events().AppendEmpty()

spanEvent.SetName("bear")
spanEvent.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(100)))
Expand Down Expand Up @@ -562,9 +575,6 @@ func createTelemetry() (ptrace.SpanEvent, ptrace.Span, pcommon.InstrumentationSc
s := spanEvent.Attributes().PutEmptySlice("slice")
s.AppendEmpty().SetEmptyMap().PutStr("map", "pass")

span := ptrace.NewSpan()
span.SetName("test")

il := pcommon.NewInstrumentationScope()
il.SetName("library")
il.SetVersion("version")
Expand Down
56 changes: 56 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspan"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspanevent"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/ptracetest"
Expand Down Expand Up @@ -1122,6 +1123,40 @@ func Test_ProcessTraces_TraceContext(t *testing.T) {
}
}

func Test_ProcessSpanEvents(t *testing.T) {
tests := []struct {
statement string
want func(_ ottlspanevent.TransformContext)
}{
{
statement: `set(attributes["index"], event_index)`,
want: func(tCtx ottlspanevent.TransformContext) {
tCtx.GetSpanEvent().Attributes().PutInt("index", 0)
},
},
}

for _, tt := range tests {
t.Run(tt.statement, func(t *testing.T) {
settings := componenttest.NewNopTelemetrySettings()
funcs := ottlfuncs.StandardFuncs[ottlspanevent.TransformContext]()

spanEventParser, err := ottlspanevent.NewParser(funcs, settings)
assert.NoError(t, err)
spanStatements, err := spanEventParser.ParseStatement(tt.statement)
assert.NoError(t, err)

tCtx := constructSpanEventTransformContext()
_, _, _ = spanStatements.Execute(context.Background(), tCtx)

exTCtx := constructSpanEventTransformContext()
tt.want(exTCtx)

assert.NoError(t, ptracetest.CompareSpanEvent(newSpanEvent(exTCtx), newSpanEvent(tCtx)))
})
}
}

func constructLogTransformContext() ottllog.TransformContext {
resource := pcommon.NewResource()
resource.Attributes().PutStr("host.name", "localhost")
Expand Down Expand Up @@ -1176,6 +1211,21 @@ func constructSpanTransformContext() ottlspan.TransformContext {
return ottlspan.NewTransformContext(td, scope, resource, ptrace.NewScopeSpans(), ptrace.NewResourceSpans())
}

func constructSpanEventTransformContext() ottlspanevent.TransformContext {
resource := pcommon.NewResource()

scope := pcommon.NewInstrumentationScope()
scope.SetName("scope")

span := ptrace.NewSpan()
fillSpanOne(span)

ev1 := span.Events().AppendEmpty()
ev1.SetName("event-1")

return ottlspanevent.NewTransformContext(ev1, span, scope, resource, ptrace.NewScopeSpans(), ptrace.NewResourceSpans())
}

func newResourceLogs(tCtx ottllog.TransformContext) plog.ResourceLogs {
rl := plog.NewResourceLogs()
tCtx.GetResource().CopyTo(rl.Resource())
Expand All @@ -1196,6 +1246,12 @@ func newResourceSpans(tCtx ottlspan.TransformContext) ptrace.ResourceSpans {
return rl
}

func newSpanEvent(tCtx ottlspanevent.TransformContext) ptrace.SpanEvent {
dst := ptrace.NewSpanEvent()
tCtx.GetSpanEvent().CopyTo(dst)
return dst
}

func fillSpanOne(span ptrace.Span) {
span.SetName("operationB")
span.SetSpanID(spanID)
Expand Down
Loading