opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

[exporter-otlp-proto-common] Include metric info in encoding exception

Open pmcollins opened this issue 5 months ago • 0 comments

Also remove redundant str(exception) from the exception message

Description

Fixes #4093

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.

Before this change:

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

After this change:

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

Type of change

Please delete options that are not relevant.

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] This change requires a documentation update

How Has This Been Tested?

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

Does This PR Require a Contrib Repo Change?

  • [ ] Yes. - Link to PR:
  • [x] No.

Checklist:

  • [ ] Followed the style guidelines of this project
  • [ ] Changelogs have been updated
  • [x] Unit tests have been added
  • [ ] Documentation has been updated

pmcollins avatar Aug 28 '24 15:08 pmcollins