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

feat: Add new stdlib function to_json #2860

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ravishankar15
Copy link
Contributor

PR Description

Adds a new stdlib function for json encoding to_json which converts a map to JSON

Which issue(s) this PR fixes

Fixes #152

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated

`encoding.to_json` fails if the map argument provided can't be parsed as json string.

A common use case of `encoding.to_json` is to encode a config of component which is expected to be a JSON
For example, `config` of a [`prometheus.exporter.blackbox`][]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
For example, `config` of a [`prometheus.exporter.blackbox`][]
For example, configuration of a [`prometheus.exporter.blackbox`][]

Missing a word here... a prometheus.exporter.blackbox string?

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 wanted to refer the param name in the component updated accordingly.

Copy link
Contributor

@wildum wildum left a comment

Choose a reason for hiding this comment

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

Thanks, I agree that only supporting map is better and if users need another type we can always extend it later :)

Copy link
Contributor

@wildum wildum left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Over to @clayton-cornell for another doc review

Copy link
Contributor

@clayton-cornell clayton-cornell left a comment

Choose a reason for hiding this comment

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

Two small doc changes. If the question I have on line 70 is "it depends" then we can leave it as-is for now and just add the two missing . on lines 69 and 70.

`encoding.to_json` fails if the input argument provided can't be parsed as JSON string.

A common use case for `encoding.to_json` is to encode a configuration of component which is expected to be a JSON string
For example, `config` param of [`prometheus.exporter.blackbox`][]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
For example, `config` param of [`prometheus.exporter.blackbox`][]
For example, `config` param of [`prometheus.exporter.blackbox`][].

I'm not sure what "config param" means in this context.

Can this be reworked as:

For example, a configuration parameter in [prometheus.exporter.blackbox][].

Does this mean the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prometheus.exporter.blackbox component accepts a argument called config which is expected to be a JSON string. May be changing the word param can be more appropriate I believe. https://grafana.com/docs/alloy/latest/reference/components/prometheus/prometheus.exporter.blackbox/#arguments

@clayton-cornell clayton-cornell added the type/docs Docs Squad label across all Grafana Labs repos label Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/docs Docs Squad label across all Grafana Labs repos
Projects
None yet
Development

Successfully merging this pull request may close these issues.

syntax: Add json_encode to stdlib
3 participants