-
Notifications
You must be signed in to change notification settings - Fork 28
Add prometheus-lwt as a forward-compatible Lwt interface #66
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (library | ||
| (name prometheus_lwt) | ||
| (public_name prometheus-lwt) | ||
| (libraries prometheus lwt)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| [@@@alert "-deprecated"] | ||
|
|
||
| (* This transition module reexports the core operations under their | ||
| new names to allow the core package to drop the Lwt dependency | ||
| in a future release. This interface will not change. *) | ||
| module CollectorRegistry = struct | ||
| type t = Prometheus.CollectorRegistry.t | ||
|
|
||
| let of_registry core = core | ||
|
|
||
| let default = Prometheus.CollectorRegistry.default | ||
|
|
||
| let core t = t | ||
|
|
||
| let collect = Prometheus.CollectorRegistry.collect | ||
|
|
||
| let register = Prometheus.CollectorRegistry.register_lwt | ||
|
|
||
| let register_pre_collect = Prometheus.CollectorRegistry.register_pre_collect_lwt | ||
| end | ||
|
|
||
| module Gauge = struct | ||
| let track_in_progress = Prometheus.Gauge.track_inprogress | ||
|
|
||
| let set_time = Prometheus.Gauge.time | ||
| end | ||
|
|
||
| module Summary = struct | ||
| let observe_time = Prometheus.Summary.time | ||
| end | ||
|
|
||
| module Histogram (H : Prometheus.HISTOGRAM) = struct | ||
| let observe_time = H.time | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| (** Lwt support for Prometheus. | ||
|
|
||
| Provide collectors that may suspend before producing their metrics. *) | ||
|
|
||
| module CollectorRegistry : sig | ||
| type t | ||
|
|
||
| val of_registry : Prometheus.CollectorRegistry.t -> t | ||
| (** [of_registry core] is the Lwt view over the existing registry [core]. *) | ||
|
|
||
| val default : t | ||
| (** [default] is the view over {!Prometheus.CollectorRegistry.default}. *) | ||
|
|
||
| val core : t -> Prometheus.CollectorRegistry.t | ||
| (** [core t] is the underlying core registry. Use it to register | ||
| synchronous metrics. *) | ||
|
|
||
| val collect : t -> Prometheus.CollectorRegistry.snapshot Lwt.t | ||
| (** [collect t] reads the current value of every metric. *) | ||
|
|
||
| val register : | ||
| t -> Prometheus.MetricInfo.t -> | ||
| (unit -> Prometheus.Sample_set.t Prometheus.LabelSetMap.t Lwt.t) -> unit | ||
| (** [register t info collector] registers a collector that may suspend | ||
| before producing its samples. *) | ||
|
|
||
| val register_pre_collect : t -> (unit -> unit Lwt.t) -> unit | ||
| (** [register_pre_collect t f] arranges for [f ()] to run at the start | ||
| of each collection. *) | ||
| end | ||
|
|
||
| module Gauge : sig | ||
| val track_in_progress : Prometheus.Gauge.t -> (unit -> 'a Lwt.t) -> 'a Lwt.t | ||
| (** [track_in_progress t f] increases [t] by one while [f ()] is running. *) | ||
|
|
||
| val set_time : Prometheus.Gauge.t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t | ||
| (** [set_time t gettime f] calls [gettime ()] before and after executing [f ()] and | ||
| sets [t] to the difference. *) | ||
| end | ||
|
|
||
| module Summary : sig | ||
| val observe_time : Prometheus.Summary.t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t | ||
| (** [observe_time t gettime f] observes the duration of [f ()]. *) | ||
| end | ||
|
|
||
| module Histogram (H : Prometheus.HISTOGRAM) : sig | ||
| val observe_time : H.t -> (unit -> float) -> (unit -> 'a Lwt.t) -> 'a Lwt.t | ||
| (** [observe_time t gettime f] observes the duration of [f ()]. *) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| (test | ||
| (name test_lwt) | ||
| (package prometheus-lwt) | ||
| (libraries prometheus prometheus-app prometheus-lwt alcotest alcotest-lwt lwt fmt)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| open Prometheus | ||
| open Prometheus_app | ||
|
|
||
| open Lwt.Infix | ||
|
|
||
| let test_lwt_collectors () = | ||
| let registry = Prometheus_lwt.CollectorRegistry.of_registry (CollectorRegistry.create ()) in | ||
| let sync_counter = | ||
| Counter.v ~registry:(Prometheus_lwt.CollectorRegistry.core registry) | ||
| ~help:"A synchronous counter" "counter_0" | ||
| in | ||
| Counter.inc_one sync_counter; | ||
| let register_counter ~name ~help value = | ||
| let metric_info = { | ||
| MetricInfo.name = MetricName.v name; | ||
| metric_type = Counter; | ||
| help; | ||
| label_names = [] | ||
| } | ||
| in | ||
| let collector () = | ||
| Lwt.pause () >|= fun () -> | ||
| LabelSetMap.singleton [] [Prometheus.Sample_set.sample value] | ||
| in | ||
| Prometheus_lwt.CollectorRegistry.register registry metric_info collector | ||
| in | ||
| register_counter ~name:"counter_1" ~help:"The first counter" 1.0; | ||
| register_counter ~name:"counter_2" ~help:"The second counter" 2.0; | ||
| Prometheus_lwt.CollectorRegistry.collect registry >|= fun collected -> | ||
| let output = Fmt.to_to_string TextFormat_0_0_4.output collected in | ||
| Alcotest.(check string) "Text output" | ||
| "# HELP counter_0 A synchronous counter\n\ | ||
| # TYPE counter_0 counter\n\ | ||
| counter_0 1\n\ | ||
| # HELP counter_1 The first counter\n\ | ||
| # TYPE counter_1 counter\n\ | ||
| counter_1 1\n\ | ||
| # HELP counter_2 The second counter\n\ | ||
| # TYPE counter_2 counter\n\ | ||
| counter_2 2\n" | ||
| output | ||
|
|
||
| let test_timers () = | ||
| let registry = Prometheus_lwt.CollectorRegistry.of_registry (CollectorRegistry.create ()) in | ||
| let core = Prometheus_lwt.CollectorRegistry.core registry in | ||
| let gauge = Gauge.v ~registry:core ~help:"Time taken" "gauge_time" in | ||
| let clock = ref 0.0 in | ||
| let gettime () = !clock in | ||
| Prometheus_lwt.Gauge.set_time gauge gettime | ||
| (fun () -> clock := !clock +. 1.5; Lwt.pause ()) | ||
| >>= fun () -> | ||
| Prometheus_lwt.CollectorRegistry.collect registry >|= fun collected -> | ||
| let output = Fmt.to_to_string TextFormat_0_0_4.output collected in | ||
| Alcotest.(check string) "Text output" | ||
| "# HELP gauge_time Time taken\n\ | ||
| # TYPE gauge_time gauge\n\ | ||
| gauge_time 1.5\n" | ||
| output | ||
|
|
||
| let () = | ||
| Lwt_main.run @@ Alcotest_lwt.run "prometheus-lwt" [ | ||
| "main", [ | ||
| "Lwt collectors", `Quick, test_lwt_collectors; | ||
| "Lwt timers", `Quick, test_timers; | ||
| ]; | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| opam-version: "2.0" | ||
| synopsis: "Lwt support for Prometheus monitoring" | ||
| description: "Lwt collectors and timing helpers for Prometheus metrics." | ||
| maintainer: "talex5@gmail.com" | ||
| authors: ["Thomas Leonard" "Anil Madhavapeddy" "David Scott"] | ||
| license: "Apache-2.0" | ||
| homepage: "https://github.com/mirage/prometheus" | ||
| doc: "https://mirage.github.io/prometheus/" | ||
| bug-reports: "https://github.com/mirage/prometheus/issues" | ||
| depends: [ | ||
| "ocaml" {>= "4.11.0"} | ||
| "dune" {>= "2.3"} | ||
| "prometheus" {= version} | ||
| "lwt" {>= "2.5.0"} | ||
| "prometheus-app" {= version & with-test} | ||
| "fmt" {>= "0.8.7" & with-test} | ||
| "alcotest" {with-test} | ||
| "alcotest-lwt" {with-test} | ||
| ] | ||
| build: [ | ||
| ["dune" "build" "-p" name "-j" jobs] | ||
| ["dune" "runtest" "-p" name "-j" jobs] {with-test} | ||
| ] | ||
| dev-repo: "git+https://github.com/mirage/prometheus.git" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a minor thing, but is the deprecated tag needed outside of the mli?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wouldn't build without it for some reason (the error is pretty confusing):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, it doesn't fire an error when using let bindings and val signatures, only when module types are involved.