Skip to content

Commit 8d933d3

Browse files
committed
Family{C}: consistent variable naming (NFC).
1 parent 696d9c1 commit 8d933d3

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

src/Prometheus.jl

+33-30
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ function verify_label_name(label_name::String)
418418
end
419419

420420
struct LabelNames{N}
421-
labelnames::NTuple{N, String}
421+
label_names::NTuple{N, String}
422422
function LabelNames(label_names::NTuple{N, String}) where N
423423
for label_name in label_names
424424
verify_label_name(label_name)
@@ -428,36 +428,36 @@ struct LabelNames{N}
428428
end
429429

430430
struct LabelValues{N}
431-
labelvalues::NTuple{N, String}
431+
label_values::NTuple{N, String}
432432
end
433433

434434
function Base.hash(l::LabelValues, h::UInt)
435435
h = hash(0x94a2d04ee9e5a55b, h) # hash("Prometheus.LabelValues") on Julia 1.9.3
436-
for v in l.labelvalues
436+
for v in l.label_values
437437
h = hash(v, h)
438438
end
439439
return h
440440
end
441441

442442
function Base.:(==)(l1::LabelValues, l2::LabelValues)
443-
return l1.labelvalues == l2.labelvalues
443+
return l1.label_values == l2.label_values
444444
end
445445

446446
struct Family{C, N} <: Collector
447447
metric_name::String
448448
help::String
449-
labelnames::LabelNames{N}
449+
label_names::LabelNames{N}
450450
children::Dict{LabelValues{N}, C}
451451
lock::ReentrantLock
452452

453453
function Family{C}(
454-
metric_name::String, help::String, labelnames::NTuple{N, String};
454+
metric_name::String, help::String, label_names::NTuple{N, String};
455455
registry::Union{CollectorRegistry, Nothing}=DEFAULT_REGISTRY,
456456
) where {C, N}
457457
children = Dict{LabelValues{N}, C}()
458458
lock = ReentrantLock()
459459
family = new{C, N}(
460-
verify_metric_name(metric_name), help, LabelNames(labelnames), children, lock,
460+
verify_metric_name(metric_name), help, LabelNames(label_names), children, lock,
461461
)
462462
if registry !== nothing
463463
register(registry, family)
@@ -467,15 +467,15 @@ struct Family{C, N} <: Collector
467467
end
468468

