1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
import functools
import inspect
import os
import re
import sys
import threading
import warnings
from timeit import default_timer
from flask import Flask, Response
from flask import request, make_response, current_app
from flask.views import MethodView
from prometheus_client import Counter, Histogram, Gauge, Summary
from prometheus_client import multiprocess as pc_multiprocess, CollectorRegistry
try:
# prometheus-client >= 0.14.0
from prometheus_client.exposition import choose_encoder
except ImportError:
# prometheus-client < 0.14.0
from prometheus_client.exposition import choose_formatter as choose_encoder
from werkzeug.serving import is_running_from_reloader
if sys.version_info[0:2] >= (3, 4):
# Python v3.4+ has a built-in has __wrapped__ attribute
wraps = functools.wraps
else:
# in previous Python version we have to set the missing attribute
def wraps(wrapped, assigned=functools.WRAPPER_ASSIGNMENTS,
updated=functools.WRAPPER_UPDATES):
def wrapper(f):
f = functools.wraps(wrapped, assigned, updated)(f)
f.__wrapped__ = wrapped
return f
return wrapper
try:
# try to convert http.HTTPStatus to int status codes
from http import HTTPStatus
def _to_status_code(response_status):
if isinstance(response_status, HTTPStatus):
return response_status.value
else:
return response_status
except ImportError:
# otherwise simply use the status as is
def _to_status_code(response_status):
return response_status
NO_PREFIX = '#no_prefix'
"""
Constant indicating that default metrics should not have any prefix applied.
It purposely uses invalid characters defined for metrics names as specified in Prometheus
documentation (see: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels)
"""
class PrometheusMetrics:
"""
Prometheus metrics export configuration for Flask.
The default metrics include a Histogram for HTTP request latencies
and number of HTTP requests plus a Counter for the total number
of HTTP requests.
Sample usage:
app = Flask(__name__)
metrics = PrometheusMetrics(app)
# static information as metric
metrics.info('app_info', 'Application info', version='1.0.3')
@app.route('/')
def main():
pass # requests tracked by default
@app.route('/skip')
@metrics.do_not_track()
def skip():
pass # default metrics are not collected
@app.route('/<item_type>')
@metrics.do_not_track()
@metrics.counter('invocation_by_type', 'Number of invocations by type',
labels={'item_type': lambda: request.view_args['type']})
def by_type(item_type):
pass # only the counter is collected, not the default metrics
@app.route('/long-running')
@metrics.gauge('in_progress', 'Long running requests in progress')
def long_running():
pass
@app.route('/status/<int:status>')
@metrics.do_not_track()
@metrics.summary('requests_by_status', 'Request latencies by status',
labels={'status': lambda r: r.status_code})
@metrics.histogram('requests_by_status_and_path', 'Request latencies by status and path',
labels={'status': lambda r: r.status_code, 'path': lambda: request.path})
def echo_status(status):
return 'Status: %s' % status, status
Label values can be defined as callables:
- With a single argument that will be the Flask Response object
- Without an argument, possibly to use with the Flask `request` object
"""
def __init__(self, app,
path='/metrics',
export_defaults=True,
defaults_prefix='flask',
group_by='path',
buckets=None,
default_latency_as_histogram=True,
default_labels=None,
response_converter=None,
excluded_paths=None,
exclude_user_defaults=True,
metrics_decorator=None,
registry=None, **kwargs):
"""
Create a new Prometheus metrics export configuration.
:param app: the Flask application
:param path: the metrics path (defaults to `/metrics`)
:param export_defaults: expose all HTTP request latencies
and number of HTTP requests
:param defaults_prefix: string to prefix the default exported
metrics name with (when either `export_defaults=True` or
`export_defaults(..)` is called) or in case you don't want
any prefix then use `NO_PREFIX` constant
:param group_by: group default HTTP metrics by
this request property, like `path`, `endpoint`, `url_rule`, etc.
(defaults to `path`)
:param buckets: the time buckets for request latencies
(will use the default when `None`)
:param default_latency_as_histogram: export request latencies
as a Histogram (defaults), otherwise use a Summary
:param default_labels: default labels to attach to each of the
metrics exposed by this `PrometheusMetrics` instance
:param response_converter: a function that converts the captured
the produced response object to a Flask friendly representation
:param metrics_decorator: an optional decorator to apply to the
metrics endpoint, takes a function and needs to return a function
:param excluded_paths: regular expression(s) as a string or
a list of strings for paths to exclude from tracking
:param exclude_user_defaults: also apply the `excluded_paths`
exclusions to user-defined defaults (not only built-in ones)
:param registry: the Prometheus Registry to use
"""
self.app = app
self.path = path
self._export_defaults = export_defaults
self._defaults_prefix = defaults_prefix or 'flask'
self._default_labels = default_labels or {}
self._default_latency_as_histogram = default_latency_as_histogram
self._response_converter = response_converter or make_response
self._metrics_decorator = metrics_decorator
self.buckets = buckets
self.version = __version__
if registry:
self.registry = registry
else:
# load the default registry from the underlying
# Prometheus library here for easier unit testing
# see https://github.com/rycus86/prometheus_flask_exporter/pull/20
from prometheus_client import REGISTRY as DEFAULT_REGISTRY
self.registry = DEFAULT_REGISTRY
if kwargs.get('static_labels'):
warnings.warn(
'The `static_labels` argument of `PrometheusMetrics` is '
'deprecated since 0.15.0, please use the '
'new `default_labels` argument.', DeprecationWarning
)
for key, value in kwargs.get('static_labels', dict()).items():
if key not in self._default_labels:
self._default_labels[key] = value
if kwargs.get('group_by_endpoint') is True:
warnings.warn(
'The `group_by_endpoint` argument of `PrometheusMetrics` is '
'deprecated since 0.4.0, please use the '
'new `group_by` argument.', DeprecationWarning
)
self.group_by = 'endpoint'
elif group_by:
self.group_by = group_by
else:
self.group_by = 'path'
if excluded_paths:
if PrometheusMetrics._is_string(excluded_paths):
excluded_paths = [excluded_paths]
self.excluded_paths = [
re.compile(p) for p in excluded_paths
]
else:
self.excluded_paths = None
self.exclude_user_defaults = exclude_user_defaults
if app is not None:
self.init_app(app)
@classmethod
def for_app_factory(cls, **kwargs):
"""
A convenience method to create a new instance that is
suitable for Flask "app factory" configurations. Please
see: http://flask.pocoo.org/docs/1.0/patterns/appfactories/
Note, that you will need to call `init_app(...)` later
with the Flask application as its parameter.
This method takes the same keyword arguments as the
default constructor.
"""
return cls(app=None, **kwargs)
def init_app(self, app):
"""
This callback can be used to initialize an application for the
use with this prometheus reporter setup.
This is usually used with a Flask "app factory" configuration.
Please see: http://flask.pocoo.org/docs/1.0/patterns/appfactories/
Note, that you need to use `PrometheusMetrics.for_app_factory()`
for this mode, otherwise it is called automatically.
:param app: the Flask application
"""
if self.path:
self.register_endpoint(self.path, app)
if self._export_defaults:
self.export_defaults(
buckets=self.buckets, group_by=self.group_by,
latency_as_histogram=self._default_latency_as_histogram,
prefix=self._defaults_prefix, app=app
)
def register_endpoint(self, path, app=None):
"""
Register the metrics endpoint on the Flask application.
:param path: the path of the endpoint
:param app: the Flask application to register the endpoint on
(by default it is the application registered with this class)
"""
if app is None:
app = self.app or current_app
if is_running_from_reloader() and not os.environ.get('DEBUG_METRICS'):
app.logger.debug(
'Metrics are disabled when run in the Flask development server'
' with reload enabled. Set the environment variable'
' DEBUG_METRICS=1 to enable them anyway.'
)
return
@self.do_not_track()
def prometheus_metrics():
accept_header = request.headers.get("Accept")
if 'name[]' in request.args:
names = request.args.getlist('name[]')
else:
names = None
generated_data, content_type = self.generate_metrics(accept_header, names)
headers = {'Content-Type': content_type}
return generated_data, 200, headers
# apply any user supplied decorators, like authentication
if self._metrics_decorator:
prometheus_metrics = self._metrics_decorator(prometheus_metrics)
# apply the Flask route decorator on our metrics endpoint
app.route(path)(prometheus_metrics)
def generate_metrics(self, accept_header=None, names=None):
"""
Generate the metrics output for Prometheus to consume.
This can be exposed on a dedicated server, or on the Flask app, or for
local development you can use the shorthand method to expose it on a
new Flask app, see `PrometheusMetrics.start_http_server()`.
:param accept_header: The value of the HTTP Accept request header
(default `None`)
:param names: Names to only return samples for, must be a list of
strings if not `None` (default `None`)
:return: a tuple of response content and response content type
(both `str` types)
"""
if 'PROMETHEUS_MULTIPROC_DIR' in os.environ or 'prometheus_multiproc_dir' in os.environ:
registry = CollectorRegistry()
else:
registry = self.registry
if names:
registry = registry.restricted_registry(names)
if 'PROMETHEUS_MULTIPROC_DIR' in os.environ or 'prometheus_multiproc_dir' in os.environ:
pc_multiprocess.MultiProcessCollector(registry)
generate_latest, content_type = choose_encoder(accept_header)
generated_content = generate_latest(registry).decode('utf-8')
return generated_content, content_type
def start_http_server(self, port, host='0.0.0.0', endpoint='/metrics', ssl=None):
"""
Start an HTTP server for exposing the metrics.
This will be an individual Flask application,
not the one registered with this class.
:param port: the HTTP port to expose the metrics endpoint on
:param host: the HTTP host to listen on (default: `0.0.0.0`)
:param endpoint: the URL path to expose the endpoint on
(default: `/metrics`)
:param ssl: enable SSL to http server
It expects a dict with 2 keys: `cert` and `key` with
certificate and key paths.
Default: `None`
"""
if is_running_from_reloader():
return
app = Flask('prometheus-flask-exporter-%d' % port)
self.register_endpoint(endpoint, app)
def run_app():
if ssl is None:
app.run(host=host, port=port)
else:
app.run(host=host, port=port, ssl_context=(ssl["cert"], ssl["key"]))
thread = threading.Thread(target=run_app)
thread.daemon = True
thread.start()
def export_defaults(self, buckets=None, group_by='path',
latency_as_histogram=True,
prefix='flask', app=None, **kwargs):
"""
Export the default metrics:
- HTTP request latencies
- HTTP request exceptions
- Number of HTTP requests
:param buckets: the time buckets for request latencies
(will use the default when `None`)
:param group_by: group default HTTP metrics by
this request property, like `path`, `endpoint`, `rule`, etc.
(defaults to `path`)
:param latency_as_histogram: export request latencies
as a Histogram, otherwise use a Summary instead
(defaults to `True` to export as a Histogram)
:param prefix: prefix to start the default metrics names with
or `NO_PREFIX` (to skip prefix)
:param app: the Flask application
"""
if app is None:
app = self.app or current_app
if not prefix:
prefix = self._defaults_prefix or 'flask'
if kwargs.get('group_by_endpoint') is True:
warnings.warn(
'The `group_by_endpoint` argument of '
'`PrometheusMetrics.export_defaults` is deprecated since 0.4.0, '
'please use the new `group_by` argument.', DeprecationWarning
)
duration_group = 'endpoint'
elif group_by:
duration_group = group_by
else:
duration_group = 'path'
if callable(duration_group):
duration_group_name = duration_group.__name__
else:
duration_group_name = duration_group
if prefix == NO_PREFIX:
prefix = ""
else:
prefix = prefix + "_"
try:
self.info(
'%sexporter_info' % prefix,
'Information about the Prometheus Flask exporter',
version=self.version
)
except ValueError:
return # looks like we have already exported the default metrics
labels = self._get_combined_labels(None)
if latency_as_histogram:
# use the default buckets from prometheus_client if not given here
buckets_as_kwargs = {}
if buckets is not None:
buckets_as_kwargs['buckets'] = buckets
request_duration_metric = Histogram(
'%shttp_request_duration_seconds' % prefix,
'Flask HTTP request duration in seconds',
('method', duration_group_name, 'status') + labels.keys(),
registry=self.registry,
**buckets_as_kwargs
)
else:
# export as Summary instead
request_duration_metric = Summary(
'%shttp_request_duration_seconds' % prefix,
'Flask HTTP request duration in seconds',
('method', duration_group_name, 'status') + labels.keys(),
registry=self.registry
)
counter_labels = ('method', 'status') + labels.keys()
request_total_metric = Counter(
'%shttp_request_total' % prefix,
'Total number of HTTP requests',
counter_labels,
registry=self.registry
)
request_exceptions_metric = Counter(
'%shttp_request_exceptions_total' % prefix,
'Total number of HTTP requests which resulted in an exception',
counter_labels,
registry=self.registry
)
def before_request():
request.prom_start_time = default_timer()
def after_request(response):
if hasattr(request, 'prom_do_not_track') or hasattr(request, 'prom_exclude_all'):
return response
if self.excluded_paths:
if any(pattern.match(request.path) for pattern in self.excluded_paths):
return response
if hasattr(request, 'prom_start_time') and self._not_yet_handled('duration_reported'):
total_time = max(default_timer() - request.prom_start_time, 0)
if callable(duration_group):
group = duration_group(request)
else:
group = getattr(request, duration_group)
request_duration_labels = {
'method': request.method,
'status': _to_status_code(response.status_code),
duration_group_name: group
}
request_duration_labels.update(labels.values_for(response))
request_duration_metric.labels(**request_duration_labels).observe(total_time)
if self._not_yet_handled('total_reported'):
request_total_metric.labels(
method=request.method, status=_to_status_code(response.status_code),
**labels.values_for(response)
).inc()
return response
def teardown_request(exception=None):
if not exception or hasattr(request, 'prom_do_not_track') or hasattr(request, 'prom_exclude_all'):
return
if self.excluded_paths:
if any(pattern.match(request.path) for pattern in self.excluded_paths):
return
response = make_response('Exception: %s' % exception, 500)
if callable(duration_group):
group = duration_group(request)
else:
group = getattr(request, duration_group)
request_exceptions_metric.labels(
method=request.method, status=500,
**labels.values_for(response)
).inc()
if hasattr(request, 'prom_start_time') and self._not_yet_handled('duration_reported'):
total_time = max(default_timer() - request.prom_start_time, 0)
request_duration_labels = {
'method': request.method,
'status': 500,
duration_group_name: group
}
request_duration_labels.update(labels.values_for(response))
request_duration_metric.labels(**request_duration_labels).observe(total_time)
if self._not_yet_handled('total_reported'):
request_total_metric.labels(
method=request.method, status=500,
**labels.values_for(response)
).inc()
return
app.before_request(before_request)
app.after_request(after_request)
app.teardown_request(teardown_request)
def register_default(self, *metric_wrappers, **kwargs):
"""
Registers metric wrappers to track all endpoints,
similar to `export_defaults` but with user defined metrics.
Call this function after all routes have been set up.
Use the metric wrappers as arguments:
- metrics.counter(..)
- metrics.gauge(..)
- metrics.summary(..)
- metrics.histogram(..)
:param metric_wrappers: one or more metric wrappers to register
for all available endpoints
:param app: the Flask application to register the default metric for
(by default it is the application registered with this class)
"""
app = kwargs.get('app')
if app is None:
app = self.app or current_app
for endpoint, view_func in app.view_functions.items():
for wrapper in metric_wrappers:
view_func = wrapper(view_func)
app.view_functions[endpoint] = view_func
def histogram(self, name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
"""
Use a Histogram to track the execution time and invocation count
of the method.
:param name: the name of the metric
:param description: the description of the metric
:param labels: a dictionary of `{labelname: callable_or_value}` for labels
:param initial_value_when_only_static_labels: whether to give metric an initial value
when only static labels are present
:param kwargs: additional keyword arguments for creating the Histogram
"""
return self._track(
Histogram,
lambda metric, time: metric.observe(time),
kwargs, name, description, labels,
initial_value_when_only_static_labels=initial_value_when_only_static_labels,
registry=self.registry
)
def summary(self, name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
"""
Use a Summary to track the execution time and invocation count
of the method.
:param name: the name of the metric
:param description: the description of the metric
:param labels: a dictionary of `{labelname: callable_or_value}` for labels
:param initial_value_when_only_static_labels: whether to give metric an initial value
when only static labels are present
:param kwargs: additional keyword arguments for creating the Summary
"""
return self._track(
Summary,
lambda metric, time: metric.observe(time),
kwargs, name, description, labels,
initial_value_when_only_static_labels=initial_value_when_only_static_labels,
registry=self.registry
)
def gauge(self, name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
"""
Use a Gauge to track the number of invocations in progress
for the method.
:param name: the name of the metric
:param description: the description of the metric
:param labels: a dictionary of `{labelname: callable_or_value}` for labels
:param initial_value_when_only_static_labels: whether to give metric an initial value
when only static labels are present
:param kwargs: additional keyword arguments for creating the Gauge
"""
return self._track(
Gauge,
lambda metric, time: metric.dec(),
kwargs, name, description, labels,
initial_value_when_only_static_labels=initial_value_when_only_static_labels,
registry=self.registry,
before=lambda metric: metric.inc(),
revert_when_not_tracked=lambda metric: metric.dec()
)
def counter(self, name, description, labels=None, initial_value_when_only_static_labels=True, **kwargs):
"""
Use a Counter to track the total number of invocations of the method.
:param name: the name of the metric
:param description: the description of the metric
:param labels: a dictionary of `{labelname: callable_or_value}` for labels
:param initial_value_when_only_static_labels: whether to give metric an initial value
when only static labels are present
:param kwargs: additional keyword arguments for creating the Counter
"""
return self._track(
Counter,
lambda metric, time: metric.inc(),
kwargs,
name,
description,
labels,
initial_value_when_only_static_labels=initial_value_when_only_static_labels,
registry=self.registry
)
def _track(self, metric_type, metric_call, metric_kwargs, name, description, labels,
initial_value_when_only_static_labels, registry, before=None, revert_when_not_tracked=None):
"""
Internal method decorator logic.
:param metric_type: the type of the metric from the `prometheus_client` library
:param metric_call: the invocation to execute as a callable with `(metric, time)`
:param metric_kwargs: additional keyword arguments for creating the metric
:param name: the name of the metric
:param description: the description of the metric
:param labels: a dictionary of `{labelname: callable_or_value}` for labels
:param initial_value_when_only_static_labels: whether to give metric an initial value
when only static labels are present
:param registry: the Prometheus Registry to use
:param before: an optional callable to invoke before executing the
request handler method accepting the single `metric` argument
:param revert_when_not_tracked: an optional callable to invoke when
a non-tracked endpoint is being handled to undo any actions already
done on it, accepts a single `metric` argument
"""
if labels is not None and not isinstance(labels, dict):
raise TypeError('labels needs to be a dictionary of {labelname: callable}')
labels = self._get_combined_labels(labels)
parent_metric = metric_type(
name, description, labelnames=labels.keys(), registry=registry,
**metric_kwargs
)
# When all labels are already known at this point, the metric can get an initial value.
if initial_value_when_only_static_labels and labels.has_keys() and labels.has_only_static_values():
parent_metric.labels(*labels.get_default_values())
def get_metric(response):
if labels.has_keys():
return parent_metric.labels(**labels.values_for(response))
else:
return parent_metric
def decorator(f):
@wraps(f)
def func(*args, **kwargs):
if self.exclude_user_defaults and self.excluded_paths:
# exclude based on default excludes
if any(pattern.match(request.path) for pattern in self.excluded_paths):
return f(*args, **kwargs)
if before:
metric = get_metric(None)
before(metric)
else:
metric = None
exception = None
start_time = default_timer()
try:
try:
# execute the handler function
response = f(*args, **kwargs)
except Exception as ex:
# let Flask decide to wrap or reraise the Exception
response = current_app.handle_user_exception(ex)
except Exception as ex:
# if it was re-raised, treat it as an InternalServerError
exception = ex
response = make_response(f'Exception: {ex}', 500)
if hasattr(request, 'prom_exclude_all'):
if metric and revert_when_not_tracked:
# special handling for Gauge metrics
revert_when_not_tracked(metric)
return response
total_time = max(default_timer() - start_time, 0)
if not metric:
if not isinstance(response, Response) and request.endpoint:
view_func = current_app.view_functions[request.endpoint]
# There may be decorators 'above' us,
# but before the function is registered with Flask
while view_func and view_func != f:
try:
view_func = view_func.__wrapped__
except AttributeError:
break
if view_func == f:
# we are in a request handler method
response = self._response_converter(response)
elif hasattr(view_func, 'view_class') and issubclass(view_func.view_class, MethodView):
# we are in a method view (for Flask-RESTful for example)
response = self._response_converter(response)
metric = get_metric(response)
metric_call(metric, time=total_time)
if exception:
try:
# re-raise for the Flask error handler
raise exception
except Exception as ex:
return current_app.handle_user_exception(ex)
else:
return response
return func
return decorator
def _get_combined_labels(self, labels):
"""
Combines the given labels with static and default labels
and wraps them into an object that can efficiently return
the keys and values of these combined labels.
"""
labels = labels.copy() if labels else dict()
if self._default_labels:
labels.update(self._default_labels.copy())
def argspec(func):
if hasattr(inspect, 'getfullargspec'):
return inspect.getfullargspec(func)
else:
return inspect.getargspec(func)
def label_value(f):
if not callable(f):
return lambda x: f
if argspec(f).args:
return lambda x: f(x)
else:
return lambda x: f()
class CombinedLabels:
def __init__(self, _labels):
self.labels = _labels.items()
def keys(self):
return tuple(map(lambda k: k[0], self.labels))
def has_keys(self):
return len(self.labels) > 0
def has_only_static_values(self):
for key, value in self.labels:
if callable(value):
return False
return True
def get_default_values(self):
return list(value for key, value in self.labels)
def values_for(self, response):
label_generator = tuple(
(key, label_value(call))
for key, call in self.labels
) if labels else tuple()
return {key: value(response) for key, value in label_generator}
return CombinedLabels(labels)
@staticmethod
def do_not_track():
"""
Decorator to skip the default metrics collection for the method.
*Note*: explicit metrics decorators will still collect the data
"""
def decorator(f):
@wraps(f)
def func(*args, **kwargs):
request.prom_do_not_track = True
return f(*args, **kwargs)
return func
return decorator
@staticmethod
def exclude_all_metrics():
"""
Decorator to skip all metrics collection for the method.
"""
def decorator(f):
@wraps(f)
def func(*args, **kwargs):
request.prom_exclude_all = True
return f(*args, **kwargs)
return func
return decorator
def info(self, name, description, labelnames=None, labelvalues=None, **labels):
"""
Report any information as a Prometheus metric.
This will create a `Gauge` with the initial value of 1.
The easiest way to use it is:
metrics = PrometheusMetrics(app)
metrics.info(
'app_info', 'Application info',
version='1.0', major=1, minor=0
)
If the order of the labels matters:
metrics = PrometheusMetrics(app)
metrics.info(
'app_info', 'Application info',
('version', 'major', 'minor'),
('1.0', 1, 0)
)
:param name: the name of the metric
:param description: the description of the metric
:param labelnames: the names of the labels
:param labelvalues: the values of the labels
:param labels: the names and values of the labels
:return: the newly created `Gauge` metric
"""
if labels and labelnames:
raise ValueError(
'Cannot have labels defined as `dict` '
'and collections of names and values'
)
if labelnames is None and labels:
labelnames = labels.keys()
elif labelnames and labelvalues:
for idx, label_name in enumerate(labelnames):
labels[label_name] = labelvalues[idx]
gauge = Gauge(
name, description, labelnames or tuple(),
registry=self.registry,
multiprocess_mode='max'
)
if labels:
gauge = gauge.labels(**labels)
gauge.set(1)
return gauge
@staticmethod
def _is_string(value):
try:
return isinstance(value, str) # python3
except NameError:
return isinstance(value, basestring) # python2
@staticmethod
def _not_yet_handled(tracking_key):
"""
Check if the request has not handled some tracking yet,
and mark the request if this is the first time.
This is to avoid follow-up actions.
:param tracking_key: a key identifying a processing step
:return: True if this is the first time the request is
trying to handle this processing step
"""
key = f'prom_{tracking_key}'
if hasattr(request, key):
return False
else:
setattr(request, key, True)
return True
class ConnexionPrometheusMetrics(PrometheusMetrics):
"""
Specific extension for Connexion (https://connexion.readthedocs.io/)
that makes sure responses are converted to Flask responses.
"""
def __init__(self, app, default_mimetype='application/json', **kwargs):
flask_app = app.app if app else None
if 'response_converter' not in kwargs:
kwargs['response_converter'] = self._create_response_converter(default_mimetype)
super().__init__(flask_app, **kwargs)
@staticmethod
def content_type(content_type):
"""
Force the content type of the response,
which would be otherwise overwritten by the metrics conversion
to application/json.
:param content_type: the value to send in the
Content-Type response header
"""
def decorator(f):
@wraps(f)
def func(*args, **kwargs):
request.prom_connexion_content_type = content_type
return f(*args, **kwargs)
return func
return decorator
@staticmethod
def _create_response_converter(default_mimetype):
def _make_response(response):
from connexion.apis.flask_api import FlaskApi
mimetype = default_mimetype
if hasattr(request, 'prom_connexion_content_type'):
mimetype = request.prom_connexion_content_type
return FlaskApi.get_response(response, mimetype=mimetype)
return _make_response
class RESTfulPrometheusMetrics(PrometheusMetrics):
"""
Specific extension for Flask-RESTful (https://flask-restful.readthedocs.io/)
that makes sure API responses are converted to Flask responses.
"""
def __init__(self, app, api, **kwargs):
"""
Initializes a new PrometheusMetrics instance that is appropriate
for a Flask-RESTful application.
:param app: the Flask application
:param api: the Flask-RESTful API instance
"""
if api and 'response_converter' not in kwargs:
kwargs['response_converter'] = self._create_response_converter(api)
super().__init__(app, **kwargs)
@classmethod
def for_app_factory(cls, api=None, **kwargs):
return cls(app=None, api=api, **kwargs)
def init_app(self, app, api=None):
if api:
self._response_converter = self._create_response_converter(api)
return super().init_app(app)
@staticmethod
def _create_response_converter(api):
def _make_response(response):
if response is None:
response = (None, 200)
return api.make_response(*response)
return _make_response
__version__ = '0.23.1'
|