Esempio n. 1
0
    def requests(self, session: requests.Session) -> requests.Session:
        """
        A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
        second try without a delay). urllib3 will sleep for: {backoff factor} * (2 ^ ({number of total retries} - 1))
        seconds. If the backoff_factor is 0.1, then sleep() will sleep for [0.0s, 0.2s, 0.4s, ...] between retries.
        It will never be longer than Retry.BACKOFF_MAX. By default, backoff is disabled (set to 0).
        :param session:
        :return:
        """
        session_r = session or requests.Session()
        max_retries = Retry(
            total=self.retries,
            read=self.retries,
            connect=self.retries,
            backoff_factor=0.3,
            status_forcelist=self.status_retries,
        )
        adapter = HTTPAdapter(max_retries=max_retries)
        session_r.mount("http://", adapter)
        session_r.mount("https://", adapter)

        metrics_enabled = get_conf(service=get_service_name(service="metrics"), empty_init=True)
        if metrics_enabled:
            session_r.hooks["response"] = [self.observe_requests]
        return session_r
Esempio n. 2
0
 def init_jaeger_tracer(self):
     """This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use
     one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_
     :param service_name: the name of your application to register in the tracer
     :return: opentracing.Tracer
     """
     check_package_exists("jaeger_client")
     Config = import_from("jaeger_client", "Config")
     host = {}
     if self.host:
         host = {'local_agent': {'reporting_host': self.host}}
     metrics_config = get_conf(service=get_service_name(service="metrics"),
                               empty_init=True)
     metrics = ""
     if metrics_config:
         service_name = self.component_name.lower().replace("-",
                                                            "_").replace(
                                                                " ", "_")
         metrics = PrometheusMetricsFactory(service_name_label=service_name)
     config = Config(config={
         **{
             'sampler': {
                 'type': 'const',
                 'param': 1,
             },
             'propagation': 'b3',
             'logging': True
         },
         **host
     },
                     service_name=self.component_name,
                     metrics_factory=metrics,
                     validate=True)
     return config.initialize_tracer()
Esempio n. 3
0
    def create_app(self):
        """Initialize the Flask app, register blueprints and initialize
        all libraries like Swagger, database,
        the trace system...
        return the app and the database objects.
        :return:
        """
        config = get_conf(service=self.service)
        app = connexion.App(__name__,
                            specification_dir=os.path.join(
                                self.path, 'swagger'))
        app.add_api('swagger.yaml',
                    arguments={'title': config.APP_NAME},
                    base_path=config.APPLICATION_ROOT)

        self.application = app.app
        self.application._connexion_app = app
        self.application.config.from_object(config)
        self.application.tracer = None

        # Initialize Blueprints
        self.application.register_blueprint(healthcheck_blueprint)

        self.init_libs()
        self.add_error_handlers()

        # Inject Modules
        formatter = CustomJsonFormatter(
            '(timestamp) (level) (name) (module) (funcName) (lineno) (message)'
        )
        if not self.application.config["TESTING"]:
            log_handler = logging.StreamHandler()

            self.application.tracer = FlaskTracer(init_jaeger_tracer(), True,
                                                  self.application)
            formatter.add_service_name(self.application.config["APP_NAME"])
            formatter.add_trace_span(self.application.tracer)
            log_handler.setFormatter(formatter)
            self.application.logger.addHandler(log_handler)
            self.application.logger.propagate = False
            self.application.logger.setLevel(logging.INFO)

        return self.application
Esempio n. 4
0
import logging
import time

from flask import Blueprint, Flask, Response, request
from prometheus_client import REGISTRY, CollectorRegistry, Counter, Histogram, generate_latest, multiprocess

from pyms.flask.services.driver import DriverService, get_service_name
from pyms.config.conf import get_conf

# Based on https://github.com/sbarratt/flask-prometheus
# and https://github.com/korfuri/python-logging-prometheus/

METRICS_CONFIG = get_conf(service=get_service_name(service="metrics"),
                          empty_init=True)

FLASK_REQUEST_COUNT = Counter("http_server_requests_count",
                              "Flask Request Count",
                              ["service", "method", "uri", "status"])

FLASK_REQUEST_LATENCY = Histogram("http_server_requests_seconds",
                                  "Flask Request Latency",
                                  ["service", "method", "uri", "status"])

LOGGER_TOTAL_MESSAGES = Counter(
    "logger_messages_total",
    "Count of log entries by service and level.",
    ["service", "level"],
)


class FlaskMetricsWrapper:
Esempio n. 5
0
 def __init__(self, service: Text, path=__file__):
     self.service = service
     self.path = os.path.dirname(path)
     self.config = get_conf(service=self.service)
     self.init_services()
Esempio n. 6
0
 def __init__(self, service=None):
     self.service = (service if service else SERVICE_BASE)
     self.config = get_conf(service=self.service, empty_init=True)
Esempio n. 7
0
 def __init__(self, service, *args, **kwargs):
     self.service = ".".join([service, self.service])
     self.config = get_conf(service=self.service, empty_init=True)