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

[chore][pkg/ottl] Move scope and resource PathGetSetters to internal ctx packages #38225

Merged
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
10 changes: 5 additions & 5 deletions pkg/ottl/contexts/internal/ctxdatapoint/datapoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func TestPathGetSetter_NumberDataPoint(t *testing.T) {

numberDataPoint := createNumberDataPoint(tt.valueType)

ctx := newContext(numberDataPoint)
ctx := newTestContext(numberDataPoint)

got, err := accessor.Get(context.Background(), ctx)
assert.NoError(t, err)
Expand Down Expand Up @@ -896,7 +896,7 @@ func TestPathGetSetter_HistogramDataPoint(t *testing.T) {

histogramDataPoint := createHistogramDataPointTelemetry()

ctx := newContext(histogramDataPoint)
ctx := newTestContext(histogramDataPoint)

got, err := accessor.Get(context.Background(), ctx)
assert.NoError(t, err)
Expand Down Expand Up @@ -1446,7 +1446,7 @@ func TestPathGetSetter_ExpoHistogramDataPoint(t *testing.T) {

expoHistogramDataPoint := createExpoHistogramDataPointTelemetry()

ctx := newContext(expoHistogramDataPoint)
ctx := newTestContext(expoHistogramDataPoint)

got, err := accessor.Get(context.Background(), ctx)
assert.NoError(t, err)
Expand Down Expand Up @@ -1896,7 +1896,7 @@ func TestPathGetSetter_SummaryDataPoint(t *testing.T) {

summaryDataPoint := createSummaryDataPointTelemetry()

ctx := newContext(summaryDataPoint)
ctx := newTestContext(summaryDataPoint)

got, err := accessor.Get(context.Background(), ctx)
assert.NoError(t, err)
Expand Down Expand Up @@ -1984,6 +1984,6 @@ func (m *testContext) GetDataPoint() any {
return m.dataPoint
}

func newContext(dataPoint any) *testContext {
func newTestContext(dataPoint any) *testContext {
return &testContext{dataPoint: dataPoint}
}
6 changes: 3 additions & 3 deletions pkg/ottl/contexts/internal/ctxlog/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,11 @@ func TestPathGetSetter(t *testing.T) {

log := createTelemetry(tt.bodyType)

got, err := accessor.Get(context.Background(), newtestContext(log))
got, err := accessor.Get(context.Background(), newTestContext(log))
assert.NoError(t, err)
assert.Equal(t, tt.orig, got)

err = accessor.Set(context.Background(), newtestContext(log), tt.newVal)
err = accessor.Set(context.Background(), newTestContext(log), tt.newVal)
assert.NoError(t, err)

expectedLog := createTelemetry(tt.bodyType)
Expand Down Expand Up @@ -424,6 +424,6 @@ func (l *testContext) GetLogRecord() plog.LogRecord {
return l.log
}

func newtestContext(log plog.LogRecord) *testContext {
func newTestContext(log plog.LogRecord) *testContext {
return &testContext{log: log}
}
6 changes: 3 additions & 3 deletions pkg/ottl/contexts/internal/ctxmetric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func TestPathGetSetter(t *testing.T) {

metric := createTelemetry()

got, err := accessor.Get(context.Background(), newContext(metric))
got, err := accessor.Get(context.Background(), newTestContext(metric))
assert.NoError(t, err)
assert.Equal(t, tt.orig, got)

err = accessor.Set(context.Background(), newContext(metric), tt.newVal)
err = accessor.Set(context.Background(), newTestContext(metric), tt.newVal)
assert.NoError(t, err)

expectedMetric := createTelemetry()
Expand Down Expand Up @@ -149,6 +149,6 @@ func (m *testContext) GetMetric() pmetric.Metric {
return m.metric
}

func newContext(metric pmetric.Metric) *testContext {
func newTestContext(metric pmetric.Metric) *testContext {
return &testContext{metric: metric}
}
11 changes: 11 additions & 0 deletions pkg/ottl/contexts/internal/ctxresource/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@

package ctxresource // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxresource"

import (
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

const (
Name = "resource"
DocRef = "https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/contexts/ottlresource"
)

type Context interface {
GetResource() pcommon.Resource
GetResourceSchemaURLItem() ctxutil.SchemaURLItem
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
package ctxresource // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxresource"

import (
"context"
Expand All @@ -11,18 +11,12 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxcache"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxresource"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

type ResourceContext interface {
GetResource() pcommon.Resource
GetResourceSchemaURLItem() SchemaURLItem
}

func ResourcePathGetSetter[K ResourceContext](lowerContext string, path ottl.Path[K]) (ottl.GetSetter[K], error) {
func PathGetSetter[K Context](lowerContext string, path ottl.Path[K]) (ottl.GetSetter[K], error) {
if path == nil {
return nil, ctxerror.New("nil", "nil", ctxresource.Name, ctxresource.DocRef)
return nil, ctxerror.New("nil", "nil", Name, DocRef)
}
switch path.Name() {
case "attributes":
Expand All @@ -37,11 +31,11 @@ func ResourcePathGetSetter[K ResourceContext](lowerContext string, path ottl.Pat
case "cache":
return nil, ctxcache.NewError(lowerContext, path.Context(), path.String())
default:
return nil, ctxerror.New(path.Name(), path.String(), ctxresource.Name, ctxresource.DocRef)
return nil, ctxerror.New(path.Name(), path.String(), Name, DocRef)
}
}

func accessResourceAttributes[K ResourceContext]() ottl.StandardGetSetter[K] {
func accessResourceAttributes[K Context]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(_ context.Context, tCtx K) (any, error) {
return tCtx.GetResource().Attributes(), nil
Expand All @@ -55,7 +49,7 @@ func accessResourceAttributes[K ResourceContext]() ottl.StandardGetSetter[K] {
}
}

func accessResourceAttributesKey[K ResourceContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
func accessResourceAttributesKey[K Context](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return ctxutil.GetMapValue[K](ctx, tCtx, tCtx.GetResource().Attributes(), keys)
Expand All @@ -66,7 +60,7 @@ func accessResourceAttributesKey[K ResourceContext](keys []ottl.Key[K]) ottl.Sta
}
}

func accessResourceDroppedAttributesCount[K ResourceContext]() ottl.StandardGetSetter[K] {
func accessResourceDroppedAttributesCount[K Context]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(_ context.Context, tCtx K) (any, error) {
return int64(tCtx.GetResource().DroppedAttributesCount()), nil
Expand All @@ -80,7 +74,7 @@ func accessResourceDroppedAttributesCount[K ResourceContext]() ottl.StandardGetS
}
}

func accessResourceSchemaURLItem[K ResourceContext]() ottl.StandardGetSetter[K] {
func accessResourceSchemaURLItem[K Context]() ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(_ context.Context, tCtx K) (any, error) {
return tCtx.GetResourceSchemaURLItem().SchemaUrl(), nil
Expand Down
Loading