Skip to content

feat: Make PrometheusClientLayer Clonable#3352

Merged
Xuanwo merged 4 commits intoapache:mainfrom
flaneur2020:fix-prometheus-client
Oct 23, 2023
Merged

feat: Make PrometheusClientLayer Clonable#3352
Xuanwo merged 4 commits intoapache:mainfrom
flaneur2020:fix-prometheus-client

Conversation

@flaneur2020
Copy link
Copy Markdown
Contributor

we met an issue in databendlabs/databend#13373 , as sometimes we'd call build_operator() on some queries, this causes duplicated metrics get registered into the registry, which finally produced millions of metrics.

the solution is to make the PrometheusClientLayer a singleton, but currently the PrometheusClientLayer have to be moved. we need allow it passing a reference before making the singleton in the layer() call.

@flaneur2020 flaneur2020 requested a review from Xuanwo as a code owner October 21, 2023 12:36
@github-actions github-actions Bot added the releases-note/fix The PR fixes a bug or has a title that begins with "fix" label Oct 21, 2023
Copy link
Copy Markdown
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

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

I don't like the idea of implementing Layer for a reference. Do we have better choices? For example, changing the PromClientLayer's API to avoid such thing happens.

@Xuanwo
Copy link
Copy Markdown
Member

Xuanwo commented Oct 22, 2023

Seems We need to make PrometheusClientMetrics accessible to users, allowing them to initialize and register it as needed. Also, PrometheusClientLayer should be modified to accept a PrometheusClientMetrics.

The API will be:

#[derive(Default, Clone)]
struct PrometheusClientMetrics {..}

impl PrometheusClientMetrics {
    pub fn register(&self, registry: &mut Registry);
}

struct PrometheusClientLayer {..}

impl PrometheusClientLayer {
    pub fn new(metrics: PrometheusClientMetrics) -> Self;
}

In this way, users only need to store PrometheusClientMetrics and calling register() while they needed. For example, at the time of application start.

@flaneur2020
Copy link
Copy Markdown
Contributor Author

flaneur2020 commented Oct 23, 2023

Seems We need to make PrometheusClientMetrics accessible to users, allowing them to initialize and register it as needed. Also, PrometheusClientLayer should be modified to accept a PrometheusClientMetrics.

The API will be:

#[derive(Default, Clone)]
struct PrometheusClientMetrics {..}

impl PrometheusClientMetrics {
    pub fn register(&self, registry: &mut Registry);
}

struct PrometheusClientLayer {..}

impl PrometheusClientLayer {
    pub fn new(metrics: PrometheusClientMetrics) -> Self;
}

In this way, users only need to store PrometheusClientMetrics and calling register() while they needed. For example, at the time of application start.

IMHO this api is also error prone as the PrometheusClientMetrics struct is passed by moving instead of a reference. We have no way to pass a PrometheusClientMetrics singleton from adhoc build_operator() in a query basis unless using interior mutability.

any other suggestion about the API design? or just use interior mutability?

@Xuanwo
Copy link
Copy Markdown
Member

Xuanwo commented Oct 23, 2023

IMHO this api is also error prone as the PrometheusClientMetrics struct is passed by moving instead of a reference. We have no way to pass a PrometheusClientMetrics singleton from adhoc build_operator() in a query basis unless using interior mutability.

PrometheusClientMetrics implements Clone, so it's ok to clone it. Family has Arc internally.

@flaneur2020
Copy link
Copy Markdown
Contributor Author

if do not want using reference on the Layer side, I think we need an Arc over a PrometheusClientMetrics.

however this may introduce an extra Arc dereference cost on every metric modification over a direct referencing.

#[derive(Debug, Clone)]
pub struct PrometheusClientLayer {
    metrics: Arc<PrometheusClientMetrics>,
}

impl PrometheusClientLayer {
    pub fn register(&self, registry: &mut Registry) -> Self;
}
  .layer(prometheus_client_layer.clone())

@flaneur2020
Copy link
Copy Markdown
Contributor Author

flaneur2020 commented Oct 23, 2023

IMHO this api is also error prone as the PrometheusClientMetrics struct is passed by moving instead of a reference. We have no way to pass a PrometheusClientMetrics singleton from adhoc build_operator() in a query basis unless using interior mutability.

PrometheusClientMetrics implements Clone, so it's ok to clone it. Family has Arc internally.

no. not all metrics are Family, the simple metric like Counter is a simply atomic u64, if you clone it, you ends up with multiple metrics. it has to be placed in one single memory location.

@Xuanwo
Copy link
Copy Markdown
Member

Xuanwo commented Oct 23, 2023

not all metrics are Family, the simple metric like Counter is a simply atomic u64

But all metrics in opendal will be Family? All metrics in opendal will be connectted with a OperationLabels

@flaneur2020
Copy link
Copy Markdown
Contributor Author

not all metrics are Family, the simple metric like Counter is a simply atomic u64

But all metrics in opendal will be Family?

no, Family is used for the metrics with labels. if your labels exploded, you'd like to replace some families with simple metrics to make the metrics crapper happy.

@Xuanwo
Copy link
Copy Markdown
Member

Xuanwo commented Oct 23, 2023

no, Family is used for the metrics with labels. if your labels exploded, you'd like to replace some families with simple metrics to make the metrics crapper happy.

I didn't get this. Are you talking about changing PrometheusClientMetrics too?

@flaneur2020
Copy link
Copy Markdown
Contributor Author

oh I got it wrong, the simple metrics like Counter, Gauge are also wrapped within an Arc, so PrometheusClientMetrics is safe to clone.

@flaneur2020
Copy link
Copy Markdown
Contributor Author

so we can consider just make PrometheusClientLayer clone-able, so it allows expose less things outside.

@Xuanwo
Copy link
Copy Markdown
Member

Xuanwo commented Oct 23, 2023

so we can consider just make PrometheusClientLayer clone-able, so it allows expose less things outside.

LGTM.

@flaneur2020
Copy link
Copy Markdown
Contributor Author

updated the PR, after when only one line of code is changed, PTAL @Xuanwo

@flaneur2020 flaneur2020 requested a review from Xuanwo October 23, 2023 05:28
@Xuanwo Xuanwo changed the title fix: allow passing a referenced PrometheusClientLayer feat: Make PrometheusClientLayer Clonable Oct 23, 2023
@github-actions github-actions Bot added the releases-note/feat The PR implements a new feature or has a title that begins with "feat" label Oct 23, 2023
Copy link
Copy Markdown
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

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

Mostly LGTM, please make rustfmt happy.

@Xuanwo Xuanwo merged commit 5f947be into apache:main Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

releases-note/feat The PR implements a new feature or has a title that begins with "feat" releases-note/fix The PR fixes a bug or has a title that begins with "fix"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants