Skip to content

Commit

Permalink
Merge branch 'main' into promreceiver-unit-test-protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
krajorama authored Nov 13, 2023
2 parents bc5e5f3 + 78da59a commit 0cb6476
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 18 deletions.
27 changes: 27 additions & 0 deletions .chloggen/issue-27897-IsBool-Converter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

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

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

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

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

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
28 changes: 28 additions & 0 deletions .chloggen/mx-psi_change-to-string.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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: processor/resourcedetection

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add `processor.resourcedetection.hostCPUModelAndFamilyAsString` feature gate to change the type of `host.cpu.family` and `host.cpu.model.id` attributes from `int` to `string`.

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

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
This feature gate will graduate to beta in the next release.
# 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: []
2 changes: 2 additions & 0 deletions pkg/ottl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ The following types are supported for single-value parameters in OTTL functions:
- `StringLikeGetter`
- `IntGetter`
- `IntLikeGetter`
- `BoolGetter`
- `BoolLikeGetter`
- `Enum`
- `string`
- `float64`
Expand Down
93 changes: 93 additions & 0 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,41 @@ func (g StandardFloatGetter[K]) Get(ctx context.Context, tCtx K) (float64, error
}
}

// BoolGetter is a Getter that must return a bool.
type BoolGetter[K any] interface {
// Get retrieves a bool value.
Get(ctx context.Context, tCtx K) (bool, error)
}

// StandardBoolGetter is a basic implementation of BoolGetter
type StandardBoolGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}

// Get retrieves a bool value.
// If the value is not a bool a new TypeError is returned.
// If there is an error getting the value it will be returned.
func (g StandardBoolGetter[K]) Get(ctx context.Context, tCtx K) (bool, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return false, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return false, TypeError("expected bool but got nil")
}
switch v := val.(type) {
case bool:
return v, nil
case pcommon.Value:
if v.Type() == pcommon.ValueTypeBool {
return v.Bool(), nil
}
return false, TypeError(fmt.Sprintf("expected bool but got %v", v.Type()))
default:
return false, TypeError(fmt.Sprintf("expected bool but got %T", val))
}
}

// FunctionGetter uses a function factory to return an instantiated function as an Expr.
type FunctionGetter[K any] interface {
Get(args Arguments) (Expr[K], error)
Expand Down Expand Up @@ -507,6 +542,64 @@ func (g StandardIntLikeGetter[K]) Get(ctx context.Context, tCtx K) (*int64, erro
return &result, nil
}

// BoolLikeGetter is a Getter that returns a bool by converting the underlying value to a bool if necessary.
type BoolLikeGetter[K any] interface {
// Get retrieves a bool value.
// Unlike `BoolGetter`, the expectation is that the underlying value is converted to a bool if possible.
// If the value cannot be converted to a bool, nil and an error are returned.
// If the value is nil, nil is returned without an error.
Get(ctx context.Context, tCtx K) (*bool, error)
}

type StandardBoolLikeGetter[K any] struct {
Getter func(ctx context.Context, tCtx K) (any, error)
}

func (g StandardBoolLikeGetter[K]) Get(ctx context.Context, tCtx K) (*bool, error) {
val, err := g.Getter(ctx, tCtx)
if err != nil {
return nil, fmt.Errorf("error getting value in %T: %w", g, err)
}
if val == nil {
return nil, nil
}
var result bool
switch v := val.(type) {
case bool:
result = v
case int:
result = v != 0
case int64:
result = v != 0
case string:
result, err = strconv.ParseBool(v)
if err != nil {
return nil, err
}
case float64:
result = v != 0.0
case pcommon.Value:
switch v.Type() {
case pcommon.ValueTypeBool:
result = v.Bool()
case pcommon.ValueTypeInt:
result = v.Int() != 0
case pcommon.ValueTypeStr:
result, err = strconv.ParseBool(v.Str())
if err != nil {
return nil, err
}
case pcommon.ValueTypeDouble:
result = v.Double() != 0.0
default:
return nil, TypeError(fmt.Sprintf("unsupported value type: %v", v.Type()))
}
default:
return nil, TypeError(fmt.Sprintf("unsupported type: %T", val))
}
return &result, nil
}

func (p *Parser[K]) newGetter(val value) (Getter[K], error) {
if val.IsNil != nil && *val.IsNil {
return &literal[K]{value: nil}, nil
Expand Down
Loading

0 comments on commit 0cb6476

Please sign in to comment.