Skip to content

Add prom gauge for partitions latest fetched block - #383

Merged
JonoPrest merged 2 commits into
mainfrom
jp/partition-prom-counter
Dec 11, 2024
Merged

Add prom gauge for partitions latest fetched block#383
JonoPrest merged 2 commits into
mainfrom
jp/partition-prom-counter

Conversation

@JonoPrest

Copy link
Copy Markdown
Collaborator

No description provided.

@JonoPrest
JonoPrest requested a review from DZakh December 11, 2024 15:07
Comment on lines +74 to +138
}

let metricNames: Utils.Set.t<string> = Utils.Set.make()

module MakeSafePromMetric = (
M: {
type t
let make: {"name": string, "help": string, "labelNames": array<string>} => t
let labels: (t, 'a) => t
let handleFloat: (t, float) => unit
let handleInt: (t, int) => unit
},
): {
type t<'a>
let makeOrThrow: (~name: string, ~help: string, ~labelSchema: S.t<'a>) => t<'a>
let handleInt: (t<'a>, ~labels: 'a, ~value: int) => unit
let handleFloat: (t<'a>, ~labels: 'a, ~value: float) => unit
} => {
type t<'a> = {metric: M.t, labelSchema: S.t<'a>}

let makeOrThrow = (~name, ~help, ~labelSchema: S.t<'a>): t<'a> =>
switch labelSchema->getLabelNames {
switch labelSchema->Labels.getLabelNames {
| Ok(labelNames) =>
PromClient.Gauge.makeGauge({
"name": name,
"help": help,
"labelNames": labelNames,
})
if metricNames->Utils.Set.has(name) {
Js.Exn.raiseError("Duplicate prometheus metric name: " ++ name)
} else {
metricNames->Utils.Set.add(name)->ignore
let metric = M.make({
"name": name,
"help": help,
"labelNames": labelNames,
})

{metric, labelSchema}
}

| Error(error) => Js.Exn.raiseError(error)
}

let makeLabels = (self: t<'a>, ~labels: 'a): t<'a> => self->PromClient.Gauge.labels(labels)

let setFloat = (self: t<'a>, ~labels: 'a, ~value) =>
self
->makeLabels(~labels)
->PromClient.Gauge.setFloat(value)
let handleFloat = ({metric, labelSchema}: t<'a>, ~labels: 'a, ~value) =>
metric
->M.labels(labels->S.serializeOrRaiseWith(labelSchema))
->M.handleFloat(value)

let setInt = (self: t<'a>, ~labels: 'a, ~value) =>
self
->makeLabels(~labels)
->PromClient.Gauge.set(value)
let handleInt = ({metric, labelSchema}: t<'a>, ~labels: 'a, ~value) =>
metric
->M.labels(labels->S.serializeOrRaiseWith(labelSchema))
->M.handleInt(value)
}

module SafeCounter = MakeSafePromMetric({
type t = PromClient.Counter.counter
let make = PromClient.Counter.makeCounter
let labels = PromClient.Counter.labels
let handleInt = PromClient.Counter.incMany
let handleFloat = PromClient.Counter.incMany->Utils.magic
})

module SafeGauge = MakeSafePromMetric({
type t = PromClient.Gauge.gauge
let make = PromClient.Gauge.makeGauge
let labels = PromClient.Gauge.labels
let handleInt = PromClient.Gauge.set
let handleFloat = PromClient.Gauge.setFloat
})

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

TBH this refactor was not necessary because I didn't end up using the safe counter anyways

->PromClient.Gauge.setFloat(value)
let handleFloat = ({metric, labelSchema}: t<'a>, ~labels: 'a, ~value) =>
metric
->M.labels(labels->S.serializeOrRaiseWith(labelSchema))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

But I kept it here because it actually runs the serializer on the labels schema

Comment on lines +97 to +100
if metricNames->Utils.Set.has(name) {
Js.Exn.raiseError("Duplicate prometheus metric name: " ++ name)
} else {
metricNames->Utils.Set.add(name)->ignore

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

And has a check that you don't register the same name multiple times

@JonoPrest
JonoPrest enabled auto-merge (rebase) December 11, 2024 15:10
@JonoPrest
JonoPrest merged commit bc2801a into main Dec 11, 2024
@JonoPrest
JonoPrest deleted the jp/partition-prom-counter branch December 11, 2024 15:49
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 this pull request may close these issues.

2 participants