[exporter-otlp-proto-common] Include metric info in encoding exception by pmcollins · Pull Request #4154 · open-telemetry/opentelemetry-python

As the linked issue mentions, if a failure occurs during converting a metric to proto, the exception message doesn't contain information about the metric that couldn't be converted. This change adds that information to the exception.

Exception while exporting metrics Value out of range: 17131960503145336928
Traceback (most recent call last):
  File "/Users/user/github/open-telemetry/opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py", line 541, in _receive_metrics
    self._exporter.export(
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py", line 160, in export
    return self._export(data=metrics_data)
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py", line 263, in _export
    request=self._translate_data(data),
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py", line 150, in _translate_data
    return encode_metrics(data)
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py", line 260, in encode_metrics
    pt.as_int = data_point.value
ValueError: Value out of range: 17131960503145336928
Exception while exporting metrics
Traceback (most recent call last):
  File "/Users/user/github/open-telemetry/opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/export/__init__.py", line 541, in _receive_metrics
    self._exporter.export(
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py", line 165, in export
    return self._export(data=metrics_data)
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/exporter.py", line 301, in _export
    request=self._translate_data(data),
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-grpc/src/opentelemetry/exporter/otlp/proto/grpc/metric_exporter/__init__.py", line 155, in _translate_data
    return encode_metrics(data)
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py", line 195, in encode_metrics
    _encode_resource_metrics(resource_metrics, resource_metrics_dict)
  File "/Users/user/github/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/metrics_encoder/__init__.py", line 246, in _encode_resource_metrics
    raise EncodingException(ex, metric) from None
opentelemetry.exporter.otlp.proto.common._internal.metrics_encoder.EncodingException: Metric(name='process.runtime.cpython.context_switches', description='Runtime context switches', unit='switches', data=Sum(data_points=[NumberDataPoint(attributes={'type': 'involuntary'}, start_time_unix_nano=1724856334261458000, time_unix_nano=1724856334261510000, value=0), NumberDataPoint(attributes={'type': 'voluntary'}, start_time_unix_nano=1724856334261468000, time_unix_nano=1724856334261510000, value=15841456515849392318)], aggregation_temporality=<AggregationTemporality.CUMULATIVE: 2>, is_monotonic=True))
Value out of range: 15841456515849392318

Please delete options that are not relevant.

Manually end-to-end tested and unit test coverage added.