469469
"""
470-
Prometheus.Family{C}(name, help, labelnames; registry=DEFAULT_REGISTRY)
470+
Prometheus.Family{C}(name, help, label_names; registry=DEFAULT_REGISTRY)
471471
472-
Create a labeled collector family with labels given by `labelnames`. For every new set of
472+
Create a labeled collector family with labels given by `label_names`. For every new set of
473473
label values encountered a new collector of type `C <: Collector` will be created.
474474
475475
**Arguments**
476476
- `name :: String`: the name of the family metric.
477477
- `help :: String`: the documentation for the family metric.
478-
- `labelnames :: Tuple{String, ...}`: the label names.
478+
- `label_names :: Tuple{String, ...}`: the label names.
479479
480480
**Keyword arguments**
481481
- `registry :: Prometheus.CollectorRegistry`: the registry in which to register the
@@ -505,37 +505,37 @@ function metric_names(family::Family)
505505
end
506506

507507
"""
508-
Prometheus.labels(family::Family{C}, labelvalues::Tuple{String, ...}) where C
508+
Prometheus.labels(family::Family{C}, label_values::Tuple{String, ...}) where C
509509
510510
Return the collector of type `C` from the family corresponding to the labels given by
511-
`labelvalues`.
511+
`label_values`.
512512
513513
!!! note
514514
This method does an acquire/release of a lock, and a dictionary lookup, to find the
515515
collector matching the label names. For typical applications this overhead does not
516516
matter (below 100ns for some basic benchmarks) but it is safe to cache the returned
517517
collector if required.
518518
"""
519-
function labels(family::Family{C, N}, labelvalues::NTuple{N, String}) where {C, N}
520-
collector = @lock family.lock get!(family.children, LabelValues(labelvalues)) do
519+
function labels(family::Family{C, N}, label_values::NTuple{N, String}) where {C, N}
520+
collector = @lock family.lock get!(family.children, LabelValues(label_values)) do
521521
# TODO: Avoid the re-verification of the metric name?
522522
C(family.metric_name, family.help; registry=nothing)
523523
end
524524
return collector
525525
end
526526

527527
"""
528-
Prometheus.remove(family::Family, labelvalues::Tuple{String, ...})
528+
Prometheus.remove(family::Family, label_values::Tuple{String, ...})
529529
530-
Remove the collector corresponding to `labelvalues`. Effectively this resets the collector
530+
Remove the collector corresponding to `label_values`. Effectively this resets the collector
531531
since [`Prometheus.labels`](@ref) will recreate the collector when called with the same
532532
label names.
533533
534534
!!! note
535535
This method invalidates cached collectors for the label names.
536536
"""
537-
function remove(family::Family{<:Any, N}, labelvalues::NTuple{N, String}) where N
538-
@lock family.lock delete!(family.children, LabelValues(labelvalues))
537+
function remove(family::Family{<:Any, N}, label_values::NTuple{N, String}) where N
538+
@lock family.lock delete!(family.children, LabelValues(label_values))
539539
return
540540
end
541541

@@ -576,19 +576,22 @@ function collect!(metrics::Vector, family::Family{C}) where C
576576
else
577577
@assert(child_samples isa Vector{Sample})
578578
for child_sample in child_samples
579-
@assert(child_sample.labels === nothing)
579+
@assert(child_sample.label_values === nothing)
580580
push!(samples, Sample(child_sample.suffix, labels, child_sample.value))
581581
end
582582
end
583583
end
584584
end
585585
# Sort samples lexicographically by the labels
586586
sort!(samples; by = function(x)
587-
labels = x.labels
587+
labels = x.label_values
588588
@assert(labels !== nothing)
589-
return labels.labelvalues
589+
return labels.label_values
590590
end)
591-
push!(metrics, Metric(type, family.metric_name, family.help, family.labelnames, samples))
591+
push!(
592+
metrics,
593+
Metric(type, family.metric_name, family.help, family.label_names, samples),
594+
)
592595
return metrics
593596
end
594597

@@ -599,15 +602,15 @@ end
599602

600603
struct Sample
601604
suffix::Union{String, Nothing} # e.g. _count or _sum
602-
labels::Union{LabelValues, Nothing}
605+
label_values::Union{LabelValues, Nothing}
603606
value::Float64
604607
end
605608

606609
struct Metric
607610
type::String
608611
metric_name::String
609612
help::String
610-
labelnames::Union{LabelNames, Nothing}
613+
label_names::Union{LabelNames, Nothing}
611614
# TODO: Union{Tuple{Sample}, Vector{Sample}} would always make this iterable.
612615
samples::Union{Sample, Vector{Sample}}
613616
end
@@ -628,12 +631,12 @@ function expose_metric(io::IO, metric::Metric)
628631
print_escaped(io, metric.help, ('\\', '\n'))
629632
println(io)
630633
println(io, "# TYPE ", metric.metric_name, " ", metric.type)
631-
labelnames = metric.labelnames
634+
label_names = metric.label_names
632635
samples = metric.samples
633636
if samples isa Sample
634637
# Single sample, no labels
635-
@assert(labelnames === nothing)
636-
@assert(samples.labels === nothing)
638+
@assert(label_names === nothing)
639+
@assert(samples.label_values === nothing)
637640
@assert(samples.suffix === nothing)
638641
val = samples.value
639642
println(io, metric.metric_name, " ", isinteger(val) ? Int(val) : val)
@@ -648,11 +651,11 @@ function expose_metric(io::IO, metric::Metric)
648651
print(io, sample.suffix)
649652
end
650653
# Print potential labels
651-
labels = sample.labels
652-
if labelnames !== nothing && labels !== nothing
654+
labels = sample.label_values
655+
if label_names !== nothing && labels !== nothing
653656
first = true
654657
print(io, "{")
655-
for (name, value) in zip(labelnames.labelnames, labels.labelvalues)
658+
for (name, value) in zip(label_names.label_names, labels.label_values)
656659
first || print(io, ",")
657660
print(io, name, "=\"")
658661
print_escaped(io, value, ('\\', '\"', '\n'))

test/runtests.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ end
152152
s1, s2 = metric.samples[1], metric.samples[2]
153153
@test s1.suffix == "_count"
154154
@test s2.suffix == "_sum"
155-
@test s1.labels === nothing
156-
@test s2.labels === nothing
155+
@test s1.label_values === nothing
156+
@test s2.label_values === nothing
157157
@test s1.value == 2
158158
@test s2.value == 11
159159
# Prometheus.expose_metric(...)
@@ -237,8 +237,8 @@ end
237237
@test metric.help == c.help
238238
@test length(metric.samples) == 2
239239
s1, s2 = metric.samples[1], metric.samples[2]
240-
@test s1.labels.labelvalues == ("/bar/", "404")
241-
@test s2.labels.labelvalues == ("/foo/", "200")
240+
@test s1.label_values.label_values == ("/bar/", "404")
241+
@test s2.label_values.label_values == ("/foo/", "200")
242242
@test s1.value == 3
243243
@test s2.value == 3
244244
# Prometheus.expose_metric(...)
@@ -292,8 +292,8 @@ end
292292
@test metric.help == c.help
293293
@test length(metric.samples) == 4
294294
s1, s2, s3, s4 = metric.samples
295-
@test s1.labels.labelvalues == s2.labels.labelvalues == ("/bar/", "404")
296-
@test s3.labels.labelvalues == s4.labels.labelvalues == ("/foo/", "200")
295+
@test s1.label_values.label_values == s2.label_values.label_values == ("/bar/", "404")
296+
@test s3.label_values.label_values == s4.label_values.label_values == ("/foo/", "200")
297297
@test s1.value == 2 # _count
298298
@test s2.value == 6.4 # _sum
299299
@test s3.value == 2 # _count

0 commit comments

Comments
 (0)