diff --git a/build/semconv/policies/prometheus.rego b/build/semconv/policies/prometheus.rego index 41eb3b9d8b..824b1e924d 100644 --- a/build/semconv/policies/prometheus.rego +++ b/build/semconv/policies/prometheus.rego @@ -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 # ============================================================================= diff --git a/build/semconv/templates/go/metrics.go.j2 b/build/semconv/templates/go/metrics.go.j2 index cb79006291..5358486f1e 100644 --- a/build/semconv/templates/go/metrics.go.j2 +++ b/build/semconv/templates/go/metrics.go.j2 @@ -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 %}