Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use gauge with changing attributes #2739

Closed
tkinz27 opened this issue Jun 3, 2022 · 1 comment
Closed

Cannot use gauge with changing attributes #2739

tkinz27 opened this issue Jun 3, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@tkinz27
Copy link

tkinz27 commented Jun 3, 2022

Describe your environment Describe any aspect of your environment relevant to the problem, including your Python version, platform, version numbers of installed dependencies, information about your cloud hosting provider, etc. If you're reporting a problem with a specific version of a library in this repo, please check whether the problem has been fixed on main.

I am running from a virtual env with the latest main branch installed (f8d6795).

Steps to reproduce
Describe exactly how to reproduce the error. Include a code sample if applicable.

With the following example code I get the following traceback after the attribute value for my gauge changes

import time

from typing import Iterable

# METRICS
from prometheus_client import start_http_server
from opentelemetry import metrics
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.exporter.prometheus import PrometheusMetricReader#

# METRICS
start_http_server(port=9092, addr='localhost')
metrics.set_meter_provider(MeterProvider(
    metric_readers=[PrometheusMetricReader("test")])
)

meter = metrics.get_meter("test")
c = meter.create_counter(name="exceptions", description="number of exceptions caught", unit="1")
staging_attributes = {"environment": "staging"}
c.add(1, {})

b = 1
def gauge_cb(opts: metrics.CallbackOptions) -> Iterable[metrics.Observation]:
    global b
    b += 1
    labels = {"activityId": str(b % 3)}
    yield metrics.Observation(1, labels)

gauge = meter.create_observable_gauge(name="activity_state", description="1 while in activity", callbacks=[gauge_cb])

while True:
    time.sleep(3)
    c.add(1, staging_attributes)
    # requests_size.record(100, staging_attributes)
Traceback (most recent call last):
  File "/usr/lib/python3.10/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/stupid/venv/lib/python3.10/site-packages/prometheus_client/exposition.py", line 128, in prometheus_app
    status, headers, output = _bake_output(registry, accept_header, accept_encoding_header, params, disable_compression)
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/stupid/venv/lib/python3.10/site-packages/prometheus_client/exposition.py", line 104, in _bake_output
    output = encoder(registry)
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/stupid/venv/lib/python3.10/site-packages/prometheus_client/exposition.py", line 197, in generate_latest
    for metric in registry.collect():
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/stupid/venv/lib/python3.10/site-packages/prometheus_client/registry.py", line 97, in collect
    yield from collector.collect()
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py", line 166, in collect
    self._translate_to_prometheus(
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/exporter/opentelemetry-exporter-prometheus/src/opentelemetry/exporter/prometheus/__init__.py", line 204, in _translate_to_prometheus
    for key, value in number_data_point.attributes.items():
AttributeError: 'NoneType' object has no attribute 'attributes'
^CTraceback (most recent call last):
  File "/home/tony.kinsley/code/github.com/open-telemetry/opentelemetry-python/stupid/metrics-test.py", line 40, in <module>
    time.sleep(3)
KeyboardInterrupt

What is the expected behavior?
What did you expect to see?

I expected to be able to export gauge metrics with differing label values based on when the scrape happened

What is the actual behavior?
What did you see instead?

See traceback

Additional context
Add any other context about the problem here.

I assume this is a misunderstanding on my part, but the code is fairly abstracted so its been difficult for me to follow along.

@tkinz27 tkinz27 added the bug Something isn't working label Jun 3, 2022
@srikanthccv
Copy link
Member

@tkinz27 This is a known bug and duplicate of #2725 . Hence closing. Let's follow up there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants