Skip to content

Metric attributes does not accept all types of sequences #3750

@ericstengard

Description

@ericstengard

Describe your environment

Python version: 3.10.10
Opentelemetry-python version: 1.23.0

Steps to reproduce
It is defined in types.py, https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/util/types.py#L18, that we can supply a sequence of say strings as an attribute to a metric. This does not work with lists and sets but using a n-tuple works.

Offending line seems to be:

Below is a minimal example which proves that using an n-tuple works and a list does not.

from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter

reader = PeriodicExportingMetricReader(ConsoleMetricExporter())
meterProvider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(meterProvider)

attributes = {"tuple_attr": ("string", "another_string"), "list_attr": ["string"]}

metrics.get_meter_provider().get_meter("my_test_meter").create_counter("test_counter").add(
    1, attributes=attributes
)

Example output from running the above minimal example:

Traceback (most recent call last):
  File "/home/<REDACTED>/test.py", line 12, in <module>
    metrics.get_meter_provider().get_meter("my_test_meter").create_counter("test_counter").add(
  File "/home/<REDACTED>/lib/python3.10/site-packages/opentelemetry/sdk/metrics/_internal/instrument.py", line 159, in add
    self._measurement_consumer.consume_measurement(
  File "/home/<REDACTED>/lib/python3.10/site-packages/opentelemetry/sdk/metrics/_internal/measurement_consumer.py", line 82, in consume_measurement
    reader_storage.consume_measurement(measurement)
  File "/home/<REDACTED>/lib/python3.10/site-packages/opentelemetry/sdk/metrics/_internal/metric_reader_storage.py", line 120, in consume_measurement
    view_instrument_match.consume_measurement(measurement)
  File "/home/<REDACTED>/lib/python3.10/site-packages/opentelemetry/sdk/metrics/_internal/_view_instrument_match.py", line 98, in consume_measurement
    aggr_key = frozenset(attributes.items())
TypeError: unhashable type: 'list'

What is the expected behavior?
All sequences to work as attributes.

What is the actual behavior?
Not all sequences work as attributes.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions