Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Family{C}: Generalize labeling specification #7

Merged
merged 1 commit into from
Nov 12, 2023
Merged

Conversation

fredrikekre
Copy link
Owner

This patch changes how label names and label values can be passed to the constructor Prometheus.Family{C} and Prometheus.labels. In particular, the label names are now stored as symbols internally. This enables using getfield when creating the corresponding label values. Concretely this enables using e.g. named tuples, and custom structs, as label names/values.

For example, the following now works

struct RequestLabels
    target::String
    status_code::Int
end
request_counter = Prometheus.Family{Prometheus.Counter}(
    "http_requests", "Total number of HTTP requests", RequestLabels
)
counter = Prometheus.labels(request_counter, RequestLabels("/api", 200))

In this example, the field names of the type, RequestLabels, are used as label names in the Family{C} constructor. When extracting the counter for a specific set of labels an instance of the struct is used. All non-string values are stringified using string (status_code::Int in the example above, for example).

This patch changes how label names and label values can be passed to the
constructor `Prometheus.Family{C}` and `Prometheus.labels`. In
particular, the label names are now stored as symbols internally. This
enables using `getfield` when creating the corresponding label values.
Concretely this enables using e.g. named tuples, and custom structs, as
label names/values.

For example, the following now works
```julia
struct RequestLabels
    target::String
    status_code::Int
end
request_counter = Prometheus.Family{Prometheus.Counter}(
    "http_requests", "Total number of HTTP requests", RequestLabels
)
counter = Prometheus.labels(request_counter, RequestLabels("/api", 200))
```
In this example, the field names of the type, `RequestLabels`, are used
as label names in the `Family{C}` constructor. When extracting the
counter for a specific set of labels an instance of the struct is used.
All non-string values are stringified using string (`status_code::Int`
in the example above, for example).
Copy link

codecov bot commented Nov 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Files Coverage Δ
src/Prometheus.jl 94.77% <100.00%> (+0.32%) ⬆️

📢 Thoughts on this report? Let us know!

@fredrikekre fredrikekre merged commit e27d602 into master Nov 12, 2023
@fredrikekre fredrikekre deleted the fe/labels branch November 12, 2023 22:50
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant