Browse Source

ProcessCollector: user keyword arguments for construction.

pull/4/head
Fredrik Ekre 2 years ago
parent
commit
da4deee555
  1. 2
      src/Prometheus.jl
  2. 10
      src/process_collector.jl
  3. 4
      test/runtests.jl

2
src/Prometheus.jl

@ -631,6 +631,6 @@ include("process_collector.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

10
src/process_collector.jl

@ -3,12 +3,13 @@ @@ -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 @@ -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) @@ -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

4
test/runtests.jl

@ -318,7 +318,7 @@ end @@ -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 @@ -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

Loading…
Cancel
Save