Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some confusion about the vrl to_int and greater than documentation #1138

Closed
paomian opened this issue Nov 25, 2024 · 2 comments
Closed

Some confusion about the vrl to_int and greater than documentation #1138

paomian opened this issue Nov 25, 2024 · 2 comments

Comments

@paomian
Copy link

paomian commented Nov 25, 2024

vector version: timberio/vector:0.42.0-debian
I have this example toml to test drop_in_abort

[sources.demo_source]
type = "demo_logs"
format = "apache_common"
lines = [ "line1" ]

[transforms.transform_nginx_log]
type = "remap"
inputs = [ "demo_source" ]
drop_on_error = true
drop_on_abort = true
reroute_dropped = true
source = """
log = parse_apache_log!(.message,format: "common")
if to_int(log.status) > 200 {
  abort
}
. = log
.mark = "transform_nginx_log"
"""

[sinks.transform_nginx_log_sink]
type = "console"
inputs = [ "transform_nginx_log" ]
encoding.codec = "json"


[transforms.transform_nginx_log_dropped]
type = "remap"
inputs = [ "transform_nginx_log.dropped" ]
source = """
.mark = "dropped"
"""

[sinks.dropped_msg_sink]
type = "console"
inputs = [ "transform_nginx_log_dropped" ]
encoding.codec = "json"

But when writing the vrl for transform_nginx_log, I'm a bit confused.
my first version is

log = parse_apache_log!(.message,format: "common")
if log.status > 200 {
  abort
}

but I got error

error[E100]: fallible predicate
  ┌─ :2:4
  │
2 │ if log.status > 200 {
  │    ^^^^^^^^^^^^^^^^
  │    │
  │    expression can result in runtime error
  │    handle the error case to ensure runtime success

Ok,I think it's vrl thinks the type of log.status is uncertain, so I'm going to use to_int to convert the type of log.status. So I followed the documentation and wrote a second version.

log = parse_apache_log!(.message,format: "common")
if to_int!(log.status) > 200 {
  abort
}

I got a new error.

error[E620]: can't abort infallible function
  ┌─ :2:4
  │
2 │ if to_int!(log.status) > 200 {
  │    ^^^^^^- remove this abort-instruction
  │    │
  │    this function can't fail

I'm not sure if I'm missing any content. The documentation says that to_int is failable.
Eventually I got a usable version.

if to_int(log.status) > 200 {
  abort
}

Is there a problem I'm overlooking?

@paomian paomian changed the title Some confusion about the vrl documentation, I don't know how to briefly describe this issue Some confusion about the vrl to_int and greater than documentation Nov 25, 2024
@pront
Copy link
Member

pront commented Nov 25, 2024

Basically the compiler has different type information for each example you mentioned. This example demonstrates when ! is needed and when it is not based on the type info: example

See that log.status is known to be:

"t2": {
  "integer": true,
  "null": true
}

and to_int works on both the above types.

@paomian
Copy link
Author

paomian commented Nov 26, 2024

Thank you. The exception thrown there by the greater-than is log.status, which may be null. I incorrectly assumed that any type was the cause.

@paomian paomian closed this as completed Nov 26, 2024
@vectordotdev vectordotdev locked and limited conversation to collaborators Nov 26, 2024
@pront pront converted this issue into discussion #1144 Nov 26, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants