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(log_to_metric transform): support tags expansion similar to labels expansion for loki sink #21939

Merged
merged 26 commits into from
Jan 13, 2025

Conversation

titaneric
Copy link
Contributor

@titaneric titaneric commented Dec 3, 2024

Summary

This PR aims to support tags expansion for log_to_metric transform similar Loki's sink's labels expansion. As a new vector contributor, I copy the code from Loki sink and add unit test cases for these changes. If the further code refactor or docs are needed, I would immediately apply the changes. I would like to hear there's a more better implementation to this feature, and I am welcome to hear any comments.

Change Type

  • Bug fix
  • New feature
  • Non-functional (chore, refactoring, docs)
  • Performance

Is this a breaking change?

  • Yes
  • No

How did you test this PR?

make test

Does this PR include user facing changes?

  • Yes. Please add a changelog fragment based on our guidelines.
  • No. A maintainer will apply the "no-changelog" label to this PR.

Checklist

  • Please read our Vector contributor resources.
  • If this PR introduces changes Vector dependencies (modifies Cargo.lock), please
    run dd-rust-license-tool write to regenerate the license inventory and commit the changes (if any). More details here.

References

Close #14744.

Partially solve #15332 ?

@titaneric titaneric requested a review from a team as a code owner December 3, 2024 17:12
@bits-bot
Copy link

bits-bot commented Dec 3, 2024

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the domain: transforms Anything related to Vector's transform components label Dec 3, 2024
@titaneric titaneric changed the title feat(log_to_metric transformer): support tags expansion similar to labels expansion for loki sink feat(log_to_metric transform): support tags expansion similar to labels expansion for loki sink Dec 3, 2024
@titaneric titaneric marked this pull request as draft December 3, 2024 17:32
@pront
Copy link
Member

pront commented Dec 3, 2024

Hi @titaneric, thank you for this contribution. I see this is marked as draft, feel free to give me a shout when you want a review.

@titaneric titaneric force-pushed the feat/tags-expansion branch from 4135b02 to 24a90b5 Compare December 6, 2024 02:09
@titaneric titaneric marked this pull request as ready for review December 6, 2024 02:33
@titaneric
Copy link
Contributor Author

There is a testcase named variants::disk_v2::tests::invariants::reader_deletes_data_file_around_record_id_wraparound did not pass, and it seems that it's not related to my change.

I have added new testcase borrowed from loki's sink. @pront Please take your time to review it.

Copy link
Member

@pront pront left a comment

Choose a reason for hiding this comment

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

Thanks @titaneric, this change makes sense to me 👍

Left a few comments. Also, please run make generate-component-docs

src/transforms/log_to_metric.rs Outdated Show resolved Hide resolved
changelog.d/21939_log_to_metric_tags_expansion.feature.md Outdated Show resolved Hide resolved
src/transforms/log_to_metric.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot added the domain: sinks Anything related to the Vector's sinks label Dec 22, 2024
@titaneric
Copy link
Contributor Author

I have moved the core expansion function to common mod, and adopt them in both loki sink and log_to_metrics transforms. Please review them if you are available!

Copy link
Contributor

@drichards-87 drichards-87 left a comment

Choose a reason for hiding this comment

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

Left a very minor suggestion from Docs and approved the PR.

@titaneric
Copy link
Contributor Author

@pront Would you please take a look at this PR when you are available?

@pront
Copy link
Member

pront commented Jan 3, 2025

@pront Would you please take a look at this PR when you are available?

Yes, it's on my radar.

Copy link
Member

@pront pront left a comment

Choose a reason for hiding this comment

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

Thank you @titaneric, this is looking good. Left some suggestions for minor improvements.

src/common/expansion.rs Outdated Show resolved Hide resolved
src/common/expansion.rs Outdated Show resolved Hide resolved
src/sinks/loki/sink.rs Outdated Show resolved Hide resolved
src/transforms/log_to_metric.rs Outdated Show resolved Hide resolved
src/transforms/log_to_metric.rs Outdated Show resolved Hide resolved
} else {
static_labels.insert(key_s, value_s);
}
let _ = pair_expansion(key_s, value_s, &mut static_labels, &mut dynamic_labels);
Copy link
Member

Choose a reason for hiding this comment

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

Hm, should we check if this returned an error and if so, continue;? Let's be extra careful to not change the loki sink behavior.

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 guess we could leave it unchanged? It's basically do the same thing as expanding structured_metadata.

Copy link
Member

@pront pront Jan 6, 2025

Choose a reason for hiding this comment

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

I am referring to this existing code: https://github.com/vectordotdev/vector/blob/master/src/sinks/loki/sink.rs#L195

