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][draft] Update statements to reflect their context #35050

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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_change_statements_reflect_context_draft.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 support for statements to express their contexts via path names"

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

# (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: [api]
7 changes: 6 additions & 1 deletion pkg/ottl/contexts/internal/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

const (
MetricContextName = "Metric"
MetricPathContext = "metric"
)

type MetricContext interface {
GetMetric() pmetric.Metric
}
Expand Down Expand Up @@ -47,7 +52,7 @@ func MetricPathGetSetter[K MetricContext](path ottl.Path[K]) (ottl.GetSetter[K],
case "data_points":
return accessDataPoints[K](), nil
default:
return nil, FormatDefaultErrorMessage(path.Name(), path.String(), "Metric", MetricRef)
return nil, FormatDefaultErrorMessage(path.Name(), path.String(), MetricContextName, MetricRef)
}
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/ottl/contexts/internal/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type ResourceContext interface {
GetResourceSchemaURLItem() SchemaURLItem
}

const (
ResourceContextName = "Resource"
ResourcePathContext = "resource"
)

func ResourcePathGetSetter[K ResourceContext](path ottl.Path[K]) (ottl.GetSetter[K], error) {
if path == nil {
return accessResource[K](), nil
Expand All @@ -31,7 +36,7 @@ func ResourcePathGetSetter[K ResourceContext](path ottl.Path[K]) (ottl.GetSetter
case "schema_url":
return accessResourceSchemaURLItem[K](), nil
default:
return nil, FormatDefaultErrorMessage(path.Name(), path.String(), "Resource", ResourceContextRef)
return nil, FormatDefaultErrorMessage(path.Name(), path.String(), ResourceContextName, ResourceContextRef)
}
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/ottl/contexts/internal/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

const (
InstrumentationScopeContextName = "Instrumentation Scope"
InstrumentationScopePathContext = "instrumentation_scope"
ScopePathContext = "scope"
)

type InstrumentationScopeContext interface {
GetInstrumentationScope() pcommon.InstrumentationScope
GetScopeSchemaURLItem() SchemaURLItem
Expand All @@ -36,7 +42,7 @@ func ScopePathGetSetter[K InstrumentationScopeContext](path ottl.Path[K]) (ottl.
case "schema_url":
return accessInstrumentationScopeSchemaURLItem[K](), nil
default:
return nil, FormatDefaultErrorMessage(path.Name(), path.String(), "Instrumentation Scope", InstrumentationScopeRef)
return nil, FormatDefaultErrorMessage(path.Name(), path.String(), InstrumentationScopeContextName, InstrumentationScopeRef)
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/ottl/contexts/internal/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

const (
SpanContextName = "Span"
SpanPathContext = "span"
)

type SpanContext interface {
Expand Down
168 changes: 96 additions & 72 deletions pkg/ottl/contexts/ottldatapoint/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
)

const (
contextName = "DataPoint"
contextName = "DataPoint"
PathContextName = "datapoint"
)

var _ internal.ResourceContext = (*TransformContext)(nil)
Expand Down Expand Up @@ -122,6 +123,17 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
return p, nil
}

func WithPathContextNames() Option {
return func(p *ottl.Parser[TransformContext]) {
ottl.WithPathContextNames[TransformContext]([]string{
PathContextName,
internal.ResourcePathContext,
internal.InstrumentationScopePathContext,
internal.MetricPathContext,
})(p)
}
}

type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])

func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
Expand Down Expand Up @@ -183,81 +195,93 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot
if path == nil {
return nil, fmt.Errorf("path cannot be nil")
}
switch path.Name() {
case "cache":
if path.Keys() == nil {
return accessCache(), nil
}
return accessCacheKey(path.Keys()), nil
case "resource":
return internal.ResourcePathGetSetter[TransformContext](path.Next())
case "instrumentation_scope":
return internal.ScopePathGetSetter[TransformContext](path.Next())
case "metric":
return internal.MetricPathGetSetter[TransformContext](path.Next())
case "attributes":
if path.Keys() == nil {
return accessAttributes(), nil
}
return accessAttributesKey(path.Keys()), nil
case "start_time_unix_nano":
return accessStartTimeUnixNano(), nil
case "time_unix_nano":
return accessTimeUnixNano(), nil
case "start_time":
return accessStartTime(), nil
case "time":
return accessTime(), nil
case "value_double":
return accessDoubleValue(), nil
case "value_int":
return accessIntValue(), nil
case "exemplars":
return accessExemplars(), nil
case "flags":
return accessFlags(), nil
case "count":
return accessCount(), nil
case "sum":
return accessSum(), nil
case "bucket_counts":
return accessBucketCounts(), nil
case "explicit_bounds":
return accessExplicitBounds(), nil
case "scale":
return accessScale(), nil
case "zero_count":
return accessZeroCount(), nil
case "positive":
nextPath := path.Next()
if nextPath != nil {
switch nextPath.Name() {
case "offset":
return accessPositiveOffset(), nil
case "bucket_counts":
return accessPositiveBucketCounts(), nil
default:
return nil, internal.FormatDefaultErrorMessage(nextPath.Name(), path.String(), contextName, internal.DataPointRef)

if path.Context() == PathContextName || path.Context() == "" {
switch path.Name() {
case "cache":
if path.Keys() == nil {
return accessCache(), nil
}
return accessCacheKey(path.Keys()), nil
case "attributes":
if path.Keys() == nil {
return accessAttributes(), nil
}
return accessAttributesKey(path.Keys()), nil
case "start_time_unix_nano":
return accessStartTimeUnixNano(), nil
case "time_unix_nano":
return accessTimeUnixNano(), nil
case "start_time":
return accessStartTime(), nil
case "time":
return accessTime(), nil
case "value_double":
return accessDoubleValue(), nil
case "value_int":
return accessIntValue(), nil
case "exemplars":
return accessExemplars(), nil
case "flags":
return accessFlags(), nil
case "count":
return accessCount(), nil
case "sum":
return accessSum(), nil
case "bucket_counts":
return accessBucketCounts(), nil
case "explicit_bounds":
return accessExplicitBounds(), nil
case "scale":
return accessScale(), nil
case "zero_count":
return accessZeroCount(), nil
case "positive":
nextPath := path.Next()
if nextPath != nil {
switch nextPath.Name() {
case "offset":
return accessPositiveOffset(), nil
case "bucket_counts":
return accessPositiveBucketCounts(), nil
default:
return nil, internal.FormatDefaultErrorMessage(nextPath.Name(), path.String(), contextName, internal.DataPointRef)
}
}
}
return accessPositive(), nil
case "negative":
nextPath := path.Next()
if nextPath != nil {
switch nextPath.Name() {
case "offset":
return accessNegativeOffset(), nil
case "bucket_counts":
return accessNegativeBucketCounts(), nil
default:
return nil, internal.FormatDefaultErrorMessage(nextPath.Name(), path.String(), contextName, internal.DataPointRef)
return accessPositive(), nil
case "negative":
nextPath := path.Next()
if nextPath != nil {
switch nextPath.Name() {
case "offset":
return accessNegativeOffset(), nil
case "bucket_counts":
return accessNegativeBucketCounts(), nil
default:
return nil, internal.FormatDefaultErrorMessage(nextPath.Name(), path.String(), contextName, internal.DataPointRef)
}
}
return accessNegative(), nil
case "quantile_values":
return accessQuantileValues(), nil
default:
return pep.parseLowerContextPath(path.Name(), path.Next()) // BC paths without context
}
return accessNegative(), nil
case "quantile_values":
return accessQuantileValues(), nil
}

return pep.parseLowerContextPath(path.Context(), path)
}

func (pep *pathExpressionParser) parseLowerContextPath(context string, path ottl.Path[TransformContext]) (ottl.GetSetter[TransformContext], error) {
switch context {
case internal.ResourcePathContext:
return internal.ResourcePathGetSetter(path)
case internal.InstrumentationScopePathContext:
return internal.ScopePathGetSetter(path)
case internal.MetricPathContext:
return internal.MetricPathGetSetter(path)
default:
return nil, internal.FormatDefaultErrorMessage(path.Name(), path.String(), contextName, internal.DataPointRef)
return nil, internal.FormatDefaultErrorMessage(context, path.String(), contextName, internal.DataPointRef)
}
}

Expand Down
Loading