census-instrumentation / opencensus-python

A stats collection and distributed tracing framework
Apache License 2.0
668 stars 250 forks source link

Exporter/Prometheus: broken for SumAggregation and LastValueAggregation on 0.2.1 with OpenCensus 0.7.0 and later #807

Open micolous opened 4 years ago

micolous commented 4 years ago

https://github.com/census-instrumentation/opencensus-python/commit/0d004585e945292df8fe042f7415c675fff2c14c renamed opencensus.stats.aggregation_data.SumAggregationDataFloat to SumAggregationData in #696.

The opencensus-ext-prometheus module was updated to support this in the same commit. However, there has been no release cut of opencensus-ext-prometheus since 2019-04-25, which appears to correspond to opencensus 0.5.0.

As a result, metric export is broken for any View using SumAggregation or LastValueAggregation, as when Prometheus collection runs, this if-elif chain results in:

  File "/usr/local/lib/python3.7/site-packages/prometheus_client/exposition.py", line 152, in do_GET
    output = encoder(registry)
  File "/usr/local/lib/python3.7/site-packages/prometheus_client/openmetrics/exposition.py", line 14, in generate_latest
    for metric in registry.collect():
  File "/usr/local/lib/python3.7/site-packages/prometheus_client/registry.py", line 75, in collect
    for metric in collector.collect():
  File "/usr/local/lib/python3.7/site-packages/opencensus/ext/prometheus/stats_exporter/__init__.py", line 242, in collect
    metric = self.to_metric(desc, tag_values, agg_data)
  File "/usr/local/lib/python3.7/site-packages/opencensus/ext/prometheus/stats_exporter/__init__.py", line 209, in to_metric
    aggregation_data_module.SumAggregationDataFloat):
AttributeError: module 'opencensus.stats.aggregation_data' has no attribute 'SumAggregationDataFloat'

Package versions:

opencensus==0.7.5
opencensus-context==0.1.1
opencensus-ext-prometheus==0.2.1
prometheus-client==0.7.1

Also, the version numbering appears inconsistent, which makes things more confusing...

micolous commented 4 years ago

A quick work-around, which makes LastValueAggregation work:

from opencensus.stats import aggregation_data
aggregation_data.SumAggregationDataFloat = aggregation_data.SumAggregationData

This should be safe to also apply to current git master of opencensus.ext.prometheus (as SumAggregationDataFloat is never referenced).

klaasjanelzinga commented 4 years ago

I think the source looks ok on master but it is not released to pypi. I think it just has to be released. Line 209 says SumAggregationData

I checked circle ci but saw no releases regarding this.

klaasjanelzinga commented 4 years ago

I added the __init__.py to my own code-tree and it works:


from app.opencensus.ext.prometheus import new_stats_exporter
from app.opencensus.ext.prometheus import Options

...
new_stats_exporter(Options(address="", port=8081, namespace="rss-local"))

(added the init.py in app/opencensus.ext.prometheus).

So I am pretty sure the lacking uploads to pypi are the cause of this issue.