It's hard to follow without checking it out locally (too many nested blocks in this code).

Edit:
Can you just double check that the behavior is equivalent?
As long as we figure this out this PR is good to go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, let me double-check it

Copy link
Contributor Author

@titaneric titaneric Jan 6, 2025

Choose a reason for hiding this comment

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

I think they are equivalent. The original loki sink continues the next iteration while having the serde error in the for loop. On the other hand, my implementation return the error from the pair_expansion function, and the caller side just drop the error in the for loop. As a result, they do not continue to make further progress while having the serde error.

Honestly, I think loki sink needs some comments to explain, or handle the error properly instead of dropping it.

Copy link
Member

Choose a reason for hiding this comment

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

Honestly, I think loki sink needs some comments to explain, or handle the error properly instead of dropping it

Agreed!

@titaneric titaneric requested a review from pront January 4, 2025 05:09
src/common/expansion.rs Outdated Show resolved Hide resolved
static_pairs: &mut HashMap<String, String>,
dynamic_pairs: &mut HashMap<String, String>,
) -> Result<HashMap<String, String>, serde_json::Error> {
let mut expanded_pairs = HashMap::new();
if let Some(opening_prefix) = key_s.strip_suffix('*') {
let output: Result<serde_json::map::Map<String, serde_json::Value>, serde_json::Error> =
serde_json::from_str(value_s.clone().as_str());
serde_json::from_str(value_s);
Copy link
Member

Choose a reason for hiding this comment

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

✨ nice we got rid of a clone call

} else {
static_labels.insert(key_s, value_s);
}
let _ = pair_expansion(key_s, value_s, &mut static_labels, &mut dynamic_labels);
Copy link
Member

@pront pront Jan 6, 2025

Choose a reason for hiding this comment

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

I am referring to this existing code: https://github.com/vectordotdev/vector/blob/master/src/sinks/loki/sink.rs#L195

It's hard to follow without checking it out locally (too many nested blocks in this code).

Edit:
Can you just double check that the behavior is equivalent?
As long as we figure this out this PR is good to go.

@titaneric
Copy link
Contributor Author

It appears that test suite does not pass, let me fix it and double-check the equivalence of changed code.

@titaneric titaneric force-pushed the feat/tags-expansion branch from 5005b31 to 32ed91d Compare January 6, 2025 17:14
@titaneric
Copy link
Contributor Author

I think log_to_metric transform deserve another PR to update the documents.

@titaneric
Copy link
Contributor Author

titaneric commented Jan 7, 2025

I have updated the example to adopt tags expansion feature.

rendered page:
image

Not sure whether it's a known issue or not, I notice that metric option could not be properly rendered on the documents, and it appears that it also affect the static_metrics docs. It seems that the page builder could not support array of object in cue, and I would open another issue to report it.

preview page:

image

cue:

image

@titaneric
Copy link
Contributor Author

Open #22136 to discuss the document render bugs.

@pront
Copy link
Member

pront commented Jan 7, 2025

It appears that test suite does not pass, let me fix it and double-check the equivalence of changed code.

I think your latest changes fixed this.

@titaneric
Copy link
Contributor Author

Hi @pront , is it possible this PR would be merged in the upcoming days?

@pront pront added this pull request to the merge queue Jan 13, 2025
Merged via the queue into vectordotdev:master with commit ff8dfb7 Jan 13, 2025
40 checks passed
@titaneric
Copy link
Contributor Author

@pront , thanks for merging this PR, and I just find another related issue at #7565 which I believed it could be closed!

titaneric added a commit to titaneric/vector that referenced this pull request Jan 15, 2025
…els expansion` for loki sink (vectordotdev#21939)

* Try tag expansion

* Add tags expansion test

* Extract dynamic labels into tags result

* Update tags expansion testcase

* Refactor render_tag_into

* Add multi_value_tags_expansion_yaml test

* Add basic docs for tags

* Add change log

* Rename changelog file

* rename labels to tags and inset the tag value in render_tag_into

* Remove debug msg

* Add colliding tags test

* Add source reference

* fix comment and changelog

* Extract pair expansion to common mod

* Adopt pair expansion in loki sink

* Generate component docs

* Add PairExpansion Error

* Extend the result with extended pairs

* Update docs

* Re-generate docs

* Add docs for pair_expansion and change signature to string literal

* Update signature for render_tag_into

* Handle the `pair_expansion` error in caller side

* Better error handling in `pair_expansion`

* Update example
@titaneric titaneric deleted the feat/tags-expansion branch January 17, 2025 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain: external docs Anything related to Vector's external, public documentation domain: sinks Anything related to the Vector's sinks domain: transforms Anything related to Vector's transform components
Projects
None yet
4 participants