diff --git a/pkg/ottl/contexts/internal/ctxdatapoint/context.go b/pkg/ottl/contexts/internal/ctxdatapoint/context.go index bef98abbdaa10..29af6f2fe2594 100644 --- a/pkg/ottl/contexts/internal/ctxdatapoint/context.go +++ b/pkg/ottl/contexts/internal/ctxdatapoint/context.go @@ -7,3 +7,7 @@ const ( Name = "datapoint" DocRef = "https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/contexts/ottldatapoint" ) + +type Context interface { + GetDataPoint() any +} diff --git a/pkg/ottl/contexts/internal/ctxdatapoint/datapoint.go b/pkg/ottl/contexts/internal/ctxdatapoint/datapoint.go new file mode 100644 index 0000000000000..b41893c285da6 --- /dev/null +++ b/pkg/ottl/contexts/internal/ctxdatapoint/datapoint.go @@ -0,0 +1,656 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ctxdatapoint // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxdatapoint" + +import ( + "context" + "time" + + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil" +) + +func PathGetSetter[K Context](path ottl.Path[K]) (ottl.GetSetter[K], error) { + if path == nil { + return nil, ctxerror.New("nil", "nil", Name, DocRef) + } + switch path.Name() { + case "attributes": + if path.Keys() == nil { + return accessAttributes[K](), nil + } + return accessAttributesKey(path.Keys()), nil + case "start_time_unix_nano": + return accessStartTimeUnixNano[K](), nil + case "time_unix_nano": + return accessTimeUnixNano[K](), nil + case "start_time": + return accessStartTime[K](), nil + case "time": + return accessTime[K](), nil + case "value_double": + return accessDoubleValue[K](), nil + case "value_int": + return accessIntValue[K](), nil + case "exemplars": + return accessExemplars[K](), nil + case "flags": + return accessFlags[K](), nil + case "count": + return accessCount[K](), nil + case "sum": + return accessSum[K](), nil + case "bucket_counts": + return accessBucketCounts[K](), nil + case "explicit_bounds": + return accessExplicitBounds[K](), nil + case "scale": + return accessScale[K](), nil + case "zero_count": + return accessZeroCount[K](), nil + case "positive": + nextPath := path.Next() + if nextPath != nil { + switch nextPath.Name() { + case "offset": + return accessPositiveOffset[K](), nil + case "bucket_counts": + return accessPositiveBucketCounts[K](), nil + default: + return nil, ctxerror.New(nextPath.Name(), path.String(), Name, DocRef) + } + } + return accessPositive[K](), nil + case "negative": + nextPath := path.Next() + if nextPath != nil { + switch nextPath.Name() { + case "offset": + return accessNegativeOffset[K](), nil + case "bucket_counts": + return accessNegativeBucketCounts[K](), nil + default: + return nil, ctxerror.New(nextPath.Name(), path.String(), Name, DocRef) + } + } + return accessNegative[K](), nil + case "quantile_values": + return accessQuantileValues[K](), nil + default: + return nil, ctxerror.New(path.Name(), path.String(), Name, DocRef) + } +} + +func accessAttributes[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), nil + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), nil + case pmetric.SummaryDataPoint: + return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + if attrs, ok := val.(pcommon.Map); ok { + attrs.CopyTo(tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes()) + } + case pmetric.HistogramDataPoint: + if attrs, ok := val.(pcommon.Map); ok { + attrs.CopyTo(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes()) + } + case pmetric.ExponentialHistogramDataPoint: + if attrs, ok := val.(pcommon.Map); ok { + attrs.CopyTo(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes()) + } + case pmetric.SummaryDataPoint: + if attrs, ok := val.(pcommon.Map); ok { + attrs.CopyTo(tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes()) + } + } + return nil + }, + } +} + +func accessAttributesKey[K Context](key []ottl.Key[K]) ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(ctx context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return ctxutil.GetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key) + case pmetric.HistogramDataPoint: + return ctxutil.GetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key) + case pmetric.ExponentialHistogramDataPoint: + return ctxutil.GetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key) + case pmetric.SummaryDataPoint: + return ctxutil.GetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key) + } + return nil, nil + }, + Setter: func(ctx context.Context, tCtx K, val any) error { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return ctxutil.SetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key, val) + case pmetric.HistogramDataPoint: + return ctxutil.SetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key, val) + case pmetric.ExponentialHistogramDataPoint: + return ctxutil.SetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key, val) + case pmetric.SummaryDataPoint: + return ctxutil.SetMapValue(ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key, val) + } + return nil + }, + } +} + +func accessStartTimeUnixNano[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return tCtx.GetDataPoint().(pmetric.NumberDataPoint).StartTimestamp().AsTime().UnixNano(), nil + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).StartTimestamp().AsTime().UnixNano(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).StartTimestamp().AsTime().UnixNano(), nil + case pmetric.SummaryDataPoint: + return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).StartTimestamp().AsTime().UnixNano(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newTime, ok := val.(int64); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + } + } + return nil + }, + } +} + +func accessStartTime[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return tCtx.GetDataPoint().(pmetric.NumberDataPoint).StartTimestamp().AsTime(), nil + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).StartTimestamp().AsTime(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).StartTimestamp().AsTime(), nil + case pmetric.SummaryDataPoint: + return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).StartTimestamp().AsTime(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newTime, ok := val.(time.Time); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) + } + } + return nil + }, + } +} + +func accessTimeUnixNano[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Timestamp().AsTime().UnixNano(), nil + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Timestamp().AsTime().UnixNano(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Timestamp().AsTime().UnixNano(), nil + case pmetric.SummaryDataPoint: + return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Timestamp().AsTime().UnixNano(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newTime, ok := val.(int64); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) + } + } + return nil + }, + } +} + +func accessTime[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Timestamp().AsTime(), nil + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Timestamp().AsTime(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Timestamp().AsTime(), nil + case pmetric.SummaryDataPoint: + return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Timestamp().AsTime(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newTime, ok := val.(time.Time); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) + } + } + return nil + }, + } +} + +func accessDoubleValue[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { + return numberDataPoint.DoubleValue(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newDouble, ok := val.(float64); ok { + if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { + numberDataPoint.SetDoubleValue(newDouble) + } + } + return nil + }, + } +} + +func accessIntValue[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { + return numberDataPoint.IntValue(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newInt, ok := val.(int64); ok { + if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { + numberDataPoint.SetIntValue(newInt) + } + } + return nil + }, + } +} + +func accessExemplars[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Exemplars(), nil + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Exemplars(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Exemplars(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newExemplars, ok := val.(pmetric.ExemplarSlice); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + newExemplars.CopyTo(tCtx.GetDataPoint().(pmetric.NumberDataPoint).Exemplars()) + case pmetric.HistogramDataPoint: + newExemplars.CopyTo(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Exemplars()) + case pmetric.ExponentialHistogramDataPoint: + newExemplars.CopyTo(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Exemplars()) + } + } + return nil + }, + } +} + +func accessFlags[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.NumberDataPoint).Flags()), nil + case pmetric.HistogramDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Flags()), nil + case pmetric.ExponentialHistogramDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Flags()), nil + case pmetric.SummaryDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Flags()), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newFlags, ok := val.(int64); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.NumberDataPoint: + tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) + } + } + return nil + }, + } +} + +func accessCount[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.HistogramDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Count()), nil + case pmetric.ExponentialHistogramDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Count()), nil + case pmetric.SummaryDataPoint: + return int64(tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Count()), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newCount, ok := val.(int64); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetCount(uint64(newCount)) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetCount(uint64(newCount)) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetCount(uint64(newCount)) + } + } + return nil + }, + } +} + +func accessSum[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + switch tCtx.GetDataPoint().(type) { + case pmetric.HistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Sum(), nil + case pmetric.ExponentialHistogramDataPoint: + return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Sum(), nil + case pmetric.SummaryDataPoint: + return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Sum(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newSum, ok := val.(float64); ok { + switch tCtx.GetDataPoint().(type) { + case pmetric.HistogramDataPoint: + tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetSum(newSum) + case pmetric.ExponentialHistogramDataPoint: + tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetSum(newSum) + case pmetric.SummaryDataPoint: + tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetSum(newSum) + } + } + return nil + }, + } +} + +func accessExplicitBounds[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { + return histogramDataPoint.ExplicitBounds().AsRaw(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newExplicitBounds, ok := val.([]float64); ok { + if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { + histogramDataPoint.ExplicitBounds().FromRaw(newExplicitBounds) + } + } + return nil + }, + } +} + +func accessBucketCounts[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { + return histogramDataPoint.BucketCounts().AsRaw(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newBucketCount, ok := val.([]uint64); ok { + if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { + histogramDataPoint.BucketCounts().FromRaw(newBucketCount) + } + } + return nil + }, + } +} + +func accessScale[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return int64(expoHistogramDataPoint.Scale()), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newScale, ok := val.(int64); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + expoHistogramDataPoint.SetScale(int32(newScale)) + } + } + return nil + }, + } +} + +func accessZeroCount[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return int64(expoHistogramDataPoint.ZeroCount()), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newZeroCount, ok := val.(int64); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + expoHistogramDataPoint.SetZeroCount(uint64(newZeroCount)) + } + } + return nil + }, + } +} + +func accessPositive[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return expoHistogramDataPoint.Positive(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newPositive, ok := val.(pmetric.ExponentialHistogramDataPointBuckets); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + newPositive.CopyTo(expoHistogramDataPoint.Positive()) + } + } + return nil + }, + } +} + +func accessPositiveOffset[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return int64(expoHistogramDataPoint.Positive().Offset()), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newPositiveOffset, ok := val.(int64); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + expoHistogramDataPoint.Positive().SetOffset(int32(newPositiveOffset)) + } + } + return nil + }, + } +} + +func accessPositiveBucketCounts[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return expoHistogramDataPoint.Positive().BucketCounts().AsRaw(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newPositiveBucketCounts, ok := val.([]uint64); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + expoHistogramDataPoint.Positive().BucketCounts().FromRaw(newPositiveBucketCounts) + } + } + return nil + }, + } +} + +func accessNegative[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return expoHistogramDataPoint.Negative(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newNegative, ok := val.(pmetric.ExponentialHistogramDataPointBuckets); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + newNegative.CopyTo(expoHistogramDataPoint.Negative()) + } + } + return nil + }, + } +} + +func accessNegativeOffset[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return int64(expoHistogramDataPoint.Negative().Offset()), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newNegativeOffset, ok := val.(int64); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + expoHistogramDataPoint.Negative().SetOffset(int32(newNegativeOffset)) + } + } + return nil + }, + } +} + +func accessNegativeBucketCounts[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + return expoHistogramDataPoint.Negative().BucketCounts().AsRaw(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newNegativeBucketCounts, ok := val.([]uint64); ok { + if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { + expoHistogramDataPoint.Negative().BucketCounts().FromRaw(newNegativeBucketCounts) + } + } + return nil + }, + } +} + +func accessQuantileValues[K Context]() ottl.StandardGetSetter[K] { + return ottl.StandardGetSetter[K]{ + Getter: func(_ context.Context, tCtx K) (any, error) { + if summaryDataPoint, ok := tCtx.GetDataPoint().(pmetric.SummaryDataPoint); ok { + return summaryDataPoint.QuantileValues(), nil + } + return nil, nil + }, + Setter: func(_ context.Context, tCtx K, val any) error { + if newQuantileValues, ok := val.(pmetric.SummaryDataPointValueAtQuantileSlice); ok { + if summaryDataPoint, ok := tCtx.GetDataPoint().(pmetric.SummaryDataPoint); ok { + newQuantileValues.CopyTo(summaryDataPoint.QuantileValues()) + } + } + return nil + }, + } +} diff --git a/pkg/ottl/contexts/internal/ctxdatapoint/datapoint_test.go b/pkg/ottl/contexts/internal/ctxdatapoint/datapoint_test.go new file mode 100644 index 0000000000000..3d715b85e032c --- /dev/null +++ b/pkg/ottl/contexts/internal/ctxdatapoint/datapoint_test.go @@ -0,0 +1,1989 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ctxdatapoint_test + +import ( + "context" + "slices" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxdatapoint" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/pathtest" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest" +) + +func TestPathGetSetter_NumberDataPoint(t *testing.T) { + refNumberDataPoint := createNumberDataPoint(pmetric.NumberDataPointValueTypeInt) + + newExemplars := createExamplarSlice() + newAttrs := createAttributeMap() + + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]any) + newMap2 := make(map[string]any) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + + tests := []struct { + name string + path ottl.Path[*testContext] + orig any + newVal any + modified func(pmetric.NumberDataPoint) + valueType pmetric.NumberDataPointValueType + }{ + { + name: "start_time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "start_time_unix_nano", + }, + orig: int64(100_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "time_unix_nano", + }, + orig: int64(500_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "start_time", + path: &pathtest.Path[*testContext]{ + N: "start_time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 100000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 86400000000000, time.UTC), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(86400, 0))) + }, + }, + { + name: "time", + path: &pathtest.Path[*testContext]{ + N: "time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 500000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 200000000, time.UTC), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "value_double", + path: &pathtest.Path[*testContext]{ + N: "value_double", + }, + orig: 1.1, + newVal: 2.2, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetDoubleValue(2.2) + }, + valueType: pmetric.NumberDataPointValueTypeDouble, + }, + { + name: "value_int", + path: &pathtest.Path[*testContext]{ + N: "value_int", + }, + orig: int64(1), + newVal: int64(2), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetIntValue(2) + }, + }, + { + name: "flags", + path: &pathtest.Path[*testContext]{ + N: "flags", + }, + orig: int64(0), + newVal: int64(1), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true)) + }, + }, + { + name: "exemplars", + path: &pathtest.Path[*testContext]{ + N: "exemplars", + }, + orig: refNumberDataPoint.Exemplars(), + newVal: newExemplars, + modified: func(datapoint pmetric.NumberDataPoint) { + newExemplars.CopyTo(datapoint.Exemplars()) + }, + }, + { + name: "attributes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + }, + orig: refNumberDataPoint.Attributes(), + newVal: newAttrs, + modified: func(datapoint pmetric.NumberDataPoint) { + newAttrs.CopyTo(datapoint.Attributes()) + }, + }, + { + name: "attributes string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("str"), + }, + }, + }, + orig: "val", + newVal: "newVal", + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutStr("str", "newVal") + }, + }, + { + name: "attributes bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bool"), + }, + }, + }, + orig: true, + newVal: false, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutBool("bool", false) + }, + }, + { + name: "attributes int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("int"), + }, + }, + }, + orig: int64(10), + newVal: int64(20), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutInt("int", 20) + }, + }, + { + name: "attributes float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("double"), + }, + }, + }, + orig: float64(1.2), + newVal: float64(2.4), + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutDouble("double", 2.4) + }, + }, + { + name: "attributes bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bytes"), + }, + }, + }, + orig: []byte{1, 3, 2}, + newVal: []byte{2, 3, 4}, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptyBytes("bytes").FromRaw([]byte{2, 3, 4}) + }, + }, + { + name: "attributes array string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_str"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refNumberDataPoint.Attributes().Get("arr_str") + return val.Slice() + }(), + newVal: []string{"new"}, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_str").AppendEmpty().SetStr("new") + }, + }, + { + name: "attributes array bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bool"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refNumberDataPoint.Attributes().Get("arr_bool") + return val.Slice() + }(), + newVal: []bool{false}, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bool").AppendEmpty().SetBool(false) + }, + }, + { + name: "attributes array int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_int"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refNumberDataPoint.Attributes().Get("arr_int") + return val.Slice() + }(), + newVal: []int64{20}, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_int").AppendEmpty().SetInt(20) + }, + }, + { + name: "attributes array float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_float"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refNumberDataPoint.Attributes().Get("arr_float") + return val.Slice() + }(), + newVal: []float64{2.0}, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_float").AppendEmpty().SetDouble(2.0) + }, + }, + { + name: "attributes array bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bytes"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refNumberDataPoint.Attributes().Get("arr_bytes") + return val.Slice() + }(), + newVal: [][]byte{{9, 6, 4}}, + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) + }, + }, + { + name: "attributes pcommon.Map", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("pMap"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refNumberDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.NumberDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]any", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refNumberDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.NumberDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes nested", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("slice"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() string { + val, _ := refNumberDataPoint.Attributes().Get("slice") + val, _ = val.Slice().At(0).Map().Get("map") + return val.Str() + }(), + newVal: "new", + modified: func(datapoint pmetric.NumberDataPoint) { + datapoint.Attributes().PutEmptySlice("slice").AppendEmpty().SetEmptyMap().PutStr("map", "new") + }, + }, + { + name: "attributes nested new values", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("new"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(2), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + }, + }, + orig: func() any { + return nil + }(), + newVal: "new", + modified: func(datapoint pmetric.NumberDataPoint) { + s := datapoint.Attributes().PutEmptySlice("new") + s.AppendEmpty() + s.AppendEmpty() + s.AppendEmpty().SetEmptySlice().AppendEmpty().SetStr("new") + }, + }, + } + // 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. + for _, tt := range slices.Clone(tests) { + testWithContext := tt + testWithContext.name = "with_path_context:" + tt.name + pathWithContext := *tt.path.(*pathtest.Path[*testContext]) + pathWithContext.C = ctxdatapoint.Name + testWithContext.path = ottl.Path[*testContext](&pathWithContext) + tests = append(tests, testWithContext) + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + accessor, err := ctxdatapoint.PathGetSetter(tt.path) + assert.NoError(t, err) + + numberDataPoint := createNumberDataPoint(tt.valueType) + + ctx := newContext(numberDataPoint) + + got, err := accessor.Get(context.Background(), ctx) + assert.NoError(t, err) + assert.Equal(t, tt.orig, got) + + err = accessor.Set(context.Background(), ctx, tt.newVal) + assert.NoError(t, err) + + exNumberDataPoint := createNumberDataPoint(tt.valueType) + tt.modified(exNumberDataPoint) + + assert.Equal(t, exNumberDataPoint, numberDataPoint) + }) + } +} + +func createNumberDataPoint(valueType pmetric.NumberDataPointValueType) pmetric.NumberDataPoint { + numberDataPoint := pmetric.NewNumberDataPoint() + numberDataPoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(100))) + numberDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(500))) + + if valueType == pmetric.NumberDataPointValueTypeDouble { + numberDataPoint.SetDoubleValue(1.1) + } else { + numberDataPoint.SetIntValue(1) + } + + createAttributeTelemetry(numberDataPoint.Attributes()) + + numberDataPoint.Exemplars().AppendEmpty().SetIntValue(0) + + return numberDataPoint +} + +func TestPathGetSetter_HistogramDataPoint(t *testing.T) { + refHistogramDataPoint := createHistogramDataPointTelemetry() + + newExemplars := createExamplarSlice() + newAttrs := createAttributeMap() + + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]any) + newMap2 := make(map[string]any) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + + tests := []struct { + name string + path ottl.Path[*testContext] + orig any + newVal any + modified func(pmetric.HistogramDataPoint) + }{ + { + name: "start_time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "start_time_unix_nano", + }, + orig: int64(100_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "time_unix_nano", + }, + orig: int64(500_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "start_time", + path: &pathtest.Path[*testContext]{ + N: "start_time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 100000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 86400000000000, time.UTC), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(86400, 0))) + }, + }, + { + name: "time", + path: &pathtest.Path[*testContext]{ + N: "time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 500000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 200000000, time.UTC), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "flags", + path: &pathtest.Path[*testContext]{ + N: "flags", + }, + orig: int64(0), + newVal: int64(1), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true)) + }, + }, + { + name: "count", + path: &pathtest.Path[*testContext]{ + N: "count", + }, + orig: int64(2), + newVal: int64(3), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetCount(3) + }, + }, + { + name: "sum", + path: &pathtest.Path[*testContext]{ + N: "sum", + }, + orig: 10.1, + newVal: 10.2, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.SetSum(10.2) + }, + }, + { + name: "bucket_counts", + path: &pathtest.Path[*testContext]{ + N: "bucket_counts", + }, + orig: []uint64{1, 1}, + newVal: []uint64{1, 2}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.BucketCounts().FromRaw([]uint64{1, 2}) + }, + }, + { + name: "explicit_bounds", + path: &pathtest.Path[*testContext]{ + N: "explicit_bounds", + }, + orig: []float64{1, 2}, + newVal: []float64{1, 2, 3}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.ExplicitBounds().FromRaw([]float64{1, 2, 3}) + }, + }, + { + name: "exemplars", + path: &pathtest.Path[*testContext]{ + N: "exemplars", + }, + orig: refHistogramDataPoint.Exemplars(), + newVal: newExemplars, + modified: func(datapoint pmetric.HistogramDataPoint) { + newExemplars.CopyTo(datapoint.Exemplars()) + }, + }, + { + name: "attributes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + }, + orig: refHistogramDataPoint.Attributes(), + newVal: newAttrs, + modified: func(datapoint pmetric.HistogramDataPoint) { + newAttrs.CopyTo(datapoint.Attributes()) + }, + }, + { + name: "attributes string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("str"), + }, + }, + }, + orig: "val", + newVal: "newVal", + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutStr("str", "newVal") + }, + }, + { + name: "attributes bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bool"), + }, + }, + }, + orig: true, + newVal: false, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutBool("bool", false) + }, + }, + { + name: "attributes int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("int"), + }, + }, + }, + orig: int64(10), + newVal: int64(20), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutInt("int", 20) + }, + }, + { + name: "attributes float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("double"), + }, + }, + }, + orig: float64(1.2), + newVal: float64(2.4), + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutDouble("double", 2.4) + }, + }, + { + name: "attributes bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bytes"), + }, + }, + }, + orig: []byte{1, 3, 2}, + newVal: []byte{2, 3, 4}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptyBytes("bytes").FromRaw([]byte{2, 3, 4}) + }, + }, + { + name: "attributes array string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_str"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refHistogramDataPoint.Attributes().Get("arr_str") + return val.Slice() + }(), + newVal: []string{"new"}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_str").AppendEmpty().SetStr("new") + }, + }, + { + name: "attributes array bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bool"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refHistogramDataPoint.Attributes().Get("arr_bool") + return val.Slice() + }(), + newVal: []bool{false}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bool").AppendEmpty().SetBool(false) + }, + }, + { + name: "attributes array int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_int"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refHistogramDataPoint.Attributes().Get("arr_int") + return val.Slice() + }(), + newVal: []int64{20}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_int").AppendEmpty().SetInt(20) + }, + }, + { + name: "attributes array float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_float"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refHistogramDataPoint.Attributes().Get("arr_float") + return val.Slice() + }(), + newVal: []float64{2.0}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_float").AppendEmpty().SetDouble(2.0) + }, + }, + { + name: "attributes array bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bytes"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refHistogramDataPoint.Attributes().Get("arr_bytes") + return val.Slice() + }(), + newVal: [][]byte{{9, 6, 4}}, + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) + }, + }, + { + name: "attributes pcommon.Map", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("pMap"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refHistogramDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.HistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]any", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refHistogramDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.HistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes nested", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("slice"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() string { + val, _ := refHistogramDataPoint.Attributes().Get("slice") + val, _ = val.Slice().At(0).Map().Get("map") + return val.Str() + }(), + newVal: "new", + modified: func(datapoint pmetric.HistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("slice").AppendEmpty().SetEmptyMap().PutStr("map", "new") + }, + }, + { + name: "attributes nested new values", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("new"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(2), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + }, + }, + orig: func() any { + return nil + }(), + newVal: "new", + modified: func(datapoint pmetric.HistogramDataPoint) { + s := datapoint.Attributes().PutEmptySlice("new") + s.AppendEmpty() + s.AppendEmpty() + s.AppendEmpty().SetEmptySlice().AppendEmpty().SetStr("new") + }, + }, + } + // 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. + for _, tt := range slices.Clone(tests) { + testWithContext := tt + testWithContext.name = "with_path_context:" + tt.name + pathWithContext := *tt.path.(*pathtest.Path[*testContext]) + pathWithContext.C = ctxdatapoint.Name + testWithContext.path = ottl.Path[*testContext](&pathWithContext) + tests = append(tests, testWithContext) + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + accessor, err := ctxdatapoint.PathGetSetter(tt.path) + assert.NoError(t, err) + + histogramDataPoint := createHistogramDataPointTelemetry() + + ctx := newContext(histogramDataPoint) + + got, err := accessor.Get(context.Background(), ctx) + assert.NoError(t, err) + assert.Equal(t, tt.orig, got) + + err = accessor.Set(context.Background(), ctx, tt.newVal) + assert.NoError(t, err) + + exNumberDataPoint := createHistogramDataPointTelemetry() + tt.modified(exNumberDataPoint) + + assert.Equal(t, exNumberDataPoint, histogramDataPoint) + }) + } +} + +func createHistogramDataPointTelemetry() pmetric.HistogramDataPoint { + histogramDataPoint := pmetric.NewHistogramDataPoint() + histogramDataPoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(100))) + histogramDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(500))) + histogramDataPoint.SetCount(2) + histogramDataPoint.SetSum(10.1) + histogramDataPoint.BucketCounts().FromRaw([]uint64{1, 1}) + histogramDataPoint.ExplicitBounds().FromRaw([]float64{1, 2}) + + createAttributeTelemetry(histogramDataPoint.Attributes()) + + histogramDataPoint.Exemplars().AppendEmpty().SetIntValue(0) + + return histogramDataPoint +} + +func TestPathGetSetter_ExpoHistogramDataPoint(t *testing.T) { + refExpoHistogramDataPoint := createExpoHistogramDataPointTelemetry() + + newExemplars := createExamplarSlice() + newAttrs := createAttributeMap() + + newPositive := pmetric.NewExponentialHistogramDataPointBuckets() + newPositive.SetOffset(10) + newPositive.BucketCounts().FromRaw([]uint64{4, 5}) + + newNegative := pmetric.NewExponentialHistogramDataPointBuckets() + newNegative.SetOffset(10) + newNegative.BucketCounts().FromRaw([]uint64{4, 5}) + + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]any) + newMap2 := make(map[string]any) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + + tests := []struct { + name string + path ottl.Path[*testContext] + orig any + newVal any + modified func(pmetric.ExponentialHistogramDataPoint) + }{ + { + name: "start_time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "start_time_unix_nano", + }, + orig: int64(100_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "time_unix_nano", + }, + orig: int64(500_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "start_time", + path: &pathtest.Path[*testContext]{ + N: "start_time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 100000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 86400000000000, time.UTC), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(86400, 0))) + }, + }, + { + name: "time", + path: &pathtest.Path[*testContext]{ + N: "time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 500000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 200000000, time.UTC), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "flags", + path: &pathtest.Path[*testContext]{ + N: "flags", + }, + orig: int64(0), + newVal: int64(1), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true)) + }, + }, + { + name: "count", + path: &pathtest.Path[*testContext]{ + N: "count", + }, + orig: int64(2), + newVal: int64(3), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetCount(3) + }, + }, + { + name: "sum", + path: &pathtest.Path[*testContext]{ + N: "sum", + }, + orig: 10.1, + newVal: 10.2, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetSum(10.2) + }, + }, + { + name: "scale", + path: &pathtest.Path[*testContext]{ + N: "scale", + }, + orig: int64(1), + newVal: int64(2), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetScale(2) + }, + }, + { + name: "zero_count", + path: &pathtest.Path[*testContext]{ + N: "zero_count", + }, + orig: int64(1), + newVal: int64(2), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.SetZeroCount(2) + }, + }, + { + name: "positive", + path: &pathtest.Path[*testContext]{ + N: "positive", + }, + orig: refExpoHistogramDataPoint.Positive(), + newVal: newPositive, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + newPositive.CopyTo(datapoint.Positive()) + }, + }, + { + name: "positive offset", + path: &pathtest.Path[*testContext]{ + N: "positive", + NextPath: &pathtest.Path[*testContext]{ + N: "offset", + }, + }, + orig: int64(1), + newVal: int64(2), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Positive().SetOffset(2) + }, + }, + { + name: "positive bucket_counts", + path: &pathtest.Path[*testContext]{ + N: "positive", + NextPath: &pathtest.Path[*testContext]{ + N: "bucket_counts", + }, + }, + orig: []uint64{1, 1}, + newVal: []uint64{0, 1, 2}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Positive().BucketCounts().FromRaw([]uint64{0, 1, 2}) + }, + }, + { + name: "negative", + path: &pathtest.Path[*testContext]{ + N: "negative", + }, + orig: refExpoHistogramDataPoint.Negative(), + newVal: newPositive, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + newPositive.CopyTo(datapoint.Negative()) + }, + }, + { + name: "negative offset", + path: &pathtest.Path[*testContext]{ + N: "negative", + NextPath: &pathtest.Path[*testContext]{ + N: "offset", + }, + }, + orig: int64(1), + newVal: int64(2), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Negative().SetOffset(2) + }, + }, + { + name: "negative bucket_counts", + path: &pathtest.Path[*testContext]{ + N: "negative", + NextPath: &pathtest.Path[*testContext]{ + N: "bucket_counts", + }, + }, + orig: []uint64{1, 1}, + newVal: []uint64{0, 1, 2}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Negative().BucketCounts().FromRaw([]uint64{0, 1, 2}) + }, + }, + { + name: "exemplars", + path: &pathtest.Path[*testContext]{ + N: "exemplars", + }, + orig: refExpoHistogramDataPoint.Exemplars(), + newVal: newExemplars, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + newExemplars.CopyTo(datapoint.Exemplars()) + }, + }, + { + name: "attributes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + }, + orig: refExpoHistogramDataPoint.Attributes(), + newVal: newAttrs, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + newAttrs.CopyTo(datapoint.Attributes()) + }, + }, + { + name: "attributes string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("str"), + }, + }, + }, + orig: "val", + newVal: "newVal", + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutStr("str", "newVal") + }, + }, + { + name: "attributes bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bool"), + }, + }, + }, + orig: true, + newVal: false, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutBool("bool", false) + }, + }, + { + name: "attributes int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("int"), + }, + }, + }, + orig: int64(10), + newVal: int64(20), + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutInt("int", 20) + }, + }, + { + name: "attributes float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("double"), + }, + }, + }, + orig: 1.2, + newVal: 2.4, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutDouble("double", 2.4) + }, + }, + { + name: "attributes bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bytes"), + }, + }, + }, + orig: []byte{1, 3, 2}, + newVal: []byte{2, 3, 4}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptyBytes("bytes").FromRaw([]byte{2, 3, 4}) + }, + }, + { + name: "attributes array string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_str"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_str") + return val.Slice() + }(), + newVal: []string{"new"}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_str").AppendEmpty().SetStr("new") + }, + }, + { + name: "attributes array bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bool"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_bool") + return val.Slice() + }(), + newVal: []bool{false}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bool").AppendEmpty().SetBool(false) + }, + }, + { + name: "attributes array int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_int"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_int") + return val.Slice() + }(), + newVal: []int64{20}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_int").AppendEmpty().SetInt(20) + }, + }, + { + name: "attributes array float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_float"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_float") + return val.Slice() + }(), + newVal: []float64{2.0}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_float").AppendEmpty().SetDouble(2.0) + }, + }, + { + name: "attributes array bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bytes"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refExpoHistogramDataPoint.Attributes().Get("arr_bytes") + return val.Slice() + }(), + newVal: [][]byte{{9, 6, 4}}, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) + }, + }, + { + name: "attributes pcommon.Map", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("pMap"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refExpoHistogramDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]any", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refExpoHistogramDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes nested", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("slice"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() string { + val, _ := refExpoHistogramDataPoint.Attributes().Get("slice") + val, _ = val.Slice().At(0).Map().Get("map") + return val.Str() + }(), + newVal: "new", + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + datapoint.Attributes().PutEmptySlice("slice").AppendEmpty().SetEmptyMap().PutStr("map", "new") + }, + }, + { + name: "attributes nested new values", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("new"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(2), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + }, + }, + orig: func() any { + return nil + }(), + newVal: "new", + modified: func(datapoint pmetric.ExponentialHistogramDataPoint) { + s := datapoint.Attributes().PutEmptySlice("new") + s.AppendEmpty() + s.AppendEmpty() + s.AppendEmpty().SetEmptySlice().AppendEmpty().SetStr("new") + }, + }, + } + // 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. + for _, tt := range slices.Clone(tests) { + testWithContext := tt + testWithContext.name = "with_path_context:" + tt.name + pathWithContext := *tt.path.(*pathtest.Path[*testContext]) + pathWithContext.C = ctxdatapoint.Name + testWithContext.path = ottl.Path[*testContext](&pathWithContext) + tests = append(tests, testWithContext) + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + accessor, err := ctxdatapoint.PathGetSetter(tt.path) + assert.NoError(t, err) + + expoHistogramDataPoint := createExpoHistogramDataPointTelemetry() + + ctx := newContext(expoHistogramDataPoint) + + got, err := accessor.Get(context.Background(), ctx) + assert.NoError(t, err) + assert.Equal(t, tt.orig, got) + + err = accessor.Set(context.Background(), ctx, tt.newVal) + assert.NoError(t, err) + + exNumberDataPoint := createExpoHistogramDataPointTelemetry() + tt.modified(exNumberDataPoint) + + assert.Equal(t, exNumberDataPoint, expoHistogramDataPoint) + }) + } +} + +func createExpoHistogramDataPointTelemetry() pmetric.ExponentialHistogramDataPoint { + expoHistogramDataPoint := pmetric.NewExponentialHistogramDataPoint() + expoHistogramDataPoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(100))) + expoHistogramDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(500))) + expoHistogramDataPoint.SetCount(2) + expoHistogramDataPoint.SetSum(10.1) + expoHistogramDataPoint.SetScale(1) + expoHistogramDataPoint.SetZeroCount(1) + + expoHistogramDataPoint.Positive().BucketCounts().FromRaw([]uint64{1, 1}) + expoHistogramDataPoint.Positive().SetOffset(1) + + expoHistogramDataPoint.Negative().BucketCounts().FromRaw([]uint64{1, 1}) + expoHistogramDataPoint.Negative().SetOffset(1) + + createAttributeTelemetry(expoHistogramDataPoint.Attributes()) + + expoHistogramDataPoint.Exemplars().AppendEmpty().SetIntValue(0) + + return expoHistogramDataPoint +} + +func TestPathGetSetter_SummaryDataPoint(t *testing.T) { + refSummaryDataPoint := createSummaryDataPointTelemetry() + + newAttrs := createAttributeMap() + + newQuartileValues := pmetric.NewSummaryDataPointValueAtQuantileSlice() + newQuartileValues.AppendEmpty().SetValue(100) + + newPMap := pcommon.NewMap() + pMap2 := newPMap.PutEmptyMap("k2") + pMap2.PutStr("k1", "string") + + newMap := make(map[string]any) + newMap2 := make(map[string]any) + newMap2["k1"] = "string" + newMap["k2"] = newMap2 + + tests := []struct { + name string + path ottl.Path[*testContext] + orig any + newVal any + modified func(pmetric.SummaryDataPoint) + }{ + { + name: "start_time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "start_time_unix_nano", + }, + orig: int64(100_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "time_unix_nano", + path: &pathtest.Path[*testContext]{ + N: "time_unix_nano", + }, + orig: int64(500_000_000), + newVal: int64(200_000_000), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "start_time", + path: &pathtest.Path[*testContext]{ + N: "start_time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 100000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 86400000000000, time.UTC), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(86400, 0))) + }, + }, + { + name: "time", + path: &pathtest.Path[*testContext]{ + N: "time", + }, + orig: time.Date(1970, 1, 1, 0, 0, 0, 500000000, time.UTC), + newVal: time.Date(1970, 1, 1, 0, 0, 0, 200000000, time.UTC), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(200))) + }, + }, + { + name: "flags", + path: &pathtest.Path[*testContext]{ + N: "flags", + }, + orig: int64(0), + newVal: int64(1), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetFlags(pmetric.DefaultDataPointFlags.WithNoRecordedValue(true)) + }, + }, + { + name: "count", + path: &pathtest.Path[*testContext]{ + N: "count", + }, + orig: int64(2), + newVal: int64(3), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetCount(3) + }, + }, + { + name: "sum", + path: &pathtest.Path[*testContext]{ + N: "sum", + }, + orig: 10.1, + newVal: 10.2, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.SetSum(10.2) + }, + }, + { + name: "quantile_values", + path: &pathtest.Path[*testContext]{ + N: "quantile_values", + }, + orig: refSummaryDataPoint.QuantileValues(), + newVal: newQuartileValues, + modified: func(datapoint pmetric.SummaryDataPoint) { + newQuartileValues.CopyTo(datapoint.QuantileValues()) + }, + }, + { + name: "attributes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + }, + orig: refSummaryDataPoint.Attributes(), + newVal: newAttrs, + modified: func(datapoint pmetric.SummaryDataPoint) { + newAttrs.CopyTo(datapoint.Attributes()) + }, + }, + { + name: "attributes string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("str"), + }, + }, + }, + orig: "val", + newVal: "newVal", + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutStr("str", "newVal") + }, + }, + { + name: "attributes bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bool"), + }, + }, + }, + orig: true, + newVal: false, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutBool("bool", false) + }, + }, + { + name: "attributes int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("int"), + }, + }, + }, + orig: int64(10), + newVal: int64(20), + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutInt("int", 20) + }, + }, + { + name: "attributes float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("double"), + }, + }, + }, + orig: 1.2, + newVal: 2.4, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutDouble("double", 2.4) + }, + }, + { + name: "attributes bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("bytes"), + }, + }, + }, + orig: []byte{1, 3, 2}, + newVal: []byte{2, 3, 4}, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptyBytes("bytes").FromRaw([]byte{2, 3, 4}) + }, + }, + { + name: "attributes array string", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_str"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refSummaryDataPoint.Attributes().Get("arr_str") + return val.Slice() + }(), + newVal: []string{"new"}, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_str").AppendEmpty().SetStr("new") + }, + }, + { + name: "attributes array bool", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bool"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refSummaryDataPoint.Attributes().Get("arr_bool") + return val.Slice() + }(), + newVal: []bool{false}, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bool").AppendEmpty().SetBool(false) + }, + }, + { + name: "attributes array int", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_int"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refSummaryDataPoint.Attributes().Get("arr_int") + return val.Slice() + }(), + newVal: []int64{20}, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_int").AppendEmpty().SetInt(20) + }, + }, + { + name: "attributes array float", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_float"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refSummaryDataPoint.Attributes().Get("arr_float") + return val.Slice() + }(), + newVal: []float64{2.0}, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_float").AppendEmpty().SetDouble(2.0) + }, + }, + { + name: "attributes array bytes", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("arr_bytes"), + }, + }, + }, + orig: func() pcommon.Slice { + val, _ := refSummaryDataPoint.Attributes().Get("arr_bytes") + return val.Slice() + }(), + newVal: [][]byte{{9, 6, 4}}, + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptySlice("arr_bytes").AppendEmpty().SetEmptyBytes().FromRaw([]byte{9, 6, 4}) + }, + }, + { + name: "attributes pcommon.Map", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("pMap"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refSummaryDataPoint.Attributes().Get("pMap") + return val.Map() + }(), + newVal: newPMap, + modified: func(datapoint pmetric.SummaryDataPoint) { + m := datapoint.Attributes().PutEmptyMap("pMap") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes map[string]any", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() pcommon.Map { + val, _ := refSummaryDataPoint.Attributes().Get("map") + return val.Map() + }(), + newVal: newMap, + modified: func(datapoint pmetric.SummaryDataPoint) { + m := datapoint.Attributes().PutEmptyMap("map") + m2 := m.PutEmptyMap("k2") + m2.PutStr("k1", "string") + }, + }, + { + name: "attributes nested", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("slice"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + &pathtest.Key[*testContext]{ + S: ottltest.Strp("map"), + }, + }, + }, + orig: func() string { + val, _ := refSummaryDataPoint.Attributes().Get("slice") + val, _ = val.Slice().At(0).Map().Get("map") + return val.Str() + }(), + newVal: "new", + modified: func(datapoint pmetric.SummaryDataPoint) { + datapoint.Attributes().PutEmptySlice("slice").AppendEmpty().SetEmptyMap().PutStr("map", "new") + }, + }, + { + name: "attributes nested new values", + path: &pathtest.Path[*testContext]{ + N: "attributes", + KeySlice: []ottl.Key[*testContext]{ + &pathtest.Key[*testContext]{ + S: ottltest.Strp("new"), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(2), + }, + &pathtest.Key[*testContext]{ + I: ottltest.Intp(0), + }, + }, + }, + orig: func() any { + return nil + }(), + newVal: "new", + modified: func(datapoint pmetric.SummaryDataPoint) { + s := datapoint.Attributes().PutEmptySlice("new") + s.AppendEmpty() + s.AppendEmpty() + s.AppendEmpty().SetEmptySlice().AppendEmpty().SetStr("new") + }, + }, + } + // 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. + for _, tt := range slices.Clone(tests) { + testWithContext := tt + testWithContext.name = "with_path_context:" + tt.name + pathWithContext := *tt.path.(*pathtest.Path[*testContext]) + pathWithContext.C = ctxdatapoint.Name + testWithContext.path = ottl.Path[*testContext](&pathWithContext) + tests = append(tests, testWithContext) + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + accessor, err := ctxdatapoint.PathGetSetter(tt.path) + assert.NoError(t, err) + + summaryDataPoint := createSummaryDataPointTelemetry() + + ctx := newContext(summaryDataPoint) + + got, err := accessor.Get(context.Background(), ctx) + assert.NoError(t, err) + assert.Equal(t, tt.orig, got) + + err = accessor.Set(context.Background(), ctx, tt.newVal) + assert.NoError(t, err) + + exNumberDataPoint := createSummaryDataPointTelemetry() + tt.modified(exNumberDataPoint) + + assert.Equal(t, exNumberDataPoint, summaryDataPoint) + }) + } +} + +func createSummaryDataPointTelemetry() pmetric.SummaryDataPoint { + summaryDataPoint := pmetric.NewSummaryDataPoint() + summaryDataPoint.SetStartTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(100))) + summaryDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(time.UnixMilli(500))) + summaryDataPoint.SetCount(2) + summaryDataPoint.SetSum(10.1) + + summaryDataPoint.QuantileValues().AppendEmpty().SetValue(1) + + createAttributeTelemetry(summaryDataPoint.Attributes()) + + return summaryDataPoint +} + +func createAttributeTelemetry(attributes pcommon.Map) { + attributes.PutStr("str", "val") + attributes.PutBool("bool", true) + attributes.PutInt("int", 10) + attributes.PutDouble("double", 1.2) + attributes.PutEmptyBytes("bytes").FromRaw([]byte{1, 3, 2}) + + arrStr := attributes.PutEmptySlice("arr_str") + arrStr.AppendEmpty().SetStr("one") + arrStr.AppendEmpty().SetStr("two") + + arrBool := attributes.PutEmptySlice("arr_bool") + arrBool.AppendEmpty().SetBool(true) + arrBool.AppendEmpty().SetBool(false) + + arrInt := attributes.PutEmptySlice("arr_int") + arrInt.AppendEmpty().SetInt(2) + arrInt.AppendEmpty().SetInt(3) + + arrFloat := attributes.PutEmptySlice("arr_float") + arrFloat.AppendEmpty().SetDouble(1.0) + arrFloat.AppendEmpty().SetDouble(2.0) + + arrBytes := attributes.PutEmptySlice("arr_bytes") + arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{1, 2, 3}) + arrBytes.AppendEmpty().SetEmptyBytes().FromRaw([]byte{2, 3, 4}) + + s := attributes.PutEmptySlice("slice") + s.AppendEmpty().SetEmptyMap().PutStr("map", "pass") + + pMap := attributes.PutEmptyMap("pMap") + pMap.PutStr("original", "map") + + m := attributes.PutEmptyMap("map") + m.PutStr("original", "map") +} + +func createExamplarSlice() pmetric.ExemplarSlice { + newExemplars := pmetric.NewExemplarSlice() + newExemplars.AppendEmpty().SetIntValue(4) + return newExemplars +} + +func createAttributeMap() pcommon.Map { + newAttrs := pcommon.NewMap() + newAttrs.PutStr("hello", "world") + return newAttrs +} + +type testContext struct { + dataPoint any +} + +func (m *testContext) GetDataPoint() any { + return m.dataPoint +} + +func newContext(dataPoint any) *testContext { + return &testContext{dataPoint: dataPoint} +} diff --git a/pkg/ottl/contexts/ottldatapoint/datapoint.go b/pkg/ottl/contexts/ottldatapoint/datapoint.go index 17817811659b8..2dd1aece47d38 100644 --- a/pkg/ottl/contexts/ottldatapoint/datapoint.go +++ b/pkg/ottl/contexts/ottldatapoint/datapoint.go @@ -7,7 +7,6 @@ import ( "context" "errors" "fmt" - "time" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" @@ -228,69 +227,8 @@ func (pep *pathExpressionParser) parsePath(path ottl.Path[TransformContext]) (ot 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, ctxerror.New(nextPath.Name(), path.String(), ctxdatapoint.Name, ctxdatapoint.DocRef) - } - } - 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, ctxerror.New(nextPath.Name(), path.String(), ctxdatapoint.Name, ctxdatapoint.DocRef) - } - } - return accessNegative(), nil - case "quantile_values": - return accessQuantileValues(), nil default: - return nil, ctxerror.New(path.Name(), path.String(), ctxdatapoint.Name, ctxdatapoint.DocRef) + return ctxdatapoint.PathGetSetter(path) } } @@ -335,572 +273,3 @@ func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[Tra }, } } - -func accessAttributes() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), nil - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), nil - case pmetric.SummaryDataPoint: - return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - if attrs, ok := val.(pcommon.Map); ok { - attrs.CopyTo(tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes()) - } - case pmetric.HistogramDataPoint: - if attrs, ok := val.(pcommon.Map); ok { - attrs.CopyTo(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes()) - } - case pmetric.ExponentialHistogramDataPoint: - if attrs, ok := val.(pcommon.Map); ok { - attrs.CopyTo(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes()) - } - case pmetric.SummaryDataPoint: - if attrs, ok := val.(pcommon.Map); ok { - attrs.CopyTo(tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes()) - } - } - return nil - }, - } -} - -func accessAttributesKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(ctx context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key) - case pmetric.HistogramDataPoint: - return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key) - case pmetric.ExponentialHistogramDataPoint: - return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key) - case pmetric.SummaryDataPoint: - return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key) - } - return nil, nil - }, - Setter: func(ctx context.Context, tCtx TransformContext, val any) error { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key, val) - case pmetric.HistogramDataPoint: - return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key, val) - case pmetric.ExponentialHistogramDataPoint: - return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key, val) - case pmetric.SummaryDataPoint: - return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key, val) - } - return nil - }, - } -} - -func accessStartTimeUnixNano() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return tCtx.GetDataPoint().(pmetric.NumberDataPoint).StartTimestamp().AsTime().UnixNano(), nil - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).StartTimestamp().AsTime().UnixNano(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).StartTimestamp().AsTime().UnixNano(), nil - case pmetric.SummaryDataPoint: - return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).StartTimestamp().AsTime().UnixNano(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newTime, ok := val.(int64); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - } - } - return nil - }, - } -} - -func accessStartTime() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return tCtx.GetDataPoint().(pmetric.NumberDataPoint).StartTimestamp().AsTime(), nil - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).StartTimestamp().AsTime(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).StartTimestamp().AsTime(), nil - case pmetric.SummaryDataPoint: - return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).StartTimestamp().AsTime(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newTime, ok := val.(time.Time); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetStartTimestamp(pcommon.NewTimestampFromTime(newTime)) - } - } - return nil - }, - } -} - -func accessTimeUnixNano() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Timestamp().AsTime().UnixNano(), nil - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Timestamp().AsTime().UnixNano(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Timestamp().AsTime().UnixNano(), nil - case pmetric.SummaryDataPoint: - return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Timestamp().AsTime().UnixNano(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newTime, ok := val.(int64); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(time.Unix(0, newTime))) - } - } - return nil - }, - } -} - -func accessTime() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Timestamp().AsTime(), nil - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Timestamp().AsTime(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Timestamp().AsTime(), nil - case pmetric.SummaryDataPoint: - return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Timestamp().AsTime(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newTime, ok := val.(time.Time); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetTimestamp(pcommon.NewTimestampFromTime(newTime)) - } - } - return nil - }, - } -} - -func accessDoubleValue() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { - return numberDataPoint.DoubleValue(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newDouble, ok := val.(float64); ok { - if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { - numberDataPoint.SetDoubleValue(newDouble) - } - } - return nil - }, - } -} - -func accessIntValue() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { - return numberDataPoint.IntValue(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newInt, ok := val.(int64); ok { - if numberDataPoint, ok := tCtx.GetDataPoint().(pmetric.NumberDataPoint); ok { - numberDataPoint.SetIntValue(newInt) - } - } - return nil - }, - } -} - -func accessExemplars() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return tCtx.GetDataPoint().(pmetric.NumberDataPoint).Exemplars(), nil - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Exemplars(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Exemplars(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newExemplars, ok := val.(pmetric.ExemplarSlice); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - newExemplars.CopyTo(tCtx.GetDataPoint().(pmetric.NumberDataPoint).Exemplars()) - case pmetric.HistogramDataPoint: - newExemplars.CopyTo(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Exemplars()) - case pmetric.ExponentialHistogramDataPoint: - newExemplars.CopyTo(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Exemplars()) - } - } - return nil - }, - } -} - -func accessFlags() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.NumberDataPoint).Flags()), nil - case pmetric.HistogramDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Flags()), nil - case pmetric.ExponentialHistogramDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Flags()), nil - case pmetric.SummaryDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Flags()), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newFlags, ok := val.(int64); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.NumberDataPoint: - tCtx.GetDataPoint().(pmetric.NumberDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetFlags(pmetric.DataPointFlags(newFlags)) - } - } - return nil - }, - } -} - -func accessCount() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.HistogramDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Count()), nil - case pmetric.ExponentialHistogramDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Count()), nil - case pmetric.SummaryDataPoint: - return int64(tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Count()), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newCount, ok := val.(int64); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetCount(uint64(newCount)) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetCount(uint64(newCount)) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetCount(uint64(newCount)) - } - } - return nil - }, - } -} - -func accessSum() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - switch tCtx.GetDataPoint().(type) { - case pmetric.HistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Sum(), nil - case pmetric.ExponentialHistogramDataPoint: - return tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Sum(), nil - case pmetric.SummaryDataPoint: - return tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Sum(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newSum, ok := val.(float64); ok { - switch tCtx.GetDataPoint().(type) { - case pmetric.HistogramDataPoint: - tCtx.GetDataPoint().(pmetric.HistogramDataPoint).SetSum(newSum) - case pmetric.ExponentialHistogramDataPoint: - tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).SetSum(newSum) - case pmetric.SummaryDataPoint: - tCtx.GetDataPoint().(pmetric.SummaryDataPoint).SetSum(newSum) - } - } - return nil - }, - } -} - -func accessExplicitBounds() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { - return histogramDataPoint.ExplicitBounds().AsRaw(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newExplicitBounds, ok := val.([]float64); ok { - if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { - histogramDataPoint.ExplicitBounds().FromRaw(newExplicitBounds) - } - } - return nil - }, - } -} - -func accessBucketCounts() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { - return histogramDataPoint.BucketCounts().AsRaw(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newBucketCount, ok := val.([]uint64); ok { - if histogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.HistogramDataPoint); ok { - histogramDataPoint.BucketCounts().FromRaw(newBucketCount) - } - } - return nil - }, - } -} - -func accessScale() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return int64(expoHistogramDataPoint.Scale()), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newScale, ok := val.(int64); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - expoHistogramDataPoint.SetScale(int32(newScale)) - } - } - return nil - }, - } -} - -func accessZeroCount() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return int64(expoHistogramDataPoint.ZeroCount()), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newZeroCount, ok := val.(int64); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - expoHistogramDataPoint.SetZeroCount(uint64(newZeroCount)) - } - } - return nil - }, - } -} - -func accessPositive() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return expoHistogramDataPoint.Positive(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newPositive, ok := val.(pmetric.ExponentialHistogramDataPointBuckets); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - newPositive.CopyTo(expoHistogramDataPoint.Positive()) - } - } - return nil - }, - } -} - -func accessPositiveOffset() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return int64(expoHistogramDataPoint.Positive().Offset()), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newPositiveOffset, ok := val.(int64); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - expoHistogramDataPoint.Positive().SetOffset(int32(newPositiveOffset)) - } - } - return nil - }, - } -} - -func accessPositiveBucketCounts() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return expoHistogramDataPoint.Positive().BucketCounts().AsRaw(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newPositiveBucketCounts, ok := val.([]uint64); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - expoHistogramDataPoint.Positive().BucketCounts().FromRaw(newPositiveBucketCounts) - } - } - return nil - }, - } -} - -func accessNegative() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return expoHistogramDataPoint.Negative(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newNegative, ok := val.(pmetric.ExponentialHistogramDataPointBuckets); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - newNegative.CopyTo(expoHistogramDataPoint.Negative()) - } - } - return nil - }, - } -} - -func accessNegativeOffset() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return int64(expoHistogramDataPoint.Negative().Offset()), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newNegativeOffset, ok := val.(int64); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - expoHistogramDataPoint.Negative().SetOffset(int32(newNegativeOffset)) - } - } - return nil - }, - } -} - -func accessNegativeBucketCounts() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - return expoHistogramDataPoint.Negative().BucketCounts().AsRaw(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newNegativeBucketCounts, ok := val.([]uint64); ok { - if expoHistogramDataPoint, ok := tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint); ok { - expoHistogramDataPoint.Negative().BucketCounts().FromRaw(newNegativeBucketCounts) - } - } - return nil - }, - } -} - -func accessQuantileValues() ottl.StandardGetSetter[TransformContext] { - return ottl.StandardGetSetter[TransformContext]{ - Getter: func(_ context.Context, tCtx TransformContext) (any, error) { - if summaryDataPoint, ok := tCtx.GetDataPoint().(pmetric.SummaryDataPoint); ok { - return summaryDataPoint.QuantileValues(), nil - } - return nil, nil - }, - Setter: func(_ context.Context, tCtx TransformContext, val any) error { - if newQuantileValues, ok := val.(pmetric.SummaryDataPointValueAtQuantileSlice); ok { - if summaryDataPoint, ok := tCtx.GetDataPoint().(pmetric.SummaryDataPoint); ok { - newQuantileValues.CopyTo(summaryDataPoint.QuantileValues()) - } - } - return nil - }, - } -}