|
|
|
@ -13,16 +13,32 @@ abstract type Collector end |
|
|
|
|
|
|
|
|
|
|
|
abstract type PrometheusException <: Exception end |
|
|
|
abstract type PrometheusException <: Exception end |
|
|
|
|
|
|
|
|
|
|
|
struct PrometheusUnreachable <: PrometheusException end |
|
|
|
struct ArgumentError <: PrometheusException |
|
|
|
unreachable() = throw(PrometheusUnreachable()) |
|
|
|
msg::String |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
function Base.showerror(io::IO, err::ArgumentError) |
|
|
|
|
|
|
|
print(io, "Prometheus.", nameof(typeof(err)), ": ", err.msg) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
struct PrometheusAssert <: PrometheusException end |
|
|
|
struct UnreachableError <: PrometheusException end |
|
|
|
|
|
|
|
unreachable() = throw(UnreachableError()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct AssertionError <: PrometheusException end |
|
|
|
macro assert(cond) |
|
|
|
macro assert(cond) |
|
|
|
return quote |
|
|
|
return quote |
|
|
|
$(esc(cond)) || throw(PrometheusAssert()) |
|
|
|
$(esc(cond)) || throw(AssertionError()) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Base.showerror(io::IO, err::Union{AssertionError, UnreachableError}) |
|
|
|
|
|
|
|
print( |
|
|
|
|
|
|
|
io, |
|
|
|
|
|
|
|
"Prometheus.", nameof(typeof(err)), |
|
|
|
|
|
|
|
": this is unexpected. Please file an issue at " * |
|
|
|
|
|
|
|
"https://github.com/fredrikekre/Prometheus.jl/issues/new", |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
########################################### |
|
|
|
########################################### |
|
|
|
# Compat for const fields, @lock, @atomic # |
|
|
|
# Compat for const fields, @lock, @atomic # |
|
|
|
########################################### |
|
|
|
########################################### |
|
|
|
@ -75,8 +91,12 @@ function register(reg::CollectorRegistry, collector::Collector) |
|
|
|
for c in reg.collectors |
|
|
|
for c in reg.collectors |
|
|
|
union!(existing_names, metric_names(c)) |
|
|
|
union!(existing_names, metric_names(c)) |
|
|
|
end |
|
|
|
end |
|
|
|
if any(in(existing_names), metric_names(collector)) |
|
|
|
for metric_name in metric_names(collector) |
|
|
|
error("not allowed") |
|
|
|
if metric_name in existing_names |
|
|
|
|
|
|
|
throw(ArgumentError( |
|
|
|
|
|
|
|
"collector already contains a metric with the name \"$(metric_name)\"" |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
push!(reg.collectors, collector) |
|
|
|
push!(reg.collectors, collector) |
|
|
|
end |
|
|
|
end |
|
|
|
@ -151,12 +171,15 @@ end |
|
|
|
""" |
|
|
|
""" |
|
|
|
Prometheus.inc(counter::Counter, v = 1) |
|
|
|
Prometheus.inc(counter::Counter, v = 1) |
|
|
|
|
|
|
|
|
|
|
|
Increment the value of the counter with `v`. |
|
|
|
Increment the value of the counter with `v`. The value defaults to `v = 1`. |
|
|
|
`v` must be non-negative, and defaults to `v = 1`. |
|
|
|
|
|
|
|
|
|
|
|
Throw a `Prometheus.ArgumentError` if `v < 0` (a counter must not decrease). |
|
|
|
""" |
|
|
|
""" |
|
|
|
function inc(counter::Counter, v = 1.0) |
|
|
|
function inc(counter::Counter, v = 1.0) |
|
|
|
if v < 0 |
|
|
|
if v < 0 |
|
|
|
error("counting backwards") |
|
|
|
throw(ArgumentError( |
|
|
|
|
|
|
|
"invalid value $v: a counter must not decrease" |
|
|
|
|
|
|
|
)) |
|
|
|
end |
|
|
|
end |
|
|
|
@atomic counter.value += v |
|
|
|
@atomic counter.value += v |
|
|
|
return nothing |
|
|
|
return nothing |
|
|
|
@ -507,7 +530,7 @@ function collect!(metrics::Vector, family::Family{C}) where C |
|
|
|
for (labels, child) in family.children |
|
|
|
for (labels, child) in family.children |
|
|
|
# collect!(...) the child, throw away the metric, but keep the samples |
|
|
|
# collect!(...) the child, throw away the metric, but keep the samples |
|
|
|
child_metrics = collect!(resize!(buf, 0), child) |
|
|
|
child_metrics = collect!(resize!(buf, 0), child) |
|
|
|
length(child_metrics) !=1 && error("multiple metrics not supported (yet?)") |
|
|
|
length(child_metrics) != 1 && unreachable() # TODO: maybe this should be supported? |
|
|
|
child_metric = child_metrics[1] |
|
|
|
child_metric = child_metrics[1] |
|
|
|
@assert(child_metric.type == type) |
|
|
|
@assert(child_metric.type == type) |
|
|
|
# Unwrap and rewrap samples with the labels |
|
|
|
# Unwrap and rewrap samples with the labels |
|
|
|
|