Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[processor/transform] not enough preconditions to guard against warnings #23847

Closed
ajsaclayan opened this issue Jun 29, 2023 · 5 comments · Fixed by #24420
Closed

[processor/transform] not enough preconditions to guard against warnings #23847

ajsaclayan opened this issue Jun 29, 2023 · 5 comments · Fixed by #24420
Labels
enhancement New feature or request pkg/ottl priority:p2 Medium processor/transform Transform processor

Comments

@ajsaclayan
Copy link
Contributor

ajsaclayan commented Jun 29, 2023

Component(s)

processor/transform

Is your feature request related to a problem? Please describe.

I have a transform processor configured to truncate log attributes and the body field:

  transform/newrelic:
    log_statements:
      - context: resource
        statements:
          - truncate_all(attributes, 4090)
      - context: log
        statements:
          - truncate_all(attributes, 4090)
          - set(body, Substring(body.string, 0, 4090))

This works, but the problem is when we send a body whose length is less than 4090 this warning message occurs:
invalid range for substring function, 4090 cannot be greater than the length of target string

Describe the solution you'd like

It would be nice to be able to have a precondition like WHERE body.Length() > 4090 to be able to guard against this warning.

Describe alternatives you've considered

No response

Additional context

No response

@ajsaclayan ajsaclayan added enhancement New feature or request needs triage New item requiring triage labels Jun 29, 2023
@github-actions github-actions bot added the processor/transform Transform processor label Jun 29, 2023
@github-actions
Copy link
Contributor

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@jack-berg
Copy link
Member

Another alternative would be to adjust allow truncate to be called on on body when its an AnyValue string. Right now, calling truncate_all(body, 4090) errors with:

failed to execute statement: truncate_all(body, 4095), expected pcommon.Map but got string"}

Definitely useful to have some way to truncate string log bodies in the transform processor.

@TylerHelmuth
Copy link
Member

Both suggestions sound valuable.

It would be good to have a way to check the lengths of things (string, maps, slices, etc). It would also be good to have a function that can truncate strings.

In the meantime, this will work and will not cause a warning when len(body) is less than 4090:

transform/newrelic:
    log_statements:
      - context: resource
        statements:
          - truncate_all(attributes, 4090)
      - context: log
        statements:
          - truncate_all(attributes, 4090)
          - set(cache["body"], body)
          - truncate_all(cache, 4090)
          - set(body, cache["body"])

@TylerHelmuth
Copy link
Member

@ajsaclayan @jack-berg @danelson is this something you'd like to work on?

@TylerHelmuth
Copy link
Member

Technically more efficient solution:

transform/newrelic:
    log_statements:
      - context: resource
        statements:
          - truncate_all(attributes, 4090)
      - context: log
        statements:
          - set(attributes["truncate-body"], body)
          - truncate_all(attributes, 4090)
          - set(body, attributes["truncate-body"])
          - delete_key(attributes, "truncate-body")

But won't be viable if truncate-body already exists in attributes as you'd be overwriting the value.

@TylerHelmuth TylerHelmuth added priority:p2 Medium pkg/ottl and removed needs triage New item requiring triage labels Jun 29, 2023
TylerHelmuth added a commit that referenced this issue Jul 27, 2023
**Description:** 

Add ottl `Len` converter to make `Substring` useful for truncating.
E.g.:
```
    log_statements:
      - context: log
        statements:
        - set(body, Substring(body, 0, 10)) where Len(body) >= 10
```

**Link to tracking Issue:**  Resolves #23847.

**Testing:** Unit tests, local verification with running collector.

**Documentation:** Added documentation to
`pkg/ottl/ottlfuncs/README.md`.

This PR is the result of
[conversation](#23880 (comment))
on #23880.

---------

Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg/ottl priority:p2 Medium processor/transform Transform processor
Projects
None yet
3 participants