diff --git a/src/Prometheus.jl b/src/Prometheus.jl index 5813b0a..8a38c5b 100644 --- a/src/Prometheus.jl +++ b/src/Prometheus.jl @@ -631,6 +631,6 @@ include("process_collector.jl") # Default registry and collectors const DEFAULT_REGISTRY = CollectorRegistry() const GC_COLLECTOR = GCCollector(; registry=DEFAULT_REGISTRY) -const PROCESS_COLLECTOR = ProcessCollector(DEFAULT_REGISTRY) +const PROCESS_COLLECTOR = ProcessCollector(; registry=DEFAULT_REGISTRY) end # module Prometheus diff --git a/src/process_collector.jl b/src/process_collector.jl index 8d77456..74cdc93 100644 --- a/src/process_collector.jl +++ b/src/process_collector.jl @@ -3,12 +3,13 @@ ################################# mutable struct ProcessCollector <: Collector - @const pid_f::Function + @const pid::Function @const system_boot_time::Int @const clock_ticks_per_second::Int @const pagesize::Int function ProcessCollector( - registry::Union{CollectorRegistry, Nothing}, pid_f::Function = () -> "self", + pid::Function = () -> "self"; + registry::Union{CollectorRegistry, Nothing}=DEFAULT_REGISTRY, ) # Read boot time as a way to check if /proc is available and readable system_boot_time = 0 @@ -44,14 +45,13 @@ mutable struct ProcessCollector <: Collector end end # Create the collector - procc = new(pid_f, system_boot_time, clock_ticks_per_second, pagesize) + procc = new(pid, system_boot_time, clock_ticks_per_second, pagesize) if registry !== nothing register(registry, procc) end return procc end end -ProcessCollector(pid_f::Function = () -> "self") = ProcessCollector(DEFAULT_REGISTRY, pid_f) function metric_names(::ProcessCollector) return ( @@ -68,7 +68,7 @@ function collect!(metrics::Vector, procc::ProcessCollector) procc.system_boot_time == 0 && return metrics # Fetch the pid pid = try - strip(string(procc.pid_f())) + String(strip(string(procc.pid()::Union{AbstractString,Integer})))::String catch e @error "ProcessCollector: could not look up the pid from the lambda" e return metrics diff --git a/test/runtests.jl b/test/runtests.jl index 566cfa9..3a95c1a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -318,7 +318,7 @@ end @testset "Prometheus.ProcessCollector" begin r = Prometheus.CollectorRegistry() - c = Prometheus.ProcessCollector(r) + c = Prometheus.ProcessCollector(; registry=r) @test c in r.collectors metrics = Prometheus.collect(c) procfs_available = c.system_boot_time > 0 @@ -368,7 +368,7 @@ end @test isempty(metrics) end # Test that pid function works - procc = Prometheus.ProcessCollector(nothing, () -> getpid()) + procc = Prometheus.ProcessCollector(() -> getpid(); registry=nothing) metrics = Prometheus.collect(procc) if procfs_available @test length(metrics) > 0