From e7163b64d56c89fe90dc3737e4575d9403be7da6 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sat, 4 Nov 2023 16:07:27 +0100 Subject: [PATCH] Counter+Gauge+Summary: use keyword arguments for construction. --- docs/src/index.md | 6 +++--- src/Prometheus.jl | 50 ++++++++++++++++++----------------------------- test/runtests.jl | 14 ++++++------- 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index ae692e1..85449f8 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -85,7 +85,7 @@ documentation](https://prometheus.io/docs/concepts/metric_types/#counter): #### Counter API reference ```@docs -Prometheus.Counter +Prometheus.Counter(::String, ::String; kwargs...) Prometheus.inc(::Prometheus.Counter, ::Any) ``` @@ -102,7 +102,7 @@ documentation](https://prometheus.io/docs/concepts/metric_types/#gauge): #### Gauge API reference ```@docs -Prometheus.Gauge +Prometheus.Gauge(::String, ::String; kwargs...) Prometheus.inc(::Prometheus.Gauge, ::Any) Prometheus.dec(::Prometheus.Gauge, ::Any) Prometheus.set(::Prometheus.Gauge, ::Any) @@ -121,7 +121,7 @@ documentation](https://prometheus.io/docs/concepts/metric_types/#summary): #### Summary API reference ```@docs -Prometheus.Summary +Prometheus.Summary(::String, ::String; kwargs...) Prometheus.observe(::Prometheus.Summary, ::Any) ``` diff --git a/src/Prometheus.jl b/src/Prometheus.jl index 2efeef1..6fb28c5 100644 --- a/src/Prometheus.jl +++ b/src/Prometheus.jl @@ -113,8 +113,8 @@ mutable struct Counter <: Collector @atomic value::Float64 function Counter( - registry::Union{CollectorRegistry, Nothing}, - metric_name::String, help::String, + metric_name::String, help::String; + registry::Union{CollectorRegistry, Nothing}=DEFAULT_REGISTRY, ) initial_value = 0.0 counter = new(metric_name, help, initial_value) @@ -125,25 +125,21 @@ mutable struct Counter <: Collector end end -function Counter(metric_name::String, help::String) - return Counter(DEFAULT_REGISTRY, metric_name, help) -end - """ - Prometheus.Counter(; name, help, registry=DEFAULT_REGISTRY) + Prometheus.Counter(name, help; registry=DEFAULT_REGISTRY) Construct a Counter collector. -**Required keyword arguments** +**Arguments** - `name :: String`: the name of the counter metric. - `help :: String`: the documentation for the counter metric. -**Optional keyword arguments** +**Keyword arguments** - `registry :: Prometheus.CollectorRegistry`: the registry in which to register the collector. If not specified the default registry is used. Pass `registry = nothing` to skip registration. """ -Counter +Counter(::String, ::String; kwargs...) function metric_names(counter::Counter) return (counter.metric_name, ) @@ -193,8 +189,8 @@ mutable struct Gauge <: Collector @atomic value::Float64 function Gauge( - registry::Union{CollectorRegistry, Nothing}, - metric_name::String, help::String, + metric_name::String, help::String; + registry::Union{CollectorRegistry, Nothing}=DEFAULT_REGISTRY, ) initial_value = 0.0 gauge = new(metric_name, help, initial_value) @@ -205,25 +201,21 @@ mutable struct Gauge <: Collector end end -function Gauge(metric_name::String, help::String) - return Gauge(DEFAULT_REGISTRY, metric_name, help) -end - """ - Prometheus.Gauge(; name, help, registry=DEFAULT_REGISTRY) + Prometheus.Gauge(name, help; registry=DEFAULT_REGISTRY) Construct a Gauge collector. -**Required keyword arguments** +**Arguments** - `name :: String`: the name of the gauge metric. - `help :: String`: the documentation for the gauge metric. -**Optional keyword arguments** +**Keyword arguments** - `registry :: Prometheus.CollectorRegistry`: the registry in which to register the collector. If not specified the default registry is used. Pass `registry = nothing` to skip registration. """ -Gauge +Gauge(::String, ::String; kwargs...) function metric_names(gauge::Gauge) return (gauge.metric_name, ) @@ -300,8 +292,8 @@ mutable struct Summary <: Collector @atomic _sum::Float64 function Summary( - registry::Union{CollectorRegistry, Nothing}, - metric_name::String, help::String, + metric_name::String, help::String; + registry::Union{CollectorRegistry, Nothing}=DEFAULT_REGISTRY, ) initial_count = 0 initial_sum = 0.0 @@ -313,25 +305,21 @@ mutable struct Summary <: Collector end end -function Summary(metric_name::String, help::String) - return Summary(DEFAULT_REGISTRY, metric_name, help) -end - """ - Prometheus.Summary(; name, help, registry=DEFAULT_REGISTRY) + Prometheus.Summary(name, help; registry=DEFAULT_REGISTRY) Construct a Summary collector. -**Required keyword arguments** +**Arguments** - `name :: String`: the name of the summary metric. - `help :: String`: the documentation for the summary metric. -**Optional keyword arguments** +**Keyword arguments** - `registry :: Prometheus.CollectorRegistry`: the registry in which to register the collector. If not specified the default registry is used. Pass `registry = nothing` to skip registration. """ -Summary +Summary(::String, ::String; kwargs...) function metric_names(summary::Summary) return (summary.metric_name * "_count", summary.metric_name * "_sum") @@ -419,7 +407,7 @@ end function labels(family::Family{C}, labelvalues::LabelValues) where C collector = @lock family.lock get!(family.children, labelvalues) do - C(nothing, family.metric_name, family.help) + C(family.metric_name, family.help; registry=nothing) end return collector end diff --git a/test/runtests.jl b/test/runtests.jl index c14f7d4..32ed895 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -14,11 +14,11 @@ using Test: @test, @test_logs, @test_throws, @testset @test c2 in Prometheus.DEFAULT_REGISTRY.collectors # Provided registry r = Prometheus.CollectorRegistry() - c = Prometheus.Counter(r, "metric_name_counter", "A counter.") + c = Prometheus.Counter("metric_name_counter", "A counter."; registry=r) @test c in r.collectors @test !(c in Prometheus.DEFAULT_REGISTRY.collectors) # No registry on construction, register after - c = Prometheus.Counter(nothing, "metric_name_counter", "A counter.") + c = Prometheus.Counter("metric_name_counter", "A counter."; registry=nothing) @test !(c in Prometheus.DEFAULT_REGISTRY.collectors) r = Prometheus.CollectorRegistry() Prometheus.register(r, c) @@ -32,7 +32,7 @@ end c = Prometheus.Counter("metric_name_counter", "A counter.") @test c in Prometheus.DEFAULT_REGISTRY.collectors r = Prometheus.CollectorRegistry() - c = Prometheus.Counter(r, "metric_name_counter", "A counter.") + c = Prometheus.Counter("metric_name_counter", "A counter."; registry=r) @test c in r.collectors @test c.value == 0 # Prometheus.inc(...) @@ -66,7 +66,7 @@ end c = Prometheus.Gauge("metric_name_gauge", "A gauge.") @test c in Prometheus.DEFAULT_REGISTRY.collectors r = Prometheus.CollectorRegistry() - c = Prometheus.Gauge(r, "metric_name_gauge", "A gauge.") + c = Prometheus.Gauge("metric_name_gauge", "A gauge."; registry=r) @test c in r.collectors @test c.value == 0 # Prometheus.inc(...) @@ -113,7 +113,7 @@ end c = Prometheus.Summary("metric_name_summary", "A summary.") @test c in Prometheus.DEFAULT_REGISTRY.collectors r = Prometheus.CollectorRegistry() - c = Prometheus.Summary(r, "metric_name_summary", "A summary.") + c = Prometheus.Summary("metric_name_summary", "A summary."; registry=r) @test c in r.collectors @test c._count == 0 @test c._sum == 0 @@ -393,8 +393,8 @@ end @testset "Prometheus.expose(::Union{String, IO})" begin r = Prometheus.DEFAULT_REGISTRY empty!(r.collectors) - Prometheus.inc(Prometheus.Counter(r, "prom_counter", "Counting things")) - Prometheus.set(Prometheus.Gauge(r, "prom_gauge", "Gauging things"), 1.2) + Prometheus.inc(Prometheus.Counter("prom_counter", "Counting things"; registry=r)) + Prometheus.set(Prometheus.Gauge("prom_gauge", "Gauging things"; registry=r), 1.2) mktempdir() do dir default = joinpath(dir, "default.prom") Prometheus.expose(default)