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

add expansion module support to rhai #3240

Merged
merged 14 commits into from
Jun 29, 2023
Merged

add expansion module support to rhai #3240

merged 14 commits into from
Jun 29, 2023

Conversation

garypen
Copy link
Contributor

@garypen garypen commented Jun 8, 2023

This refactors existing functionality within Expansion to expose a new function for environment variable access. This leverages the existing mechanism for limiting access to environment variables.

This functionality is then exposed to rhai scripts as in this example code:

    try {
        print(`LANG: ${env::get("LANG")}`);
    } catch(err) {
        print(`exception: ${err}`);
    }

In the sample, we use the new env module to access environment variables, in this case "LANG".

fixes: #1744

Checklist

Complete the checklist (and note appropriate exceptions) before a final PR is raised.

  • Changes are compatible[^1]
  • Documentation[^2] completed
  • Performance impact assessed and acceptable
  • Tests added and passing[^3]
    • Unit Tests
    • Integration Tests
    • Manual Tests

Exceptions

Note any exceptions here

Notes

[^1]. It may be appropriate to bring upcoming changes to the attention of other (impacted) groups. Please endeavour to do this before seeking PR approval. The mechanism for doing this will vary considerably, so use your judgement as to how and when to do this.
[^2]. Configuration is an important part of many changes. Where applicable please try to document configuration examples.
[^3]. Tick whichever testing boxes are applicable. If you are adding Manual Tests:
- please document the manual testing (extensively) in the Exceptions.
- please raise a separate issue to automate the test and label it (or ask for it to be labeled) as manual test

fixes: #1744

This refactors existing functionality within `Expansion` to add support
for a default rhai expander and to expose a new function for environment
variable access. This leverages the existing mechanism for limiting
access to environment variables.

This functionality is then exposed to rhai scripts as in this example
code:

```
    let expander = expansion::create();
    try {
        print(`LANG: ${expansion::env(expander, "LANG")}`);
    } catch(err) {
        print(`exception: ${err}`);
    }
```

In the sample, we use the new expansion module to create an expander. We
then use the expander to access environment variables, in this case
"LANG".
@garypen garypen requested review from BrynCooke and a team June 8, 2023 12:32
@garypen garypen self-assigned this Jun 8, 2023
@garypen garypen requested review from bnjjj and o0Ignition0o June 8, 2023 12:32
@github-actions

This comment has been minimized.

@router-perf
Copy link

router-perf bot commented Jun 8, 2023

CI performance tests

  • const - Basic stress test that runs with a constant number of users
  • events_without_dedup - Stress test for events with a lot of users and deduplication DISABLED
  • no-graphos - Basic stress test, no GraphOS.
  • step - Basic stress test that steps up the number of users over time
  • reload - Reload test over a long period of time at a constant rate of users
  • xlarge-request - Stress test with 10 MB request payload
  • events_big_cap_high_rate - Stress test for events with a lot of users, deduplication enabled and high rate event with a big queue capacity
  • large-request - Stress test with a 1 MB request payload
  • events - Stress test for events with a lot of users and deduplication ENABLED
  • xxlarge-request - Stress test with 100 MB request payload

garypen added 2 commits June 8, 2023 13:41
Introduce private prefix() fn to use from default() and default_rhai().
@@ -142,6 +144,18 @@ impl Expansion {
}
}

pub(crate) fn expand_env(&self, key: &str) -> Result<Option<String>, ConfigurationError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that the error message in here is targeted at config expansion.
The rhai expansion should have it's own error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want the Rhai functionality to look/smell the same, so I think this message is perfect. In the future we may add more configuration/expansion capabilities into Rhai, so deviation is probably not a good thing.

garypen added 4 commits June 8, 2023 14:57
make cargo happy
Adding the global flag to the env method resolves the object method
invocation problem.

also: add docs and unit tests for the new methods.
@garypen garypen requested a review from StephenBarlow as a code owner June 14, 2023 13:13
garypen added 2 commits June 28, 2023 11:17
The expansion module is now exposed as the `router_env` module. The
`create()` function is removed and the `env()` method is replaced by the
`get()` function.

Docs are updated.
@garypen garypen requested review from BrynCooke and bnjjj June 28, 2023 12:19
@garypen garypen enabled auto-merge (squash) June 29, 2023 12:48
@garypen garypen merged commit 2a0cf86 into dev Jun 29, 2023
@garypen garypen deleted the garypen/1744-rhai-env branch June 29, 2023 13:06
@BrynCooke BrynCooke mentioned this pull request Jul 7, 2023
BrynCooke pushed a commit that referenced this pull request Jul 12, 2023
This refactors existing functionality within `Expansion` to expose a new
function for environment variable access. This leverages the existing
mechanism for limiting access to environment variables.

This functionality is then exposed to rhai scripts as in this example
code:

```
    try {
        print(`LANG: ${env::get("LANG")}`);
    } catch(err) {
        print(`exception: ${err}`);
    }
```

In the sample, we use the new env module to access environment
variables, in this case "LANG".

fixes: #1744

<!-- start metadata -->

**Checklist**

Complete the checklist (and note appropriate exceptions) before a final
PR is raised.

- [x] Changes are compatible[^1]
- [x] Documentation[^2] completed
- [x] Performance impact assessed and acceptable
- Tests added and passing[^3]
    - [x] Unit Tests
    - [ ] Integration Tests
    - [ ] Manual Tests

**Exceptions**

*Note any exceptions here*

**Notes**

[^1]. It may be appropriate to bring upcoming changes to the attention
of other (impacted) groups. Please endeavour to do this before seeking
PR approval. The mechanism for doing this will vary considerably, so use
your judgement as to how and when to do this.
[^2]. Configuration is an important part of many changes. Where
applicable please try to document configuration examples.
[^3]. Tick whichever testing boxes are applicable. If you are adding
Manual Tests:
- please document the manual testing (extensively) in the Exceptions.
- please raise a separate issue to automate the test and label it (or
ask for it to be labeled) as `manual test`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add support for accessing environment variables from rhai scripts
3 participants