Browse Source

Counter+Gauge+Summary: use keyword arguments for construction.

pull/6/head
Fredrik Ekre 2 years ago
parent
commit
e7163b64d5
  1. 6
      docs/src/index.md
  2. 50
      src/Prometheus.jl
  3. 14
      test/runtests.jl

6
docs/src/index.md

@ -85,7 +85,7 @@ documentation](https://prometheus.io/docs/concepts/metric_types/#counter):
#### Counter API reference #### Counter API reference
```@docs ```@docs
Prometheus.Counter Prometheus.Counter(::String, ::String; kwargs...)
Prometheus.inc(::Prometheus.Counter, ::Any) Prometheus.inc(::Prometheus.Counter, ::Any)
``` ```
@ -102,7 +102,7 @@ documentation](https://prometheus.io/docs/concepts/metric_types/#gauge):
#### Gauge API reference #### Gauge API reference
```@docs ```@docs
Prometheus.Gauge Prometheus.Gauge(::String, ::String; kwargs...)
Prometheus.inc(::Prometheus.Gauge, ::Any) Prometheus.inc(::Prometheus.Gauge, ::Any)
Prometheus.dec(::Prometheus.Gauge, ::Any) Prometheus.dec(::Prometheus.Gauge, ::Any)
Prometheus.set(::Prometheus.Gauge, ::Any) Prometheus.set(::Prometheus.Gauge, ::Any)
@ -121,7 +121,7 @@ documentation](https://prometheus.io/docs/concepts/metric_types/#summary):
#### Summary API reference #### Summary API reference
```@docs ```@docs
Prometheus.Summary Prometheus.Summary(::String, ::String; kwargs...)
Prometheus.observe(::Prometheus.Summary, ::Any) Prometheus.observe(::Prometheus.Summary, ::Any)
``` ```

50
src/Prometheus.jl

@ -113,8 +113,8 @@ mutable struct Counter <: Collector
@atomic value::Float64 @atomic value::Float64
function Counter( 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 initial_value = 0.0
counter = new(metric_name, help, initial_value) counter = new(metric_name, help, initial_value)
@ -125,25 +125,21 @@ mutable struct Counter <: Collector
end end
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. Construct a Counter collector.
**Required keyword arguments** **Arguments**
- `name :: String`: the name of the counter metric. - `name :: String`: the name of the counter metric.
- `help :: String`: the documentation for 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 - `registry :: Prometheus.CollectorRegistry`: the registry in which to register the
collector. If not specified the default registry is used. Pass `registry = nothing` to collector. If not specified the default registry is used. Pass `registry = nothing` to
skip registration. skip registration.
""" """
Counter Counter(::String, ::String; kwargs...)
function metric_names(counter::Counter) function metric_names(counter::Counter)
return (counter.metric_name, ) return (counter.metric_name, )
@ -193,8 +189,8 @@ mutable struct Gauge <: Collector
@atomic value::Float64 @atomic value::Float64
function Gauge( 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 initial_value = 0.0
gauge = new(metric_name, help, initial_value) gauge = new(metric_name, help, initial_value)
@ -205,25 +201,21 @@ mutable struct Gauge <: Collector
end end
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. Construct a Gauge collector.
**Required keyword arguments** **Arguments**
- `name :: String`: the name of the gauge metric. - `name :: String`: the name of the gauge metric.
- `help :: String`: the documentation for 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 - `registry :: Prometheus.CollectorRegistry`: the registry in which to register the
collector. If not specified the default registry is used. Pass `registry = nothing` to collector. If not specified the default registry is used. Pass `registry = nothing` to
skip registration. skip registration.
""" """
Gauge Gauge(::String, ::String; kwargs...)
function metric_names(gauge::Gauge) function metric_names(gauge::Gauge)
return (gauge.metric_name, ) return (gauge.metric_name, )
@ -300,8 +292,8 @@ mutable struct Summary <: Collector
@atomic _sum::Float64 @atomic _sum::Float64
function Summary( 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_count = 0
initial_sum = 0.0 initial_sum = 0.0
@ -313,25 +305,21 @@ mutable struct Summary <: Collector
end end
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. Construct a Summary collector.
**Required keyword arguments** **Arguments**
- `name :: String`: the name of the summary metric. - `name :: String`: the name of the summary metric.
- `help :: String`: the documentation for 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 - `registry :: Prometheus.CollectorRegistry`: the registry in which to register the
collector. If not specified the default registry is used. Pass `registry = nothing` to collector. If not specified the default registry is used. Pass `registry = nothing` to
skip registration. skip registration.
""" """
Summary Summary(::String, ::String; kwargs...)
function metric_names(summary::Summary) function metric_names(summary::Summary)
return (summary.metric_name * "_count", summary.metric_name * "_sum") return (summary.metric_name * "_count", summary.metric_name * "_sum")
@ -419,7 +407,7 @@ end
function labels(family::Family{C}, labelvalues::LabelValues) where C function labels(family::Family{C}, labelvalues::LabelValues) where C
collector = @lock family.lock get!(family.children, labelvalues) do 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 end
return collector return collector
end end

14
test/runtests.jl

@ -14,11 +14,11 @@ using Test: @test, @test_logs, @test_throws, @testset
@test c2 in Prometheus.DEFAULT_REGISTRY.collectors @test c2 in Prometheus.DEFAULT_REGISTRY.collectors
# Provided registry # Provided registry
r = Prometheus.CollectorRegistry() 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 r.collectors
@test !(c in Prometheus.DEFAULT_REGISTRY.collectors) @test !(c in Prometheus.DEFAULT_REGISTRY.collectors)
# No registry on construction, register after # 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) @test !(c in Prometheus.DEFAULT_REGISTRY.collectors)
r = Prometheus.CollectorRegistry() r = Prometheus.CollectorRegistry()
Prometheus.register(r, c) Prometheus.register(r, c)
@ -32,7 +32,7 @@ end
c = Prometheus.Counter("metric_name_counter", "A counter.") c = Prometheus.Counter("metric_name_counter", "A counter.")
@test c in Prometheus.DEFAULT_REGISTRY.collectors @test c in Prometheus.DEFAULT_REGISTRY.collectors
r = Prometheus.CollectorRegistry() 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 r.collectors
@test c.value == 0 @test c.value == 0
# Prometheus.inc(...) # Prometheus.inc(...)
@ -66,7 +66,7 @@ end
c = Prometheus.Gauge("metric_name_gauge", "A gauge.") c = Prometheus.Gauge("metric_name_gauge", "A gauge.")
@test c in Prometheus.DEFAULT_REGISTRY.collectors @test c in Prometheus.DEFAULT_REGISTRY.collectors
r = Prometheus.CollectorRegistry() 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 in r.collectors
@test c.value == 0 @test c.value == 0
# Prometheus.inc(...) # Prometheus.inc(...)
@ -113,7 +113,7 @@ end
c = Prometheus.Summary("metric_name_summary", "A summary.") c = Prometheus.Summary("metric_name_summary", "A summary.")
@test c in Prometheus.DEFAULT_REGISTRY.collectors @test c in Prometheus.DEFAULT_REGISTRY.collectors
r = Prometheus.CollectorRegistry() 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 in r.collectors
@test c._count == 0 @test c._count == 0
@test c._sum == 0 @test c._sum == 0
@ -393,8 +393,8 @@ end
@testset "Prometheus.expose(::Union{String, IO})" begin @testset "Prometheus.expose(::Union{String, IO})" begin
r = Prometheus.DEFAULT_REGISTRY r = Prometheus.DEFAULT_REGISTRY
empty!(r.collectors) empty!(r.collectors)
Prometheus.inc(Prometheus.Counter(r, "prom_counter", "Counting things")) Prometheus.inc(Prometheus.Counter("prom_counter", "Counting things"; registry=r))
Prometheus.set(Prometheus.Gauge(r, "prom_gauge", "Gauging things"), 1.2) Prometheus.set(Prometheus.Gauge("prom_gauge", "Gauging things"; registry=r), 1.2)
mktempdir() do dir mktempdir() do dir
default = joinpath(dir, "default.prom") default = joinpath(dir, "default.prom")
Prometheus.expose(default) Prometheus.expose(default)

Loading…
Cancel
Save