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 #[flatten] directive for derivation of nested structs #117

Closed
howardjohn opened this issue Dec 12, 2022 · 2 comments · Fixed by #118
Closed

Add #[flatten] directive for derivation of nested structs #117

howardjohn opened this issue Dec 12, 2022 · 2 comments · Fixed by #118

Comments

@howardjohn
Copy link
Contributor

Given the structs like:

struct Common {
  field_a: String,
  field_b: String,
  field_c: String
}

#[derive(Encode)]
struct Metric1 {
  unique_field: String
  #[flatten]
  common: Common
}

#[derive(Encode)]
struct Metric2 {
  unique_field_2: String
  #[serde(flatten)]
  common: Common
}

I want the resulting metrics:

metric1{field_a="foo", field_b="bar", field_c="baz",unique_field="something"}
metric2{field_a="foo", field_b="bar", field_c="baz",unique_field_2="something_else"}

(Note: in real world Common and the number of metrics are much larger)

This would match the semantics of serde(flatten)

See #105 (comment) for more discussion

@mxinden
Copy link
Member

mxinden commented Dec 14, 2022

I have had that same use-case in rust-libp2p before. I am in favor of adding this to prometheus-client-derive-encode.

I foresee having the capacity to implement this any time soon. Focusing all my capacity on #74. @howardjohn are you interested in making the contribution?

@howardjohn
Copy link
Contributor Author

@mxinden I sent a PR in #118. it might need some improvements but it works in the basic case right now, at least.

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 a pull request may close this issue.

2 participants