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: Make PrometheusClientLayer Clonable #3352

Merged
merged 4 commits into from
Oct 23, 2023

Conversation

flaneur2020
Copy link
Contributor

we met an issue in datafuselabs/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.

Copy link
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
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
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
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
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
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
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
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
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
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
Contributor Author

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

@Xuanwo
Copy link
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
Contributor Author

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

@Xuanwo Xuanwo changed the title fix: allow passing a referenced PrometheusClientLayer feat: Make PrometheusClientLayer Clonable Oct 23, 2023
Copy link
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
111 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants