Add support for generating only Opts types

Signed-off-by: Arthur Silva Sens <arthursens2005@gmail.com>
This commit is contained in:
Arthur Silva Sens 2026-01-20 11:08:48 -03:00
parent f7b009de9b
commit ea2370e773
No known key found for this signature in database
2 changed files with 26 additions and 0 deletions

View file

@ -268,6 +268,20 @@ deny contains metric_violation(
not is_number(val)
}
# =============================================================================
# Rule: only_opts must be a boolean when present
# =============================================================================
deny contains metric_violation(
sprintf("Metric '%s': annotations.prometheus.only_opts must be a boolean, got %v", [group.metric_name, type_name(group.annotations.prometheus.only_opts)]),
group.id,
group.metric_name
) if {
group := input.groups[_]
group.type == "metric"
group.annotations.prometheus.only_opts
not is_boolean(group.annotations.prometheus.only_opts)
}
# =============================================================================
# Helper Functions
# =============================================================================

View file

@ -102,6 +102,17 @@ func (a {{ name }}Attr) Value() string {
{%- else -%}
{%- set metric_inst = (metric.instrument | default("gauge")) | map_text("go_instrument_type") -%}
{%- endif -%}
{#- Check if only_opts is set - generate just an Opts function instead of full type -#}
{%- if prom.only_opts %}
{{ h.metric_typedoc(metric, "") | comment | trim }}
func {{ metric_name }}Opts() prometheus.{{ metric_inst }}Opts {
return prometheus.{{ metric_inst }}Opts{
Name: "{{ metric.metric_name }}",
Help: "{{ metric.brief | default("") | trim }}",
}
}
{%- else %}
{{ h.metric_typedoc(metric, "") | comment | trim }}
type {{ metric_name }} struct {
@ -242,6 +253,7 @@ func (m {{ metric_name }}) With(
return m.{{ metric_inst }}Vec.With(labels)
}
{%- endif %}
{%- endif %}{# end only_opts else block #}
{%- if not loop.last %}
{% endif %}