Browse Source

ProcessCollector: user keyword arguments for construction.

pull/6/head
Fredrik Ekre 2 years ago
parent
commit
87a02f8bfa
  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")
# Default registry and collectors # Default registry and collectors
const DEFAULT_REGISTRY = CollectorRegistry() const DEFAULT_REGISTRY = CollectorRegistry()
const GC_COLLECTOR = GCCollector(; registry=DEFAULT_REGISTRY) const GC_COLLECTOR = GCCollector(; registry=DEFAULT_REGISTRY)
const PROCESS_COLLECTOR = ProcessCollector(DEFAULT_REGISTRY) const PROCESS_COLLECTOR = ProcessCollector(; registry=DEFAULT_REGISTRY)
end # module Prometheus end # module Prometheus

10
src/process_collector.jl

@ -3,12 +3,13 @@
################################# #################################
mutable struct ProcessCollector <: Collector mutable struct ProcessCollector <: Collector
@const pid_f::Function @const pid::Function
@const system_boot_time::Int @const system_boot_time::Int
@const clock_ticks_per_second::Int @const clock_ticks_per_second::Int
@const pagesize::Int @const pagesize::Int
function ProcessCollector( 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 # Read boot time as a way to check if /proc is available and readable
system_boot_time = 0 system_boot_time = 0
@ -44,14 +45,13 @@ mutable struct ProcessCollector <: Collector
end end
end end
# Create the collector # 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 if registry !== nothing
register(registry, procc) register(registry, procc)
end end
return procc return procc
end end
end end
ProcessCollector(pid_f::Function = () -> "self") = ProcessCollector(DEFAULT_REGISTRY, pid_f)
function metric_names(::ProcessCollector) function metric_names(::ProcessCollector)
return ( return (
@ -68,7 +68,7 @@ function collect!(metrics::Vector, procc::ProcessCollector)
procc.system_boot_time == 0 && return metrics procc.system_boot_time == 0 && return metrics
# Fetch the pid # Fetch the pid
pid = try pid = try
strip(string(procc.pid_f())) String(strip(string(procc.pid()::Union{AbstractString,Integer})))::String
catch e catch e
@error "ProcessCollector: could not look up the pid from the lambda" e @error "ProcessCollector: could not look up the pid from the lambda" e
return metrics return metrics

4
test/runtests.jl

@ -318,7 +318,7 @@ end
@testset "Prometheus.ProcessCollector" begin @testset "Prometheus.ProcessCollector" begin
r = Prometheus.CollectorRegistry() r = Prometheus.CollectorRegistry()
c = Prometheus.ProcessCollector(r) c = Prometheus.ProcessCollector(; registry=r)
@test c in r.collectors @test c in r.collectors
metrics = Prometheus.collect(c) metrics = Prometheus.collect(c)
procfs_available = c.system_boot_time > 0 procfs_available = c.system_boot_time > 0
@ -368,7 +368,7 @@ end
@test isempty(metrics) @test isempty(metrics)
end end
# Test that pid function works # Test that pid function works
procc = Prometheus.ProcessCollector(nothing, () -> getpid()) procc = Prometheus.ProcessCollector(() -> getpid(); registry=nothing)
metrics = Prometheus.collect(procc) metrics = Prometheus.collect(procc)
if procfs_available if procfs_available
@test length(metrics) > 0 @test length(metrics) > 0

Loading…
Cancel
Save