Adding a new metric type - Python

Re-export generated API

By default a metric type gets an auto-generated API from the definition in glean.udl. If this API is sufficient it needs to be re-exported.

In glean-core/python/glean/metrics/ add a new re-export:

from .._uniffi import CounterMetric as CounterMetricType

Extend and modify API

If the generated API is not sufficient, convenient or needs additional language-specific constructs or conversions the generated API can be wrapped.

Create a new Python file, e.g. glean-core/python/glean/metrics/ Then create a new class, that delegates functionality to the generated metric type class.

from .._uniffi import CommonMetricData
from .._uniffi import CounterMetric

class CounterMetricType:
    def __init__(self, common_metric_data: CommonMetricData):
        self._inner = CounterMetric(common_metric_data)

    # Wrap existing functionality
    def add(self, amount = 1):

    # Add a new method
    def add_two(self):

The new metric type also needs to be imported from glean-core/python/glean/metrics/

from .counter import CounterMetricType

__all__ = [
    # ...

It also must be added to the _TYPE_MAPPING in glean-core/python/glean/

    "counter": metrics.CounterMetricType,
    # ...