67
67
import collections
68
68
import logging
69
69
import re
70
- from typing import Sequence
70
+ from typing import Iterable , Optional , Sequence , Union
71
71
72
- from prometheus_client import start_http_server
73
72
from prometheus_client .core import (
74
73
REGISTRY ,
75
- CollectorRegistry ,
76
74
CounterMetricFamily ,
75
+ SummaryMetricFamily ,
77
76
UnknownMetricFamily ,
78
77
)
79
78
80
- from opentelemetry .metrics import Counter , Metric , ValueRecorder
79
+ from opentelemetry .metrics import Counter , ValueRecorder
81
80
from opentelemetry .sdk .metrics .export import (
82
81
MetricRecord ,
83
82
MetricsExporter ,
84
83
MetricsExportResult ,
85
84
)
85
+ from opentelemetry .sdk .metrics .export .aggregate import MinMaxSumCountAggregator
86
86
87
87
logger = logging .getLogger (__name__ )
88
88
@@ -110,8 +110,8 @@ def shutdown(self) -> None:
110
110
111
111
112
112
class CustomCollector :
113
- """ CustomCollector represents the Prometheus Collector object
114
- https://github.com/prometheus/client_python#custom-collectors
113
+ """CustomCollector represents the Prometheus Collector object
114
+ https://github.com/prometheus/client_python#custom-collectors
115
115
"""
116
116
117
117
def __init__ (self , prefix : str = "" ):
@@ -121,7 +121,7 @@ def __init__(self, prefix: str = ""):
121
121
r"[^\w]" , re .UNICODE | re .IGNORECASE
122
122
)
123
123
124
- def add_metrics_data (self , metric_records : Sequence [MetricRecord ]):
124
+ def add_metrics_data (self , metric_records : Sequence [MetricRecord ]) -> None :
125
125
self ._metrics_to_export .append (metric_records )
126
126
127
127
def collect (self ):
@@ -152,34 +152,44 @@ def _translate_to_prometheus(self, metric_record: MetricRecord):
152
152
metric_name = self ._prefix + "_"
153
153
metric_name += self ._sanitize (metric_record .instrument .name )
154
154
155
+ description = getattr (metric_record .instrument , "description" , "" )
155
156
if isinstance (metric_record .instrument , Counter ):
156
157
prometheus_metric = CounterMetricFamily (
157
- name = metric_name ,
158
- documentation = metric_record .instrument .description ,
159
- labels = label_keys ,
158
+ name = metric_name , documentation = description , labels = label_keys
160
159
)
161
160
prometheus_metric .add_metric (
162
161
labels = label_values , value = metric_record .aggregator .checkpoint
163
162
)
164
163
# TODO: Add support for histograms when supported in OT
165
164
elif isinstance (metric_record .instrument , ValueRecorder ):
166
- prometheus_metric = UnknownMetricFamily (
167
- name = metric_name ,
168
- documentation = metric_record .instrument .description ,
169
- labels = label_keys ,
170
- )
171
- prometheus_metric .add_metric (
172
- labels = label_values , value = metric_record .aggregator .checkpoint
173
- )
165
+ value = metric_record .aggregator .checkpoint
166
+ if isinstance (metric_record .aggregator , MinMaxSumCountAggregator ):
167
+ prometheus_metric = SummaryMetricFamily (
168
+ name = metric_name ,
169
+ documentation = description ,
170
+ labels = label_keys ,
171
+ )
172
+ prometheus_metric .add_metric (
173
+ labels = label_values ,
174
+ count_value = value .count ,
175
+ sum_value = value .sum ,
176
+ )
177
+ else :
178
+ prometheus_metric = UnknownMetricFamily (
179
+ name = metric_name ,
180
+ documentation = description ,
181
+ labels = label_keys ,
182
+ )
183
+ prometheus_metric .add_metric (labels = label_values , value = value )
174
184
175
185
else :
176
186
logger .warning (
177
187
"Unsupported metric type. %s" , type (metric_record .instrument )
178
188
)
179
189
return prometheus_metric
180
190
181
- def _sanitize (self , key ) :
182
- """ sanitize the given metric name or label according to Prometheus rule.
191
+ def _sanitize (self , key : str ) -> str :
192
+ """sanitize the given metric name or label according to Prometheus rule.
183
193
Replace all characters other than [A-Za-z0-9_] with '_'.
184
194
"""
185
195
return self ._non_letters_nor_digits_re .sub ("_" , key )
0 commit comments