Skip to content

Commit

Permalink
bug 1909244 - Update rust codegen to handle LabeledMetricData
Browse files Browse the repository at this point in the history
  • Loading branch information
chutten committed Jul 22, 2024
1 parent 64a46aa commit d246213
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add the `module_spec` option to the javascript_server outputter ([#726](https://github.com/mozilla/glean_parser/pull/726))
- BUGFIX: Fix the Rust codegen for changes to how `labeled_*` metrics are constructed ([bug 1909244](https://bugzilla.mozilla.org/show_bug.cgi?id=1909244))

## 14.2.0

Expand Down
52 changes: 42 additions & 10 deletions glean_parser/templates/rust.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ impl ExtraKeys for {{ obj.name|Camelize }}{{ suffix }} {
}
}
{% endmacro %}
{% macro common_metric_data(obj) %}
CommonMetricData {
category: {{ obj.category|rust }},
name: {{ obj.name|rust }},
send_in_pings: {{ obj.send_in_pings|rust }},
lifetime: {{ obj.lifetime|rust }},
disabled: {{ obj.is_disabled()|rust }},
..Default::default()
}
{% endmacro %}
{% for category in categories %}
{% if category.contains_pings %}
{% for obj in category.objs.values() %}
Expand All @@ -92,7 +102,7 @@ pub static {{ obj.name|snake_case }}: ::glean::private::__export::Lazy<::glean::
{% else %}
pub mod {{ category.name|snake_case }} {
#[allow(unused_imports)] // HistogramType might be unusued, let's avoid warnings
use glean::{private::*, traits::ExtraKeys, traits::NoExtraKeys, CommonMetricData, HistogramType, Lifetime, TimeUnit, MemoryUnit};
use glean::{private::*, traits::ExtraKeys, traits::NoExtraKeys, CommonMetricData, HistogramType, LabeledMetricData, Lifetime, TimeUnit, MemoryUnit};
{% for obj in category.objs.values() %}

{% if obj|attr("_generate_structure") %}
Expand All @@ -107,15 +117,37 @@ pub mod {{ category.name|snake_case }} {
///
/// {{ obj.description|wordwrap() | replace('\n', '\n /// ') }}
pub static {{ obj.name|snake_case }}: ::glean::private::__export::Lazy<{{ obj|type_name }}> = ::glean::private::__export::Lazy::new(|| {
{{ obj|ctor }}(CommonMetricData {
category: {{ obj.category|rust }},
name: {{ obj.name|rust }},
send_in_pings: {{ obj.send_in_pings|rust }},
lifetime: {{ obj.lifetime|rust }},
disabled: {{ obj.is_disabled()|rust }},
..Default::default()
}
{%- for arg_name in extra_metric_args if obj[arg_name] is defined and arg_name != 'allowed_extra_keys' -%}
let meta =
{% if obj.type == "labeled_custom_distribution" %}
LabeledMetricData::CustomDistribution {
cmd: {{ common_metric_data(obj)|indent(16) }}
{%- for arg_name in extra_metric_args if obj[arg_name] is defined and arg_name != 'allowed_extra_keys' -%}
, {{ arg_name }}: {{ obj[arg_name]|rust }}
{%- endfor -%}
};
{% elif obj.type == "labeled_memory_distribution" %}
LabeledMetricData::MemoryDistribution {
cmd: {{ common_metric_data(obj)|indent(16) }}
{%- for arg_name in extra_metric_args if obj[arg_name] is defined and arg_name != 'allowed_extra_keys' -%}
, {{ "unit" if arg_name == "memory_unit" else arg_name }}: {{ obj[arg_name]|rust }}
{%- endfor -%}
};
{% elif obj.type == "labeled_timing_distribution" %}
LabeledMetricData::TimingDistribution {
cmd: {{ common_metric_data(obj)|indent(16) }}
{%- for arg_name in extra_metric_args if obj[arg_name] is defined and arg_name != 'allowed_extra_keys' -%}
, {{ "unit" if arg_name == "time_unit" else arg_name }}: {{ obj[arg_name]|rust }}
{%- endfor -%}
};
{% elif obj.labeled %}
LabeledMetricData::Common {
cmd: {{common_metric_data(obj)|indent(16) }},
};
{% else %}
{{ common_metric_data(obj)|indent(12) }};
{% endif %}
{{ obj|ctor }}(meta
{%- for arg_name in extra_metric_args if not obj.labeled and obj[arg_name] is defined and arg_name != 'allowed_extra_keys' -%}
, {{ obj[arg_name]|rust }}
{%- endfor -%}
{{ ", " if obj.labeled else ")\n" }}
Expand Down

0 comments on commit d246213

Please sign in to comment.