Browse Source

Family{C}: verify that label names are valid.

pull/6/head
Fredrik Ekre 2 years ago
parent
commit
ca8fcb08af
  1. 20
      src/Prometheus.jl
  2. 10
      test/runtests.jl

20
src/Prometheus.jl

@ -405,13 +405,32 @@ end
# Family{<:Collector} <: Collector # # Family{<:Collector} <: Collector #
#################################### ####################################
# https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
# - Labels may contain ASCII letters, numbers, as well as underscores.
# They must match the regex [a-zA-Z_][a-zA-Z0-9_]*.
# - Label names beginning with __ (two "_") are reserved for internal use.
function verify_label_name(label_name::String)
label_name_regex = r"^[a-zA-Z_][a-zA-Z0-9_]*$"
if !occursin(label_name_regex, label_name) || startswith(label_name, "__")
throw(ArgumentError("label name \"$(label_name)\" is invalid"))
end
return label_name
end
struct LabelNames{N} struct LabelNames{N}
labelnames::NTuple{N, String} labelnames::NTuple{N, String}
function LabelNames(label_names::NTuple{N, String}) where N
for label_name in label_names
verify_label_name(label_name)
end
return new{N}(label_names)
end
end end
struct LabelValues{N} struct LabelValues{N}
labelvalues::NTuple{N, String} labelvalues::NTuple{N, String}
end end
function Base.hash(l::LabelValues, h::UInt) function Base.hash(l::LabelValues, h::UInt)
h = hash(0x94a2d04ee9e5a55b, h) # hash("Prometheus.LabelValues") on Julia 1.9.3 h = hash(0x94a2d04ee9e5a55b, h) # hash("Prometheus.LabelValues") on Julia 1.9.3
for v in l.labelvalues for v in l.labelvalues
@ -419,6 +438,7 @@ function Base.hash(l::LabelValues, h::UInt)
end end
return h return h
end end
function Base.:(==)(l1::LabelValues, l2::LabelValues) function Base.:(==)(l1::LabelValues, l2::LabelValues)
return l1.labelvalues == l2.labelvalues return l1.labelvalues == l2.labelvalues
end end

10
test/runtests.jl

@ -168,7 +168,11 @@ end
end end
@testset "Prometheus.LabelNames and Prometheus.LabelValues" begin @testset "Prometheus.LabelNames and Prometheus.LabelValues" begin
# Custom hashing @test_throws(
Prometheus.ArgumentError("label name \"invalid-label\" is invalid"),
Prometheus.LabelNames(("invalid-label",)),
)
# Custom hashing of values
v1 = Prometheus.LabelValues(("foo", "bar")) v1 = Prometheus.LabelValues(("foo", "bar"))
v2 = Prometheus.LabelValues(("foo", "bar")) v2 = Prometheus.LabelValues(("foo", "bar"))
v3 = Prometheus.LabelValues(("foo", "baz")) v3 = Prometheus.LabelValues(("foo", "baz"))
@ -200,6 +204,10 @@ end
Prometheus.ArgumentError("metric name \"invalid-name\" is invalid"), Prometheus.ArgumentError("metric name \"invalid-name\" is invalid"),
Prometheus.Family{Collector}("invalid-name", "help", ("label",)), Prometheus.Family{Collector}("invalid-name", "help", ("label",)),
) )
@test_throws(
Prometheus.ArgumentError("label name \"invalid-label\" is invalid"),
Prometheus.Family{Collector}("valid_name", "help", ("invalid-label",)),
)
# Prometheus.labels(...), Prometheus.remove(...), Prometheus.clear() # Prometheus.labels(...), Prometheus.remove(...), Prometheus.clear()
l1 = ("/foo/", "200") l1 = ("/foo/", "200")
l2 = ("/bar/", "404") l2 = ("/bar/", "404")

Loading…
Cancel
Save