Skip to content

Commit 7785acc

Browse files
committed
Add metric from/to protobuf v1alpha8 conversion
To make sure we can decouple the clients from the underlying protocol, we need to make sure we always use explicit conversion between Python enums and protobuf enums. This is especially important to be able to support multiple versions of a protobuf message, for example an upcoming v1alpha9 Metric enum, so downstream project can migrate to new versions in a backwards-compatible way. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent e3c0feb commit 7785acc

5 files changed

Lines changed: 209 additions & 161 deletions

File tree

RELEASE_NOTES.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
<!-- Here goes a general summary of what this release is about -->
66

7-
## Upgrading
7+
## Deprecation
88

9-
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
9+
- Converting `Metric` enums from/to protobuf directly is deprecated and will be dropped in the next breaking release.
10+
11+
You should switch to use the new conversion functions in `frequenz.client.common.metrics.proto.v1alpha8` to convert from/to protobuf.
12+
13+
Since we can't emit deprecation messages for this (as they will trigger every time a metric value is used), please consider using the new conversion functions as soon as possible so the migration to the next breaking release is smooth.
1014

1115
## New Features
1216

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
17+
- A new module `frequenz.client.common.metrics.proto.v1alpha8` has been added to provide conversion functions for `Metric`s from/to protobuf version `v1alpha8`.
1418

1519
## Bug Fixes
1620

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# License: MIT
2+
# Copyright © 2026 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Conversion of Metric from/to protobuf v1alpha8."""
5+
6+
from ._metric import metric_from_proto, metric_to_proto
7+
8+
__all__ = [
9+
"metric_from_proto",
10+
"metric_to_proto",
11+
]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# License: MIT
2+
# Copyright © 2026 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Coversion of Metric to/from protobuf v1alpha8."""
5+
6+
7+
from frequenz.api.common.v1alpha8.metrics import metrics_pb2
8+
9+
from ....proto import enum_from_proto
10+
from ..._metric import Metric
11+
12+
13+
def metric_from_proto(message: metrics_pb2.Metric.ValueType) -> Metric | int:
14+
"""Convert a protobuf Metric message to a Metric enum member.
15+
16+
Args:
17+
message: A protobuf Metric message.
18+
19+
Returns:
20+
The corresponding Metric enum member.
21+
"""
22+
return enum_from_proto(message, Metric)
23+
24+
25+
def metric_to_proto(metric: Metric) -> metrics_pb2.Metric.ValueType:
26+
"""Convert a Metric enum member to a protobuf Metric message.
27+
28+
Args:
29+
metric: A Metric enum member.
30+
31+
Returns:
32+
The corresponding protobuf Metric message.
33+
"""
34+
return metrics_pb2.Metric.ValueType(metric.value)

0 commit comments

Comments
 (0)