Skip to content

Commit

Permalink
Merge branch 'main' into balance-metrics-by-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
SHaaD94 committed Apr 17, 2024
2 parents 13d6284 + 134c3a8 commit e09d644
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 5 deletions.
22 changes: 22 additions & 0 deletions .chloggen/ottl-islist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `IsList` OTTL Function

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

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
4 changes: 2 additions & 2 deletions internal/coreinternal/timeutils/internal/ctimefmt/ctimefmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ var ctimeSubstitutes = map[string]string{
// %S - Second as a zero-padded decimal number (00, 01, ..., 59)
// %L - Millisecond as a decimal number, zero-padded on the left (000, 001, ..., 999)
// %f - Microsecond as a decimal number, zero-padded on the left (000000, ..., 999999)
// %s - Nanosecond as a decimal number, zero-padded on the left (000000, ..., 999999)
// %s - Nanosecond as a decimal number, zero-padded on the left (00000000, ..., 99999999)
// %z - UTC offset in the form ±HHMM[SS[.ffffff]] or empty(+0000, -0400)
// %Z - Timezone name or abbreviation or empty (UTC, EST, CST)
// %D, %x - Short MM/DD/YY date, equivalent to %m/%d/%y
// %D, %x - Short MM/DD/YYYY date, equivalent to %m/%d/%y
// %F - Short YYYY-MM-DD date, equivalent to %Y-%m-%d
// %T, %X - ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S
// %r - 12-hour clock time (02:55:02 pm)
Expand Down
6 changes: 6 additions & 0 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ func Test_e2e_converters(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
statement: `set(attributes["test"], "pass") where IsList(attributes["foo"]["slice"])`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
statement: `set(attributes["test"], "pass") where IsMatch("aa123bb", "\\d{3}")`,
want: func(tCtx ottllog.TransformContext) {
Expand Down
66 changes: 63 additions & 3 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ Available Converters:
- [IsInt](#isint)
- [IsMap](#ismap)
- [IsMatch](#ismatch)
- [IsList](#islist)
- [IsString](#isstring)
- [Len](#len)
- [Log](#log)
Expand Down Expand Up @@ -708,6 +709,22 @@ Examples:

- `IsMatch("string", ".*ring")`

### IsList

`IsList(value)`

The `IsList` Converter returns true if the given value is a list.

The `value` is either a path expression to a telemetry field to retrieve or a literal.

If `value` is a `list`, `pcommon.ValueTypeSlice`. `pcommon.Slice`, or any other list type, then returns `true`, otherwise returns `false`.

Examples:

- `IsList(body)`

- `IsList(attributes["maybe a slice"])`

### IsString

`IsString(value)`
Expand Down Expand Up @@ -1114,15 +1131,58 @@ Examples:

### Time

The `Time` Converter takes a string representation of a time and converts it to a Golang `time.Time`.
`Time(target, format)`

`time` is a string. `format` is a string.
The `Time` Converter takes a string representation of a time and converts it to a Golang `time.Time`.

If either `time` or `format` are nil, an error is returned. The parser used is the parser at [internal/coreinternal/parser](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/internal/coreinternal/timeutils). If the time and format do not follow the parsing rules used by this parser, an error is returned.
`target` is a string. `format` is a string.

If either `target` or `format` are nil, an error is returned. The parser used is the parser at [internal/coreinternal/parser](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/internal/coreinternal/timeutils). If the `target` and `format` do not follow the parsing rules used by this parser, an error is returned.

`format` denotes a textual representation of the time value formatted according to ctime-like format string. It follows [standard Go Layout formatting](https://pkg.go.dev/time#pkg-constants) with few additional substitutes:
| substitution | description | examples |
|-----|-----|-----|
|`%Y` | Year as a zero-padded number | 0001, 0002, ..., 2019, 2020, ..., 9999 |
|`%y` | Year, last two digits as a zero-padded number | 01, ..., 99 |
|`%m` | Month as a zero-padded number | 01, 02, ..., 12 |
|`%o` | Month as a space-padded number | 1, 2, ..., 12 |
|`%q` | Month as an unpadded number | 1,2,...,12 |
|`%b`, `%h` | Abbreviated month name | Jan, Feb, ... |
|`%B` | Full month name | January, February, ... |
|`%d` | Day of the month as a zero-padded number | 01, 02, ..., 31 |
|`%e` | Day of the month as a space-padded number| 1, 2, ..., 31 |
|`%g` | Day of the month as a unpadded number | 1,2,...,31 |
|`%a` | Abbreviated weekday name | Sun, Mon, ... |
|`%A` | Full weekday name | Sunday, Monday, ... |
|`%H` | Hour (24-hour clock) as a zero-padded number | 00, ..., 24 |
|`%I` | Hour (12-hour clock) as a zero-padded number | 00, ..., 12 |
|`%l` | Hour 12-hour clock | 0, ..., 24 |
|`%p` | Locale’s equivalent of either AM or PM | AM, PM |
|`%P` | Locale’s equivalent of either am or pm | am, pm |
|`%M` | Minute as a zero-padded number | 00, 01, ..., 59 |
|`%S` | Second as a zero-padded number | 00, 01, ..., 59 |
|`%L` | Millisecond as a zero-padded number | 000, 001, ..., 999 |
|`%f` | Microsecond as a zero-padded number | 000000, ..., 999999 |
|`%s` | Nanosecond as a zero-padded number | 00000000, ..., 99999999 |
|`%z` | UTC offset in the form ±HHMM[SS[.ffffff]] or empty | +0000, -0400 |
|`%Z` | Timezone name or abbreviation or empty | UTC, EST, CST |
|`%D`, `%x` | Short MM/DD/YYYY date, equivalent to %m/%d/%y | 01/21/2031 |
|`%F` | Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2031-01-21 |
|`%T`,`%X` | ISO 8601 time format (HH:MM:SS), equivalent to %H:%M:%S | 02:55:02 |
|`%r` | 12-hour clock time | 02:55:02 pm |
|`%R` | 24-hour HH:MM time, equivalent to %H:%M | 13:55 |
|`%n` | New-line character ('\n') | |
|`%t` | Horizontal-tab character ('\t') | |
|`%%` | A % sign | |
|`%c` | Date and time representation | Mon Jan 02 15:04:05 2006 |

Examples:

- `Time("02/04/2023", "%m/%d/%Y")`
- `Time("Feb 15, 2023", "%b %d, %Y")`
- `Time("2023-05-26 12:34:56 HST", "%Y-%m-%d %H:%M:%S %Z")`
- `Time("1986-10-01T00:17:33 MST", "%Y-%m-%dT%H:%M:%S %Z")`
- `Time("2012-11-01T22:08:41+0000 EST", "%Y-%m-%dT%H:%M:%S%z %Z")`

### TraceID

Expand Down
53 changes: 53 additions & 0 deletions pkg/ottl/ottlfuncs/func_is_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"

import (
"context"
"fmt"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"

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

type IsListArguments[K any] struct {
Target ottl.Getter[K]
}

func NewIsListFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("IsList", &IsListArguments[K]{}, createIsListFunction[K])
}

func createIsListFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*IsListArguments[K])

if !ok {
return nil, fmt.Errorf("IsListFactory args must be of type *IsListArguments[K]")
}

return isList(args.Target), nil
}

func isList[K any](target ottl.Getter[K]) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
val, err := target.Get(ctx, tCtx)
if err != nil {
return false, err
}

switch valType := val.(type) {
case pcommon.Value:
return valType.Type() == pcommon.ValueTypeSlice, nil

case pcommon.Slice, plog.LogRecordSlice, plog.ResourceLogsSlice, plog.ScopeLogsSlice, pmetric.ExemplarSlice, pmetric.ExponentialHistogramDataPointSlice, pmetric.HistogramDataPointSlice, pmetric.MetricSlice, pmetric.NumberDataPointSlice, pmetric.ResourceMetricsSlice, pmetric.ScopeMetricsSlice, pmetric.SummaryDataPointSlice, pmetric.SummaryDataPointValueAtQuantileSlice, ptrace.ResourceSpansSlice, ptrace.ScopeSpansSlice, ptrace.SpanEventSlice, ptrace.SpanLinkSlice, ptrace.SpanSlice, []string, []bool, []int64, []float64, [][]byte, []any:
return true, nil
}

return false, nil
}
}
189 changes: 189 additions & 0 deletions pkg/ottl/ottlfuncs/func_is_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"

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

func Test_IsList(t *testing.T) {
tests := []struct {
name string
value any
expected bool
}{
{
name: "map",
value: make(map[string]any, 0),
expected: false,
},
{
name: "ValueTypeMap",
value: pcommon.NewValueMap(),
expected: false,
},
{
name: "not map",
value: "not a map",
expected: false,
},
{
name: "ValueTypeSlice",
value: pcommon.NewValueSlice(),
expected: true,
},
{
name: "nil",
value: nil,
expected: false,
},
{
name: "plog.LogRecordSlice",
value: plog.NewLogRecordSlice(),
expected: true,
},
{
name: "plog.ResourceLogsSlice",
value: plog.NewResourceLogsSlice(),
expected: true,
},
{
name: "plog.ScopeLogsSlice",
value: plog.NewScopeLogsSlice(),
expected: true,
},
{
name: "pmetric.ExemplarSlice",
value: pmetric.NewExemplarSlice(),
expected: true,
},
{
name: "pmetric.ExponentialHistogramDataPointSlice",
value: pmetric.NewExponentialHistogramDataPointSlice(),
expected: true,
},
{
name: "pmetric.HistogramDataPointSlice",
value: pmetric.NewHistogramDataPointSlice(),
expected: true,
},
{
name: "pmetric.MetricSlice",
value: pmetric.NewMetricSlice(),
expected: true,
},
{
name: "pmetric.NumberDataPointSlice",
value: pmetric.NewNumberDataPointSlice(),
expected: true,
},
{
name: "pmetric.ResourceMetricsSlice",
value: pmetric.NewResourceMetricsSlice(),
expected: true,
},
{
name: "pmetric.ScopeMetricsSlice",
value: pmetric.NewScopeMetricsSlice(),
expected: true,
},
{
name: "pmetric.SummaryDataPointSlice",
value: pmetric.NewSummaryDataPointSlice(),
expected: true,
},
{
name: "pmetric.SummaryDataPointValueAtQuantileSlice",
value: pmetric.NewSummaryDataPointValueAtQuantileSlice(),
expected: true,
},
{
name: "ptrace.ResourceSpansSlice",
value: ptrace.NewResourceSpansSlice(),
expected: true,
},
{
name: "ptrace.ScopeSpansSlice",
value: ptrace.NewScopeSpansSlice(),
expected: true,
},
{
name: "ptrace.SpanEventSlice",
value: ptrace.NewSpanEventSlice(),
expected: true,
},
{
name: "ptrace.SpanLinkSlice",
value: ptrace.NewSpanLinkSlice(),
expected: true,
},
{
name: "ptrace.SpanSlice",
value: ptrace.NewSpanSlice(),
expected: true,
},
{
name: "[]string",
value: []string{},
expected: true,
},
{
name: "[]bool",
value: []bool{},
expected: true,
},
{
name: "[]int64",
value: []int64{},
expected: true,
},
{
name: "[]float64",
value: []float64{},
expected: true,
},
{
name: "[][]byte",
value: [][]byte{},
expected: true,
},
{
name: "[]any",
value: []any{},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc := isList[any](&ottl.StandardGetSetter[any]{
Getter: func(context.Context, any) (any, error) {
return tt.value, nil
},
})
result, err := exprFunc(context.Background(), nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}

func Test_IsList_Error(t *testing.T) {
exprFunc := isList[any](&ottl.StandardGetSetter[any]{
Getter: func(context.Context, any) (any, error) {
return nil, ottl.TypeError("")
},
})
result, err := exprFunc(context.Background(), nil)
assert.Equal(t, false, result)
assert.IsType(t, ottl.TypeError(""), err)
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func converters[K any]() []ottl.Factory[K] {
NewIntFactory[K](),
NewIsBoolFactory[K](),
NewIsDoubleFactory[K](),
NewIsListFactory[K](),
NewIsIntFactory[K](),
NewIsMapFactory[K](),
NewIsMatchFactory[K](),
Expand Down

0 comments on commit e09d644

Please sign in to comment.